| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | #include "win32_window.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | #include <dwmapi.h>
 | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | #include <flutter_windows.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "resource.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 16:54:50 +07:00
										 |  |  | #include "app_links/app_links_plugin_c_api.h"
 | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | namespace { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | /// Window attribute that enables dark mode window decorations.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// Redefined in case the developer's machine has a Windows SDK older than
 | 
					
						
							|  |  |  | /// version 10.0.22000.0.
 | 
					
						
							|  |  |  | /// See: https://docs.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
 | 
					
						
							|  |  |  | #ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
 | 
					
						
							|  |  |  | #define DWMWA_USE_IMMERSIVE_DARK_MODE 20
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW"; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | /// Registry key for app theme preference.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// A value of 0 indicates apps should use dark mode. A non-zero or missing
 | 
					
						
							|  |  |  | /// value indicates apps should use light mode.
 | 
					
						
							|  |  |  | constexpr const wchar_t kGetPreferredBrightnessRegKey[] = | 
					
						
							|  |  |  |   L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; | 
					
						
							|  |  |  | constexpr const wchar_t kGetPreferredBrightnessRegValue[] = L"AppsUseLightTheme"; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | // The number of Win32Window objects that currently exist.
 | 
					
						
							|  |  |  | static int g_active_window_count = 0; | 
					
						
							| 
									
										
										
										
											2023-08-07 16:54:50 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Scale helper to convert logical scaler values to physical using passed in
 | 
					
						
							|  |  |  | // scale factor
 | 
					
						
							|  |  |  | int Scale(int source, double scale_factor) { | 
					
						
							|  |  |  |   return static_cast<int>(source * scale_factor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module.
 | 
					
						
							|  |  |  | // This API is only needed for PerMonitor V1 awareness mode.
 | 
					
						
							|  |  |  | void EnableFullDpiSupportIfAvailable(HWND hwnd) { | 
					
						
							|  |  |  |   HMODULE user32_module = LoadLibraryA("User32.dll"); | 
					
						
							|  |  |  |   if (!user32_module) { | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   auto enable_non_client_dpi_scaling = | 
					
						
							|  |  |  |       reinterpret_cast<EnableNonClientDpiScaling*>( | 
					
						
							|  |  |  |           GetProcAddress(user32_module, "EnableNonClientDpiScaling")); | 
					
						
							|  |  |  |   if (enable_non_client_dpi_scaling != nullptr) { | 
					
						
							|  |  |  |     enable_non_client_dpi_scaling(hwnd); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   FreeLibrary(user32_module); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | }  // namespace
 | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Manages the Win32Window's window class registration.
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | class WindowClassRegistrar { | 
					
						
							|  |  |  |  public: | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   ~WindowClassRegistrar() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   // Returns the singleton registrar instance.
 | 
					
						
							|  |  |  |   static WindowClassRegistrar* GetInstance() { | 
					
						
							|  |  |  |     if (!instance_) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |       instance_ = new WindowClassRegistrar(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return instance_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Returns the name of the window class, registering the class if it hasn't
 | 
					
						
							|  |  |  |   // previously been registered.
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   const wchar_t* GetWindowClass(); | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Unregisters the window class. Should only be called if there are no
 | 
					
						
							|  |  |  |   // instances of the window.
 | 
					
						
							|  |  |  |   void UnregisterWindowClass(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |  private: | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   WindowClassRegistrar() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   static WindowClassRegistrar* instance_; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   bool class_registered_ = false; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | WindowClassRegistrar* WindowClassRegistrar::instance_ = nullptr; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | const wchar_t* WindowClassRegistrar::GetWindowClass() { | 
					
						
							|  |  |  |   if (!class_registered_) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     WNDCLASS window_class{}; | 
					
						
							|  |  |  |     window_class.hCursor = LoadCursor(nullptr, IDC_ARROW); | 
					
						
							|  |  |  |     window_class.lpszClassName = kWindowClassName; | 
					
						
							|  |  |  |     window_class.style = CS_HREDRAW | CS_VREDRAW; | 
					
						
							|  |  |  |     window_class.cbClsExtra = 0; | 
					
						
							|  |  |  |     window_class.cbWndExtra = 0; | 
					
						
							|  |  |  |     window_class.hInstance = GetModuleHandle(nullptr); | 
					
						
							|  |  |  |     window_class.hIcon = | 
					
						
							|  |  |  |         LoadIcon(window_class.hInstance, MAKEINTRESOURCE(IDI_APP_ICON)); | 
					
						
							|  |  |  |     window_class.hbrBackground = 0; | 
					
						
							|  |  |  |     window_class.lpszMenuName = nullptr; | 
					
						
							|  |  |  |     window_class.lpfnWndProc = Win32Window::WndProc; | 
					
						
							|  |  |  |     RegisterClass(&window_class); | 
					
						
							|  |  |  |     class_registered_ = true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return kWindowClassName; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | void WindowClassRegistrar::UnregisterWindowClass() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   UnregisterClass(kWindowClassName, nullptr); | 
					
						
							|  |  |  |   class_registered_ = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | Win32Window::Win32Window() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   ++g_active_window_count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | Win32Window::~Win32Window() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   --g_active_window_count; | 
					
						
							|  |  |  |   Destroy(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | bool Win32Window::Create(const std::wstring& title, | 
					
						
							|  |  |  |                          const Point& origin, | 
					
						
							|  |  |  |                          const Size& size) { | 
					
						
							|  |  |  |   Destroy(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 16:54:50 +07:00
										 |  |  |   if (SendAppLinkToInstance(title)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   const wchar_t* window_class = | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |       WindowClassRegistrar::GetInstance()->GetWindowClass(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const POINT target_point = {static_cast<LONG>(origin.x), | 
					
						
							|  |  |  |                               static_cast<LONG>(origin.y)}; | 
					
						
							|  |  |  |   HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST); | 
					
						
							|  |  |  |   UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); | 
					
						
							|  |  |  |   double scale_factor = dpi / 96.0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   HWND window = CreateWindow( | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |       window_class, title.c_str(), WS_OVERLAPPEDWINDOW, | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |       Scale(origin.x, scale_factor), Scale(origin.y, scale_factor), | 
					
						
							|  |  |  |       Scale(size.width, scale_factor), Scale(size.height, scale_factor), | 
					
						
							|  |  |  |       nullptr, nullptr, GetModuleHandle(nullptr), this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   if (!window) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   UpdateTheme(window); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   return OnCreate(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | bool Win32Window::Show() { | 
					
						
							|  |  |  |   return ShowWindow(window_handle_, SW_SHOWNORMAL); | 
					
						
							| 
									
										
										
										
											2023-08-07 16:54:50 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | LRESULT CALLBACK Win32Window::WndProc(HWND const window, | 
					
						
							|  |  |  |                                       UINT const message, | 
					
						
							|  |  |  |                                       WPARAM const wparam, | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |                                       LPARAM const lparam) noexcept { | 
					
						
							|  |  |  |   if (message == WM_NCCREATE) { | 
					
						
							|  |  |  |     auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam); | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     SetWindowLongPtr(window, GWLP_USERDATA, | 
					
						
							|  |  |  |                      reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |     auto that = static_cast<Win32Window*>(window_struct->lpCreateParams); | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     EnableFullDpiSupportIfAvailable(window); | 
					
						
							|  |  |  |     that->window_handle_ = window; | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   } else if (Win32Window* that = GetThisFromHandle(window)) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     return that->MessageHandler(window, message, wparam, lparam); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return DefWindowProc(window, message, wparam, lparam); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LRESULT | 
					
						
							|  |  |  | Win32Window::MessageHandler(HWND hwnd, | 
					
						
							|  |  |  |                             UINT const message, | 
					
						
							|  |  |  |                             WPARAM const wparam, | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |                             LPARAM const lparam) noexcept { | 
					
						
							|  |  |  |   switch (message) { | 
					
						
							|  |  |  |     case WM_DESTROY: | 
					
						
							|  |  |  |       window_handle_ = nullptr; | 
					
						
							|  |  |  |       Destroy(); | 
					
						
							|  |  |  |       if (quit_on_close_) { | 
					
						
							|  |  |  |         PostQuitMessage(0); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case WM_DPICHANGED: { | 
					
						
							|  |  |  |       auto newRectSize = reinterpret_cast<RECT*>(lparam); | 
					
						
							|  |  |  |       LONG newWidth = newRectSize->right - newRectSize->left; | 
					
						
							|  |  |  |       LONG newHeight = newRectSize->bottom - newRectSize->top; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth, | 
					
						
							|  |  |  |                    newHeight, SWP_NOZORDER | SWP_NOACTIVATE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return 0; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |     case WM_SIZE: { | 
					
						
							|  |  |  |       RECT rect = GetClientArea(); | 
					
						
							|  |  |  |       if (child_content_ != nullptr) { | 
					
						
							|  |  |  |         // Size and position the child window.
 | 
					
						
							|  |  |  |         MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left, | 
					
						
							|  |  |  |                    rect.bottom - rect.top, TRUE); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return 0; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |     case WM_ACTIVATE: | 
					
						
							|  |  |  |       if (child_content_ != nullptr) { | 
					
						
							|  |  |  |         SetFocus(child_content_); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case WM_DWMCOLORIZATIONCOLORCHANGED: | 
					
						
							|  |  |  |       UpdateTheme(hwnd); | 
					
						
							|  |  |  |       return 0; | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return DefWindowProc(window_handle_, message, wparam, lparam); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | void Win32Window::Destroy() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   OnDestroy(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   if (window_handle_) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     DestroyWindow(window_handle_); | 
					
						
							|  |  |  |     window_handle_ = nullptr; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  |   if (g_active_window_count == 0) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |     WindowClassRegistrar::GetInstance()->UnregisterWindowClass(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept { | 
					
						
							|  |  |  |   return reinterpret_cast<Win32Window*>( | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |       GetWindowLongPtr(window, GWLP_USERDATA)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | void Win32Window::SetChildContent(HWND content) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   child_content_ = content; | 
					
						
							|  |  |  |   SetParent(content, window_handle_); | 
					
						
							|  |  |  |   RECT frame = GetClientArea(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MoveWindow(content, frame.left, frame.top, frame.right - frame.left, | 
					
						
							|  |  |  |              frame.bottom - frame.top, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SetFocus(child_content_); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | RECT Win32Window::GetClientArea() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   RECT frame; | 
					
						
							|  |  |  |   GetClientRect(window_handle_, &frame); | 
					
						
							|  |  |  |   return frame; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | HWND Win32Window::GetHandle() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   return window_handle_; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | void Win32Window::SetQuitOnClose(bool quit_on_close) { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   quit_on_close_ = quit_on_close; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | bool Win32Window::OnCreate() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   // No-op; provided for subclasses.
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | void Win32Window::OnDestroy() { | 
					
						
							| 
									
										
										
										
											2021-07-09 14:50:57 +08:00
										 |  |  |   // No-op; provided for subclasses.
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-11-07 15:55:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Win32Window::UpdateTheme(HWND const window) { | 
					
						
							|  |  |  |   DWORD light_mode; | 
					
						
							|  |  |  |   DWORD light_mode_size = sizeof(light_mode); | 
					
						
							|  |  |  |   LSTATUS result = RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey, | 
					
						
							|  |  |  |                                kGetPreferredBrightnessRegValue, | 
					
						
							|  |  |  |                                RRF_RT_REG_DWORD, nullptr, &light_mode, | 
					
						
							|  |  |  |                                &light_mode_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (result == ERROR_SUCCESS) { | 
					
						
							|  |  |  |     BOOL enable_dark_mode = light_mode == 0; | 
					
						
							|  |  |  |     DwmSetWindowAttribute(window, DWMWA_USE_IMMERSIVE_DARK_MODE, | 
					
						
							|  |  |  |                           &enable_dark_mode, sizeof(enable_dark_mode)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Win32Window::SendAppLinkToInstance(const std::wstring &title) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   // Find our exact window
 | 
					
						
							|  |  |  |   HWND hwnd = ::FindWindow(kWindowClassName, title.c_str()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (hwnd) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Dispatch new link to current window
 | 
					
						
							|  |  |  |     SendAppLink(hwnd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // (Optional) Restore our window to front in same state
 | 
					
						
							|  |  |  |     WINDOWPLACEMENT place = {sizeof(WINDOWPLACEMENT)}; | 
					
						
							|  |  |  |     GetWindowPlacement(hwnd, &place); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (place.showCmd) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     case SW_SHOWMAXIMIZED: | 
					
						
							|  |  |  |       ShowWindow(hwnd, SW_SHOWMAXIMIZED); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     case SW_SHOWMINIMIZED: | 
					
						
							|  |  |  |       ShowWindow(hwnd, SW_RESTORE); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       ShowWindow(hwnd, SW_NORMAL); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SetWindowPos(0, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE); | 
					
						
							|  |  |  |     SetForegroundWindow(hwnd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Window has been found, don't create another one.
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | } |