internal/ui: bug fix: wrong monitor initialization on macOS

* `currentMouseLocation()` returned a position in the macOS native
coordinate. This means the Y axis is upward, while the Y axis is
downward in the GLFW coordinate. This change adjusts the Y position.
* `(*monitors).monitorFromGLFWMonitor` always returned nil at least
on macOS. This change fixes the implementation of this.

Updates #807
Closes #2794
This commit is contained in:
Hajime Hoshi 2023-09-29 12:20:07 +09:00
parent ecb55e0fdd
commit e1fb389dc0
2 changed files with 24 additions and 4 deletions

View File

@ -72,12 +72,23 @@ func (m *monitors) append(ms []*Monitor) []*Monitor {
return append(ms, m.monitors...)
}
func (m *monitors) primaryMonitor() *Monitor {
if atomic.LoadInt32(&m.updateCalled) == 0 {
panic("ui: (*monitors).primaryMonitor must be called before (*monitors).append is called")
}
m.m.Lock()
defer m.m.Unlock()
return m.monitors[0]
}
func (m *monitors) monitorFromGLFWMonitor(glfwMonitor *glfw.Monitor) *Monitor {
m.m.Lock()
defer m.m.Unlock()
for _, m := range m.monitors {
if m.m == glfwMonitor {
if x, y := glfwMonitor.GetPos(); m.x == x && m.y == y {
return m
}
}

View File

@ -243,7 +243,7 @@ var (
sel_windowWillExitFullScreen = objc.RegisterName("windowWillExitFullScreen:")
)
func currentMouseLocation() (x, y int) {
func currentMouseLocationInDIP() (x, y int) {
sig := cocoa.NSMethodSignature_signatureWithObjCTypes("{NSPoint=dd}@:")
inv := cocoa.NSInvocation_invocationWithMethodSignature(sig)
inv.SetTarget(objc.ID(class_NSEvent))
@ -251,11 +251,20 @@ func currentMouseLocation() (x, y int) {
inv.Invoke()
var point cocoa.NSPoint
inv.GetReturnValue(unsafe.Pointer(&point))
return int(point.X), int(point.Y)
// 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)
// On macOS, the Y axis is upward. Adjust the Y position (#807, #2794).
y = -y
m := theMonitors.primaryMonitor()
y += m.vm.Height
return x, y
}
func initialMonitorByOS() (*glfw.Monitor, error) {
x, y := currentMouseLocation()
x, y := currentMouseLocationInDIP()
// Find the monitor including the cursor.
for _, m := range theMonitors.append(nil) {