From 1508354f5a06aba6fa78b0094aafe8eacf80897a Mon Sep 17 00:00:00 2001 From: Madis Liias Date: Thu, 5 Jan 2023 22:29:34 +0200 Subject: [PATCH] add move to top/bottom actions --- resources/i18n/en-US/builtin.ftl | 2 + src/lib.rs | 64 +++++++++++++++++++------------- src/ui.rs | 47 +++++++++++++++++++---- 3 files changed, 81 insertions(+), 32 deletions(-) diff --git a/resources/i18n/en-US/builtin.ftl b/resources/i18n/en-US/builtin.ftl index 94485f1..1fa5ad9 100644 --- a/resources/i18n/en-US/builtin.ftl +++ b/resources/i18n/en-US/builtin.ftl @@ -1,4 +1,6 @@ +move-profile-to-top = Move { $item-name } to Top move-profile-higher = Move { $item-name } Up move-profile-lower = Move { $item-name } Down +move-profile-to-bottom = Move { $item-name } to Bottom hide-profile = Hide { $item-name } hide-app = Hide all profiles of { $app-name } diff --git a/src/lib.rs b/src/lib.rs index 59b65a6..9beea45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ use url::Url; use ui::UI; use crate::browser_repository::{SupportedApp, SupportedAppRepository}; +use crate::ui::MoveTo; use crate::utils::OSAppFinder; mod ui; @@ -631,7 +632,7 @@ pub fn basically_main() { .ok(); } } - MessageToMain::MoveAppProfile(unique_id, to_higher) => { + MessageToMain::MoveAppProfile(unique_id, move_to) => { let visible_profile_index_maybe = visible_browser_profiles .iter() .position(|p| p.get_unique_id() == unique_id); @@ -642,34 +643,47 @@ pub fn basically_main() { } let visible_profile_index = visible_profile_index_maybe.unwrap(); - if to_higher { - if visible_profile_index == 0 { - info!( - "Not moving profile {} higher as it's already first", - unique_id - ); - continue; + match move_to { + MoveTo::UP | MoveTo::TOP => { + if visible_profile_index == 0 { + info!( + "Not moving profile {} higher as it's already first", + unique_id + ); + continue; + } + info!("Moving profile {} higher", unique_id); } - info!("Moving profile {} higher", unique_id); - } else { - if visible_profile_index == visible_browser_profiles.len() - 1 { - info!( - "Not moving profile {} lower as it's already last", - unique_id - ); - continue; + MoveTo::DOWN | MoveTo::BOTTOM => { + if visible_profile_index == visible_browser_profiles.len() - 1 { + info!( + "Not moving profile {} lower as it's already last", + unique_id + ); + continue; + } + info!("Moving profile {} lower", unique_id); } - info!("Moving profile {} lower", unique_id); } // 1. update visible_browser_profiles - if to_higher { - visible_browser_profiles - [visible_profile_index - 1..visible_profile_index + 1] - .rotate_left(1); - } else { - visible_browser_profiles[visible_profile_index..visible_profile_index + 2] - .rotate_right(1); + match move_to { + MoveTo::UP => { + visible_browser_profiles + [visible_profile_index - 1..visible_profile_index + 1] + .rotate_left(1); + } + MoveTo::DOWN => { + visible_browser_profiles + [visible_profile_index..visible_profile_index + 2] + .rotate_right(1); + } + MoveTo::TOP => { + visible_browser_profiles[0..visible_profile_index + 1].rotate_right(1); + } + MoveTo::BOTTOM => { + visible_browser_profiles[visible_profile_index..].rotate_left(1); + } } // 2. send visible_browser_profiles to ui @@ -707,5 +721,5 @@ pub enum MessageToMain { HideAppProfile(String), HideAllProfiles(String), RestoreAppProfile(String), - MoveAppProfile(String, bool), + MoveAppProfile(String, MoveTo), } diff --git a/src/ui.rs b/src/ui.rs index 1f5e66c..888545f 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -384,8 +384,15 @@ pub const HIDE_ALL_PROFILES: Selector = Selector::new("browsers.hide_all pub const RESTORE_HIDDEN_PROFILE: Selector = Selector::new("browsers.restore_hidden_profile"); -pub const MOVE_PROFILE: Selector<(String, bool)> = Selector::new("browsers.move_profile"); - +pub const MOVE_PROFILE: Selector<(String, MoveTo)> = Selector::new("browsers.move_profile"); + +#[derive(Clone, Copy, Debug)] +pub enum MoveTo { + UP, + DOWN, + TOP, + BOTTOM, +} pub const SHOW_ABOUT_DIALOG: Selector<()> = Selector::new("browsers.show_about_dialog"); pub struct UIDelegate { @@ -585,10 +592,10 @@ impl AppDelegate for UIDelegate { .ok(); Handled::Yes } else if cmd.is(MOVE_PROFILE) { - let (unique_id, to_higher) = cmd.get_unchecked(MOVE_PROFILE); + let (unique_id, move_to) = cmd.get_unchecked(MOVE_PROFILE); let unique_id = unique_id.clone(); self.main_sender - .send(MessageToMain::MoveAppProfile(unique_id, to_higher.clone())) + .send(MessageToMain::MoveAppProfile(unique_id, move_to.clone())) .ok(); Handled::Yes } else if cmd.is(SHOW_ABOUT_DIALOG) { @@ -1239,9 +1246,22 @@ fn make_context_menu(browser: &UIBrowser) -> Menu { if !browser.has_priority_ordering() { let is_visible = browser.browser_profile_index > 0; - let item_name = browser.get_full_name(); + let move_profile_to_top_label = LocalizedString::new("move-profile-to-top") + .with_arg("item-name", move |_, _| item_name.clone().into()); + + let this_id = id.clone(); + menu = menu.entry( + MenuItem::new(move_profile_to_top_label) + .on_activate(move |ctx, _data: &mut UIState, _env| { + let command = MOVE_PROFILE.with((this_id.clone(), MoveTo::TOP)); + ctx.submit_command(command); + }) + .enabled_if(move |_, _| is_visible), + ); + + let item_name = browser.get_full_name(); let move_profile_higher_label = LocalizedString::new("move-profile-higher") .with_arg("item-name", move |_, _| item_name.clone().into()); @@ -1249,7 +1269,7 @@ fn make_context_menu(browser: &UIBrowser) -> Menu { menu = menu.entry( MenuItem::new(move_profile_higher_label) .on_activate(move |ctx, _data: &mut UIState, _env| { - let command = MOVE_PROFILE.with((this_id.clone(), true)); + let command = MOVE_PROFILE.with((this_id.clone(), MoveTo::UP)); ctx.submit_command(command); }) .enabled_if(move |_, _| is_visible), @@ -1264,7 +1284,20 @@ fn make_context_menu(browser: &UIBrowser) -> Menu { menu = menu.entry( MenuItem::new(move_profile_lower_label) .on_activate(move |ctx, _data: &mut UIState, _env| { - let command = MOVE_PROFILE.with((this_id.clone(), false)); + let command = MOVE_PROFILE.with((this_id.clone(), MoveTo::DOWN)); + ctx.submit_command(command); + }) + .enabled_if(move |_, _| is_visible), + ); + + let this_id = id.clone(); + let item_name = browser.get_full_name(); + let move_profile_bottom_label = LocalizedString::new("move-profile-bottom") + .with_arg("item-name", move |_, _| item_name.to_string().into()); + menu = menu.entry( + MenuItem::new(move_profile_bottom_label) + .on_activate(move |ctx, _data: &mut UIState, _env| { + let command = MOVE_PROFILE.with((this_id.clone(), MoveTo::BOTTOM)); ctx.submit_command(command); }) .enabled_if(move |_, _| is_visible),