From d9ddbe60f42a94d46528b3442f84ed6e94d17704 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 1 Aug 2024 11:30:25 +0200 Subject: [PATCH] layout,io/input: [API] change Context.Disabled to only disable events Also unexport Source.Enabled because the nil-ness of its embedded router is now an implementation detail. Fixes: https://todo.sr.ht/~eliasnaur/gio/605 Signed-off-by: Elias Naur --- io/input/router.go | 10 +++++----- layout/context.go | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/io/input/router.go b/io/input/router.go index de6efa34..fcca91cd 100644 --- a/io/input/router.go +++ b/io/input/router.go @@ -171,22 +171,22 @@ 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 +// enabled reports whether the source is enabled. Only enabled // Sources deliver events and respond to commands. -func (s Source) Enabled() bool { +func (s Source) enabled() bool { return s.r != nil } // 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 @@ -194,7 +194,7 @@ func (s Source) Focused(tag event.Tag) bool { // Event returns the next event that matches at least one of filters. 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 58ee4f86..bf6e8013 100644 --- a/layout/context.go +++ b/layout/context.go @@ -5,6 +5,7 @@ package layout import ( "time" + "gioui.org/io/event" "gioui.org/io/input" "gioui.org/io/system" "gioui.org/op" @@ -28,6 +29,7 @@ type Context struct { // Interested users must look up and populate these values manually. Locale system.Locale + disabled bool input.Source *op.Ops } @@ -42,9 +44,21 @@ func (c Context) Sp(v unit.Sp) int { return c.Metric.Sp(v) } -// Disabled returns a copy of this context with a disabled Source, -// blocking widgets from changing its state and receiving events. +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.Source = input.Source{} + c.disabled = true return c } -- 2.45.2