Skip to content

Commit

Permalink
Adjust the api for filtering and mapping
Browse files Browse the repository at this point in the history
- Node does not have an empty variant in this branch so the filter
method must return an option instead
- The question of how to handle the retain case is still open.
  • Loading branch information
LennysLounge committed Feb 11, 2024
1 parent 34f159a commit 8d7da82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
8 changes: 1 addition & 7 deletions src/dock_state/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,7 @@ impl<Tab> Tree<Tab> {
} = self;
let nodes = nodes
.iter()
.filter_map(|node| {
let node = node.filter_map_tabs(function.clone());
match node {
Node::Leaf { ref tabs, .. } => (!tabs.is_empty()).then_some(node),
_ => Some(node),
}
})
.filter_map(|node| node.filter_map_tabs(function.clone()))
.collect();
Tree {
nodes,
Expand Down
47 changes: 23 additions & 24 deletions src/dock_state/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ impl<Tab> Node<Tab> {
}

/// Returns a new [`Node`] while mapping and filtering the tab type.
/// If this [`Node`] remains empty, it will change to [`Node::Empty`].
pub fn filter_map_tabs<F, NewTab>(&self, function: F) -> Node<NewTab>
/// If this [`Node`] is empty after filtering, `None` is returned.
pub fn filter_map_tabs<F, NewTab>(&self, function: F) -> Option<Node<NewTab>>
where
F: FnMut(&Tab) -> Option<NewTab>,
{
Expand All @@ -285,43 +285,41 @@ impl<Tab> Node<Tab> {
active,
scroll,
} => {
// TODO(LennysLounge): Fix this
todo!()
// let tabs: Vec<_> = tabs.iter().filter_map(function).collect();
// if tabs.is_empty() {
// Node::Empty
// } else {
// Node::Leaf {
// rect: *rect,
// viewport: *viewport,
// tabs,
// active: *active,
// scroll: *scroll,
// }
// }
let tabs: Vec<_> = tabs.iter().filter_map(function).collect();
if tabs.is_empty() {
None
} else {
Some(Node::Leaf {
rect: *rect,
viewport: *viewport,
tabs,
active: *active,
scroll: *scroll,
})
}
}
Node::Vertical {
rect,
fraction,
above,
below,
} => Node::Vertical {
} => Some(Node::Vertical {
rect: *rect,
fraction: *fraction,
above: *above,
below: *below,
},
}),
Node::Horizontal {
rect,
fraction,
left,
right,
} => Node::Horizontal {
} => Some(Node::Horizontal {
rect: *rect,
fraction: *fraction,
left: *left,
right: *right,
},
}),
}
}

Expand All @@ -331,11 +329,12 @@ impl<Tab> Node<Tab> {
F: FnMut(&Tab) -> NewTab,
{
self.filter_map_tabs(move |tab| Some(function(tab)))
.expect("Mapping tabs in a node should always produce a node")
}

/// Returns a new [`Node`] while filtering the tab type.
/// If this [`Node`] remains empty, it will change to [`Node::Empty`].
pub fn filter_tabs<F>(&self, mut predicate: F) -> Node<Tab>
/// If this [`Node`] is empty after filtering, `None` is returned.
pub fn filter_tabs<F>(&self, mut predicate: F) -> Option<Node<Tab>>
where
F: Clone + FnMut(&Tab) -> bool,
Tab: Clone,
Expand All @@ -353,7 +352,7 @@ impl<Tab> Node<Tab> {
tabs.retain_mut(predicate);
}

// TODO(LennyLounge): Fix this
todo!();
// TODO(LennyLounge): If no tabs are retained, then the node should
// be removed. How should this api work then?
}
}

0 comments on commit 8d7da82

Please sign in to comment.