internal/ui: add SetWindowKeepAspectRatio (#1985)

Updates #1804
This commit is contained in:
Tom Lister 2022-02-09 21:11:25 +11:00 committed by GitHub
parent 180eb483bb
commit 18659ef4ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 12 deletions

View File

@ -208,6 +208,14 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
w.w.SetSizeLimits(minw, minh, maxw, maxh) 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) { func (w *Window) SetIcon(images []image.Image) {
w.w.SetIcon(images) w.w.SetIcon(images)
} }

View File

@ -261,6 +261,15 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
panicError() 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) { func (w *Window) SetIcon(images []image.Image) {
gimgs := make([]glfwImage, len(images)) gimgs := make([]glfwImage, len(images))
defer runtime.KeepAlive(gimgs) defer runtime.KeepAlive(gimgs)

View File

@ -85,18 +85,19 @@ type UserInterface struct {
initFullscreenWidthInDIP int initFullscreenWidthInDIP int
initFullscreenHeightInDIP int initFullscreenHeightInDIP int
initFullscreen bool initFullscreen bool
initCursorMode CursorMode initCursorMode CursorMode
initWindowDecorated bool initWindowDecorated bool
initWindowResizable bool initWindowResizable bool
initWindowPositionXInDIP int initWindowPositionXInDIP int
initWindowPositionYInDIP int initWindowPositionYInDIP int
initWindowWidthInDIP int initWindowWidthInDIP int
initWindowHeightInDIP int initWindowHeightInDIP int
initWindowFloating bool initWindowFloating bool
initWindowMaximized bool initWindowMaximized bool
initScreenTransparent bool initWindowKeepAspectRatio bool
initFocused bool initScreenTransparent bool
initFocused bool
fpsModeInited bool fpsModeInited bool
@ -279,6 +280,19 @@ func (u *UserInterface) setWindowSizeLimitsInDIP(minw, minh, maxw, maxh int) boo
return true 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 { func (u *UserInterface) isInitFullscreen() bool {
u.m.RLock() u.m.RLock()
v := u.initFullscreen v := u.initFullscreen
@ -918,6 +932,9 @@ func (u *UserInterface) init() error {
u.window.Maximize() u.window.Maximize()
} }
keepAspectRatio := u.isInitWindowKeepAspectRatio()
u.window.SetKeepAspectRatio(keepAspectRatio)
u.window.Show() u.window.Show()
if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok { if g, ok := Graphics().(interface{ SetWindow(uintptr) }); ok {

View File

@ -227,6 +227,16 @@ func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
w.ui.t.Call(w.ui.updateWindowSizeLimits) 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) { func (w *Window) SetIcon(iconImages []image.Image) {
// The icons are actually set at (*UserInterface).loop. // The icons are actually set at (*UserInterface).loop.
w.ui.setIconImages(iconImages) w.ui.setIconImages(iconImages)

View File

@ -79,6 +79,9 @@ func (*Window) IsMinimized() bool {
return false return false
} }
func (*Window) SetKeepAspectRatio(keep bool) {
}
func (*Window) SetIcon(iconImages []image.Image) { func (*Window) SetIcon(iconImages []image.Image) {
} }

View File

@ -72,6 +72,13 @@ func SetWindowResizable(resizable bool) {
ui.Get().Window().SetResizable(resizable) 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 sets the title of the window.
// //
// SetWindowTitle does nothing on browsers or mobiles. // SetWindowTitle does nothing on browsers or mobiles.