diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 42489fdd2..742e2a3fa 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -1316,18 +1316,19 @@ func (u *UserInterface) updateVsync() { // // currentMonitor must be called on the main thread. func (u *UserInterface) currentMonitor() *glfw.Monitor { - if !u.isRunning() { + if u.window == nil { return u.initMonitor } - return currentMonitorImpl(u.window) + if m := monitorFromWindow(u.window); m != nil { + return m + } + return glfw.GetPrimaryMonitor() } -// currentMonitorImpl returns the current active monitor. +// monitorFromWindow returns the monitor from the given window. // -// The given window might or might not be used to detect the monitor. -// -// currentMonitorImpl must be called on the main thread. -func currentMonitorImpl(window *glfw.Window) *glfw.Monitor { +// monitorFromWindow must be called on the main thread. +func monitorFromWindow(window *glfw.Window) *glfw.Monitor { // GetMonitor is available only in fullscreen. if m := window.GetMonitor(); m != nil { return m @@ -1336,7 +1337,7 @@ func currentMonitorImpl(window *glfw.Window) *glfw.Monitor { // Getting a monitor from a window position is not reliable in general (e.g., when a window is put across // multiple monitors, or, before SetWindowPosition is called.). // Get the monitor which the current window belongs to. This requires OS API. - if m := currentMonitorByOS(window); m != nil { + if m := monitorFromWindowByOS(window); m != nil { return m } @@ -1344,7 +1345,8 @@ func currentMonitorImpl(window *glfw.Window) *glfw.Monitor { if m := getMonitorFromPosition(window.GetPos()); m != nil { return m.m } - return glfw.GetPrimaryMonitor() + + return nil } func (u *UserInterface) SetScreenTransparent(transparent bool) { diff --git a/internal/ui/ui_glfw_darwin.go b/internal/ui/ui_glfw_darwin.go index 097310ea3..c950c8ef5 100644 --- a/internal/ui/ui_glfw_darwin.go +++ b/internal/ui/ui_glfw_darwin.go @@ -183,11 +183,9 @@ func initialMonitorByOS() (*glfw.Monitor, error) { return nil, nil } -func currentMonitorByOS(w *glfw.Window) *glfw.Monitor { +func monitorFromWindowByOS(w *glfw.Window) *glfw.Monitor { var x, y C.int - // Note: [NSApp mainWindow] is nil when it doesn't have its border. Use w here. - win := w.GetCocoaWindow() - C.currentMonitorPos(C.uintptr_t(win), &x, &y) + C.currentMonitorPos(C.uintptr_t(w.GetCocoaWindow()), &x, &y) for _, m := range ensureMonitors() { if int(x) == m.x && int(y) == m.y { return m.m diff --git a/internal/ui/ui_glfw_unix.go b/internal/ui/ui_glfw_unix.go index 00fee87b4..5d59adc72 100644 --- a/internal/ui/ui_glfw_unix.go +++ b/internal/ui/ui_glfw_unix.go @@ -159,7 +159,7 @@ func initialMonitorByOS() (*glfw.Monitor, error) { return nil, nil } -func currentMonitorByOS(_ *glfw.Window) *glfw.Monitor { +func monitorFromWindowByOS(_ *glfw.Window) *glfw.Monitor { // TODO: Implement this correctly. (#1119). return nil } diff --git a/internal/ui/ui_glfw_windows.go b/internal/ui/ui_glfw_windows.go index e5beed0c4..d4b118415 100644 --- a/internal/ui/ui_glfw_windows.go +++ b/internal/ui/ui_glfw_windows.go @@ -69,7 +69,7 @@ func getSystemMetrics(nIndex int) (int32, error) { return int32(r), nil } -func monitorFromWindow(hwnd windows.HWND, dwFlags uint32) uintptr { +func monitorFromWindow_(hwnd windows.HWND, dwFlags uint32) uintptr { r, _, _ := procMonitorFromWindow.Call(uintptr(hwnd), uintptr(dwFlags)) return r } @@ -150,7 +150,7 @@ func initialMonitorByOS() (*glfw.Monitor, error) { return nil, nil } -func currentMonitorByOS(w *glfw.Window) *glfw.Monitor { +func monitorFromWindowByOS(w *glfw.Window) *glfw.Monitor { return monitorFromWin32Window(windows.HWND(w.GetWin32Window())) } @@ -158,7 +158,7 @@ func monitorFromWin32Window(w windows.HWND) *glfw.Monitor { // Get the current monitor by the window handle instead of the window position. It is because the window // position is not relaiable in some cases e.g. when the window is put across multiple monitors. - m := monitorFromWindow(w, monitorDefaultToNearest) + m := monitorFromWindow_(w, monitorDefaultToNearest) if m == 0 { // monitorFromWindow can return error on Wine. Ignore this. return nil