From 8bebd966dccb075e12b1989f106beb3d4f5a637d Mon Sep 17 00:00:00 2001 From: Madis Liias Date: Sat, 7 Jan 2023 00:34:16 +0200 Subject: [PATCH] fix ordering if (hidden) custom apps are present never store the custom apps in the serialized order list --- src/lib.rs | 33 ++++++++++++++++++++++++++++++--- src/ui.rs | 12 +++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9c597e6..ac00287 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ use url::Url; use ui::UI; use crate::browser_repository::{SupportedApp, SupportedAppRepository}; -use crate::ui::MoveTo; +use crate::ui::{MoveTo, UIBrowser}; use crate::utils::OSAppFinder; mod ui; @@ -196,6 +196,10 @@ impl CommonBrowserProfile { return self.app.borrow(); } + pub fn has_priority_ordering(&self) -> bool { + return !self.get_restricted_domains().is_empty(); + } + fn get_restricted_domains(&self) -> &Vec { return self .get_browser_common() @@ -343,6 +347,9 @@ fn generate_all_browser_profiles( return order_maybe.unwrap_or(unordered_index); }); + // always show special apps first + visible_browser_profiles.sort_by_key(|b| !b.has_priority_ordering()); + return ( opening_rules, visible_browser_profiles, @@ -613,6 +620,9 @@ pub fn basically_main() { let hidden_profile = hidden_browser_profiles.remove(hidden_profile_index); visible_browser_profiles.push(hidden_profile); + // always show special apps first + visible_browser_profiles.sort_by_key(|b| !b.has_priority_ordering()); + let ui_browsers = UI::real_to_ui_browsers(&visible_browser_profiles); ui_event_sink .submit_command(ui::NEW_BROWSERS_RECEIVED, ui_browsers, Target::Global) @@ -662,9 +672,24 @@ fn move_app_profile( } let visible_profile_index = visible_profile_index_maybe.unwrap(); + // TODO: this is a bit ugly; we keep profiles with has_priority_ordering() always on top + // and everything else comes after; it might make sense to keep them in two separate + // vectors (or slices) + let first_orderable_item_index_maybe = visible_browser_profiles + .iter() + .position(|b| !b.has_priority_ordering()); + + let first_orderable_item_index = match first_orderable_item_index_maybe { + Some(first_orderable_item_index) => first_orderable_item_index, + None => { + warn!("Could not find orderable profiles"); + return; + } + }; + match move_to { MoveTo::UP | MoveTo::TOP => { - if visible_profile_index == 0 { + if visible_profile_index <= first_orderable_item_index { info!( "Not moving profile {} higher as it's already first", unique_id @@ -696,7 +721,8 @@ fn move_app_profile( .rotate_right(1); } MoveTo::TOP => { - visible_browser_profiles[0..visible_profile_index + 1].rotate_right(1); + visible_browser_profiles[first_orderable_item_index..visible_profile_index + 1] + .rotate_right(1); } MoveTo::BOTTOM => { visible_browser_profiles[visible_profile_index..].rotate_left(1); @@ -712,6 +738,7 @@ fn move_app_profile( // 3. update config file let profile_ids_sorted: Vec = visible_browser_profiles .iter() + .filter(|b| b.get_restricted_domains().is_empty()) .map(|p| p.get_unique_id().clone()) .collect(); diff --git a/src/ui.rs b/src/ui.rs index d78a1ff..fa2dad9 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -44,12 +44,21 @@ pub struct UI { impl UI { pub fn real_to_ui_browsers(all_browser_profiles: &[CommonBrowserProfile]) -> Vec { + // TODO: this is a bit ugly; we keep profiles with has_priority_ordering() always on top + // and everything else comes after; it might make sense to keep them in two separate + // vectors (or slices) + let first_orderable_item_index_maybe = all_browser_profiles + .iter() + .position(|b| !b.has_priority_ordering()); + let first_orderable_item_index = first_orderable_item_index_maybe.unwrap(); + let profiles_count = all_browser_profiles.len(); return all_browser_profiles .iter() .enumerate() .map(|(i, p)| UIBrowser { browser_profile_index: i, + is_first: i == first_orderable_item_index, is_last: i == profiles_count - 1, restricted_domains: Arc::new(p.get_restricted_domains().clone()), browser_name: p.get_browser_name().to_string(), @@ -319,6 +328,7 @@ impl FocusData for (bool, UIBrowser) { #[derive(Clone, Data, Lens)] pub struct UIBrowser { browser_profile_index: usize, + is_first: bool, is_last: bool, restricted_domains: Arc>, browser_name: String, @@ -1243,7 +1253,7 @@ fn make_context_menu(browser: &UIBrowser) -> Menu { let app_name = browser.browser_name.to_string(); if !browser.has_priority_ordering() { - let is_visible = browser.browser_profile_index > 0; + let is_visible = !browser.is_first; let item_name = browser.get_full_name(); let move_profile_to_top_label = LocalizedString::new("move-profile-to-top")