From fff2375470342c8a790a29ac046b7e0e3e1e4e57 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 24 Mar 2025 08:45:51 +0100 Subject: [PATCH] layout,io/input: move disabling events from layout.Context to input.Source The fix for #605 moved the disabling of event delivery from Source to Context to enable disabled Contexts to still react to commands (invalidate, focus etc.). However, that change in turn caused #641 where the exported Context.Source field would no longer know not to deliver events. This change partially reverts the fix for #605 by moving disabledness back to Source, fixing #641. Disabled Sources are left capable of executing commands, thus keeping #605 fixed. Thanks to Chris et al for keeping the use-cases straight enough for me to come up with this (hopefully final) fix. Signed-off-by: Elias Naur Fixes: https://todo.sr.ht/~eliasnaur/gio/641 References: https://todo.sr.ht/~eliasnaur/gio/605 --- io/input/router.go | 25 +++++++++++++++++-------- layout/context.go | 17 +---------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/io/input/router.go b/io/input/router.go index fcca91cd..c18ae066 100644 --- a/io/input/router.go +++ b/io/input/router.go @@ -62,7 +62,8 @@ type Router struct { // Source implements the interface between a Router and user interface widgets. // The zero-value Source is disabled. type Source struct { - r *Router + r *Router + disabled bool } // Command represents a request such as moving the focus, or initiating a clipboard read. @@ -171,30 +172,38 @@ func (q *Router) Source() Source { // Execute a command. func (s Source) Execute(c Command) { - if !s.enabled() { + if !s.Enabled() { return } s.r.execute(c) } -// enabled reports whether the source is enabled. Only enabled -// Sources deliver events and respond to commands. -func (s Source) enabled() bool { - return s.r != nil +// Disabled returns a copy of this source that don't deliver any events. +func (s Source) Disabled() Source { + s2 := s + s2.disabled = true + return s2 +} + +// Enabled reports whether the source is enabled. Only enabled +// Sources deliver events. +func (s Source) Enabled() bool { + return s.r != nil && !s.disabled } // Focused reports whether tag is focused, according to the most recent // [key.FocusEvent] delivered. func (s Source) Focused(tag event.Tag) bool { - if !s.enabled() { + if !s.Enabled() { return false } return s.r.state().keyState.focus == tag } // Event returns the next event that matches at least one of filters. +// If the source is disabled, no events will be reported. func (s Source) Event(filters ...event.Filter) (event.Event, bool) { - if !s.enabled() { + if !s.Enabled() { return nil, false } return s.r.Event(filters...) diff --git a/layout/context.go b/layout/context.go index bf6e8013..00d52f9b 100644 --- a/layout/context.go +++ b/layout/context.go @@ -5,7 +5,6 @@ package layout import ( "time" - "gioui.org/io/event" "gioui.org/io/input" "gioui.org/io/system" "gioui.org/op" @@ -29,7 +28,6 @@ type Context struct { // Interested users must look up and populate these values manually. Locale system.Locale - disabled bool input.Source *op.Ops } @@ -44,21 +42,8 @@ func (c Context) Sp(v unit.Sp) int { return c.Metric.Sp(v) } -func (c Context) Event(filters ...event.Filter) (event.Event, bool) { - if c.disabled { - return nil, false - } - return c.Source.Event(filters...) -} - -// Enabled reports whether this context is enabled. Disabled contexts -// don't report events. -func (c Context) Enabled() bool { - return !c.disabled -} - // Disabled returns a copy of this context that don't deliver any events. func (c Context) Disabled() Context { - c.disabled = true + c.Source = c.Source.Disabled() return c }