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 }