From d78a3aa6b2a9a0bfb811fac9497366baedef84d7 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 13 Nov 2026 16:18:05 +0000 Subject: [PATCH] theme-store: has_theme and in_library check the suite's themes without copying a definition --- crates/theme-store/src/client.rs | 12 +++++++++- crates/theme-store/src/client/tests.rs | 4 +++- crates/theme-store/src/watch.rs | 31 ++++++++++++++++++-------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/crates/theme-store/src/client.rs b/crates/theme-store/src/client.rs index fcc08d721..023c99675 100644 --- a/crates/theme-store/src/client.rs +++ b/crates/theme-store/src/client.rs @@ -160,16 +160,26 @@ impl SuiteClient { self.snapshot.theme(id) } + /// Whether [`theme`](Self::theme) has `id`, without copying it. + pub fn has_theme(&self, id: &ThemeId) -> bool { + self.snapshot.has_theme(id) + } + /// Any built-in or library theme, for a preview card ([`Snapshot::preview`]). pub fn preview(&self, id: &ThemeId) -> Option { self.snapshot.preview(id) } + /// Whether `id` is a library theme in the suite's library, loaded or not. + pub fn in_library(&self, id: &ThemeId) -> bool { + self.snapshot.in_library(id) + } + /// True while `id` is a library theme the snapshot hasn't loaded yet: one just picked for the /// suite or named with [`set_app_theme`](Self::set_app_theme), with no problem reported for /// it. An app showing its own theme keeps its current look while this is true. pub fn is_loading(&self, id: &ThemeId) -> bool { - !id.is_builtin() && self.snapshot.theme(id).is_none() && self.is_requested(id) && !self.has_problem(id) + !id.is_builtin() && !self.snapshot.has_theme(id) && self.is_requested(id) && !self.has_problem(id) } fn is_requested(&self, id: &ThemeId) -> bool { diff --git a/crates/theme-store/src/client/tests.rs b/crates/theme-store/src/client/tests.rs index 9da95c225..04c10a5c5 100644 --- a/crates/theme-store/src/client/tests.rs +++ b/crates/theme-store/src/client/tests.rs @@ -99,6 +99,8 @@ fn a_library_theme_on_its_way_never_shows_the_app_default() { client.change(theme("craft.graphite")); settle(&mut client); let sample = id(SAMPLE_THEME_ID); + // In the library, but nothing picked it, so the suite hasn't loaded it. + assert!(client.in_library(&sample) && !client.has_theme(&sample) && !client.is_loading(&sample)); // Picked for the dark slot: the previous theme stays there until the record is there, and // the light slot is untouched. @@ -108,7 +110,7 @@ fn a_library_theme_on_its_way_never_shows_the_app_default() { assert_eq!((client.shown().theme_light, client.shown().theme_dark), (Some(id("craft.graphite")), Some(id("craft.graphite")))); settle(&mut client); assert_eq!((client.shown().theme_light, client.shown().theme_dark), (Some(id("craft.graphite")), Some(sample.clone()))); - assert!(client.theme(&sample).is_some()); + assert!(client.theme(&sample).is_some() && client.has_theme(&sample)); assert!(!client.is_loading(&sample)); // Picked for the app alone: loading until the snapshot has it. diff --git a/crates/theme-store/src/watch.rs b/crates/theme-store/src/watch.rs index 7f5794913..5301b4c3d 100644 --- a/crates/theme-store/src/watch.rs +++ b/crates/theme-store/src/watch.rs @@ -111,20 +111,33 @@ impl Snapshot { /// [`resolve_selection`](craft_theme::resolve_selection)'s `lookup`. Other library /// themes give `None` until the app names them with [`Op::AppTheme`]. pub fn theme(&self, id: &ThemeId) -> Option { - if let Some(theme) = builtin(id) { - return Some(theme.clone()); - } - self.active.iter().chain(&self.app_theme).find(|r| r.theme.id == *id).map(|r| r.theme.clone()) + self.loaded(id).cloned() + } + + /// Whether [`theme`](Self::theme) has `id`, without copying it. + pub fn has_theme(&self, id: &ThemeId) -> bool { + self.loaded(id).is_some() } /// Any theme the suite knows, for a preview card: a built-in, an active or app theme, or a /// library theme in [`index`](Self::index), with no file read or [`Op::Export`]. pub fn preview(&self, id: &ThemeId) -> Option { - self.theme(id).or_else(|| { - // The index is sorted by file name, and a library file's name is its id. - let at = self.index.binary_search_by(|e| e.id.as_str().cmp(id.as_str())).ok()?; - self.index.get(at).map(|e| e.theme.clone()) - }) + self.loaded(id).or_else(|| self.indexed(id)).cloned() + } + + /// Whether `id` is a library theme in [`index`](Self::index). + pub fn in_library(&self, id: &ThemeId) -> bool { + self.indexed(id).is_some() + } + + fn loaded(&self, id: &ThemeId) -> Option<&ThemeDef> { + builtin(id).or_else(|| self.active.iter().chain(&self.app_theme).find(|r| r.theme.id == *id).map(|r| &r.theme)) + } + + fn indexed(&self, id: &ThemeId) -> Option<&ThemeDef> { + // The index is sorted by file name, and a library file's name is its id. + let at = self.index.binary_search_by(|e| e.id.as_str().cmp(id.as_str())).ok()?; + self.index.get(at).map(|e| &e.theme) } }