瀏覽代碼

clean up and document focus system

Evan Almloff 3 年之前
父節點
當前提交
dd7784d205
共有 2 個文件被更改,包括 12 次插入26 次删除
  1. 2 0
      packages/native-core/src/utils.rs
  2. 10 26
      packages/tui/src/focus.rs

+ 2 - 0
packages/native-core/src/utils.rs

@@ -78,6 +78,7 @@ impl PersistantElementIter {
                 }
             })
             .collect();
+        // if any element is removed in the chain, remove it and its children from the stack
         if let Some(r) = self
             .stack
             .iter()
@@ -165,6 +166,7 @@ impl PersistantElementIter {
 
     /// get the previous element
     pub fn prev<S: State>(&mut self, rdom: &RealDom<S>) -> ElementProduced {
+        // recursively add the last child element to the stack
         fn push_back<S: State>(
             stack: &mut smallvec::SmallVec<[(ElementId, NodePosition); 5]>,
             new_node: ElementId,

+ 10 - 26
packages/tui/src/focus.rs

@@ -120,11 +120,10 @@ impl FocusState {
                 return false;
             }
         }
+        // the id that started focused to track when a loop has happened
         let mut loop_marker_id = self.last_focused_id;
         let focus_level = &mut self.focus_level;
         let mut next_focus = None;
-        let starting_focus_level = *focus_level;
-        let mut focus_level_changed = false;
 
         loop {
             let new = if forward {
@@ -175,9 +174,6 @@ impl FocusState {
 
                 if let Some(level) = closest_level {
                     *focus_level = level;
-                    if *focus_level != starting_focus_level {
-                        focus_level_changed = true;
-                    }
                 } else {
                     if forward {
                         *focus_level = FocusLevel::Unfocusable;
@@ -185,11 +181,6 @@ impl FocusState {
                         *focus_level = FocusLevel::Focusable;
                     }
                 }
-
-                // if the focus level looped, we are done
-                if *focus_level == starting_focus_level && focus_level_changed {
-                    break;
-                }
             }
 
             // once we have looked at all the elements exit the loop
@@ -209,28 +200,21 @@ impl FocusState {
             };
             if after_previous_focused && current_level.focusable() {
                 if current_level == *focus_level {
-                    next_focus = Some((new_id, current_level));
+                    next_focus = Some(new_id);
                     break;
                 }
             }
         }
 
-        if let Some((id, order)) = next_focus {
-            if order.focusable() {
-                rdom[id].state.focused = true;
-                if let Some(old) = self.last_focused_id.replace(id) {
-                    rdom[old].state.focused = false;
-                }
-                // reset the position to the currently focused element
-                while if forward {
-                    self.focus_iter.next(&rdom).id()
-                } else {
-                    self.focus_iter.prev(&rdom).id()
-                } != id
-                {}
-                self.dirty = true;
-                return true;
+        if let Some(id) = next_focus {
+            rdom[id].state.focused = true;
+            if let Some(old) = self.last_focused_id.replace(id) {
+                rdom[old].state.focused = false;
             }
+            // reset the position to the currently focused element
+            while self.focus_iter.next(&rdom).id() != id {}
+            self.dirty = true;
+            return true;
         }
 
         false