From 74acc90789aa514153189cc0429d13e37721c049 Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Mon, 31 Jan 2022 18:31:00 +0100 Subject: [PATCH] app: [API] add Window.Perform, replacing Close, Raise, Centered This new method performs user related actions on the Window, supporting custom window decorations. It supersedes the Close and Raise methods as well as the Centered window option. Signed-off-by: Pierre Curto --- app/os_wayland.go | 8 ------ app/window.go | 58 ++++++++++++++++++++++------------------- io/system/decoration.go | 9 +++++++ 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/app/os_wayland.go b/app/os_wayland.go index 56b97e60..840aa006 100644 --- a/app/os_wayland.go +++ b/app/os_wayland.go @@ -1024,14 +1024,6 @@ func (w *window) setTitle(prev, cnf Config) { func (w *window) Perform(actions system.Action) { walkActions(actions, func(action system.Action) { switch action { - case system.ActionMinimize: - w.Configure([]Option{Minimized.Option()}) - case system.ActionMaximize: - w.Configure([]Option{Maximized.Option()}) - case system.ActionUnmaximize: - w.Configure([]Option{Windowed.Option()}) - case system.ActionClose: - w.Close() case system.ActionMove: w.move() default: diff --git a/app/window.go b/app/window.go index 1f63922f..46edf0bc 100644 --- a/app/window.go +++ b/app/window.go @@ -322,17 +322,6 @@ func (w *Window) SetCursorName(name pointer.CursorName) { }) } -// Close the window. The window's event loop should exit when it receives -// system.DestroyEvent. -// -// Currently, only macOS, Windows, X11 and Wayland drivers implement this functionality, -// all others are stubbed. -func (w *Window) Close() { - w.driverDefer(func(d driver) { - w.closing = true - }) -} - // Run f in the same thread as the native window event loop, and wait for f to // return or the window to close. Run is guaranteed not to deadlock if it is // invoked during the handling of a ViewEvent, system.FrameEvent, @@ -737,7 +726,7 @@ func (w *Window) decorate(d driver, e system.FrameEvent, o *op.Ops) image.Point dims := style.Layout(gtx) op.Defer(o, rec.Stop()) // Update the window based on the actions on the decorations. - d.Perform(deco.Actions()) + w.Perform(deco.Actions()) // Offset to place the frame content below the decorations. size := image.Point{Y: dims.Size.Y} op.Offset(f32.Point{Y: float32(size.Y)}).Add(o) @@ -751,13 +740,37 @@ func (w *Window) decorate(d driver, e system.FrameEvent, o *op.Ops) image.Point return appSize } -// Raise requests that the platform bring this window to the top of all open windows. -// Some platforms do not allow this except under certain circumstances, such as when -// a window from the same application already has focus. If the platform does not -// support it, this method will do nothing. -func (w *Window) Raise() { +// Perform the actions on the window. +func (w *Window) Perform(actions system.Action) { w.driverDefer(func(d driver) { - d.Raise() + var options []Option + walkActions(actions, func(action system.Action) { + switch action { + case system.ActionMinimize: + options = append(options, Minimized.Option()) + case system.ActionMaximize: + options = append(options, Maximized.Option()) + case system.ActionUnmaximize: + options = append(options, Windowed.Option()) + case system.ActionRaise: + d.Raise() + case system.ActionCenter: + options = append(options, + func(m unit.Metric, cnf *Config) { + // Set the flag so the driver can later do the actual centering. + cnf.center = true + }) + case system.ActionClose: + w.closing = true + default: + return + } + actions &^= action + }) + if len(options) > 0 { + d.Configure(options) + } + d.Perform(actions) }) } @@ -789,15 +802,6 @@ func Size(w, h unit.Value) Option { } } -// Centered is an option to center the window on the screen. -// The option is ignored in Fullscreen mode and on Wayland. -func Centered() Option { - return func(m unit.Metric, cnf *Config) { - // Set the flag so the driver can later do the actual centering. - cnf.center = true - } -} - // MaxSize sets the maximum size of the window. func MaxSize(w, h unit.Value) Option { if w.V <= 0 { diff --git a/io/system/decoration.go b/io/system/decoration.go index ad008666..d8e07b55 100644 --- a/io/system/decoration.go +++ b/io/system/decoration.go @@ -18,7 +18,16 @@ const ( ActionUnmaximize // ActionFullscreen makes a window fullscreen. ActionFullscreen + // ActionRaise requests that the platform bring this window to the top of all open windows. + // Some platforms do not allow this except under certain circumstances, such as when + // a window from the same application already has focus. If the platform does not + // support it, this method will do nothing. + ActionRaise + // ActionCenter centers the window on the screen. + // It is ignored in Fullscreen mode and on Wayland. + ActionCenter // ActionClose closes a window. + // Only applicable on macOS, Windows, X11 and Wayland. ActionClose // ActionMove moves a window directed by the user. ActionMove