Skip to content

Commit

Permalink
internal/uidriver/glfw: Bug fix: Treat pixel units correctly
Browse files Browse the repository at this point in the history
Updates #1385
  • Loading branch information
hajimehoshi committed Apr 16, 2021
1 parent 99b2b5c commit 585f173
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
62 changes: 39 additions & 23 deletions internal/uidriver/glfw/ui.go
Expand Up @@ -53,13 +53,15 @@ type UserInterface struct {
window *glfw.Window

// windowWidth and windowHeight represents a window size.
// The unit is device-dependent pixels.
windowWidth int
windowHeight int
minWindowWidth int
minWindowHeight int
maxWindowWidth int
maxWindowHeight int
// The units are device-dependent pixels.
windowWidth int
windowHeight int

// The units are device-independent pixels.
minWindowWidthInDP int
minWindowHeightInDP int
maxWindowWidthInDP int
maxWindowHeightInDP int

running uint32
toChangeSize bool
Expand Down Expand Up @@ -114,10 +116,10 @@ const (
var (
theUI = &UserInterface{
runnableOnUnfocused: true,
minWindowWidth: glfw.DontCare,
minWindowHeight: glfw.DontCare,
maxWindowWidth: glfw.DontCare,
maxWindowHeight: glfw.DontCare,
minWindowWidthInDP: glfw.DontCare,
minWindowHeightInDP: glfw.DontCare,
maxWindowWidthInDP: glfw.DontCare,
maxWindowHeightInDP: glfw.DontCare,
origPosX: invalidPos,
origPosY: invalidPos,
initVsync: true,
Expand Down Expand Up @@ -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()
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()
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
}
u.minWindowWidth = minw
u.minWindowHeight = minh
u.maxWindowWidth = maxw
u.maxWindowHeight = maxh
u.minWindowWidthInDP = minw
u.minWindowHeightInDP = minh
u.maxWindowWidthInDP = maxw
u.maxWindowHeightInDP = maxh
return true
}

Expand Down Expand Up @@ -1008,24 +1010,34 @@ func (u *UserInterface) swapBuffers() {

// updateWindowSizeLimits must be called from the main thread.
func (u *UserInterface) updateWindowSizeLimits() {
minw, minh, maxw, maxh := u.getWindowSizeLimits()
minw, minh, maxw, maxh := u.getWindowSizeLimitsInDP()
if minw < 0 {
minw = glfw.DontCare
} else {
minw = int(u.toGLFWPixel(float64(minw)))
}
if minh < 0 {
minh = glfw.DontCare
} else {
minh = int(u.toGLFWPixel(float64(minh)))
}
if maxw < 0 {
maxw = glfw.DontCare
} else {
maxw = int(u.toGLFWPixel(float64(maxw)))
}
if maxh < 0 {
maxh = glfw.DontCare
} else {
maxh = int(u.toGLFWPixel(float64(maxh)))
}
u.window.SetSizeLimits(minw, minh, maxw, maxh)
}

func (u *UserInterface) adjustWindowSizeBasedOnSizeLimits(width, height int) (int, int) {
minw, minh, maxw, maxh := u.getWindowSizeLimits()
// 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) {
minw, minh, maxw, maxh := u.getWindowSizeLimitsInDP()
if minw >= 0 && width < minw {
width = minw
}
Expand All @@ -1043,7 +1055,11 @@ func (u *UserInterface) adjustWindowSizeBasedOnSizeLimits(width, height int) (in

// setWindowSize must be called from the main thread.
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() {
return
Expand Down
30 changes: 3 additions & 27 deletions internal/uidriver/glfw/window.go
Expand Up @@ -229,7 +229,7 @@ func (w *window) setPosition(x, y int) {
func (w *window) Size() (int, int) {
if !w.ui.isRunning() {
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)))
wh := int(w.ui.fromGLFWPixel(float64(w.ui.windowHeight)))
Expand All @@ -250,36 +250,12 @@ func (w *window) SetSize(width, height int) {
}

func (w *window) SizeLimits() (minw, minh, maxw, maxh int) {
minw, minh, maxw, maxh = w.ui.getWindowSizeLimits()
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)))
}
minw, minh, maxw, maxh = w.ui.getWindowSizeLimitsInDP()
return
}

func (w *window) SetSizeLimits(minw, minh, maxw, maxh int) {
if minw >= 0 {
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) {
if !w.ui.setWindowSizeLimitsInDP(minw, minh, maxw, maxh) {
return
}
if !w.ui.isRunning() {
Expand Down

0 comments on commit 585f173

Please sign in to comment.