From 9999b6526185ba007b85a3d892406296159385bb Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 3 Feb 2022 01:00:05 +0900 Subject: [PATCH] internal/uidriver/glfw: bug fix: a callback must be registered at createWindow --- internal/uidriver/glfw/ui.go | 40 +++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 63ef1c722..7996007d0 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -711,6 +711,7 @@ func (u *UserInterface) createWindow() error { u.registerWindowSetSizeCallback() u.registerWindowCloseCallback() + u.registerWindowFramebufferSizeCallback() return nil } @@ -782,6 +783,28 @@ func (u *UserInterface) registerWindowCloseCallback() { u.window.SetCloseCallback(u.closeCallback) } +// registerWindowFramebufferSizeCallback must be called from the main thread. +func (u *UserInterface) registerWindowFramebufferSizeCallback() { + if u.defaultFramebufferSizeCallback == 0 { + // When the window gets resized (either by manual window resize or a window + // manager), glfw sends a framebuffer size callback which we need to handle (#1960). + // This event is the only way to handle the size change at least on i3 window manager. + u.defaultFramebufferSizeCallback = glfw.ToFramebufferSizeCallback(func(_ *glfw.Window, w, h int) { + if u.isFullscreen() { + return + } + + // The framebuffer size is always scaled by the device scale factor (#1975). + // See also the implementation in uiContext.updateOffscreen. + s := u.deviceScaleFactor(u.currentMonitor()) + ww := int(float64(w) / s) + wh := int(float64(h) / s) + u.setWindowSizeInDIP(ww, wh, u.isFullscreen()) + }) + } + u.window.SetFramebufferSizeCallback(u.defaultFramebufferSizeCallback) +} + // waitForFramebufferSizeCallback waits for GLFW's FramebufferSize callback. // f is a process executed after registering the callback. // If the callback is not invoked for a while, waitForFramebufferSizeCallback times out and return. @@ -905,23 +928,6 @@ func (u *UserInterface) init() error { u.window.Maximize() } - // When the window gets resized (either by manual window resize or a window - // manager), glfw sends a framebuffer size callback which we need to handle (#1960). - // This event is the only way to handle the size change at least on i3 window manager. - u.defaultFramebufferSizeCallback = glfw.ToFramebufferSizeCallback(func(_ *glfw.Window, w, h int) { - if u.isFullscreen() { - return - } - - // The framebuffer size is always scaled by the device scale factor (#1975). - // See also the implementation in uiContext.updateOffscreen. - s := u.deviceScaleFactor(u.currentMonitor()) - ww := int(float64(w) / s) - wh := int(float64(h) / s) - u.setWindowSizeInDIP(ww, wh, u.isFullscreen()) - }) - u.window.SetFramebufferSizeCallback(u.defaultFramebufferSizeCallback) - u.window.SetTitle(u.title) u.window.Show()