diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index 8a814a5..ef741b6 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -384,31 +384,14 @@ impl AppState { /// arrive as files of a folder of their own in the temporary directory. fn load_demo(&mut self) { use voelin_ui_vm::dev::logic as sample; - static FOLDERS: AtomicUsize = AtomicUsize::new(0); self.vm.bookmarks = sample::bookmarks(); self.vm.settings.last_voice = Some(sample::last_voice()); self.vm.current = Some(sample::DEMO); - let folder = std::env::temp_dir().join(format!( - "voelin-gpui-demo-{}-{}", - std::process::id(), - FOLDERS.fetch_add(1, Ordering::Relaxed) - )); - let address = self.vm.bookmarks.first().map(|b| b.address.clone()).unwrap_or_default(); - let pictures = match std::fs::create_dir_all(&folder) { - Ok(()) => { - self.demo_dir = Some(folder.clone()); - sample::pictures(&address, |name, png| { - let path = folder.join(name); - if let Err(e) = std::fs::write(&path, png) { - warn!(%e, "a sample picture was not written"); - } - path - }) - } - Err(e) => { - warn!(%e, "the sample pictures have no folder"); - Vec::new() - } + let pictures = if self.switches.demo_pictures { + let address = self.vm.bookmarks.first().map(|b| b.address.clone()).unwrap_or_default(); + self.write_demo_pictures(&address) + } else { + Vec::new() }; for event in sample::server_events().into_iter().chain(pictures) { let update = self.handle(event); @@ -428,6 +411,31 @@ impl AppState { self.counters.connections.set(self.connections() as u64); } + /// The sample server's pictures as the events the engine sends once they are + /// in its cache, written to a folder of their own in the temporary + /// directory, which goes when the window closes ([`AppState::shutdown`]) or + /// the state is dropped. + fn write_demo_pictures(&mut self, address: &str) -> Vec { + static FOLDERS: AtomicUsize = AtomicUsize::new(0); + let folder = std::env::temp_dir().join(format!( + "voelin-gpui-demo-{}-{}", + std::process::id(), + FOLDERS.fetch_add(1, Ordering::Relaxed) + )); + if let Err(e) = std::fs::create_dir_all(&folder) { + warn!(%e, "the sample pictures have no folder"); + return Vec::new(); + } + self.demo_dir = Some(folder.clone()); + voelin_ui_vm::dev::logic::pictures(address, |name, png| { + let path = folder.join(name); + if let Err(e) = std::fs::write(&path, png) { + warn!(%e, "a sample picture was not written"); + } + path + }) + } + /// The handle other threads use to run something on GPUI's thread. pub fn later(&self) -> Later { self.later.clone() @@ -951,15 +959,16 @@ impl AppState { .map(|view| view.collapsed.iter().copied().collect()) .unwrap_or_default(); collapsed.sort_unstable(); - let state = self.tree_states.entry(id.to_string()).or_default(); - if state.collapsed == collapsed { - return; - } + let key = id.to_string(); + let state = self.tree_states.entry(key.clone()).or_default(); + let changed = state.collapsed != collapsed; state.collapsed = collapsed; if state.is_empty() { - self.tree_states.remove(&id.to_string()); + self.tree_states.remove(&key); + } + if changed { + self.keep_tree_states(); } - self.keep_tree_states(); } /// Write what the trees remember to the settings (nothing in demo mode). @@ -1077,6 +1086,10 @@ impl AppState { /// engine, none in demo mode). pub fn shutdown(&mut self, cx: &mut Context) -> usize { self.flush_window(); + // The sample pictures are not needed once the window is closing. + if let Some(folder) = self.demo_dir.take() { + let _ = std::fs::remove_dir_all(folder); + } let ids: Vec = self.vm.sessions.keys().copied().collect(); for id in &ids { self.transition(cx, |state, cx| voelin_ui_vm::chat::logic::save_reads(state, cx, *id)); @@ -1705,7 +1718,7 @@ mod tests { /// through the events a server would send, and go with the state. #[gpui::test] fn the_sample_servers_pictures_arrive_as_files_and_go_with_the_state(cx: &mut TestAppContext) { - let switches = Switches { demo_ui: true, ..Switches::default() }; + let switches = Switches { demo_ui: true, demo_pictures: true, ..Switches::default() }; let (app, _, _) = entity_over(cx, Backend::in_memory().unwrap(), switches); let folder = app.read_with(cx, |app, _| { let view = &app.vm.sessions[&sample::DEMO]; @@ -1715,9 +1728,25 @@ mod tests { app.demo_dir.clone().expect("the folder") }); assert!(folder.is_dir()); - drop(app); + // Closing the window removes it, and so would dropping the state. + app.update(cx, |app, cx| app.shutdown(cx)); + assert!(!folder.exists(), "removed when the window closes"); + + let switches = Switches { demo_ui: true, demo_pictures: true, ..Switches::default() }; + let (second, _, _) = entity_over(cx, Backend::in_memory().unwrap(), switches); + let folder = second.read_with(cx, |app, _| app.demo_dir.clone().expect("a folder")); + assert!(folder.is_dir()); + drop(second); cx.update(|_| {}); assert!(!folder.exists(), "removed with the state"); + + // Without the switch there is no folder and no picture. + let switches = Switches { demo_ui: true, ..Switches::default() }; + let (plain, _, _) = entity_over(cx, Backend::in_memory().unwrap(), switches); + plain.read_with(cx, |app, _| { + assert!(app.demo_dir.is_none()); + assert!(app.vm.sessions[&sample::DEMO].avatars.is_empty()); + }); } /// `VOELIN_DEMO_SERVER` puts another sample server on screen: the second is diff --git a/crates/voelin-gpui/src/dev.rs b/crates/voelin-gpui/src/dev.rs index 6d59e6a..00c3cca 100644 --- a/crates/voelin-gpui/src/dev.rs +++ b/crates/voelin-gpui/src/dev.rs @@ -8,7 +8,9 @@ //! other pages do not exist here yet; they are named in a warning and //! skipped. //! - `VOELIN_DEMO_UI=1`: the Slint front end's sample servers, shown through -//! the app's own events; nothing is sent to the engine or kept in the store. +//! the app's own events, with their pictures as files of a folder in the +//! temporary directory (removed when the window closes); nothing is sent to +//! the engine or kept in the store. //! - `VOELIN_DEMO_SERVER=2`: with `VOELIN_DEMO_UI`, the sample server on //! screen, counting from 0 (the first, which is connected and populated, is //! the default; the second is observed, the third not connected). @@ -42,6 +44,11 @@ pub struct Switches { pub open: Page, /// Show the sample servers instead of the stored ones. pub demo_ui: bool, + /// Write the sample servers' pictures to a folder of the temporary + /// directory and deliver them: what the binary does with the sample + /// servers, and what a test asks for by name (a test that does not leaves + /// nothing behind). + pub demo_pictures: bool, /// The sample server to show, by its place in the list, when the switch /// names one. pub demo_server: Option, @@ -63,6 +70,7 @@ impl Default for Switches { Self { open: Page::Shell, demo_ui: false, + demo_pictures: false, demo_server: None, demo_talk: None, theme: None, @@ -125,6 +133,7 @@ impl Switches { } let flag = |name: &str| get(name).is_some_and(|v| !matches!(v.trim(), "" | "0")); switches.demo_ui = flag("VOELIN_DEMO_UI"); + switches.demo_pictures = switches.demo_ui; switches.meter = flag("VOELIN_METER"); switches } @@ -167,7 +176,7 @@ mod tests { ("VOELIN_METER", "1"), ]); assert_eq!(s.open, Page::Swatch); - assert!(s.demo_ui); + assert!(s.demo_ui && s.demo_pictures, "the sample servers come with their pictures"); assert_eq!(s.demo_server, Some(2)); assert_eq!(s.demo_talk, Some(10.0)); assert_eq!(s.theme, Some(Choice::Light)); diff --git a/crates/voelin-gpui/src/shell/channels.rs b/crates/voelin-gpui/src/shell/channels.rs index e249581..b18e814 100644 --- a/crates/voelin-gpui/src/shell/channels.rs +++ b/crates/voelin-gpui/src/shell/channels.rs @@ -63,7 +63,14 @@ impl ChannelTree { this.apply(changes, new); } TreeEvent::Selection => this.selected = model.read(cx).selected(), - TreeEvent::Lists => this.emptiness = model.read(cx).emptiness(), + // The count of people is the members panel's. + TreeEvent::Lists => { + let emptiness = model.read(cx).emptiness(); + if emptiness == this.emptiness { + return; + } + this.emptiness = emptiness; + } TreeEvent::People(_) | TreeEvent::Card => return, } cx.notify(); diff --git a/crates/voelin-gpui/src/shell/members.rs b/crates/voelin-gpui/src/shell/members.rs index 6b7f62b..423e13f 100644 --- a/crates/voelin-gpui/src/shell/members.rs +++ b/crates/voelin-gpui/src/shell/members.rs @@ -75,7 +75,11 @@ impl MembersPane { } TreeEvent::Lists => { let model = model.read(cx); - (this.total, this.emptiness) = (model.total(), model.emptiness()); + let now = (model.total(), model.emptiness()); + if now == (this.total, this.emptiness) { + return; + } + (this.total, this.emptiness) = now; } TreeEvent::Card => this.open = model.read(cx).card().map(|card| card.id), TreeEvent::Rows(_) | TreeEvent::Selection => return,