diff --git a/unshell-gui/src/app/mod.rs b/unshell-gui/src/app/mod.rs index c6dfe51..8aa71b8 100644 --- a/unshell-gui/src/app/mod.rs +++ b/unshell-gui/src/app/mod.rs @@ -1,7 +1,7 @@ mod app; mod windows; -use std::collections::HashMap; +use std::collections::HashSet; use crate::{ app::windows::WindowWrapper, auth::Auth, config::Config, flowchart::FlowChart, @@ -14,7 +14,7 @@ use egui_tiles::{TileId, Tree}; pub struct AppState { pub auth: Auth, - pub open_windows: HashMap, + pub open_windows: HashSet, pub flowchart: FlowChart, pub config: Config, @@ -31,14 +31,26 @@ impl AppState { .iter() .enumerate() { - let enabled = self.open_windows.contains_key(&key); + let enabled = self.open_windows.contains(&key); if ui.selectable_label(enabled, *name).clicked() { if enabled { - let tid = *self.open_windows.get(&key).unwrap(); - tree.remove_recursively(tid); - tree.tiles.remove(tid); - self.open_windows.remove(&key); + // if let Some(tid) = Self::find_pane_id(*key, tree) { + // tree.remove_recursively(*tid); + // tree.tiles.remove(*tid); + // self.open_windows.remove(&key); + // } + // let tid = *self.open_windows.get(&key).unwrap(); + + match Self::find_pane_id(*key, tree) { + Some(tid) => { + let tid = tid.clone(); + tree.remove_recursively(tid); + tree.tiles.remove(tid); + self.open_windows.remove(&key); + } + None => unreachable!(), + } // if self.open_windows.is_empty() } else { @@ -63,11 +75,25 @@ impl AppState { tree.move_tile_to_container(tid, tree.root.unwrap().clone(), n, true); } } - self.open_windows.insert(key.clone(), tid); + self.open_windows.insert(key.clone()); } } } } + + fn find_pane_id(window_type: AppWindow, tree: &Tree) -> Option<&TileId> { + for (tid, window) in tree.tiles.iter() { + match window { + egui_tiles::Tile::Pane(pane) => { + if pane.window == window_type { + return Some(tid); + } + } + egui_tiles::Tile::Container(_) => {} + } + } + None + } } #[derive(Clone, Copy, serde::Deserialize, serde::Serialize, PartialEq, Eq, Hash)] diff --git a/unshell-gui/src/app/windows.rs b/unshell-gui/src/app/windows.rs index a40ece5..7f9a8da 100644 --- a/unshell-gui/src/app/windows.rs +++ b/unshell-gui/src/app/windows.rs @@ -63,6 +63,28 @@ impl egui_tiles::Behavior for AppState { ret } + fn is_tab_closable( + &self, + tiles: &egui_tiles::Tiles, + tile_id: egui_tiles::TileId, + ) -> bool { + match tiles.get(tile_id).unwrap() { + egui_tiles::Tile::Pane(_) => true, + egui_tiles::Tile::Container(_) => false, + } + } + + fn on_tab_close( + &mut self, + tiles: &mut egui_tiles::Tiles, + tile_id: egui_tiles::TileId, + ) -> bool { + match tiles.get(tile_id).unwrap() { + egui_tiles::Tile::Pane(pane) => self.open_windows.remove(&pane.window), + egui_tiles::Tile::Container(_) => false, + } + } + fn tab_bar_color(&self, visuals: &egui::Visuals) -> egui::Color32 { visuals.panel_fill // same as the tab contents } diff --git a/unshell-gui/src/config/mod.rs b/unshell-gui/src/config/mod.rs index 350160d..f2c33c3 100644 --- a/unshell-gui/src/config/mod.rs +++ b/unshell-gui/src/config/mod.rs @@ -41,8 +41,8 @@ impl Default for Config { impl Config { pub fn update(&mut self, auth: &mut Auth, ui: &mut egui::Ui) { let state_lock = self.state.lock().unwrap(); - let mut tree_list_none = state_lock.trees.is_none(); - let mut key_list_none = state_lock.tree_keys.is_none(); + let tree_list_none = state_lock.trees.is_none(); + let key_list_none = state_lock.tree_keys.is_none(); let is_requesting = state_lock.is_requesting; if !tree_list_none @@ -153,8 +153,7 @@ impl Config { pub fn titlebar_buttons(&mut self, ui: &mut egui::Ui) { let state_lock = self.state.lock().unwrap(); let mut tree_list_none = state_lock.trees.is_none(); - let mut key_list_none = state_lock.tree_keys.is_none(); - let is_requesting = state_lock.is_requesting; + drop(state_lock); if ui.button("Refresh").clicked() { @@ -165,7 +164,6 @@ impl Config { drop(state_lock); tree_list_none = true; - key_list_none = true; } if !tree_list_none { @@ -184,7 +182,6 @@ impl Config { if before.ne(&self.tree_option) { (*self.state.lock().unwrap()).tree_keys = None; - key_list_none = true; } } }