From 1f225d19ce582423fe8713fc6669c51d162f256f Mon Sep 17 00:00:00 2001 From: manascb1344 Date: Fri, 8 May 2026 15:52:34 +0530 Subject: [PATCH] refactor: extract sorting logic for startup applications and all apps - Introduced a new function `sort_entries_by_name` to encapsulate the sorting logic for desktop entries by name, improving code readability and maintainability. - Updated existing sorting implementations to use the new function, ensuring consistent behavior across the application. --- .../src/pages/applications/startup_apps.rs | 47 ++++++------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/cosmic-settings/src/pages/applications/startup_apps.rs b/cosmic-settings/src/pages/applications/startup_apps.rs index 3f03e75..d5cd855 100644 --- a/cosmic-settings/src/pages/applications/startup_apps.rs +++ b/cosmic-settings/src/pages/applications/startup_apps.rs @@ -143,17 +143,7 @@ impl page::Page for Page { freedesktop_desktop_entry::Iter::new(user_dirs.into_iter()).entries(Some(&locales)); let mut user_entries_vec = user_entries.collect_vec(); - user_entries_vec.sort_by(|a, b| { - let name_a = a - .name(&locales) - .map(|n| n.to_lowercase()) - .unwrap_or_else(|| a.appid.to_lowercase()); - let name_b = b - .name(&locales) - .map(|n| n.to_lowercase()) - .unwrap_or_else(|| b.appid.to_lowercase()); - name_a.cmp(&name_b) - }); + sort_entries_by_name(&mut user_entries_vec, &locales); let mut apps_hash = HashMap::with_capacity(1); apps_hash.insert(DirectoryType::User, user_entries_vec); @@ -259,17 +249,10 @@ impl Page { if let Some(target_apps) = target_apps { let mut new_apps = target_apps.clone(); new_apps.push(app.clone()); - new_apps.sort_by(|a, b| { - let name_a = a - .name(&cached_startup_apps.locales) - .map(|n| n.to_lowercase()) - .unwrap_or_else(|| a.appid.to_lowercase()); - let name_b = b - .name(&cached_startup_apps.locales) - .map(|n| n.to_lowercase()) - .unwrap_or_else(|| b.appid.to_lowercase()); - name_a.cmp(&name_b) - }); + sort_entries_by_name( + &mut new_apps, + &cached_startup_apps.locales, + ); cached_startup_apps .apps @@ -457,6 +440,14 @@ fn apps() -> Section { }) } +fn sort_entries_by_name(entries: &mut [DesktopEntry], locales: &[String]) { + entries.sort_by_cached_key(|e| { + e.name(locales) + .map(|n| n.to_lowercase()) + .unwrap_or_else(|| e.appid.to_lowercase()) + }); +} + fn get_all_apps(locales: Vec) -> Vec { let mut dedupe = HashSet::new(); @@ -497,17 +488,7 @@ fn get_all_apps(locales: Vec) -> Vec { dedupe.insert(app_id.to_owned()); } - result.sort_by(|a, b| { - let name_a = a - .name(&locales) - .map(|n| n.to_lowercase()) - .unwrap_or_else(|| a.appid.to_lowercase()); - let name_b = b - .name(&locales) - .map(|n| n.to_lowercase()) - .unwrap_or_else(|| b.appid.to_lowercase()); - name_a.cmp(&name_b) - }); + sort_entries_by_name(&mut result, &locales); result }