voelin-gpui: the sample pictures only when asked and removed at close; two small fixes

- The sample servers' pictures were written to a folder of the temporary
  directory by every state that showed them, tests included, and the folder
  went only with a dropped state, which the test platform and a killed run
  never do. The pictures are now a switch (Switches::demo_pictures: what
  VOELIN_DEMO_UI sets, and what a test asks for by name) and the folder is
  removed when the window closes (AppState::shutdown) as well as with the
  state.
- The tree does not redraw for a change of the people's count, which is the
  members panel's, and an unfolded channel leaves no empty entry in what the
  trees remember.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-28 00:40:25 +03:00
1 parent 68a1694d7b
commit faef1325ab
4 files changed
+83 -34

No files matched your search

+59 -30
View File
@@ -384,31 +384,14 @@ impl AppState {
/// arrive as files of a folder of their own in the temporary directory. /// arrive as files of a folder of their own in the temporary directory.
fn load_demo(&mut self) { fn load_demo(&mut self) {
use voelin_ui_vm::dev::logic as sample; use voelin_ui_vm::dev::logic as sample;
static FOLDERS: AtomicUsize = AtomicUsize::new(0);
self.vm.bookmarks = sample::bookmarks(); self.vm.bookmarks = sample::bookmarks();
self.vm.settings.last_voice = Some(sample::last_voice()); self.vm.settings.last_voice = Some(sample::last_voice());
self.vm.current = Some(sample::DEMO); self.vm.current = Some(sample::DEMO);
let folder = std::env::temp_dir().join(format!( let pictures = if self.switches.demo_pictures {
"voelin-gpui-demo-{}-{}", let address = self.vm.bookmarks.first().map(|b| b.address.clone()).unwrap_or_default();
std::process::id(), self.write_demo_pictures(&address)
FOLDERS.fetch_add(1, Ordering::Relaxed) } else {
)); Vec::new()
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()
}
}; };
for event in sample::server_events().into_iter().chain(pictures) { for event in sample::server_events().into_iter().chain(pictures) {
let update = self.handle(event); let update = self.handle(event);
@@ -428,6 +411,31 @@ impl AppState {
self.counters.connections.set(self.connections() as u64); 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<Event> {
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. /// The handle other threads use to run something on GPUI's thread.
pub fn later(&self) -> Later { pub fn later(&self) -> Later {
self.later.clone() self.later.clone()
@@ -951,15 +959,16 @@ impl AppState {
.map(|view| view.collapsed.iter().copied().collect()) .map(|view| view.collapsed.iter().copied().collect())
.unwrap_or_default(); .unwrap_or_default();
collapsed.sort_unstable(); collapsed.sort_unstable();
let state = self.tree_states.entry(id.to_string()).or_default(); let key = id.to_string();
if state.collapsed == collapsed { let state = self.tree_states.entry(key.clone()).or_default();
return; let changed = state.collapsed != collapsed;
}
state.collapsed = collapsed; state.collapsed = collapsed;
if state.is_empty() { 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). /// Write what the trees remember to the settings (nothing in demo mode).
@@ -1077,6 +1086,10 @@ impl AppState {
/// engine, none in demo mode). /// engine, none in demo mode).
pub fn shutdown(&mut self, cx: &mut Context<Self>) -> usize { pub fn shutdown(&mut self, cx: &mut Context<Self>) -> usize {
self.flush_window(); 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<i64> = self.vm.sessions.keys().copied().collect(); let ids: Vec<i64> = self.vm.sessions.keys().copied().collect();
for id in &ids { for id in &ids {
self.transition(cx, |state, cx| voelin_ui_vm::chat::logic::save_reads(state, cx, *id)); 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. /// through the events a server would send, and go with the state.
#[gpui::test] #[gpui::test]
fn the_sample_servers_pictures_arrive_as_files_and_go_with_the_state(cx: &mut TestAppContext) { 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 (app, _, _) = entity_over(cx, Backend::in_memory().unwrap(), switches);
let folder = app.read_with(cx, |app, _| { let folder = app.read_with(cx, |app, _| {
let view = &app.vm.sessions[&sample::DEMO]; let view = &app.vm.sessions[&sample::DEMO];
@@ -1715,9 +1728,25 @@ mod tests {
app.demo_dir.clone().expect("the folder") app.demo_dir.clone().expect("the folder")
}); });
assert!(folder.is_dir()); 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(|_| {}); cx.update(|_| {});
assert!(!folder.exists(), "removed with the state"); 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 /// `VOELIN_DEMO_SERVER` puts another sample server on screen: the second is
+11 -2
View File
@@ -8,7 +8,9 @@
//! other pages do not exist here yet; they are named in a warning and //! other pages do not exist here yet; they are named in a warning and
//! skipped. //! skipped.
//! - `VOELIN_DEMO_UI=1`: the Slint front end's sample servers, shown through //! - `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 //! - `VOELIN_DEMO_SERVER=2`: with `VOELIN_DEMO_UI`, the sample server on
//! screen, counting from 0 (the first, which is connected and populated, is //! screen, counting from 0 (the first, which is connected and populated, is
//! the default; the second is observed, the third not connected). //! the default; the second is observed, the third not connected).
@@ -42,6 +44,11 @@ pub struct Switches {
pub open: Page, pub open: Page,
/// Show the sample servers instead of the stored ones. /// Show the sample servers instead of the stored ones.
pub demo_ui: bool, 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 /// The sample server to show, by its place in the list, when the switch
/// names one. /// names one.
pub demo_server: Option<usize>, pub demo_server: Option<usize>,
@@ -63,6 +70,7 @@ impl Default for Switches {
Self { Self {
open: Page::Shell, open: Page::Shell,
demo_ui: false, demo_ui: false,
demo_pictures: false,
demo_server: None, demo_server: None,
demo_talk: None, demo_talk: None,
theme: None, theme: None,
@@ -125,6 +133,7 @@ impl Switches {
} }
let flag = |name: &str| get(name).is_some_and(|v| !matches!(v.trim(), "" | "0")); let flag = |name: &str| get(name).is_some_and(|v| !matches!(v.trim(), "" | "0"));
switches.demo_ui = flag("VOELIN_DEMO_UI"); switches.demo_ui = flag("VOELIN_DEMO_UI");
switches.demo_pictures = switches.demo_ui;
switches.meter = flag("VOELIN_METER"); switches.meter = flag("VOELIN_METER");
switches switches
} }
@@ -167,7 +176,7 @@ mod tests {
("VOELIN_METER", "1"), ("VOELIN_METER", "1"),
]); ]);
assert_eq!(s.open, Page::Swatch); 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_server, Some(2));
assert_eq!(s.demo_talk, Some(10.0)); assert_eq!(s.demo_talk, Some(10.0));
assert_eq!(s.theme, Some(Choice::Light)); assert_eq!(s.theme, Some(Choice::Light));
+8 -1
View File
@@ -63,7 +63,14 @@ impl ChannelTree {
this.apply(changes, new); this.apply(changes, new);
} }
TreeEvent::Selection => this.selected = model.read(cx).selected(), 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, TreeEvent::People(_) | TreeEvent::Card => return,
} }
cx.notify(); cx.notify();
+5 -1
View File
@@ -75,7 +75,11 @@ impl MembersPane {
} }
TreeEvent::Lists => { TreeEvent::Lists => {
let model = model.read(cx); 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::Card => this.open = model.read(cx).card().map(|card| card.id),
TreeEvent::Rows(_) | TreeEvent::Selection => return, TreeEvent::Rows(_) | TreeEvent::Selection => return,