From e0cd031aeaca84a1f630b8a56e22c42603aa0a12 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 23 Sep 2023 17:09:52 +0900 Subject: [PATCH] internal/ui: bug fix: replace initWindowMonitor usages with initMonitor There were two similar member variables internalWindowMonitor and initMonitor, and when SetMonitor is called, probably there were some inconsitency between them. This change fixes the issue by not using initWindowMonitor. Closes #2779 --- internal/ui/ui_glfw.go | 46 +++++++++----------------------------- internal/ui/window_glfw.go | 2 +- 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 69bc11da9..baf014545 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -87,7 +87,6 @@ type userInterfaceImpl struct { initFullscreen bool initCursorMode CursorMode initWindowDecorated bool - initWindowMonitor int initWindowPositionXInDIP int initWindowPositionYInDIP int initWindowWidthInDIP int @@ -141,7 +140,6 @@ func init() { maxWindowHeightInDIP: glfw.DontCare, initCursorMode: CursorModeVisible, initWindowDecorated: true, - initWindowMonitor: glfw.DontCare, initWindowPositionXInDIP: invalidPos, initWindowPositionYInDIP: invalidPos, initWindowWidthInDIP: 640, @@ -207,6 +205,9 @@ func initialize() error { } func (u *userInterfaceImpl) setInitMonitor(m *glfw.Monitor) { + u.m.Lock() + defer u.m.Unlock() + u.initMonitor = m u.initDeviceScaleFactor = u.deviceScaleFactor(m) // GetVideoMode must be called from the main thread, then call this here and record @@ -426,24 +427,6 @@ func (u *userInterfaceImpl) setIconImages(iconImages []image.Image) { u.m.Unlock() } -func (u *userInterfaceImpl) getInitWindowMonitor() int { - u.m.RLock() - v := u.initWindowMonitor - u.m.RUnlock() - return v -} - -func (u *userInterfaceImpl) setInitWindowMonitor(monitor int) { - if microsoftgdk.IsXbox() { - return - } - - u.m.Lock() - defer u.m.Unlock() - - u.initWindowMonitor = monitor -} - func (u *userInterfaceImpl) getInitWindowPositionInDIP() (int, int) { if microsoftgdk.IsXbox() { return 0, 0 @@ -797,7 +780,7 @@ func init() { // createWindow must be called from the main thread. // // createWindow does not set the position or size so far. -func (u *userInterfaceImpl) createWindow(width, height int, monitor int) error { +func (u *userInterfaceImpl) createWindow(width, height int, monitor *glfw.Monitor) error { if u.window != nil { panic("ui: u.window must not exist at createWindow") } @@ -809,12 +792,10 @@ func (u *userInterfaceImpl) createWindow(width, height int, monitor int) error { } // Set our target monitor if provided. This is required to prevent an initial window flash on the default monitor. - if monitor != glfw.DontCare { - m := theMonitors.monitorFromID(monitor) - x, y := m.m.GetPos() - px, py := InitialWindowPosition(m.m.GetVideoMode().Width, m.m.GetVideoMode().Height, width, height) - window.SetPos(x+px, y+py) - } + x, y := monitor.GetPos() + px, py := InitialWindowPosition(monitor.GetVideoMode().Width, monitor.GetVideoMode().Height, width, height) + window.SetPos(x+px, y+py) + initializeWindowAfterCreation(window) u.window = window @@ -1008,23 +989,16 @@ func (u *userInterfaceImpl) initOnMainThread(options *RunOptions) error { glfw.WindowHint(glfw.Visible, glfw.True) } - // Get our target monitor. - monitor := u.getInitWindowMonitor() - - if monitor != glfw.DontCare { - u.setInitMonitor(theMonitors.monitorFromID(monitor).m) - } - ww, wh := u.getInitWindowSizeInDIP() initW := int(u.dipToGLFWPixel(float64(ww), u.initMonitor)) initH := int(u.dipToGLFWPixel(float64(wh), u.initMonitor)) - if err := u.createWindow(initW, initH, monitor); err != nil { + if err := u.createWindow(initW, initH, u.initMonitor); err != nil { return err } // The position must be set before the size is set (#1982). // setWindowSize refers the current monitor's device scale. - // TODO: currentMonitor is very hard to use correctly. Refactor this. + // TODO: The window position is already set at createWindow. Unify the logic. wx, wy := u.getInitWindowPositionInDIP() // Force to put the window in the initial monitor (#1575). if wx < 0 { diff --git a/internal/ui/window_glfw.go b/internal/ui/window_glfw.go index 7a0177ece..c2799ca41 100644 --- a/internal/ui/window_glfw.go +++ b/internal/ui/window_glfw.go @@ -234,7 +234,7 @@ func (w *glfwWindow) SetMonitor(monitor *Monitor) { return } if !w.ui.isRunning() { - w.ui.setInitWindowMonitor(monitor.id) + w.ui.setInitMonitor(monitor.m) return } w.ui.mainThread.Call(func() {