From ed3a4a734ee334d52f684bfdde230465b00ab61c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 24 Aug 2020 00:33:18 +0900 Subject: [PATCH] uidriver/glfw: Refactoring --- internal/uidriver/glfw/ui.go | 14 ++++++-------- internal/uidriver/glfw/ui_darwin.go | 8 ++++---- internal/uidriver/glfw/ui_unix.go | 4 ++-- internal/uidriver/glfw/ui_windows.go | 6 +++--- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 2b10cec28..fe48a0733 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -145,12 +145,14 @@ func initialize() error { panic("glfw: glfw.CreateWindow must not return nil") } - // Create a window and leave it as it is: this affects the result of currentMonitorFromPosition. + // Create a window and set it: this affects toDeviceIndependentPixel and deviceScaleFactor. theUI.window = w - theUI.initMonitor = theUI.currentMonitor() + theUI.initMonitor = currentMonitorByOS(w) v := theUI.initMonitor.GetVideoMode() theUI.initFullscreenWidthInDP = int(theUI.toDeviceIndependentPixel(float64(v.Width))) theUI.initFullscreenHeightInDP = int(theUI.toDeviceIndependentPixel(float64(v.Height))) + theUI.window.Destroy() + theUI.window = nil return nil } @@ -671,10 +673,6 @@ func (u *UserInterface) createWindow() error { func (u *UserInterface) run() error { if err := u.t.Call(func() error { - // The window is created at initialize(). - u.window.Destroy() - u.window = nil - if u.Graphics().IsGL() { glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI) glfw.WindowHint(glfw.ContextVersionMajor, 2) @@ -1100,9 +1098,9 @@ func (u *UserInterface) currentMonitor() *glfw.Monitor { } // Getting a monitor from a window position is not reliable in general (e.g., when a window is put across - // multiple monitors). + // multiple monitors. A window position is not reliable before SetWindowPosition is called.). // Get the monitor which the current window belongs to. This requires OS API. - return u.currentMonitorFromPosition() + return currentMonitorByOS(u.window) } func (u *UserInterface) SetScreenTransparent(transparent bool) { diff --git a/internal/uidriver/glfw/ui_darwin.go b/internal/uidriver/glfw/ui_darwin.go index eb1f3ae43..3ce0925d9 100644 --- a/internal/uidriver/glfw/ui_darwin.go +++ b/internal/uidriver/glfw/ui_darwin.go @@ -55,11 +55,11 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) { return x, y } -func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor { +func currentMonitorByOS(w *glfw.Window) *glfw.Monitor { x := C.int(0) y := C.int(0) - // Note: [NSApp mainWindow] is nil when it doesn't have its border. Use u.window here. - win := u.window.GetCocoaWindow() + // Note: [NSApp mainWindow] is nil when it doesn't have its border. Use w here. + win := w.GetCocoaWindow() C.currentMonitorPos(win, &x, &y) for _, m := range glfw.GetMonitors() { mx, my := m.GetPos() @@ -68,7 +68,7 @@ func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor { } } - if m, ok := getCachedMonitor(u.window.GetPos()); ok { + if m, ok := getCachedMonitor(w.GetPos()); ok { return m.m } return glfw.GetPrimaryMonitor() diff --git a/internal/uidriver/glfw/ui_unix.go b/internal/uidriver/glfw/ui_unix.go index f6b84b51f..b3d1f6771 100644 --- a/internal/uidriver/glfw/ui_unix.go +++ b/internal/uidriver/glfw/ui_unix.go @@ -32,9 +32,9 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) { return x, y } -func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor { +func currentMonitorByOS(w *glfw.Window) *glfw.Monitor { // TODO: Implement this correctly. (#1119). - if cm, ok := getCachedMonitor(u.window.GetPos()); ok { + if cm, ok := getCachedMonitor(w.GetPos()); ok { return cm.m } return glfw.GetPrimaryMonitor() diff --git a/internal/uidriver/glfw/ui_windows.go b/internal/uidriver/glfw/ui_windows.go index cf05e70fb..613128e07 100644 --- a/internal/uidriver/glfw/ui_windows.go +++ b/internal/uidriver/glfw/ui_windows.go @@ -120,15 +120,15 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) { return x, y } -func (u *UserInterface) currentMonitorFromPosition() *glfw.Monitor { +func currentMonitorByOS(glfww *glfw.Window) *glfw.Monitor { fallback := func() *glfw.Monitor { - if m, ok := getCachedMonitor(u.window.GetPos()); ok { + if m, ok := getCachedMonitor(glfww.GetPos()); ok { return m.m } return glfw.GetPrimaryMonitor() } - // TODO: Should we use u.window.GetWin32Window() here? + // TODO: Should we use glfww.GetWin32Window() here? w, err := getActiveWindow() if err != nil { panic(err)