uidriver/glfw: Add better fallbacks for currentMonitorFromPosition

Updates #1119
This commit is contained in:
Hajime Hoshi 2020-08-23 23:37:58 +09:00
parent 01a84a7121
commit 07480ed66d
3 changed files with 27 additions and 18 deletions

View File

@ -1094,12 +1094,13 @@ func (u *UserInterface) updateVsync() {
// //
// currentMonitor must be called on the main thread. // currentMonitor must be called on the main thread.
func (u *UserInterface) currentMonitor() *glfw.Monitor { func (u *UserInterface) currentMonitor() *glfw.Monitor {
if w := u.window; w != nil {
// GetMonitor is available only on fullscreen. // GetMonitor is available only on fullscreen.
if m := w.GetMonitor(); m != nil { if m := u.window.GetMonitor(); m != nil {
return m return m
} }
}
// Getting a monitor from a window position is not reliable in general (e.g., when a window is put across
// multiple monitors).
// Get the monitor which the current window belongs to. This requires OS API. // Get the monitor which the current window belongs to. This requires OS API.
return u.currentMonitorFromPosition() return u.currentMonitorFromPosition()
} }

View File

@ -59,7 +59,6 @@ func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor {
x := C.int(0) x := C.int(0)
y := C.int(0) y := C.int(0)
// Note: [NSApp mainWindow] is nil when it doesn't have its border. Use u.window here. // Note: [NSApp mainWindow] is nil when it doesn't have its border. Use u.window here.
if u.window != nil {
win := u.window.GetCocoaWindow() win := u.window.GetCocoaWindow()
C.currentMonitorPos(win, &x, &y) C.currentMonitorPos(win, &x, &y)
for _, m := range glfw.GetMonitors() { for _, m := range glfw.GetMonitors() {
@ -68,6 +67,9 @@ func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor {
return m return m
} }
} }
if m, ok := getCachedMonitor(u.window.GetPos()); ok {
return m.m
} }
return glfw.GetPrimaryMonitor() return glfw.GetPrimaryMonitor()
} }

View File

@ -121,6 +121,13 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
} }
func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor { func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor {
fallback := func() *glfw.Monitor {
if m, ok := getCachedMonitor(u.window.GetPos()); ok {
return m.m
}
return glfw.GetPrimaryMonitor()
}
// TODO: Should we use u.window.GetWin32Window() here? // TODO: Should we use u.window.GetWin32Window() here?
w, err := getActiveWindow() w, err := getActiveWindow()
if err != nil { if err != nil {
@ -134,9 +141,8 @@ func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor {
panic(err) panic(err)
} }
if w == 0 { if w == 0 {
// GetForegroundWindow can return null according to the document. Use // GetForegroundWindow can return null according to the document.
// the primary monitor instead. return fallback()
return glfw.GetPrimaryMonitor()
} }
} }
@ -146,7 +152,7 @@ func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor {
m, err := monitorFromWindow(w, monitorDefaultToNearest) m, err := monitorFromWindow(w, monitorDefaultToNearest)
if err != nil { if err != nil {
// monitorFromWindow can return error on Wine. Ignore this. // monitorFromWindow can return error on Wine. Ignore this.
return glfw.GetPrimaryMonitor() return fallback()
} }
mi := monitorInfo{} mi := monitorInfo{}
@ -162,7 +168,7 @@ func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor {
return m return m
} }
} }
return glfw.GetPrimaryMonitor() return fallback()
} }
func (u *UserInterface) nativeWindow() unsafe.Pointer { func (u *UserInterface) nativeWindow() unsafe.Pointer {