Separate windows shell interaction into it's own file
This commit is contained in:
@@ -8,25 +8,18 @@
|
||||
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
|
||||
#pragma comment(lib, "comctl32.lib")
|
||||
|
||||
#include <windows.h>
|
||||
#include "Util.h"
|
||||
#include "AudioApi.h"
|
||||
#include "Settings.h"
|
||||
#include "AsuroTool.h"
|
||||
#include "WindowsShell.h"
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <shellapi.h>
|
||||
#include <commctrl.h>
|
||||
#include <strsafe.h>
|
||||
|
||||
#include "Util.h"
|
||||
#include "AudioApi.h"
|
||||
#include "Settings.h"
|
||||
#include "resource.h"
|
||||
#include "AsuroTool.h"
|
||||
|
||||
const size_t MAX_FONT_PATH_LENGTH = 2048;
|
||||
const UINT TRAY_ID = 420;
|
||||
const UINT WMAPP_NOTIFYCALLBACK = WM_APP + 1;
|
||||
|
||||
// Globals for use in callbacks
|
||||
DrawData* gDrawData;
|
||||
@@ -80,59 +73,9 @@ void init(DrawData& drawData, ApplicationData& appData)
|
||||
iconFontPath.append("\\remixicon.ttf");
|
||||
io.Fonts->AddFontFromFileTTF(iconFontPath.c_str(), 14.0f, &icons_config, icons_ranges);
|
||||
|
||||
// Set window icon
|
||||
HINSTANCE instance = GetModuleHandle(NULL);
|
||||
LPWSTR iconId = MAKEINTRESOURCE(IDI_ICON1);
|
||||
HANDLE iconLarge = LoadImageW(instance, iconId, IMAGE_ICON, 64, 64, 0);
|
||||
HANDLE iconSmall = LoadImageW(instance, iconId, IMAGE_ICON, 32, 32, 0);
|
||||
SendMessage(drawData.window_handle, WM_SETICON, ICON_BIG, (LPARAM)iconLarge);
|
||||
SendMessage(drawData.window_handle, WM_SETICON, ICON_SMALL, (LPARAM)iconSmall);
|
||||
SendMessage(drawData.window_handle, WM_SETICON, ICON_SMALL2, (LPARAM)iconSmall);
|
||||
|
||||
// Set tray icon
|
||||
NOTIFYICONDATA nid = { sizeof(nid) };
|
||||
nid.hWnd = drawData.window_handle;
|
||||
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP;
|
||||
nid.uID = TRAY_ID;
|
||||
nid.uCallbackMessage = WMAPP_NOTIFYCALLBACK;
|
||||
|
||||
HRESULT iconResult;
|
||||
iconResult = LoadIconMetric(instance, iconId, LIM_SMALL, &nid.hIcon);
|
||||
isError(iconResult, "Failed to load tray icon image: ");
|
||||
|
||||
iconResult = LoadString(instance, IDS_STRING_TOOLTIP, nid.szTip, ARRAYSIZE(nid.szTip));
|
||||
isError(iconResult, "Failed to load tray icon text: ");
|
||||
|
||||
Shell_NotifyIcon(NIM_ADD, &nid);
|
||||
|
||||
nid.uVersion = NOTIFYICON_VERSION_4;
|
||||
Shell_NotifyIcon(NIM_SETVERSION, &nid);
|
||||
|
||||
if (!SetWindowsHookEx(WH_CALLWNDPROC, trayIconEventHandler, instance, GetCurrentThreadId()))
|
||||
{
|
||||
std::cout << "Failed to hook tray icon events: " << std::hex << GetLastError() << std::endl;
|
||||
}
|
||||
|
||||
// Set window minimize behavior
|
||||
glfwSetWindowIconifyCallback(drawData.window, [](GLFWwindow* window, int isIconified) {
|
||||
if (isIconified && gAppData->settings.docked)
|
||||
{
|
||||
glfwHideWindow(window);
|
||||
}
|
||||
isHidden = isIconified;
|
||||
});
|
||||
|
||||
glfwSetWindowFocusCallback(drawData.window, [](GLFWwindow* window, int isFocused) {
|
||||
if (!isFocused && gAppData->settings.docked && !justDocked)
|
||||
{
|
||||
glfwIconifyWindow(window);
|
||||
}
|
||||
});
|
||||
|
||||
// Load settings
|
||||
|
||||
initShell(drawData);
|
||||
initSettings(drawData, appData);
|
||||
|
||||
// Set up audio device api
|
||||
initAudio(appData);
|
||||
}
|
||||
|
||||
@@ -140,7 +83,6 @@ void draw(DrawData& drawData, ApplicationData& appData)
|
||||
{
|
||||
justDocked = false;
|
||||
|
||||
// Actual Drawing
|
||||
if (isHidden)
|
||||
{
|
||||
glfwWaitEvents();
|
||||
@@ -178,11 +120,7 @@ void draw(DrawData& drawData, ApplicationData& appData)
|
||||
|
||||
void cleanup(DrawData& drawData, ApplicationData& appData)
|
||||
{
|
||||
// Remove tray icon
|
||||
NOTIFYICONDATA nid = { sizeof(nid) };
|
||||
nid.hWnd = drawData.window_handle;
|
||||
nid.uID = TRAY_ID;
|
||||
Shell_NotifyIcon(NIM_DELETE, &nid);
|
||||
cleanupShell(drawData);
|
||||
}
|
||||
|
||||
ImVec2 menuBar(DrawData& drawData, ApplicationData& appData)
|
||||
@@ -408,25 +346,3 @@ void drawCircle(float radius, ImU32 color)
|
||||
ImVec2 windowPos = ImGui::GetWindowPos();
|
||||
drawList->AddCircleFilled(ImVec2(cursorPos.x + windowPos.x, cursorPos.y + windowPos.y + ImGui::GetTextLineHeight() / 2.), radius, color);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK trayIconEventHandler(int code, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (code >= 0)
|
||||
{
|
||||
CWPSTRUCT* data = (CWPSTRUCT*)lParam;
|
||||
|
||||
if (data->message == WMAPP_NOTIFYCALLBACK)
|
||||
{
|
||||
auto id = HIWORD(data->lParam);
|
||||
auto trayEvent = LOWORD(data->lParam);
|
||||
|
||||
if (id == TRAY_ID && trayEvent == WM_LBUTTONUP)
|
||||
{
|
||||
glfwShowWindow(gDrawData->window);
|
||||
glfwRestoreWindow(gDrawData->window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CallNextHookEx(NULL, code, wParam, lParam);
|
||||
}
|
||||
|
||||
@@ -12,5 +12,3 @@ void cleanup(DrawData& drawData, ApplicationData& appData);
|
||||
ImVec2 menuBar(DrawData& drawData, ApplicationData& appData);
|
||||
ImVec2 audioDeviceWindow(ApplicationData& appData, std::vector<AudioDevice>& deviceList, const char* title);
|
||||
void drawCircle(float radius, ImU32 color);
|
||||
|
||||
LRESULT trayIconEventHandler(int code, WPARAM wParam, LPARAM lParam);
|
||||
|
||||
@@ -111,7 +111,7 @@ END
|
||||
|
||||
STRINGTABLE
|
||||
BEGIN
|
||||
IDS_STRING_TOOLTIP "uwu"
|
||||
IDS_STRING_TOOLTIP "Audio Thingy"
|
||||
END
|
||||
|
||||
#endif // English (United Kingdom) resources
|
||||
|
||||
@@ -93,6 +93,7 @@
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<DisableSpecificWarnings>4305; 4244</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -111,6 +112,7 @@
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<DisableSpecificWarnings>4305; 4244</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -129,6 +131,7 @@
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<DisableSpecificWarnings>4305; 4244</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -147,6 +150,7 @@
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<DisableSpecificWarnings>4305; 4244</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -164,6 +168,7 @@
|
||||
<ClCompile Include="AudioNotificationListener.cpp" />
|
||||
<ClCompile Include="Settings.cpp" />
|
||||
<ClCompile Include="Util.cpp" />
|
||||
<ClCompile Include="WindowsShell.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ApplicationData.h" />
|
||||
@@ -175,6 +180,7 @@
|
||||
<ClInclude Include="resource.h" />
|
||||
<ClInclude Include="Settings.h" />
|
||||
<ClInclude Include="Util.h" />
|
||||
<ClInclude Include="WindowsShell.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ImguiBase\ImguiBase.vcxproj">
|
||||
|
||||
@@ -39,6 +39,9 @@
|
||||
<ClCompile Include="Settings.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="WindowsShell.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="AsuroTool.h">
|
||||
@@ -65,6 +68,9 @@
|
||||
<ClInclude Include="PolicyConfig.h">
|
||||
<Filter>Header Files\Audio</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="WindowsShell.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CopyFileToFolders Include="Montserrat-Regular.ttf">
|
||||
|
||||
102
AsuroTool/WindowsShell.cpp
Normal file
102
AsuroTool/WindowsShell.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "WindowsShell.h"
|
||||
#include "resource.h"
|
||||
#include "ApplicationData.h"
|
||||
#include "Util.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <commctrl.h>
|
||||
#include <strsafe.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
const UINT TRAY_ID = 420;
|
||||
const UINT WMAPP_NOTIFYCALLBACK = WM_APP + 1;
|
||||
|
||||
extern DrawData* gDrawData;
|
||||
extern ApplicationData* gAppData;
|
||||
extern bool justDocked;
|
||||
extern bool isHidden;
|
||||
|
||||
void initShell(DrawData& drawData)
|
||||
{
|
||||
// Set window icon
|
||||
HINSTANCE instance = GetModuleHandle(NULL);
|
||||
LPWSTR iconId = MAKEINTRESOURCE(IDI_ICON1);
|
||||
HANDLE iconLarge = LoadImageW(instance, iconId, IMAGE_ICON, 64, 64, 0);
|
||||
HANDLE iconSmall = LoadImageW(instance, iconId, IMAGE_ICON, 32, 32, 0);
|
||||
SendMessage(drawData.window_handle, WM_SETICON, ICON_BIG, (LPARAM)iconLarge);
|
||||
SendMessage(drawData.window_handle, WM_SETICON, ICON_SMALL, (LPARAM)iconSmall);
|
||||
SendMessage(drawData.window_handle, WM_SETICON, ICON_SMALL2, (LPARAM)iconSmall);
|
||||
|
||||
// Set tray icon
|
||||
NOTIFYICONDATA nid = { sizeof(nid) };
|
||||
nid.hWnd = drawData.window_handle;
|
||||
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP;
|
||||
nid.uID = TRAY_ID;
|
||||
nid.uCallbackMessage = WMAPP_NOTIFYCALLBACK;
|
||||
|
||||
HRESULT iconResult;
|
||||
iconResult = LoadIconMetric(instance, iconId, LIM_SMALL, &nid.hIcon);
|
||||
isError(iconResult, "Failed to load tray icon image: ");
|
||||
|
||||
iconResult = LoadString(instance, IDS_STRING_TOOLTIP, nid.szTip, ARRAYSIZE(nid.szTip));
|
||||
isError(iconResult, "Failed to load tray icon text: ");
|
||||
|
||||
Shell_NotifyIcon(NIM_ADD, &nid);
|
||||
|
||||
nid.uVersion = NOTIFYICON_VERSION_4;
|
||||
Shell_NotifyIcon(NIM_SETVERSION, &nid);
|
||||
|
||||
if (!SetWindowsHookEx(WH_CALLWNDPROC, trayIconEventHandler, instance, GetCurrentThreadId()))
|
||||
{
|
||||
std::cout << "Failed to hook tray icon events: " << std::hex << GetLastError() << std::endl;
|
||||
}
|
||||
|
||||
// Set window minimize behavior
|
||||
glfwSetWindowIconifyCallback(drawData.window, [](GLFWwindow* window, int isIconified) {
|
||||
if (isIconified && gAppData->settings.docked)
|
||||
{
|
||||
glfwHideWindow(window);
|
||||
}
|
||||
isHidden = isIconified;
|
||||
});
|
||||
|
||||
glfwSetWindowFocusCallback(drawData.window, [](GLFWwindow* window, int isFocused) {
|
||||
if (!isFocused && gAppData->settings.docked && !justDocked)
|
||||
{
|
||||
glfwIconifyWindow(window);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void cleanupShell(DrawData& drawData)
|
||||
{
|
||||
// Remove tray icon
|
||||
NOTIFYICONDATA nid = { sizeof(nid) };
|
||||
nid.hWnd = drawData.window_handle;
|
||||
nid.uID = TRAY_ID;
|
||||
Shell_NotifyIcon(NIM_DELETE, &nid);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK trayIconEventHandler(int code, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (code >= 0)
|
||||
{
|
||||
CWPSTRUCT* data = (CWPSTRUCT*)lParam;
|
||||
|
||||
if (data->message == WMAPP_NOTIFYCALLBACK)
|
||||
{
|
||||
auto id = HIWORD(data->lParam);
|
||||
auto trayEvent = LOWORD(data->lParam);
|
||||
|
||||
if (id == TRAY_ID && (trayEvent == WM_LBUTTONUP || trayEvent == WM_RBUTTONUP))
|
||||
{
|
||||
glfwShowWindow(gDrawData->window);
|
||||
glfwRestoreWindow(gDrawData->window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CallNextHookEx(NULL, code, wParam, lParam);
|
||||
}
|
||||
8
AsuroTool/WindowsShell.h
Normal file
8
AsuroTool/WindowsShell.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include <ImguiBase.h>
|
||||
|
||||
void initShell(DrawData& drawData);
|
||||
|
||||
void cleanupShell(DrawData& drawData);
|
||||
|
||||
LRESULT trayIconEventHandler(int code, WPARAM wParam, LPARAM lParam);
|
||||
Reference in New Issue
Block a user