Skip to content

Commit

Permalink
perf(appearance): fetch icons on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
leb-kuchen authored Jan 6, 2025
1 parent 0c9bb0c commit 66d96de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cosmic-settings/src/pages/desktop/appearance/icon_themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ pub async fn fetch() -> Message {
theme_paths.entry(name.clone()).or_insert(path);

let theme = id.clone();
/* This section is performance critical */
// `icon::from_name` may perform blocking I/O
if let Ok(handles) =
tokio::task::spawn_blocking(|| preview_handles(theme, valid_dirs)).await
{
icon_themes.insert(IconTheme { id, name }, handles);
}
/* END */
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions cosmic-settings/src/pages/desktop/appearance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum ContextView {
MonospaceFont,
SystemFont,
}

#[allow(clippy::struct_excessive_bools)]
pub struct Page {
entity: page::Entity,
on_enter_handle: Option<cosmic::iced::task::Handle>,
Expand All @@ -79,6 +79,10 @@ pub struct Page {
font_filter: Vec<Arc<str>>,
font_search: String,

/** Only fetch icons once. Allows for better cleanup. Also icon fetching can take for ages. */
icons_fetched: bool,
icon_fetch_handle: Option<cosmic::iced::task::Handle>,

icon_theme_active: Option<usize>,
icon_themes: IconThemes,
icon_handles: IconHandles,
Expand Down Expand Up @@ -203,6 +207,8 @@ impl
font_config: font_config::Model::new(),
font_filter: Vec::new(),
font_search: String::new(),
icons_fetched: false,
icon_fetch_handle: None,
icon_theme_active: None,
icon_themes: Vec::new(),
icon_handles: Vec::new(),
Expand Down Expand Up @@ -1081,10 +1087,17 @@ impl Page {

Message::IconsAndToolkit => {
self.context_view = Some(ContextView::IconsAndToolkit);
return cosmic::task::message(crate::app::Message::OpenContextDrawer(
self.entity,
"".into(),
let mut tasks = Vec::new();
tasks.push(cosmic::task::message(
crate::app::Message::OpenContextDrawer(self.entity, "".into()),
));
if !self.icons_fetched {
self.icons_fetched = true;
let (task, handle) = cosmic::task::future(icon_themes::fetch()).abortable();
self.icon_fetch_handle = Some(handle);
tasks.push(task);
}
return Task::batch(tasks);
}

Message::Daytime(day_time) => {
Expand Down Expand Up @@ -1457,7 +1470,7 @@ impl page::Page<crate::pages::Message> for Page {
) -> Task<crate::pages::Message> {
let (task, handle) = cosmic::task::batch(vec![
// Load icon themes
cosmic::task::future(icon_themes::fetch()).map(crate::pages::Message::Appearance),
// cosmic::task::future(icon_themes::fetch()).map(crate::pages::Message::Appearance),
// Load font families
cosmic::task::future(async move {
let (mono, interface) = font_config::load_font_families();
Expand All @@ -1475,7 +1488,9 @@ impl page::Page<crate::pages::Message> for Page {
if let Some(handle) = self.on_enter_handle.take() {
handle.abort();
}

if let Some(handle) = self.icon_fetch_handle.take() {
handle.abort();
}
cosmic::task::message(crate::pages::Message::Appearance(Message::Left))
}

Expand Down

0 comments on commit 66d96de

Please sign in to comment.