Skip to content

Commit

Permalink
fix ordering if (hidden) custom apps are present
Browse files Browse the repository at this point in the history
never store the custom apps in the serialized order list
  • Loading branch information
liias committed Jan 6, 2023
1 parent 7675c4d commit 8bebd96
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
33 changes: 30 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> {
return self
.get_browser_common()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -712,6 +738,7 @@ fn move_app_profile(
// 3. update config file
let profile_ids_sorted: Vec<String> = visible_browser_profiles
.iter()
.filter(|b| b.get_restricted_domains().is_empty())
.map(|p| p.get_unique_id().clone())
.collect();

Expand Down
12 changes: 11 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ pub struct UI {

impl UI {
pub fn real_to_ui_browsers(all_browser_profiles: &[CommonBrowserProfile]) -> Vec<UIBrowser> {
// 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(),
Expand Down Expand Up @@ -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<Vec<String>>,
browser_name: String,
Expand Down Expand Up @@ -1243,7 +1253,7 @@ fn make_context_menu(browser: &UIBrowser) -> Menu<UIState> {
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")
Expand Down

0 comments on commit 8bebd96

Please sign in to comment.