From 96a657025f8fa9526e5731bb1ac2fac1891b72a4 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 8 Oct 2018 03:18:24 +0900 Subject: [PATCH] ui: Bug fix: DeviceScaleFactor and MonitorSize were not correct on Windows --- internal/ui/ui_mac.go | 2 +- internal/ui/ui_unix.go | 2 +- internal/ui/ui_windows.go | 33 +++++++++++++++++++++++++-------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/internal/ui/ui_mac.go b/internal/ui/ui_mac.go index 2411c367a..80cfd25ae 100644 --- a/internal/ui/ui_mac.go +++ b/internal/ui/ui_mac.go @@ -55,5 +55,5 @@ func currentMonitor() *glfw.Monitor { return m } } - return nil + return glfw.GetPrimaryMonitor() } diff --git a/internal/ui/ui_unix.go b/internal/ui/ui_unix.go index 75103b8b1..7012911a0 100644 --- a/internal/ui/ui_unix.go +++ b/internal/ui/ui_unix.go @@ -44,5 +44,5 @@ func currentMonitor() *glfw.Monitor { return m } } - return nil + return glfw.GetPrimaryMonitor() } diff --git a/internal/ui/ui_windows.go b/internal/ui/ui_windows.go index 37dce5f87..411eadbe6 100644 --- a/internal/ui/ui_windows.go +++ b/internal/ui/ui_windows.go @@ -47,10 +47,11 @@ type monitorInfo struct { var ( // user32 is defined at hideconsole_windows.go - procGetSystemMetrics = user32.NewProc("GetSystemMetrics") - procGetActiveWindow = user32.NewProc("GetActiveWindow") - procMonitorFromWindow = user32.NewProc("MonitorFromWindow") - procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW") + procGetSystemMetrics = user32.NewProc("GetSystemMetrics") + procGetActiveWindow = user32.NewProc("GetActiveWindow") + procGetForegroundWindow = user32.NewProc("GetForegroundWindow") + procMonitorFromWindow = user32.NewProc("MonitorFromWindow") + procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW") ) func getSystemMetrics(nIndex int) (int, error) { @@ -69,6 +70,14 @@ func getActiveWindow() (uintptr, error) { return r, nil } +func getForegroundWindow() (uintptr, error) { + r, _, e := syscall.Syscall(procGetForegroundWindow.Addr(), 0, 0, 0, 0) + if e != 0 { + return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %d", e) + } + return r, nil +} + func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) { r, _, e := syscall.Syscall(procMonitorFromWindow.Addr(), 2, hwnd, uintptr(dwFlags), 0) if e != 0 { @@ -118,9 +127,17 @@ func currentMonitor() *glfw.Monitor { panic(err) } if w == 0 { - // There is no window at launching. - // TODO: Use glfw.GetCurrentContext() like currentMonitor() in ui_unix.go. - return glfw.GetPrimaryMonitor() + // There is no window at launching, but there is a hidden initialized window. + // Get the foreground window, that is common among multiple processes. + w, err = getForegroundWindow() + if err != nil { + panic(err) + } + if w == 0 { + // GetForegroundWindow can return null according to the document. Use + // the primary monitor instead. + return glfw.GetPrimaryMonitor() + } } // Get the current monitor by the window handle instead of the window position. It is because the window @@ -144,5 +161,5 @@ func currentMonitor() *glfw.Monitor { return m } } - return nil + return glfw.GetPrimaryMonitor() }