|
@@ -4,47 +4,57 @@
|
|
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
-namespace {
|
|
|
+#include "app_links/app_links_plugin_c_api.h"
|
|
|
|
|
|
-constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW";
|
|
|
+namespace
|
|
|
+{
|
|
|
|
|
|
-// The number of Win32Window objects that currently exist.
|
|
|
-static int g_active_window_count = 0;
|
|
|
+ constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW";
|
|
|
|
|
|
-using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd);
|
|
|
+ // The number of Win32Window objects that currently exist.
|
|
|
+ static int g_active_window_count = 0;
|
|
|
|
|
|
-// 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);
|
|
|
-}
|
|
|
+ using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd);
|
|
|
|
|
|
-// 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;
|
|
|
+ // 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);
|
|
|
}
|
|
|
- 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);
|
|
|
+
|
|
|
+ // 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;
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-} // namespace
|
|
|
+} // namespace
|
|
|
|
|
|
// Manages the Win32Window's window class registration.
|
|
|
-class WindowClassRegistrar {
|
|
|
- public:
|
|
|
+class WindowClassRegistrar
|
|
|
+{
|
|
|
+public:
|
|
|
~WindowClassRegistrar() = default;
|
|
|
|
|
|
// Returns the singleton registar instance.
|
|
|
- static WindowClassRegistrar* GetInstance() {
|
|
|
- if (!instance_) {
|
|
|
+ static WindowClassRegistrar *GetInstance()
|
|
|
+ {
|
|
|
+ if (!instance_)
|
|
|
+ {
|
|
|
instance_ = new WindowClassRegistrar();
|
|
|
}
|
|
|
return instance_;
|
|
@@ -52,24 +62,26 @@ class WindowClassRegistrar {
|
|
|
|
|
|
// Returns the name of the window class, registering the class if it hasn't
|
|
|
// previously been registered.
|
|
|
- const wchar_t* GetWindowClass();
|
|
|
+ const wchar_t *GetWindowClass();
|
|
|
|
|
|
// Unregisters the window class. Should only be called if there are no
|
|
|
// instances of the window.
|
|
|
void UnregisterWindowClass();
|
|
|
|
|
|
- private:
|
|
|
+private:
|
|
|
WindowClassRegistrar() = default;
|
|
|
|
|
|
- static WindowClassRegistrar* instance_;
|
|
|
+ static WindowClassRegistrar *instance_;
|
|
|
|
|
|
bool class_registered_ = false;
|
|
|
};
|
|
|
|
|
|
-WindowClassRegistrar* WindowClassRegistrar::instance_ = nullptr;
|
|
|
+WindowClassRegistrar *WindowClassRegistrar::instance_ = nullptr;
|
|
|
|
|
|
-const wchar_t* WindowClassRegistrar::GetWindowClass() {
|
|
|
- if (!class_registered_) {
|
|
|
+const wchar_t *WindowClassRegistrar::GetWindowClass()
|
|
|
+{
|
|
|
+ if (!class_registered_)
|
|
|
+ {
|
|
|
WNDCLASS window_class{};
|
|
|
window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
|
|
window_class.lpszClassName = kWindowClassName;
|
|
@@ -88,26 +100,35 @@ const wchar_t* WindowClassRegistrar::GetWindowClass() {
|
|
|
return kWindowClassName;
|
|
|
}
|
|
|
|
|
|
-void WindowClassRegistrar::UnregisterWindowClass() {
|
|
|
+void WindowClassRegistrar::UnregisterWindowClass()
|
|
|
+{
|
|
|
UnregisterClass(kWindowClassName, nullptr);
|
|
|
class_registered_ = false;
|
|
|
}
|
|
|
|
|
|
-Win32Window::Win32Window() {
|
|
|
+Win32Window::Win32Window()
|
|
|
+{
|
|
|
++g_active_window_count;
|
|
|
}
|
|
|
|
|
|
-Win32Window::~Win32Window() {
|
|
|
+Win32Window::~Win32Window()
|
|
|
+{
|
|
|
--g_active_window_count;
|
|
|
Destroy();
|
|
|
}
|
|
|
|
|
|
-bool Win32Window::CreateAndShow(const std::wstring& title,
|
|
|
- const Point& origin,
|
|
|
- const Size& size) {
|
|
|
+bool Win32Window::CreateAndShow(const std::wstring &title,
|
|
|
+ const Point &origin,
|
|
|
+ const Size &size)
|
|
|
+{
|
|
|
+ if (SendAppLinkToInstance(title))
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
Destroy();
|
|
|
|
|
|
- const wchar_t* window_class =
|
|
|
+ const wchar_t *window_class =
|
|
|
WindowClassRegistrar::GetInstance()->GetWindowClass();
|
|
|
|
|
|
const POINT target_point = {static_cast<LONG>(origin.x),
|
|
@@ -122,27 +143,69 @@ bool Win32Window::CreateAndShow(const std::wstring& title,
|
|
|
Scale(size.width, scale_factor), Scale(size.height, scale_factor),
|
|
|
nullptr, nullptr, GetModuleHandle(nullptr), this);
|
|
|
|
|
|
- if (!window) {
|
|
|
+ if (!window)
|
|
|
+ {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return OnCreate();
|
|
|
}
|
|
|
|
|
|
+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;
|
|
|
+}
|
|
|
+
|
|
|
// static
|
|
|
LRESULT CALLBACK Win32Window::WndProc(HWND const window,
|
|
|
UINT const message,
|
|
|
WPARAM const wparam,
|
|
|
- LPARAM const lparam) noexcept {
|
|
|
- if (message == WM_NCCREATE) {
|
|
|
- auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
|
|
|
+ LPARAM const lparam) noexcept
|
|
|
+{
|
|
|
+ if (message == WM_NCCREATE)
|
|
|
+ {
|
|
|
+ auto window_struct = reinterpret_cast<CREATESTRUCT *>(lparam);
|
|
|
SetWindowLongPtr(window, GWLP_USERDATA,
|
|
|
reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));
|
|
|
|
|
|
- auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
|
|
|
+ auto that = static_cast<Win32Window *>(window_struct->lpCreateParams);
|
|
|
EnableFullDpiSupportIfAvailable(window);
|
|
|
that->window_handle_ = window;
|
|
|
- } else if (Win32Window* that = GetThisFromHandle(window)) {
|
|
|
+ }
|
|
|
+ else if (Win32Window *that = GetThisFromHandle(window))
|
|
|
+ {
|
|
|
return that->MessageHandler(window, message, wparam, lparam);
|
|
|
}
|
|
|
|
|
@@ -153,64 +216,76 @@ LRESULT
|
|
|
Win32Window::MessageHandler(HWND hwnd,
|
|
|
UINT const message,
|
|
|
WPARAM const wparam,
|
|
|
- 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;
|
|
|
+ LPARAM const lparam) noexcept
|
|
|
+{
|
|
|
+ switch (message)
|
|
|
+ {
|
|
|
+ case WM_DESTROY:
|
|
|
+ window_handle_ = nullptr;
|
|
|
+ Destroy();
|
|
|
+ if (quit_on_close_)
|
|
|
+ {
|
|
|
+ PostQuitMessage(0);
|
|
|
}
|
|
|
- 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;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
- case WM_ACTIVATE:
|
|
|
- if (child_content_ != nullptr) {
|
|
|
- SetFocus(child_content_);
|
|
|
- }
|
|
|
- return 0;
|
|
|
+ case WM_ACTIVATE:
|
|
|
+ if (child_content_ != nullptr)
|
|
|
+ {
|
|
|
+ SetFocus(child_content_);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
return DefWindowProc(window_handle_, message, wparam, lparam);
|
|
|
}
|
|
|
|
|
|
-void Win32Window::Destroy() {
|
|
|
+void Win32Window::Destroy()
|
|
|
+{
|
|
|
OnDestroy();
|
|
|
|
|
|
- if (window_handle_) {
|
|
|
+ if (window_handle_)
|
|
|
+ {
|
|
|
DestroyWindow(window_handle_);
|
|
|
window_handle_ = nullptr;
|
|
|
}
|
|
|
- if (g_active_window_count == 0) {
|
|
|
+ if (g_active_window_count == 0)
|
|
|
+ {
|
|
|
WindowClassRegistrar::GetInstance()->UnregisterWindowClass();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept {
|
|
|
- return reinterpret_cast<Win32Window*>(
|
|
|
+Win32Window *Win32Window::GetThisFromHandle(HWND const window) noexcept
|
|
|
+{
|
|
|
+ return reinterpret_cast<Win32Window *>(
|
|
|
GetWindowLongPtr(window, GWLP_USERDATA));
|
|
|
}
|
|
|
|
|
|
-void Win32Window::SetChildContent(HWND content) {
|
|
|
+void Win32Window::SetChildContent(HWND content)
|
|
|
+{
|
|
|
child_content_ = content;
|
|
|
SetParent(content, window_handle_);
|
|
|
RECT frame = GetClientArea();
|
|
@@ -221,25 +296,30 @@ void Win32Window::SetChildContent(HWND content) {
|
|
|
SetFocus(child_content_);
|
|
|
}
|
|
|
|
|
|
-RECT Win32Window::GetClientArea() {
|
|
|
+RECT Win32Window::GetClientArea()
|
|
|
+{
|
|
|
RECT frame;
|
|
|
GetClientRect(window_handle_, &frame);
|
|
|
return frame;
|
|
|
}
|
|
|
|
|
|
-HWND Win32Window::GetHandle() {
|
|
|
+HWND Win32Window::GetHandle()
|
|
|
+{
|
|
|
return window_handle_;
|
|
|
}
|
|
|
|
|
|
-void Win32Window::SetQuitOnClose(bool quit_on_close) {
|
|
|
+void Win32Window::SetQuitOnClose(bool quit_on_close)
|
|
|
+{
|
|
|
quit_on_close_ = quit_on_close;
|
|
|
}
|
|
|
|
|
|
-bool Win32Window::OnCreate() {
|
|
|
+bool Win32Window::OnCreate()
|
|
|
+{
|
|
|
// No-op; provided for subclasses.
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-void Win32Window::OnDestroy() {
|
|
|
+void Win32Window::OnDestroy()
|
|
|
+{
|
|
|
// No-op; provided for subclasses.
|
|
|
}
|