From 6722c7960a999cae855c78562de56ca7af88e989 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 31 Jul 2024 18:39:30 +0200 Subject: [PATCH] widget: [API] change Decorations to leave the user in control of window state As suggested by ~egonelbre, Decorations should not be the source of truth for the windows state, because external gestures may also change state. This breaking change removes Decorations.Perform and exposes Maximized as a bool which is the user's responsibility to set. Fixes: https://todo.sr.ht/~eliasnaur/gio/600 Signed-off-by: Elias Naur --- app/window.go | 2 +- widget/decorations.go | 29 ++++++----------------------- widget/material/decorations.go | 2 +- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/app/window.go b/app/window.go index 79a8c6a8..e1615b9a 100644 --- a/app/window.go +++ b/app/window.go @@ -656,6 +656,7 @@ func (w *Window) processEvent(e event.Event) bool { } w.coalesced.view = &e2 case ConfigEvent: + w.decorations.Decorations.Maximized = e2.Config.Mode == Maximized wasFocused := w.decorations.Config.Focused w.decorations.Config = e2.Config e2.Config = w.effectiveConfig() @@ -801,7 +802,6 @@ func (w *Window) decorate(e FrameEvent, o *op.Ops) image.Point { default: panic(fmt.Errorf("unknown WindowMode %v", m)) } - deco.Perform(actions) gtx := layout.Context{ Ops: o, Now: e.Now, diff --git a/widget/decorations.go b/widget/decorations.go index 86c974d3..9f402f56 100644 --- a/widget/decorations.go +++ b/widget/decorations.go @@ -11,8 +11,11 @@ import ( // Decorations handles the states of window decorations. type Decorations struct { + // Maximized controls the look and behaviour of the maximize + // button. It is the user's responsibility to set Maximized + // according to the window state reported through [app.ConfigEvent]. + Maximized bool clicks map[int]*Clickable - maximized bool } // LayoutMove lays out the widget that makes a window movable. @@ -40,17 +43,6 @@ func (d *Decorations) Clickable(action system.Action) *Clickable { return click } -// Perform updates the decorations as if the specified actions were -// performed by the user. -func (d *Decorations) Perform(actions system.Action) { - if actions&system.ActionMaximize != 0 { - d.maximized = true - } - if actions&(system.ActionUnmaximize|system.ActionMinimize|system.ActionFullscreen) != 0 { - d.maximized = false - } -} - // Update the state and return the set of actions activated by the user. func (d *Decorations) Update(gtx layout.Context) system.Action { var actions system.Action @@ -60,21 +52,12 @@ func (d *Decorations) Update(gtx layout.Context) system.Action { } action := system.Action(1 << idx) switch { - case action == system.ActionMaximize && d.maximized: + case action == system.ActionMaximize && d.Maximized: action = system.ActionUnmaximize - case action == system.ActionUnmaximize && !d.maximized: + case action == system.ActionUnmaximize && !d.Maximized: action = system.ActionMaximize } - switch action { - case system.ActionMaximize, system.ActionUnmaximize: - d.maximized = !d.maximized - } actions |= action } return actions } - -// Maximized returns whether the window is maximized. -func (d *Decorations) Maximized() bool { - return d.maximized -} diff --git a/widget/material/decorations.go b/widget/material/decorations.go index 2c15a104..6dab1b6d 100644 --- a/widget/material/decorations.go +++ b/widget/material/decorations.go @@ -75,7 +75,7 @@ func (d DecorationsStyle) layoutDecorations(gtx layout.Context) layout.Dimension case system.ActionMinimize: w = minimizeWindow case system.ActionMaximize: - if d.Decorations.Maximized() { + if d.Decorations.Maximized { w = maximizedWindow } else { w = maximizeWindow