Skip to content
Snippets Groups Projects
Unverified Commit 0572f601 authored by John's avatar John Committed by GitHub
Browse files

Merge pull request #114 from StarArawn/revert-109-tree-order

Revert "kayak_core: Improve `Tree` methods"
parents 5801520d 4410cd98
No related branches found
No related tags found
No related merge requests found
...@@ -591,9 +591,9 @@ impl KayakContext { ...@@ -591,9 +591,9 @@ impl KayakContext {
#[allow(dead_code)] #[allow(dead_code)]
fn get_all_parents(&self, current: Index, parents: &mut Vec<Index>) { fn get_all_parents(&self, current: Index, parents: &mut Vec<Index>) {
if let Some(parent) = self.widget_manager.tree.get_parent(current) { if let Some(parent) = self.widget_manager.tree.parents.get(&current) {
parents.push(parent); parents.push(*parent);
self.get_all_parents(parent, parents); self.get_all_parents(*parent, parents);
} }
} }
......
...@@ -253,7 +253,7 @@ impl EventDispatcher { ...@@ -253,7 +253,7 @@ impl EventDispatcher {
let mut event_stream = Vec::<Event>::new(); let mut event_stream = Vec::<Event>::new();
let mut states: HashMap<EventType, EventState> = HashMap::new(); let mut states: HashMap<EventType, EventState> = HashMap::new();
let root = if let Some(root) = widget_manager.node_tree.root() { let root = if let Some(root) = widget_manager.node_tree.root_node {
root root
} else { } else {
return event_stream; return event_stream;
...@@ -351,7 +351,7 @@ impl EventDispatcher { ...@@ -351,7 +351,7 @@ impl EventDispatcher {
// --- Push Children to Stack --- // // --- Push Children to Stack --- //
if enter_children { if enter_children {
if let Some(children) = widget_manager.node_tree.get_children(current) { if let Some(children) = widget_manager.node_tree.children.get(&current) {
for child in children { for child in children {
stack.push((*child, depth + 1)); stack.push((*child, depth + 1));
} }
......
...@@ -36,7 +36,7 @@ impl FocusTree { ...@@ -36,7 +36,7 @@ impl FocusTree {
} }
} }
if self.tree.root().is_none() { if self.tree.root_node.is_none() {
// Set root node // Set root node
self.tree.add(index, None); self.tree.add(index, None);
self.focus(index); self.focus(index);
...@@ -49,7 +49,7 @@ impl FocusTree { ...@@ -49,7 +49,7 @@ impl FocusTree {
self.blur(); self.blur();
} }
if self.tree.root() == Some(index) { if self.tree.root_node == Some(index) {
self.tree.remove(index); self.tree.remove(index);
} else { } else {
self.tree.remove_and_reparent(index); self.tree.remove_and_reparent(index);
...@@ -76,7 +76,7 @@ impl FocusTree { ...@@ -76,7 +76,7 @@ impl FocusTree {
/// ///
/// This returns focus to the root node /// This returns focus to the root node
pub fn blur(&mut self) { pub fn blur(&mut self) {
self.current_focus = self.tree.root(); self.current_focus = self.tree.root_node;
} }
/// Get the currently focused index /// Get the currently focused index
...@@ -120,7 +120,7 @@ impl FocusTree { ...@@ -120,7 +120,7 @@ impl FocusTree {
} }
// Default to root node to begin the cycle again // Default to root node to begin the cycle again
self.tree.root() self.tree.root_node
} }
/// Peek the previous focusable index without actually changing focus /// Peek the previous focusable index without actually changing focus
...@@ -149,7 +149,7 @@ impl FocusTree { ...@@ -149,7 +149,7 @@ impl FocusTree {
return Some(next); return Some(next);
} }
self.tree.root() self.tree.root_node
} }
pub fn tree(&self) -> &Tree { pub fn tree(&self) -> &Tree {
......
This diff is collapsed.
...@@ -63,7 +63,7 @@ impl WidgetManager { ...@@ -63,7 +63,7 @@ impl WidgetManager {
pub fn dirty(&mut self, force: bool) { pub fn dirty(&mut self, force: bool) {
// Force tree to re-render from root. // Force tree to re-render from root.
if let Ok(mut dirty_nodes) = self.dirty_nodes.lock() { if let Ok(mut dirty_nodes) = self.dirty_nodes.lock() {
dirty_nodes.insert(self.tree.root().unwrap()); dirty_nodes.insert(self.tree.root_node.unwrap());
if force { if force {
for (node_index, _) in self.current_widgets.iter() { for (node_index, _) in self.current_widgets.iter() {
...@@ -81,7 +81,7 @@ impl WidgetManager { ...@@ -81,7 +81,7 @@ impl WidgetManager {
parent: Option<Index>, parent: Option<Index>,
) -> (bool, Index) { ) -> (bool, Index) {
let widget_id = if let Some(parent) = parent.clone() { let widget_id = if let Some(parent) = parent.clone() {
if let Some(parent_children) = self.tree.get_children(parent) { if let Some(parent_children) = self.tree.children.get_mut(&parent) {
parent_children.get(index).cloned() parent_children.get(index).cloned()
} else { } else {
None None
...@@ -203,10 +203,10 @@ impl WidgetManager { ...@@ -203,10 +203,10 @@ impl WidgetManager {
// 2. Unresolved widget prop styles // 2. Unresolved widget prop styles
// 3. Unresolved default styles // 3. Unresolved default styles
let parent_styles = let parent_styles =
if let Some(parent_widget_id) = self.tree.get_parent(dirty_node_index) { if let Some(parent_widget_id) = self.tree.parents.get(&dirty_node_index) {
if let Some(parent) = self.nodes[parent_widget_id].as_ref() { if let Some(parent) = self.nodes[*parent_widget_id].as_ref() {
parent.resolved_styles.clone() parent.resolved_styles.clone()
} else if let Some(parent) = self.current_widgets[parent_widget_id].as_ref() { } else if let Some(parent) = self.current_widgets[*parent_widget_id].as_ref() {
if let Some(styles) = parent.get_props().get_styles() { if let Some(styles) = parent.get_props().get_styles() {
styles styles
} else { } else {
...@@ -220,8 +220,9 @@ impl WidgetManager { ...@@ -220,8 +220,9 @@ impl WidgetManager {
}; };
// Get parent Z // Get parent Z
let parent_z = if let Some(parent_widget_id) = self.tree.get_parent(dirty_node_index) { let parent_z = if let Some(parent_widget_id) = self.tree.parents.get(&dirty_node_index)
if let Some(parent) = &self.nodes[parent_widget_id] { {
if let Some(parent) = &self.nodes[*parent_widget_id] {
parent.z parent.z
} else { } else {
-1.0 -1.0
...@@ -251,9 +252,10 @@ impl WidgetManager { ...@@ -251,9 +252,10 @@ impl WidgetManager {
let children = self let children = self
.tree .tree
.get_children(dirty_node_index) .children
.unwrap_or_default() .get(&dirty_node_index)
.to_vec(); .cloned()
.unwrap_or(vec![]);
let mut node = NodeBuilder::empty() let mut node = NodeBuilder::empty()
.with_id(dirty_node_index) .with_id(dirty_node_index)
...@@ -364,8 +366,8 @@ impl WidgetManager { ...@@ -364,8 +366,8 @@ impl WidgetManager {
prev_clip = new_prev_clip.clone(); prev_clip = new_prev_clip.clone();
if let Some(children) = node_tree.get_children(current_node) { if node_tree.children.contains_key(&current_node) {
for child in children { for child in node_tree.children.get(&current_node).unwrap() {
main_z_index += 1.0; main_z_index += 1.0;
render_primitives.extend(Self::recurse_node_tree_to_build_primitives( render_primitives.extend(Self::recurse_node_tree_to_build_primitives(
node_tree, node_tree,
...@@ -404,7 +406,7 @@ impl WidgetManager { ...@@ -404,7 +406,7 @@ impl WidgetManager {
} }
pub fn build_render_primitives(&self) -> Vec<RenderPrimitive> { pub fn build_render_primitives(&self) -> Vec<RenderPrimitive> {
if self.node_tree.root().is_none() { if self.node_tree.root_node.is_none() {
return vec![]; return vec![];
} }
...@@ -412,16 +414,20 @@ impl WidgetManager { ...@@ -412,16 +414,20 @@ impl WidgetManager {
&self.node_tree, &self.node_tree,
&self.layout_cache, &self.layout_cache,
&self.nodes, &self.nodes,
self.node_tree.root().unwrap(), self.node_tree.root_node.unwrap(),
0.0, 0.0,
RenderPrimitive::Empty, RenderPrimitive::Empty,
) )
} }
fn build_nodes_tree(&mut self) -> Tree { fn build_nodes_tree(&mut self) -> Tree {
let mut tree = Tree::default();
let (root_node_id, _) = self.current_widgets.iter().next().unwrap(); let (root_node_id, _) = self.current_widgets.iter().next().unwrap();
let mut tree = Tree::new(root_node_id); tree.root_node = Some(root_node_id);
tree.add_children(self.get_valid_node_children(root_node_id), root_node_id); tree.children.insert(
tree.root_node.unwrap(),
self.get_valid_node_children(tree.root_node.unwrap()),
);
let old_focus = self.focus_tree.current(); let old_focus = self.focus_tree.current();
self.focus_tree.clear(); self.focus_tree.clear();
...@@ -432,10 +438,12 @@ impl WidgetManager { ...@@ -432,10 +438,12 @@ impl WidgetManager {
if let Some(widget_styles) = widget_styles { if let Some(widget_styles) = widget_styles {
// Only add widgets who have renderable nodes. // Only add widgets who have renderable nodes.
if widget_styles.render_command.resolve() != RenderCommand::Empty { if widget_styles.render_command.resolve() != RenderCommand::Empty {
let valid_parent = self.get_valid_parent(widget_id);
tree.insert_direct(widget_id, valid_parent);
let valid_children = self.get_valid_node_children(widget_id); let valid_children = self.get_valid_node_children(widget_id);
tree.insert_children_direct(valid_children, widget_id); tree.children.insert(widget_id, valid_children);
let valid_parent = self.get_valid_parent(widget_id);
if let Some(valid_parent) = valid_parent {
tree.parents.insert(widget_id, valid_parent);
}
} }
} }
...@@ -451,14 +459,12 @@ impl WidgetManager { ...@@ -451,14 +459,12 @@ impl WidgetManager {
} }
} }
tree.recalculate_depths(None);
tree tree
} }
pub fn get_valid_node_children(&self, node_id: Index) -> Vec<Index> { pub fn get_valid_node_children(&self, node_id: Index) -> Vec<Index> {
let mut children = Vec::new(); let mut children = Vec::new();
if let Some(node_children) = self.tree.get_children(node_id) { if let Some(node_children) = self.tree.children.get(&node_id) {
for child_id in node_children { for child_id in node_children {
if let Some(child_widget) = &self.current_widgets[*child_id] { if let Some(child_widget) = &self.current_widgets[*child_id] {
if let Some(child_styles) = child_widget.get_props().get_styles() { if let Some(child_styles) = child_widget.get_props().get_styles() {
...@@ -478,13 +484,13 @@ impl WidgetManager { ...@@ -478,13 +484,13 @@ impl WidgetManager {
} }
pub fn get_valid_parent(&self, node_id: Index) -> Option<Index> { pub fn get_valid_parent(&self, node_id: Index) -> Option<Index> {
if let Some(parent_id) = self.tree.get_parent(node_id) { if let Some(parent_id) = self.tree.parents.get(&node_id) {
if let Some(parent_widget) = &self.nodes[parent_id] { if let Some(parent_widget) = &self.nodes[*parent_id] {
if parent_widget.resolved_styles.render_command.resolve() != RenderCommand::Empty { if parent_widget.resolved_styles.render_command.resolve() != RenderCommand::Empty {
return Some(parent_id); return Some(*parent_id);
} }
} }
return self.get_valid_parent(parent_id); return self.get_valid_parent(*parent_id);
} }
// assert!(node_id.into_raw_parts().0 == 0); // assert!(node_id.into_raw_parts().0 == 0);
None None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment