Refactor custom skin context menu creation

This commit is contained in:
Birunthan Mohanathas 2014-02-07 20:47:29 +02:00
parent 726d674438
commit feb90f35d2
2 changed files with 96 additions and 81 deletions

View File

@ -164,15 +164,22 @@ void ContextMenu::ShowMenu(POINT pos, MeterWindow* meterWindow)
} }
} }
DisplayMenu(pos, menu, meterWindow ? meterWindow->GetWindow() : rainmeter.m_TrayWindow->GetWindow());
DestroyMenu(menu);
m_MenuActive = false;
}
void ContextMenu::DisplayMenu(POINT pos, HMENU menu, HWND parentWindow)
{
// Set the window to foreground // Set the window to foreground
hWnd = meterWindow ? meterWindow->GetWindow() : rainmeter.m_TrayWindow->GetWindow(); HWND foregroundWindow = GetForegroundWindow();
HWND hWndForeground = GetForegroundWindow(); if (foregroundWindow != parentWindow)
if (hWndForeground != hWnd)
{ {
const DWORD foregroundThreadID = GetWindowThreadProcessId(hWndForeground, nullptr); const DWORD foregroundThreadID = GetWindowThreadProcessId(foregroundWindow, nullptr);
const DWORD currentThreadID = GetCurrentThreadId(); const DWORD currentThreadID = GetCurrentThreadId();
AttachThreadInput(currentThreadID, foregroundThreadID, TRUE); AttachThreadInput(currentThreadID, foregroundThreadID, TRUE);
SetForegroundWindow(hWnd); SetForegroundWindow(parentWindow);
AttachThreadInput(currentThreadID, foregroundThreadID, FALSE); AttachThreadInput(currentThreadID, foregroundThreadID, FALSE);
} }
@ -183,12 +190,8 @@ void ContextMenu::ShowMenu(POINT pos, MeterWindow* meterWindow)
pos.x, pos.x,
pos.y, pos.y,
0, 0,
hWnd, parentWindow,
nullptr); nullptr);
DestroyMenu(menu);
m_MenuActive = false;
} }
HMENU ContextMenu::CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU menu) HMENU ContextMenu::CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU menu)
@ -425,18 +428,31 @@ HMENU ContextMenu::CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU men
} }
} }
AppendSkinCustomMenu(meterWindow, index, skinMenu);
return skinMenu;
}
void ContextMenu::AppendSkinCustomMenu(MeterWindow* meterWindow, int index, HMENU menu)
{
// Add custom actions to the context menu // Add custom actions to the context menu
std::wstring contextTitle = meterWindow->GetParser().ReadString(L"Rainmeter", L"ContextTitle", L""); std::wstring contextTitle = meterWindow->GetParser().ReadString(L"Rainmeter", L"ContextTitle", L"");
if (!contextTitle.empty()) if (contextTitle.empty())
{ {
return;
}
auto isTitleSeparator = [](const std::wstring& title) auto isTitleSeparator = [](const std::wstring& title)
{ {
return title.find_first_not_of(L'-') == std::wstring::npos; return title.find_first_not_of(L'-') == std::wstring::npos;
}; };
std::wstring contextAction = meterWindow->GetParser().ReadString(L"Rainmeter", L"ContextAction", L""); std::wstring contextAction = meterWindow->GetParser().ReadString(L"Rainmeter", L"ContextAction", L"");
if (!contextAction.empty() || isTitleSeparator(contextTitle)) if (contextAction.empty() && !isTitleSeparator(contextTitle))
{ {
return;
}
std::vector<std::wstring> cTitles; std::vector<std::wstring> cTitles;
WCHAR buffer[128]; WCHAR buffer[128];
int i = 1; int i = 1;
@ -474,7 +490,7 @@ HMENU ContextMenu::CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU men
else else
{ {
const UINT_PTR id = (index << 16) | (IDM_SKIN_CUSTOMCONTEXTMENU_FIRST + i); const UINT_PTR id = (index << 16) | (IDM_SKIN_CUSTOMCONTEXTMENU_FIRST + i);
InsertMenu(skinMenu, position + 1, MF_BYPOSITION | MF_STRING, id, cTitles[i].c_str()); InsertMenu(menu, position + 1, MF_BYPOSITION | MF_STRING, id, cTitles[i].c_str());
} }
++position; ++position;
@ -482,14 +498,14 @@ HMENU ContextMenu::CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU men
if (position != 0) if (position != 0)
{ {
InsertMenu(skinMenu, 1, MF_BYPOSITION | MF_STRING | MF_GRAYED, 0, L"Custom skin actions"); InsertMenu(menu, 1, MF_BYPOSITION | MF_STRING | MF_GRAYED, 0, L"Custom skin actions");
InsertMenu(skinMenu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, nullptr); InsertMenu(menu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, nullptr);
} }
} }
else else
{ {
HMENU customMenu = CreatePopupMenu(); HMENU customMenu = CreatePopupMenu();
InsertMenu(skinMenu, 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)customMenu, L"Custom skin actions"); InsertMenu(menu, 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)customMenu, L"Custom skin actions");
for (size_t i = 0; i < titleSize; ++i) for (size_t i = 0; i < titleSize; ++i)
{ {
@ -504,12 +520,8 @@ HMENU ContextMenu::CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU men
} }
} }
InsertMenu(skinMenu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, nullptr); InsertMenu(menu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, nullptr);
} }
}
}
return skinMenu;
} }
int ContextMenu::CreateAllSkinsMenuRecursive(HMENU skinMenu, int index) int ContextMenu::CreateAllSkinsMenuRecursive(HMENU skinMenu, int index)

View File

@ -39,7 +39,10 @@ public:
static void CreateMonitorMenu(HMENU monitorMenu, MeterWindow* meterWindow); static void CreateMonitorMenu(HMENU monitorMenu, MeterWindow* meterWindow);
private: private:
static void DisplayMenu(POINT pos, HMENU menu, HWND parentWindow);
static HMENU CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU menu); static HMENU CreateSkinMenu(MeterWindow* meterWindow, int index, HMENU menu);
static void AppendSkinCustomMenu(MeterWindow* meterWindow, int index, HMENU menu);
static void ChangeSkinIndex(HMENU subMenu, int index); static void ChangeSkinIndex(HMENU subMenu, int index);
static void CreateAllSkinsMenu(HMENU skinMenu) { CreateAllSkinsMenuRecursive(skinMenu, 0); } static void CreateAllSkinsMenu(HMENU skinMenu) { CreateAllSkinsMenuRecursive(skinMenu, 0); }