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.
theUI.window = w
theUI.initMonitor = currentMonitorByOS(w)
theUI.initMonitor = currentMonitor(w)
v := theUI.initMonitor.GetVideoMode()
theUI.initFullscreenWidthInDP = int(theUI.toDeviceIndependentPixel(float64(v.Width)))
theUI.initFullscreenHeightInDP = int(theUI.toDeviceIndependentPixel(float64(v.Height)))
@ -409,7 +409,7 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
var w, h int
_ = u.t.Call(func() error {
v := u.currentMonitor().GetVideoMode()
v := currentMonitor(u.window).GetVideoMode()
w = int(u.toDeviceIndependentPixel(float64(v.Width)))
h = int(u.toDeviceIndependentPixel(float64(v.Height)))
return nil
@ -587,7 +587,7 @@ func (u *UserInterface) deviceScaleFactor() float64 {
return devicescale.GetAt(cm.x, cm.y)
}
}
return devicescale.GetAt(u.currentMonitor().GetPos())
return devicescale.GetAt(currentMonitor(u.window).GetPos())
}
func init() {
@ -800,7 +800,7 @@ func (u *UserInterface) updateSize() {
_ = u.t.Call(func() error {
var ww, wh int
if u.isFullscreen() {
v := u.currentMonitor().GetVideoMode()
v := currentMonitor(u.window).GetVideoMode()
ww = v.Width
wh = v.Height
} else {
@ -961,7 +961,7 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) {
if u.origPosX == invalidPos || u.origPosY == invalidPos {
u.origPosX, u.origPosY = u.window.GetPos()
}
m := u.currentMonitor()
m := currentMonitor(u.window)
v := m.GetVideoMode()
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)
}
// 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.
func (u *UserInterface) currentMonitor() *glfw.Monitor {
func currentMonitor(window *glfw.Window) *glfw.Monitor {
// GetMonitor is available only on fullscreen.
if m := u.window.GetMonitor(); m != nil {
if m := 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, or, before SetWindowPosition is called.).
// 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) {
@ -1151,7 +1161,7 @@ func (u *UserInterface) SetInitFocused(focused bool) {
func (u *UserInterface) monitorPosition() (int, int) {
// TODO: toDeviceIndependentPixel might be required.
return u.currentMonitor().GetPos()
return currentMonitor(u.window).GetPos()
}
func (u *UserInterface) Input() driver.Input {

View File

@ -55,7 +55,7 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
return x, y
}
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
func currentMonitorByOS() *glfw.Monitor {
x := C.int(0)
y := C.int(0)
// 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
}
}
if m := getCachedMonitor(w.GetPos()); m != nil {
return m.m
}
return glfw.GetPrimaryMonitor()
return nil
}
func (u *UserInterface) nativeWindow() unsafe.Pointer {

View File

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

View File

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

View File

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