From b5d4c834b8b1116468cc1722ba0f413401d4dddf Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 18 Apr 2021 00:56:35 +0900 Subject: [PATCH] internal/uidriver/glfw: Bug fix: More precise window size adjustment Before this change, setWindowSize converts the size in device- independent pixels once, invokes adjustWindowSizeBasedOnSizeLimitsInDP, and then restore the values in device-dependent pixels. This might introduce a slight error and then SetSize be unexpectedly invoked. Such SetSize call with a slightly different size might not invoke the FramebufferSize callback, and then this blocked forever when maximizing the window. This change fixes this by adjusting the size limits instead of the window size so that the window size is not modified unexpectedly. Closes #1577 --- internal/uidriver/glfw/ui.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 010cd226e..6cdddbd70 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -245,6 +245,15 @@ func (u *UserInterface) setRunning(running bool) { } } +func (u *UserInterface) getWindowSizeLimits() (minw, minh, maxw, maxh int) { + u.m.RLock() + defer u.m.RUnlock() + return int(u.toGLFWPixel(float64(u.minWindowWidthInDP))), + int(u.toGLFWPixel(float64(u.minWindowHeightInDP))), + int(u.toGLFWPixel(float64(u.maxWindowWidthInDP))), + int(u.toGLFWPixel(float64(u.maxWindowHeightInDP))) +} + func (u *UserInterface) getWindowSizeLimitsInDP() (minw, minh, maxw, maxh int) { u.m.RLock() defer u.m.RUnlock() @@ -1028,6 +1037,25 @@ func (u *UserInterface) updateWindowSizeLimits() { u.window.SetSizeLimits(minw, minh, maxw, maxh) } +// adjustWindowSizeBasedOnSizeLimitsInDP adjust the size based on the window size limits. +// width and height are in device-dependent pixels. +func (u *UserInterface) adjustWindowSizeBasedOnSizeLimits(width, height int) (int, int) { + minw, minh, maxw, maxh := u.getWindowSizeLimits() + if minw >= 0 && width < minw { + width = minw + } + if minh >= 0 && height < minh { + height = minh + } + if maxw >= 0 && width > maxw { + width = maxw + } + if maxh >= 0 && height > maxh { + height = maxh + } + return width, height +} + // adjustWindowSizeBasedOnSizeLimitsInDP adjust the size based on the window size limits. // width and height are in device-independent pixels. func (u *UserInterface) adjustWindowSizeBasedOnSizeLimitsInDP(width, height int) (int, int) { @@ -1049,11 +1077,7 @@ func (u *UserInterface) adjustWindowSizeBasedOnSizeLimitsInDP(width, height int) // setWindowSize must be called from the main thread. func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) { - wdp := int(u.fromGLFWPixel(float64(width))) - hdp := int(u.fromGLFWPixel(float64(height))) - wdp, hdp = u.adjustWindowSizeBasedOnSizeLimitsInDP(wdp, hdp) - width = int(u.toGLFWPixel(float64(wdp))) - height = int(u.toGLFWPixel(float64(hdp))) + width, height = u.adjustWindowSizeBasedOnSizeLimits(width, height) if u.windowWidth == width && u.windowHeight == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == u.deviceScaleFactor() { return