Skip to content

Commit

Permalink
Add Tree::active_tiles for getting visible tiles (#42)
Browse files Browse the repository at this point in the history
This function returns all tiles that are visible and not in a background
(inactive) tab.
  • Loading branch information
emilk authored Jan 3, 2024
1 parent b6e4fd4 commit 001f63a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 58 deletions.
92 changes: 46 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions examples/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ struct MyApp {

#[cfg_attr(feature = "serde", serde(skip))]
behavior: TreeBehavior,

#[cfg_attr(feature = "serde", serde(skip))]
last_tree_debug: String,
}

impl Default for MyApp {
Expand Down Expand Up @@ -208,7 +205,6 @@ impl Default for MyApp {
Self {
tree,
behavior: Default::default(),
last_tree_debug: Default::default(),
}
}
}
Expand All @@ -220,6 +216,26 @@ impl eframe::App for MyApp {
*self = Default::default();
}
self.behavior.ui(ui);

ui.separator();

ui.collapsing("Tree", |ui| {
ui.style_mut().wrap = Some(false);
let tree_debug = format!("{:#?}", self.tree);
ui.monospace(&tree_debug);
});

ui.separator();

ui.collapsing("Active tiles", |ui| {
let active = self.tree.active_tiles();
for tile_id in active {
use egui_tiles::Behavior as _;
let name = self.behavior.tab_title_for_tile(&self.tree.tiles, tile_id);
ui.label(format!("{} - {tile_id:?}", name.text()));
}
});

ui.separator();

if let Some(root) = self.tree.root() {
Expand All @@ -235,14 +251,6 @@ impl eframe::App for MyApp {
tabs.set_active(new_child);
}
}

ui.separator();
ui.style_mut().wrap = Some(false);
let tree_debug = format!("{:#?}", self.tree);
ui.monospace(&tree_debug);
if self.last_tree_debug != tree_debug {
self.last_tree_debug = tree_debug;
}
});

egui::CentralPanel::default().show(ctx, |ui| {
Expand Down
17 changes: 17 additions & 0 deletions src/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Container {
}
}

/// All the childrens of this container.
pub fn children(&self) -> impl Iterator<Item = &TileId> {
match self {
Self::Tabs(tabs) => itertools::Either::Left(tabs.children.iter()),
Expand All @@ -120,6 +121,22 @@ impl Container {
}
}

/// All the active childrens of this container.
///
/// For tabs, this is just the active tab.
/// For other containers, it is all children.
pub fn active_children(&self) -> impl Iterator<Item = &TileId> {
match self {
Self::Tabs(tabs) => {
itertools::Either::Left(itertools::Either::Left(tabs.active.iter()))
}
Self::Linear(linear) => {
itertools::Either::Left(itertools::Either::Right(linear.children.iter()))
}
Self::Grid(grid) => itertools::Either::Right(grid.children()),
}
}

/// If we have exactly one child, return it
pub fn only_child(&self) -> Option<TileId> {
let mut only_child = None;
Expand Down
14 changes: 14 additions & 0 deletions src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ impl<Pane> Tiles<Pane> {
self.set_visible(tile_id, !self.is_visible(tile_id));
}

/// This excludes all tiles that invisible or are inactive tabs, recursively.
pub(crate) fn collect_acticve_tiles(&self, tile_id: TileId, tiles: &mut Vec<TileId>) {
if !self.is_visible(tile_id) {
return;
}
tiles.push(tile_id);

if let Some(Tile::Container(container)) = self.get(tile_id) {
for &child_id in container.active_children() {
self.collect_acticve_tiles(child_id, tiles);
}
}
}

pub fn insert(&mut self, id: TileId, tile: Tile<Pane>) {
self.tiles.insert(id, tile);
}
Expand Down
Loading

0 comments on commit 001f63a

Please sign in to comment.