internal/uidriver/glfw: Bug fix: Treat pixel units correctly

Updates #1385
This commit is contained in:
Hajime Hoshi 2021-04-17 04:43:20 +09:00
parent 99b2b5c1ee
commit 585f173d1c
2 changed files with 42 additions and 50 deletions

View File

@ -53,13 +53,15 @@ type UserInterface struct {
window *glfw.Window window *glfw.Window
// windowWidth and windowHeight represents a window size. // windowWidth and windowHeight represents a window size.
// The unit is device-dependent pixels. // The units are device-dependent pixels.
windowWidth int windowWidth int
windowHeight int windowHeight int
minWindowWidth int
minWindowHeight int // The units are device-independent pixels.
maxWindowWidth int minWindowWidthInDP int
maxWindowHeight int minWindowHeightInDP int
maxWindowWidthInDP int
maxWindowHeightInDP int
running uint32 running uint32
toChangeSize bool toChangeSize bool
@ -114,10 +116,10 @@ const (
var ( var (
theUI = &UserInterface{ theUI = &UserInterface{
runnableOnUnfocused: true, runnableOnUnfocused: true,
minWindowWidth: glfw.DontCare, minWindowWidthInDP: glfw.DontCare,
minWindowHeight: glfw.DontCare, minWindowHeightInDP: glfw.DontCare,
maxWindowWidth: glfw.DontCare, maxWindowWidthInDP: glfw.DontCare,
maxWindowHeight: glfw.DontCare, maxWindowHeightInDP: glfw.DontCare,
origPosX: invalidPos, origPosX: invalidPos,
origPosY: invalidPos, origPosY: invalidPos,
initVsync: true, initVsync: true,
@ -243,22 +245,22 @@ func (u *UserInterface) setRunning(running bool) {
} }
} }
func (u *UserInterface) getWindowSizeLimits() (minw, minh, maxw, maxh int) { func (u *UserInterface) getWindowSizeLimitsInDP() (minw, minh, maxw, maxh int) {
u.m.RLock() u.m.RLock()
defer u.m.RUnlock() defer u.m.RUnlock()
return u.minWindowWidth, u.minWindowHeight, u.maxWindowWidth, u.maxWindowHeight return u.minWindowWidthInDP, u.minWindowHeightInDP, u.maxWindowWidthInDP, u.maxWindowHeightInDP
} }
func (u *UserInterface) setWindowSizeLimits(minw, minh, maxw, maxh int) bool { func (u *UserInterface) setWindowSizeLimitsInDP(minw, minh, maxw, maxh int) bool {
u.m.RLock() u.m.RLock()
defer u.m.RUnlock() defer u.m.RUnlock()
if u.minWindowWidth == minw && u.minWindowHeight == minh && u.maxWindowWidth == maxw && u.maxWindowHeight == maxh { if u.minWindowWidthInDP == minw && u.minWindowHeightInDP == minh && u.maxWindowWidthInDP == maxw && u.maxWindowHeightInDP == maxh {
return false return false
} }
u.minWindowWidth = minw u.minWindowWidthInDP = minw
u.minWindowHeight = minh u.minWindowHeightInDP = minh
u.maxWindowWidth = maxw u.maxWindowWidthInDP = maxw
u.maxWindowHeight = maxh u.maxWindowHeightInDP = maxh
return true return true
} }
@ -1008,24 +1010,34 @@ func (u *UserInterface) swapBuffers() {
// updateWindowSizeLimits must be called from the main thread. // updateWindowSizeLimits must be called from the main thread.
func (u *UserInterface) updateWindowSizeLimits() { func (u *UserInterface) updateWindowSizeLimits() {
minw, minh, maxw, maxh := u.getWindowSizeLimits() minw, minh, maxw, maxh := u.getWindowSizeLimitsInDP()
if minw < 0 { if minw < 0 {
minw = glfw.DontCare minw = glfw.DontCare
} else {
minw = int(u.toGLFWPixel(float64(minw)))
} }
if minh < 0 { if minh < 0 {
minh = glfw.DontCare minh = glfw.DontCare
} else {
minh = int(u.toGLFWPixel(float64(minh)))
} }
if maxw < 0 { if maxw < 0 {
maxw = glfw.DontCare maxw = glfw.DontCare
} else {
maxw = int(u.toGLFWPixel(float64(maxw)))
} }
if maxh < 0 { if maxh < 0 {
maxh = glfw.DontCare maxh = glfw.DontCare
} else {
maxh = int(u.toGLFWPixel(float64(maxh)))
} }
u.window.SetSizeLimits(minw, minh, maxw, maxh) u.window.SetSizeLimits(minw, minh, maxw, maxh)
} }
func (u *UserInterface) adjustWindowSizeBasedOnSizeLimits(width, height int) (int, int) { // adjustWindowSizeBasedOnSizeLimitsInDP adjust the size based on the window size limits.
minw, minh, maxw, maxh := u.getWindowSizeLimits() // width and height are in device-independent pixels.
func (u *UserInterface) adjustWindowSizeBasedOnSizeLimitsInDP(width, height int) (int, int) {
minw, minh, maxw, maxh := u.getWindowSizeLimitsInDP()
if minw >= 0 && width < minw { if minw >= 0 && width < minw {
width = minw width = minw
} }
@ -1043,7 +1055,11 @@ func (u *UserInterface) adjustWindowSizeBasedOnSizeLimits(width, height int) (in
// setWindowSize must be called from the main thread. // setWindowSize must be called from the main thread.
func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) { func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) {
width, height = u.adjustWindowSizeBasedOnSizeLimits(width, height) 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)))
if u.windowWidth == width && u.windowHeight == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == u.deviceScaleFactor() { if u.windowWidth == width && u.windowHeight == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == u.deviceScaleFactor() {
return return

View File

@ -229,7 +229,7 @@ func (w *window) setPosition(x, y int) {
func (w *window) Size() (int, int) { func (w *window) Size() (int, int) {
if !w.ui.isRunning() { if !w.ui.isRunning() {
ww, wh := w.ui.getInitWindowSize() ww, wh := w.ui.getInitWindowSize()
return w.ui.adjustWindowSizeBasedOnSizeLimits(ww, wh) return w.ui.adjustWindowSizeBasedOnSizeLimitsInDP(ww, wh)
} }
ww := int(w.ui.fromGLFWPixel(float64(w.ui.windowWidth))) ww := int(w.ui.fromGLFWPixel(float64(w.ui.windowWidth)))
wh := int(w.ui.fromGLFWPixel(float64(w.ui.windowHeight))) wh := int(w.ui.fromGLFWPixel(float64(w.ui.windowHeight)))
@ -250,36 +250,12 @@ func (w *window) SetSize(width, height int) {
} }
func (w *window) SizeLimits() (minw, minh, maxw, maxh int) { func (w *window) SizeLimits() (minw, minh, maxw, maxh int) {
minw, minh, maxw, maxh = w.ui.getWindowSizeLimits() minw, minh, maxw, maxh = w.ui.getWindowSizeLimitsInDP()
if minw >= 0 {
minw = int(w.ui.fromGLFWPixel(float64(minw)))
}
if minh >= 0 {
minh = int(w.ui.fromGLFWPixel(float64(minh)))
}
if maxw >= 0 {
maxw = int(w.ui.fromGLFWPixel(float64(maxw)))
}
if maxh >= 0 {
maxh = int(w.ui.fromGLFWPixel(float64(maxh)))
}
return return
} }
func (w *window) SetSizeLimits(minw, minh, maxw, maxh int) { func (w *window) SetSizeLimits(minw, minh, maxw, maxh int) {
if minw >= 0 { if !w.ui.setWindowSizeLimitsInDP(minw, minh, maxw, maxh) {
minw = int(w.ui.toGLFWPixel(float64(minw)))
}
if minh >= 0 {
minh = int(w.ui.toGLFWPixel(float64(minh)))
}
if maxw >= 0 {
maxw = int(w.ui.toGLFWPixel(float64(maxw)))
}
if maxh >= 0 {
maxh = int(w.ui.toGLFWPixel(float64(maxh)))
}
if !w.ui.setWindowSizeLimits(minw, minh, maxw, maxh) {
return return
} }
if !w.ui.isRunning() { if !w.ui.isRunning() {