io/input,widget: [API] replace per-widget Focused with Source.Focused

Widgets have themselves as tags, by convention, and so it's possible to
replace the per-widget Focused methods with a general-purpose Source.
Focused query.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-11-28 10:35:51 -06:00
parent c3f2abebca
commit e59f91dfd0
9 changed files with 65 additions and 68 deletions
+2 -2
View File
@@ -318,8 +318,8 @@ func TestKeyRouting(t *testing.T) {
func assertFocus(t *testing.T, router *Router, expected event.Tag) {
t.Helper()
if got := router.state().focus; got != expected {
t.Errorf("expected %v to be focused, got %v", expected, got)
if !router.Source().Focused(expected) {
t.Errorf("expected %v to be focused", expected)
}
}
+9
View File
@@ -184,6 +184,15 @@ 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() {
return false
}
return s.r.state().keyState.focus == tag
}
// 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() {