uidriver/glfw: Refactoring: make currentMonitorByOS more explicit

This commit is contained in:
Hajime Hoshi 2020-08-24 03:27:38 +09:00
parent 9bff1c79be
commit a7d234e3c8
5 changed files with 32 additions and 35 deletions

View File

@ -147,7 +147,7 @@ func initialize() error {
// Create a window and set it: this affects toDeviceIndependentPixel and deviceScaleFactor. // Create a window and set it: this affects toDeviceIndependentPixel and deviceScaleFactor.
theUI.window = w theUI.window = w
theUI.initMonitor = currentMonitorByOS(w) theUI.initMonitor = currentMonitor(w)
v := theUI.initMonitor.GetVideoMode() v := theUI.initMonitor.GetVideoMode()
theUI.initFullscreenWidthInDP = int(theUI.toDeviceIndependentPixel(float64(v.Width))) theUI.initFullscreenWidthInDP = int(theUI.toDeviceIndependentPixel(float64(v.Width)))
theUI.initFullscreenHeightInDP = int(theUI.toDeviceIndependentPixel(float64(v.Height))) theUI.initFullscreenHeightInDP = int(theUI.toDeviceIndependentPixel(float64(v.Height)))
@ -409,7 +409,7 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
var w, h int var w, h int
_ = u.t.Call(func() error { _ = u.t.Call(func() error {
v := u.currentMonitor().GetVideoMode() v := currentMonitor(u.window).GetVideoMode()
w = int(u.toDeviceIndependentPixel(float64(v.Width))) w = int(u.toDeviceIndependentPixel(float64(v.Width)))
h = int(u.toDeviceIndependentPixel(float64(v.Height))) h = int(u.toDeviceIndependentPixel(float64(v.Height)))
return nil return nil
@ -587,7 +587,7 @@ func (u *UserInterface) deviceScaleFactor() float64 {
return devicescale.GetAt(cm.x, cm.y) return devicescale.GetAt(cm.x, cm.y)
} }
} }
return devicescale.GetAt(u.currentMonitor().GetPos()) return devicescale.GetAt(currentMonitor(u.window).GetPos())
} }
func init() { func init() {
@ -800,7 +800,7 @@ func (u *UserInterface) updateSize() {
_ = u.t.Call(func() error { _ = u.t.Call(func() error {
var ww, wh int var ww, wh int
if u.isFullscreen() { if u.isFullscreen() {
v := u.currentMonitor().GetVideoMode() v := currentMonitor(u.window).GetVideoMode()
ww = v.Width ww = v.Width
wh = v.Height wh = v.Height
} else { } else {
@ -961,7 +961,7 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) {
if u.origPosX == invalidPos || u.origPosY == invalidPos { if u.origPosX == invalidPos || u.origPosY == invalidPos {
u.origPosX, u.origPosY = u.window.GetPos() u.origPosX, u.origPosY = u.window.GetPos()
} }
m := u.currentMonitor() m := currentMonitor(u.window)
v := m.GetVideoMode() v := m.GetVideoMode()
u.window.SetMonitor(m, 0, 0, v.Width, v.Height, v.RefreshRate) u.window.SetMonitor(m, 0, 0, v.Width, v.Height, v.RefreshRate)
@ -1089,19 +1089,29 @@ func (u *UserInterface) updateVsync() {
u.Graphics().SetVsyncEnabled(u.vsync) u.Graphics().SetVsyncEnabled(u.vsync)
} }
// currentMonitor returns the monitor most suitable with the current window. // currentMonitor returns the current active monitor.
//
// The given window might or might not be used to detect the monitor.
// //
// currentMonitor must be called on the main thread. // currentMonitor must be called on the main thread.
func (u *UserInterface) currentMonitor() *glfw.Monitor { func currentMonitor(window *glfw.Window) *glfw.Monitor {
// GetMonitor is available only on fullscreen. // GetMonitor is available only on fullscreen.
if m := u.window.GetMonitor(); m != nil { if m := 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 // 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.). // multiple monitors, or, before SetWindowPosition is called.).
// 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 currentMonitorByOS(u.window) if m := currentMonitorByOS(); m != nil {
return m
}
// As the fallback, detect the monitor from the window.
if m := getCachedMonitor(window.GetPos()); m != nil {
return m.m
}
return glfw.GetPrimaryMonitor()
} }
func (u *UserInterface) SetScreenTransparent(transparent bool) { func (u *UserInterface) SetScreenTransparent(transparent bool) {
@ -1151,7 +1161,7 @@ func (u *UserInterface) SetInitFocused(focused bool) {
func (u *UserInterface) monitorPosition() (int, int) { func (u *UserInterface) monitorPosition() (int, int) {
// TODO: toDeviceIndependentPixel might be required. // TODO: toDeviceIndependentPixel might be required.
return u.currentMonitor().GetPos() return currentMonitor(u.window).GetPos()
} }
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() driver.Input {

View File

@ -55,7 +55,7 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
return x, y return x, y
} }
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor { func currentMonitorByOS() *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 w here. // Note: [NSApp mainWindow] is nil when it doesn't have its border. Use w here.
@ -67,11 +67,7 @@ func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
return m return m
} }
} }
return nil
if m := getCachedMonitor(w.GetPos()); m != nil {
return m.m
}
return glfw.GetPrimaryMonitor()
} }
func (u *UserInterface) nativeWindow() unsafe.Pointer { func (u *UserInterface) nativeWindow() unsafe.Pointer {

View File

@ -32,12 +32,9 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
return x, y return x, y
} }
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor { func currentMonitorByOS() *glfw.Monitor {
// TODO: Implement this correctly. (#1119). // TODO: Implement this correctly. (#1119).
if cm := getCachedMonitor(w.GetPos()); cm != nil { return nil
return cm.m
}
return glfw.GetPrimaryMonitor()
} }
func (u *UserInterface) nativeWindow() unsafe.Pointer { func (u *UserInterface) nativeWindow() unsafe.Pointer {

View File

@ -120,19 +120,13 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
return x, y return x, y
} }
func currentMonitorByOS(glfww *glfw.Window) *glfw.Monitor { func currentMonitorByOS() *glfw.Monitor {
fallback := func() *glfw.Monitor { // TODO: Should we return nil here?
if m := getCachedMonitor(glfww.GetPos()); m != nil {
return m.m
}
return glfw.GetPrimaryMonitor()
}
// TODO: Should we use glfww.GetWin32Window() here?
w, err := getActiveWindow() w, err := getActiveWindow()
if err != nil { if err != nil {
panic(err) panic(err)
} }
if w == 0 { if w == 0 {
// There is no window at launching, but there is a hidden initialized window. // There is no window at launching, but there is a hidden initialized window.
// Get the foreground window, that is common among multiple processes. // Get the foreground window, that is common among multiple processes.
@ -142,7 +136,7 @@ func currentMonitorByOS(glfww *glfw.Window) *glfw.Monitor {
} }
if w == 0 { if w == 0 {
// GetForegroundWindow can return null according to the document. // GetForegroundWindow can return null according to the document.
return fallback() return nil
} }
} }
@ -152,7 +146,7 @@ func currentMonitorByOS(glfww *glfw.Window) *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 fallback() return nil
} }
mi := monitorInfo{} mi := monitorInfo{}
@ -168,7 +162,7 @@ func currentMonitorByOS(glfww *glfw.Window) *glfw.Monitor {
return m return m
} }
} }
return fallback() return nil
} }
func (u *UserInterface) nativeWindow() unsafe.Pointer { func (u *UserInterface) nativeWindow() unsafe.Pointer {

View File

@ -189,7 +189,7 @@ func (w *window) Position() (int, int) {
} else { } else {
wx, wy = w.ui.window.GetPos() wx, wy = w.ui.window.GetPos()
} }
mx, my := w.ui.currentMonitor().GetPos() mx, my := currentMonitor(w.ui.window).GetPos()
wx -= mx wx -= mx
wy -= my wy -= my
xf := w.ui.toDeviceIndependentPixel(float64(wx)) xf := w.ui.toDeviceIndependentPixel(float64(wx))
@ -209,7 +209,7 @@ func (w *window) SetPosition(x, y int) {
defer func() { defer func() {
w.setPositionCalled = true w.setPositionCalled = true
}() }()
mx, my := w.ui.currentMonitor().GetPos() mx, my := currentMonitor(w.ui.window).GetPos()
xf := w.ui.toDeviceDependentPixel(float64(x)) xf := w.ui.toDeviceDependentPixel(float64(x))
yf := w.ui.toDeviceDependentPixel(float64(y)) yf := w.ui.toDeviceDependentPixel(float64(y))
x, y := w.ui.adjustWindowPosition(mx+int(xf), my+int(yf)) x, y := w.ui.adjustWindowPosition(mx+int(xf), my+int(yf))