internal/ui: bug fix: initialMonitorByOS could return nil on macOS

initialMonitorByOS could return nil when a cursor was at an extreme
position like the bottom of the display. Apparently, a cursor position
could take an inclusive range of the monitor size.

This change fixes this issue by fixing the comparison.

Even if initialMonitorByOS returns nil, a fallback primary monitor
should be used, so this is not a critical issue.

Updates #2794
This commit is contained in:
Hajime Hoshi 2023-09-29 16:48:20 +09:00
parent 14a2c703df
commit 03d6811a65
4 changed files with 8 additions and 29 deletions

View File

@ -99,13 +99,15 @@ func (m *monitors) monitorFromID(id int) *Monitor {
// monitorFromPosition returns a monitor for the given position (x, y), // monitorFromPosition returns a monitor for the given position (x, y),
// or returns nil if monitor is not found. // or returns nil if monitor is not found.
// The position is in GLFW pixels.
func (m *monitors) monitorFromPosition(x, y int) *Monitor { func (m *monitors) monitorFromPosition(x, y int) *Monitor {
m.m.Lock() m.m.Lock()
defer m.m.Unlock() defer m.m.Unlock()
for _, m := range m.monitors { for _, m := range m.monitors {
// Use an inclusive range. On macOS, the cursor position can take this range (#2794).
// TODO: Fix incorrectness in the cases of https://github.com/glfw/glfw/issues/1961. // TODO: Fix incorrectness in the cases of https://github.com/glfw/glfw/issues/1961.
if m.x <= x && x < m.x+m.videoMode.Width && m.y <= y && y < m.y+m.videoMode.Height { if m.x <= x && x <= m.x+m.videoMode.Width && m.y <= y && y <= m.y+m.videoMode.Height {
return m return m
} }
} }

View File

@ -239,7 +239,7 @@ var (
sel_windowWillExitFullScreen = objc.RegisterName("windowWillExitFullScreen:") sel_windowWillExitFullScreen = objc.RegisterName("windowWillExitFullScreen:")
) )
func currentMouseLocationInDIP() (x, y int) { func currentMouseLocation() (x, y int) {
sig := cocoa.NSMethodSignature_signatureWithObjCTypes("{NSPoint=dd}@:") sig := cocoa.NSMethodSignature_signatureWithObjCTypes("{NSPoint=dd}@:")
inv := cocoa.NSInvocation_invocationWithMethodSignature(sig) inv := cocoa.NSInvocation_invocationWithMethodSignature(sig)
inv.SetTarget(objc.ID(class_NSEvent)) inv.SetTarget(objc.ID(class_NSEvent))
@ -248,8 +248,6 @@ func currentMouseLocationInDIP() (x, y int) {
var point cocoa.NSPoint var point cocoa.NSPoint
inv.GetReturnValue(unsafe.Pointer(&point)) inv.GetReturnValue(unsafe.Pointer(&point))
// On macOS, the unit of GLFW (OS-native) pixels' scale and device-independent pixels's scale are the same.
// The monitor sizes' scales are also the same.
x, y = int(point.X), int(point.Y) x, y = int(point.X), int(point.Y)
// On macOS, the Y axis is upward. Adjust the Y position (#807, #2794). // On macOS, the Y axis is upward. Adjust the Y position (#807, #2794).
@ -260,17 +258,10 @@ func currentMouseLocationInDIP() (x, y int) {
} }
func initialMonitorByOS() (*Monitor, error) { func initialMonitorByOS() (*Monitor, error) {
x, y := currentMouseLocationInDIP() x, y := currentMouseLocation()
// Find the monitor including the cursor. // Find the monitor including the cursor.
for _, m := range theMonitors.append(nil) { return theMonitors.monitorFromPosition(x, y), nil
w, h := m.videoMode.Width, m.videoMode.Height
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
return m, nil
}
}
return nil, nil
} }
func monitorFromWindowByOS(w *glfw.Window) *Monitor { func monitorFromWindowByOS(w *glfw.Window) *Monitor {

View File

@ -145,14 +145,7 @@ func initialMonitorByOS() (*Monitor, error) {
x, y := int(rep.RootX), int(rep.RootY) x, y := int(rep.RootX), int(rep.RootY)
// Find the monitor including the cursor. // Find the monitor including the cursor.
for _, m := range theMonitors.append(nil) { return theMonitors.monitorFromPosition(x, y), nil
w, h := m.videoMode.Width, m.videoMode.Height
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
return m, nil
}
}
return nil, nil
} }
func monitorFromWindowByOS(_ *glfw.Window) *Monitor { func monitorFromWindowByOS(_ *glfw.Window) *Monitor {

View File

@ -136,14 +136,7 @@ func initialMonitorByOS() (*Monitor, error) {
x, y := int(px), int(py) x, y := int(px), int(py)
// Find the monitor including the cursor. // Find the monitor including the cursor.
for _, m := range theMonitors.append(nil) { return theMonitors.monitorFromPosition(x, y), nil
w, h := m.videoMode.Width, m.videoMode.Height
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
return m, nil
}
}
return nil, nil
} }
func monitorFromWindowByOS(w *glfw.Window) *Monitor { func monitorFromWindowByOS(w *glfw.Window) *Monitor {