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 <pierre.curto@gmail.com>
This commit is contained in:
Pierre Curto
2022-01-31 18:31:00 +01:00
committed by Elias Naur
parent 668d0e7d79
commit 74acc90789
3 changed files with 40 additions and 35 deletions
-8
View File
@@ -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:
+31 -27
View File
@@ -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 {
+9
View File
@@ -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