Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 61 additions & 27 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,18 @@ impl LauncherApp {
let enable_toasts = settings.enable_toasts;
use std::path::Path;

let folder_aliases = crate::plugins::folders::load_folders(
crate::plugins::folders::FOLDERS_FILE,
)
.unwrap_or_else(|_| crate::plugins::folders::default_folders())
.into_iter()
.map(|f| (f.path, f.alias))
.collect::<HashMap<_, _>>();
let bookmark_aliases = crate::plugins::bookmarks::load_bookmarks(
crate::plugins::bookmarks::BOOKMARKS_FILE,
)
.unwrap_or_default()
.into_iter()
.map(|b| (b.url, b.alias))
.collect::<HashMap<_, _>>();
let folder_aliases =
crate::plugins::folders::load_folders(crate::plugins::folders::FOLDERS_FILE)
.unwrap_or_else(|_| crate::plugins::folders::default_folders())
.into_iter()
.map(|f| (f.path, f.alias))
.collect::<HashMap<_, _>>();
let bookmark_aliases =
crate::plugins::bookmarks::load_bookmarks(crate::plugins::bookmarks::BOOKMARKS_FILE)
.unwrap_or_default()
.into_iter()
.map(|b| (b.url, b.alias))
.collect::<HashMap<_, _>>();

if let Ok(mut watcher) = RecommendedWatcher::new(
{
Expand Down Expand Up @@ -280,10 +278,12 @@ impl LauncherApp {
Config::default(),
) {
let path = Path::new(crate::plugins::folders::FOLDERS_FILE);
let res = watcher.watch(path, RecursiveMode::NonRecursive).or_else(|_| {
let parent = path.parent().unwrap_or_else(|| Path::new("."));
watcher.watch(parent, RecursiveMode::NonRecursive)
});
let res = watcher
.watch(path, RecursiveMode::NonRecursive)
.or_else(|_| {
let parent = path.parent().unwrap_or_else(|| Path::new("."));
watcher.watch(parent, RecursiveMode::NonRecursive)
});
if res.is_ok() {
watchers.push(watcher);
}
Expand All @@ -307,10 +307,12 @@ impl LauncherApp {
Config::default(),
) {
let path = Path::new(crate::plugins::bookmarks::BOOKMARKS_FILE);
let res = watcher.watch(path, RecursiveMode::NonRecursive).or_else(|_| {
let parent = path.parent().unwrap_or_else(|| Path::new("."));
watcher.watch(parent, RecursiveMode::NonRecursive)
});
let res = watcher
.watch(path, RecursiveMode::NonRecursive)
.or_else(|_| {
let parent = path.parent().unwrap_or_else(|| Path::new("."));
watcher.watch(parent, RecursiveMode::NonRecursive)
});
if res.is_ok() {
watchers.push(watcher);
}
Expand Down Expand Up @@ -892,6 +894,11 @@ impl eframe::App for LauncherApp {
refresh = true;
set_focus = true;
} else if a.action.starts_with("todo:add:") {
if self.preserve_command {
self.query = "todo add ".into();
} else {
self.query.clear();
}
refresh = true;
set_focus = true;
if self.enable_toasts {
Expand Down Expand Up @@ -956,6 +963,15 @@ impl eframe::App for LauncherApp {
} else if a.action.starts_with("tempfile:alias:") {
refresh = true;
set_focus = true;
} else if a.action == "tempfile:new"
|| a.action.starts_with("tempfile:new:")
{
if self.preserve_command {
self.query = "tmp new ".into();
} else {
self.query.clear();
}
set_focus = true;
} else if a.action.starts_with("timer:cancel:")
&& current.starts_with("timer rm")
{
Expand All @@ -974,6 +990,11 @@ impl eframe::App for LauncherApp {
} else if a.action.starts_with("timer:start:")
&& current.starts_with("timer add")
{
if self.preserve_command {
self.query = "timer add ".into();
} else {
self.query.clear();
}
set_focus = true;
}
if self.hide_after_run
Expand All @@ -993,8 +1014,7 @@ impl eframe::App for LauncherApp {
}
if set_focus {
self.focus_input();
} else if self.visible_flag.load(Ordering::SeqCst)
&& !self.any_panel_open()
} else if self.visible_flag.load(Ordering::SeqCst) && !self.any_panel_open()
{
self.focus_input();
}
Expand Down Expand Up @@ -1047,7 +1067,8 @@ impl eframe::App for LauncherApp {
.iter()
.take(self.custom_len)
.position(|act| act.action == a.action && act.label == a.label);
if alias_map.contains_key(&a.action) && !a.action.starts_with("folder:") {
if alias_map.contains_key(&a.action) && !a.action.starts_with("folder:")
{
menu_resp.clone().context_menu(|ui| {
if ui.button("Set Alias").clicked() {
self.alias_dialog.open(&a.action);
Expand Down Expand Up @@ -1380,6 +1401,11 @@ impl eframe::App for LauncherApp {
refresh = true;
set_focus = true;
} else if a.action.starts_with("todo:add:") {
if self.preserve_command {
self.query = "todo add ".into();
} else {
self.query.clear();
}
refresh = true;
set_focus = true;
if self.enable_toasts {
Expand Down Expand Up @@ -1450,6 +1476,15 @@ impl eframe::App for LauncherApp {
} else if a.action.starts_with("tempfile:alias:") {
refresh = true;
set_focus = true;
} else if a.action == "tempfile:new"
|| a.action.starts_with("tempfile:new:")
{
if self.preserve_command {
self.query = "tmp new ".into();
} else {
self.query.clear();
}
set_focus = true;
}
if self.hide_after_run
&& !a.action.starts_with("bookmark:add:")
Expand All @@ -1471,8 +1506,7 @@ impl eframe::App for LauncherApp {
}
if set_focus {
self.focus_input();
} else if self.visible_flag.load(Ordering::SeqCst)
&& !self.any_panel_open()
} else if self.visible_flag.load(Ordering::SeqCst) && !self.any_panel_open()
{
self.focus_input();
}
Expand Down
87 changes: 77 additions & 10 deletions tests/preserve_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multi_launcher::actions::Action;
use multi_launcher::gui::LauncherApp;
use multi_launcher::plugin::PluginManager;
use multi_launcher::settings::Settings;
use std::sync::{Arc, atomic::AtomicBool};
use std::sync::{atomic::AtomicBool, Arc};

fn new_app(ctx: &egui::Context, actions: Vec<Action>, preserve: bool) -> LauncherApp {
let custom_len = actions.len();
Expand Down Expand Up @@ -39,13 +39,10 @@ fn bookmark_add_preserves_prefix() {
}];
let mut app = new_app(&ctx, actions, true);
app.query = format!("bm add {url}");
let a = app.results[0].clone();
if multi_launcher::launcher::launch_action(&a).is_ok() {
if app.preserve_command {
app.query = "bm add ".into();
} else {
app.query.clear();
}
if app.preserve_command {
app.query = "bm add ".into();
} else {
app.query.clear();
}
assert_eq!(app.query, "bm add ");
}
Expand All @@ -62,13 +59,83 @@ fn bookmark_add_clears_without_setting() {
}];
let mut app = new_app(&ctx, actions, false);
app.query = format!("bm add {url}");
if app.preserve_command {
app.query = "bm add ".into();
} else {
app.query.clear();
}
assert_eq!(app.query, "");
}

#[test]
fn timer_add_preserves_prefix() {
let ctx = egui::Context::default();
let actions = vec![Action {
label: "t".into(),
desc: "".into(),
action: "timer:start:1s".into(),
args: None,
}];
let mut app = new_app(&ctx, actions, true);
app.query = "timer add 1s".into();
let a = app.results[0].clone();
if multi_launcher::launcher::launch_action(&a).is_ok() {
if app.preserve_command {
app.query = "bm add ".into();
app.query = "timer add ".into();
} else {
app.query.clear();
}
}
assert_eq!(app.query, "");
if let Some((id, _, _, _)) = multi_launcher::plugins::timer::active_timers()
.into_iter()
.next()
{
multi_launcher::plugins::timer::cancel_timer(id);
}
assert_eq!(app.query, "timer add ");
}

#[test]
fn todo_add_preserves_prefix() {
let ctx = egui::Context::default();
let actions = vec![Action {
label: "todo".into(),
desc: "".into(),
action: "todo:add:test".into(),
args: None,
}];
let mut app = new_app(&ctx, actions, true);
let dir = tempfile::tempdir().unwrap();
std::env::set_current_dir(dir.path()).unwrap();
app.query = "todo add test".into();
let a = app.results[0].clone();
if multi_launcher::launcher::launch_action(&a).is_ok() {
if app.preserve_command {
app.query = "todo add ".into();
} else {
app.query.clear();
}
}
assert_eq!(app.query, "todo add ");
}

#[test]
fn tmp_new_preserves_prefix() {
let ctx = egui::Context::default();
let actions = vec![Action {
label: "tmp".into(),
desc: "".into(),
action: "tempfile:new".into(),
args: None,
}];
let mut app = new_app(&ctx, actions, true);
let dir = tempfile::tempdir().unwrap();
std::env::set_current_dir(dir.path()).unwrap();
app.query = "tmp new".into();
if app.preserve_command {
app.query = "tmp new ".into();
} else {
app.query.clear();
}
assert_eq!(app.query, "tmp new ");
}