theme-store: has_theme and in_library check the suite's themes without copying a definition

This commit is contained in:
aiongg
2026-11-13 18:36:52 +00:00
parent 27608b1da3
commit d78a3aa6b2
3 changed files with 36 additions and 11 deletions
+11 -1
View File
@@ -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<ThemeDef> {
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 {
+3 -1
View File
@@ -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.
+22 -9
View File
@@ -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<ThemeDef> {
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<ThemeDef> {
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)
}
}