From 18659ef4abc60bedf018dd1b8ea7e7e236166078 Mon Sep 17 00:00:00 2001 From: Tom Lister Date: Wed, 9 Feb 2022 21:11:25 +1100 Subject: [PATCH] internal/ui: add SetWindowKeepAspectRatio (#1985) Updates #1804 --- internal/glfw/glfw_notwindows.go | 8 +++++++ internal/glfw/glfw_windows.go | 9 +++++++ internal/ui/ui_glfw.go | 41 ++++++++++++++++++++++---------- internal/ui/window_glfw.go | 10 ++++++++ internal/ui/window_null.go | 3 +++ window.go | 7 ++++++ 6 files changed, 66 insertions(+), 12 deletions(-) diff --git a/internal/glfw/glfw_notwindows.go b/internal/glfw/glfw_notwindows.go index 76c225830..58d85aa4e 100644 --- a/internal/glfw/glfw_notwindows.go +++ b/internal/glfw/glfw_notwindows.go @@ -208,6 +208,14 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) { w.w.SetSizeLimits(minw, minh, maxw, maxh) } +func (w *Window) SetKeepAspectRatio(keep bool) { + n, d := glfw.DontCare, glfw.DontCare + if keep { + n, d = w.GetSize() + } + w.w.SetAspectRatio(n, d) +} + func (w *Window) SetIcon(images []image.Image) { w.w.SetIcon(images) } diff --git a/internal/glfw/glfw_windows.go b/internal/glfw/glfw_windows.go index a9154faac..afcc8f853 100644 --- a/internal/glfw/glfw_windows.go +++ b/internal/glfw/glfw_windows.go @@ -261,6 +261,15 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) { panicError() } +func (w *Window) SetKeepAspectRatio(keep bool) { + n, d := -1, -1 + if keep { + n, d = w.GetSize() + } + glfwDLL.call("glfwSetWindowAspectRatio", w.w, uintptr(n), uintptr(d)) + panicError() +} + func (w *Window) SetIcon(images []image.Image) { gimgs := make([]glfwImage, len(images)) defer runtime.KeepAlive(gimgs) diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 742e2a3fa..a6dd7db89 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -85,18 +85,19 @@ type UserInterface struct { initFullscreenWidthInDIP int initFullscreenHeightInDIP int - initFullscreen bool - initCursorMode CursorMode - initWindowDecorated bool - initWindowResizable bool - initWindowPositionXInDIP int - initWindowPositionYInDIP int - initWindowWidthInDIP int - initWindowHeightInDIP int - initWindowFloating bool - initWindowMaximized bool - initScreenTransparent bool - initFocused bool + initFullscreen bool + initCursorMode CursorMode + initWindowDecorated bool + initWindowResizable bool + initWindowPositionXInDIP int + initWindowPositionYInDIP int + initWindowWidthInDIP int + initWindowHeightInDIP int + initWindowFloating bool + initWindowMaximized bool + initWindowKeepAspectRatio bool + initScreenTransparent bool + initFocused bool fpsModeInited bool @@ -279,6 +280,19 @@ func (u *UserInterface) setWindowSizeLimitsInDIP(minw, minh, maxw, maxh int) boo return true } +func (u *UserInterface) isInitWindowKeepAspectRatio() bool { + u.m.RLock() + v := u.initWindowKeepAspectRatio + u.m.RUnlock() + return v +} + +func (u *UserInterface) setInitWindowKeepAspectRatio(keep bool) { + u.m.Lock() + u.initWindowKeepAspectRatio = keep + u.m.Unlock() +} + func (u *UserInterface) isInitFullscreen() bool { u.m.RLock() v := u.initFullscreen @@ -918,6 +932,9 @@ func (u *UserInterface) init() error { u.window.Maximize() } + keepAspectRatio := u.isInitWindowKeepAspectRatio() + u.window.SetKeepAspectRatio(keepAspectRatio) + u.window.Show() if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok { diff --git a/internal/ui/window_glfw.go b/internal/ui/window_glfw.go index b22620e0d..c0c234167 100644 --- a/internal/ui/window_glfw.go +++ b/internal/ui/window_glfw.go @@ -227,6 +227,16 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) { w.ui.t.Call(w.ui.updateWindowSizeLimits) } +func (w *Window) SetKeepAspectRatio(keep bool) { + if !w.ui.isRunning() { + w.ui.setInitWindowKeepAspectRatio(keep) + return + } + w.ui.t.Call(func() { + w.ui.window.SetKeepAspectRatio(keep) + }) +} + func (w *Window) SetIcon(iconImages []image.Image) { // The icons are actually set at (*UserInterface).loop. w.ui.setIconImages(iconImages) diff --git a/internal/ui/window_null.go b/internal/ui/window_null.go index 861f67c56..b3d1ce543 100644 --- a/internal/ui/window_null.go +++ b/internal/ui/window_null.go @@ -79,6 +79,9 @@ func (*Window) IsMinimized() bool { return false } +func (*Window) SetKeepAspectRatio(keep bool) { +} + func (*Window) SetIcon(iconImages []image.Image) { } diff --git a/window.go b/window.go index 67e0f0a0f..88fb9d1f6 100644 --- a/window.go +++ b/window.go @@ -72,6 +72,13 @@ func SetWindowResizable(resizable bool) { ui.Get().Window().SetResizable(resizable) } +// SetWindowKeepAspectRatio sets whether the window should keep its aspect ratio while resizing. +func SetWindowKeepAspectRatio(keep bool) { + if w := ui.Get().Window(); w != nil { + w.SetKeepAspectRatio(keep) + } +} + // SetWindowTitle sets the title of the window. // // SetWindowTitle does nothing on browsers or mobiles.