mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
theme-store: has_theme and in_library check the suite's themes without copying a definition
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user