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.
func (u *UserInterface) currentMonitor() *glfw.Monitor {
if w := u.window; w != nil {
// GetMonitor is available only on fullscreen.
if m := w.GetMonitor(); m != nil {
return m
}
// GetMonitor is available only on fullscreen.
if m := u.window.GetMonitor(); m != nil {
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.
return u.currentMonitorFromPosition()
}

View File

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

View File

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