Skip to content

Commit

Permalink
Remove surface empty
Browse files Browse the repository at this point in the history
  • Loading branch information
LennysLounge committed Feb 11, 2024
1 parent 8b3d088 commit 6c29d1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 55 deletions.
27 changes: 9 additions & 18 deletions src/dock_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ impl<Tab> std::ops::Index<SurfaceIndex> for DockState<Tab> {

#[inline(always)]
fn index(&self, index: SurfaceIndex) -> &Self::Output {
let Some(tree) = self
.surfaces
.get(&index)
.and_then(|surface| surface.node_tree())
else {
let Some(tree) = self.surfaces.get(&index).map(|surface| surface.node_tree()) else {
panic!("There did not exist a tree at surface index {}", index.0);
};
tree
Expand All @@ -60,7 +56,7 @@ impl<Tab> std::ops::IndexMut<SurfaceIndex> for DockState<Tab> {
let Some(tree) = self
.surfaces
.get_mut(&index)
.and_then(|surface| surface.node_tree_mut())
.map(|surface| surface.node_tree_mut())
else {
panic!("There did not exist a tree at surface index {}", index.0);
};
Expand Down Expand Up @@ -162,9 +158,7 @@ impl<Tab> DockState<Tab> {
/// Returns true if the specified surface exists and isn't [`Empty`](Surface::Empty).
#[inline]
pub fn is_surface_valid(&self, surface_index: SurfaceIndex) -> bool {
self.surfaces
.get(&surface_index)
.map_or(false, |surface| !surface.is_empty())
self.surfaces.get(&surface_index).is_some()
}

/// Returns a list of all valid [`SurfaceIndex`]es.
Expand Down Expand Up @@ -447,8 +441,7 @@ impl<Tab> DockState<Tab> {
pub fn iter_nodes(&self) -> impl Iterator<Item = &Node<Tab>> {
self.surfaces
.values()
.filter_map(|surface| surface.node_tree())
.flat_map(|nodes| nodes.iter())
.flat_map(|surface| surface.node_tree().iter())
}

/// Returns a new [`DockState`] while mapping and filtering the tab type.
Expand Down Expand Up @@ -476,7 +469,7 @@ impl<Tab> DockState<Tab> {
.iter()
.filter_map(|(idx, surface)| {
let surface = surface.filter_map_tabs(function.clone());
(!surface.is_empty()).then_some((*idx, surface))
(!surface.node_tree().is_empty()).then_some((*idx, surface))
})
.collect();
DockState {
Expand Down Expand Up @@ -542,7 +535,7 @@ impl<Tab> DockState<Tab> {
.values_mut()
.for_each(|surface| surface.retain_tabs(predicate.clone()));
self.surfaces
.retain(|idx, surface| idx.is_main() || !surface.is_empty());
.retain(|idx, surface| idx.is_main() || !surface.node_tree().is_empty());
}
}

Expand All @@ -560,11 +553,9 @@ where
///
/// See also: [`find_main_surface_tab`](DockState::find_main_surface_tab)
pub fn find_tab(&self, needle_tab: &Tab) -> Option<(SurfaceIndex, NodeIndex, TabIndex)> {
for &surface_index in self.valid_surface_indices().iter() {
if !self.surfaces[&surface_index].is_empty() {
if let Some((node_index, tab_index)) = self[surface_index].find_tab(needle_tab) {
return Some((surface_index, node_index, tab_index));
}
for (surface_index, surface) in self.surfaces.iter() {
if let Some((node_index, tab_index)) = surface.node_tree().find_tab(needle_tab) {
return Some((*surface_index, node_index, tab_index));
}
}
None
Expand Down
48 changes: 11 additions & 37 deletions src/dock_state/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use crate::{Node, NodeIndex, Tree, WindowState};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Surface<Tab> {
/// An empty surface, with nothing inside (practically, a null surface).
Empty,

/// The main surface of a [`DockState`](crate::DockState), only one should exist at surface index 0 at any one time.
Main(Tree<Tab>),

Expand All @@ -17,47 +14,34 @@ pub enum Surface<Tab> {
}

impl<Tab> Surface<Tab> {
/// Is this surface [`Empty`](Self::Empty) (in practice null)?
pub const fn is_empty(&self) -> bool {
matches!(self, Self::Empty)
}

/// Get access to the node tree of this surface.
pub fn node_tree(&self) -> Option<&Tree<Tab>> {
pub fn node_tree(&self) -> &Tree<Tab> {
match self {
Surface::Empty => None,
Surface::Main(tree) => Some(tree),
Surface::Window(tree, _) => Some(tree),
Surface::Main(tree) => tree,
Surface::Window(tree, _) => tree,
}
}

/// Get mutable access to the node tree of this surface.
pub fn node_tree_mut(&mut self) -> Option<&mut Tree<Tab>> {
pub fn node_tree_mut(&mut self) -> &mut Tree<Tab> {
match self {
Surface::Empty => None,
Surface::Main(tree) => Some(tree),
Surface::Window(tree, _) => Some(tree),
Surface::Main(tree) => tree,
Surface::Window(tree, _) => tree,
}
}

/// Returns an [`Iterator`] of nodes in this surface's tree.
///
/// If the surface is [`Empty`](Self::Empty), then the returned [`Iterator`] will be empty.
pub fn iter_nodes(&self) -> impl Iterator<Item = &Node<Tab>> {
match self.node_tree() {
Some(tree) => tree.iter(),
None => todo!(),
}
self.node_tree().iter()
}

/// Returns a mutable [`Iterator`] of nodes in this surface's tree.
///
/// If the surface is [`Empty`](Self::Empty), then the returned [`Iterator`] will be empty.
pub fn iter_nodes_mut(&mut self) -> impl Iterator<Item = &mut Node<Tab>> {
match self.node_tree_mut() {
Some(tree) => tree.iter_mut(),
None => todo!(),
}
self.node_tree_mut().iter_mut()
}

/// Returns an [`Iterator`] of **all** tabs in this surface's tree,
Expand All @@ -84,15 +68,9 @@ impl<Tab> Surface<Tab> {
F: Clone + FnMut(&Tab) -> Option<NewTab>,
{
match self {
Surface::Empty => Surface::Empty,
Surface::Main(tree) => Surface::Main(tree.filter_map_tabs(function)),
Surface::Window(tree, window_state) => {
let tree = tree.filter_map_tabs(function);
if tree.is_empty() {
Surface::Empty
} else {
Surface::Window(tree, window_state.clone())
}
Surface::Window(tree.filter_map_tabs(function), window_state.clone())
}
}
}
Expand Down Expand Up @@ -123,11 +101,7 @@ impl<Tab> Surface<Tab> {
where
F: Clone + FnMut(&mut Tab) -> bool,
{
if let Surface::Main(tree) | Surface::Window(tree, _) = self {
tree.retain_tabs(predicate);
if tree.is_empty() {
*self = Surface::Empty;
}
}
let (Surface::Main(tree) | Surface::Window(tree, _)) = self;
tree.retain_tabs(predicate);
}
}

0 comments on commit 6c29d1e

Please sign in to comment.