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)
}
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)
}

View File

@ -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)

View File

@ -95,6 +95,7 @@ type UserInterface struct {
initWindowHeightInDIP int
initWindowFloating bool
initWindowMaximized bool
initWindowKeepAspectRatio bool
initScreenTransparent bool
initFocused 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 {

View File

@ -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)

View File

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

View File

@ -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.