@@ -166,12 +166,12 @@ pub struct TabNavigation<'w, 's> {
166166} 
167167
168168impl  TabNavigation < ' _ ,  ' _ >  { 
169-     /// Navigate to the desired focusable entity. 
169+     /// Navigate to the desired focusable entity, relative to the current focused entity . 
170170     /// 
171171     /// Change the [`NavAction`] to navigate in a different direction. 
172172     /// Focusable entities are determined by the presence of the [`TabIndex`] component. 
173173     /// 
174-      /// If no focusable entities are found , then this function will return either the first 
174+      /// If there is no currently focused entity , then this function will return either the first 
175175     /// or last focusable entity, depending on the direction of navigation. For example, if 
176176     /// `action` is `Next` and no focusable entities are found, then this function will return 
177177     /// the first focusable entity. 
@@ -198,13 +198,46 @@ impl TabNavigation<'_, '_> {
198198                } ) 
199199        } ) ; 
200200
201+         self . navigate_internal ( focus. 0 ,  action,  tabgroup) 
202+     } 
203+ 
204+     /// Initialize focus to a focusable child of a container, either the first or last 
205+      /// depending on [`NavAction`]. This assumes that the parent entity has a [`TabGroup`] 
206+      /// component. 
207+      /// 
208+      /// Focusable entities are determined by the presence of the [`TabIndex`] component. 
209+      pub  fn  initialize ( 
210+         & self , 
211+         parent :  Entity , 
212+         action :  NavAction , 
213+     )  -> Result < Entity ,  TabNavigationError >  { 
214+         // If there are no tab groups, then there are no focusable entities. 
215+         if  self . tabgroup_query . is_empty ( )  { 
216+             return  Err ( TabNavigationError :: NoTabGroups ) ; 
217+         } 
218+ 
219+         // Look for the tab group on the parent entity. 
220+         match  self . tabgroup_query . get ( parent)  { 
221+             Ok ( tabgroup)  => self . navigate_internal ( None ,  action,  Some ( ( parent,  tabgroup. 1 ) ) ) , 
222+             Err ( _)  => Err ( TabNavigationError :: NoTabGroups ) , 
223+         } 
224+     } 
225+ 
226+     pub  fn  navigate_internal ( 
227+         & self , 
228+         focus :  Option < Entity > , 
229+         action :  NavAction , 
230+         tabgroup :  Option < ( Entity ,  & TabGroup ) > , 
231+     )  -> Result < Entity ,  TabNavigationError >  { 
201232        let  navigation_result = self . navigate_in_group ( tabgroup,  focus,  action) ; 
202233
203234        match  navigation_result { 
204235            Ok ( entity)  => { 
205-                 if  focus. 0 . is_some ( )  && tabgroup. is_none ( )  { 
236+                 if  let  Some ( previous_focus)  = focus
237+                     && tabgroup. is_none ( ) 
238+                 { 
206239                    Err ( TabNavigationError :: NoTabGroupForCurrentFocus  { 
207-                         previous_focus :  focus . 0 . unwrap ( ) , 
240+                         previous_focus, 
208241                        new_focus :  entity, 
209242                    } ) 
210243                }  else  { 
@@ -218,7 +251,7 @@ impl TabNavigation<'_, '_> {
218251    fn  navigate_in_group ( 
219252        & self , 
220253        tabgroup :  Option < ( Entity ,  & TabGroup ) > , 
221-         focus :  & InputFocus , 
254+         focus :  Option < Entity > , 
222255        action :  NavAction , 
223256    )  -> Result < Entity ,  TabNavigationError >  { 
224257        // List of all focusable entities found. 
@@ -268,7 +301,7 @@ impl TabNavigation<'_, '_> {
268301            } 
269302        } ) ; 
270303
271-         let  index = focusable. iter ( ) . position ( |e| Some ( e. 0 )  == focus. 0 ) ; 
304+         let  index = focusable. iter ( ) . position ( |e| Some ( e. 0 )  == focus) ; 
272305        let  count = focusable. len ( ) ; 
273306        let  next = match  ( index,  action)  { 
274307            ( Some ( idx) ,  NavAction :: Next )  => ( idx + 1 ) . rem_euclid ( count) , 
0 commit comments