From c13cdd570d21a047e3945d18734b8dd6755c3fdb Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 4 Mar 2023 00:38:37 -0500 Subject: [PATCH] Implemented all focus methods except for HandleFocus I am dreading this --- elements/core/propagator.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/elements/core/propagator.go b/elements/core/propagator.go index 4c6b2ad..7f7c2c8 100644 --- a/elements/core/propagator.go +++ b/elements/core/propagator.go @@ -21,6 +21,9 @@ type Propagator struct { iterator ChildIterator drags [10]elements.MouseTarget focused bool + + onFocusRequest func () (granted bool) + onFocusMotionRequest func (input.KeynavDirection) (granted bool) } // NewPropagator creates a new event propagator that uses the specified iterator @@ -45,7 +48,9 @@ func (propagator *Propagator) Focused () (focused bool) { // Focus focuses this element, if its parent element grants the // request. func (propagator *Propagator) Focus () { - // TODO + if propagator.onFocusRequest != nil { + propagator.onFocusRequest() + } } // HandleFocus causes this element to mark itself as focused. If the @@ -60,23 +65,29 @@ func (propagator *Propagator) HandleFocus (direction input.KeynavDirection) (acc // HandleDeselection causes this element to mark itself and all of its children // as unfocused. func (propagator *Propagator) HandleUnfocus () { - // TODO + propagator.forFocusable (func (child elements.Focusable) bool { + child.HandleUnfocus() + return true + }) + propagator.focused = false } // OnFocusRequest sets a function to be called when this element wants its // parent element to focus it. Parent elements should return true if the request // was granted, and false if it was not. If the parent element returns true, the // element acts as if a HandleFocus call was made with KeynavDirectionNeutral. -func (propagator *Propagator) OnFocusRequest (func () (granted bool)) { - // TODO +func (propagator *Propagator) OnFocusRequest (callback func () (granted bool)) { + propagator.onFocusRequest = callback } // OnFocusMotionRequest sets a function to be called when this element wants its // parent element to focus the element behind or in front of it, depending on // the specified direction. Parent elements should return true if the request // was granted, and false if it was not. -func (propagator *Propagator) OnFocusMotionRequest (func (direction input.KeynavDirection) (granted bool)) { - // TODO +func (propagator *Propagator) OnFocusMotionRequest ( + callback func (direction input.KeynavDirection) (granted bool), +) { + propagator.onFocusMotionRequest = callback } // HandleKeyDown propogates the keyboard event to the currently selected child.