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
121 changes: 98 additions & 23 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ fn scale_ui<R>(ui: &mut egui::Ui, scale: f32, add_contents: impl FnOnce(&mut egu

enum WatchEvent {
Actions,
Folders,
Bookmarks,
}

pub struct LauncherApp {
Expand All @@ -78,6 +80,8 @@ pub struct LauncherApp {
#[allow(dead_code)] // required to keep watchers alive
watchers: Vec<RecommendedWatcher>,
rx: Receiver<WatchEvent>,
folder_aliases: HashMap<String, Option<String>>,
bookmark_aliases: HashMap<String, Option<String>>,
plugin_dirs: Option<Vec<String>>,
index_paths: Option<Vec<String>>,
enabled_plugins: Option<Vec<String>>,
Expand Down Expand Up @@ -211,6 +215,22 @@ impl LauncherApp {
let mut watchers = Vec::new();
let toasts = Toasts::new().anchor(egui::Align2::RIGHT_TOP, [10.0, 10.0]);
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<_, _>>();

if let Ok(mut watcher) = RecommendedWatcher::new(
{
Expand All @@ -229,7 +249,6 @@ impl LauncherApp {
},
Config::default(),
) {
use std::path::Path;
if watcher
.watch(Path::new(&actions_path), RecursiveMode::NonRecursive)
.is_ok()
Expand All @@ -238,6 +257,60 @@ impl LauncherApp {
}
}

if let Ok(mut watcher) = RecommendedWatcher::new(
{
let tx = tx.clone();
move |res: notify::Result<notify::Event>| match res {
Ok(event) => {
if matches!(
event.kind,
EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_)
) {
let _ = tx.send(WatchEvent::Folders);
}
}
Err(e) => tracing::error!("watch error: {:?}", e),
}
},
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)
});
if res.is_ok() {
watchers.push(watcher);
}
}

if let Ok(mut watcher) = RecommendedWatcher::new(
{
let tx = tx.clone();
move |res: notify::Result<notify::Event>| match res {
Ok(event) => {
if matches!(
event.kind,
EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_)
) {
let _ = tx.send(WatchEvent::Bookmarks);
}
}
Err(e) => tracing::error!("watch error: {:?}", e),
}
},
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)
});
if res.is_ok() {
watchers.push(watcher);
}
}

let initial_visible = visible_flag.load(Ordering::SeqCst);

let offscreen_pos = {
Expand Down Expand Up @@ -276,6 +349,8 @@ impl LauncherApp {
settings_path,
watchers,
rx,
folder_aliases,
bookmark_aliases,
plugin_dirs,
index_paths,
enabled_plugins,
Expand Down Expand Up @@ -614,6 +689,24 @@ impl eframe::App for LauncherApp {
tracing::info!("actions reloaded");
}
}
WatchEvent::Folders => {
self.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();
}
WatchEvent::Bookmarks => {
self.bookmark_aliases = crate::plugins::bookmarks::load_bookmarks(
crate::plugins::bookmarks::BOOKMARKS_FILE,
)
.unwrap_or_default()
.into_iter()
.map(|b| (b.url, b.alias))
.collect();
}
}
}

Expand Down Expand Up @@ -862,26 +955,8 @@ impl eframe::App for LauncherApp {
scale_ui(ui, self.list_scale, |ui| {
let mut refresh = false;
let mut set_focus = false;
let alias_list = crate::plugins::folders::load_folders(
crate::plugins::folders::FOLDERS_FILE,
)
.unwrap_or_default();
let custom = alias_list
.iter()
.map(|f| f.path.clone())
.collect::<std::collections::HashSet<_>>();
let alias_map = alias_list
.into_iter()
.map(|f| (f.path, f.alias))
.collect::<std::collections::HashMap<_, _>>();
let bm_list = crate::plugins::bookmarks::load_bookmarks(
crate::plugins::bookmarks::BOOKMARKS_FILE,
)
.unwrap_or_default();
let bm_custom = bm_list
.iter()
.map(|b| b.url.clone())
.collect::<std::collections::HashSet<_>>();
let alias_map = &self.folder_aliases;
let bm_map = &self.bookmark_aliases;
let show_full = self
.enabled_capabilities
.as_ref()
Expand Down Expand Up @@ -918,7 +993,7 @@ impl eframe::App for LauncherApp {
.iter()
.take(self.custom_len)
.position(|act| act.action == a.action && act.label == a.label);
if custom.contains(&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 @@ -947,7 +1022,7 @@ impl eframe::App for LauncherApp {
ui.close_menu();
}
});
} else if bm_custom.contains(&a.action) {
} else if bm_map.contains_key(&a.action) {
menu_resp.clone().context_menu(|ui| {
if ui.button("Set Alias").clicked() {
self.bookmark_alias_dialog.open(&a.action);
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ impl BookmarksPlugin {
)
.ok();
if let Some(w) = watcher.as_mut() {
let _ = w.watch(std::path::Path::new(&path), RecursiveMode::NonRecursive);
let p = std::path::Path::new(&path);
if w.watch(p, RecursiveMode::NonRecursive).is_err() {
let parent = p.parent().unwrap_or_else(|| std::path::Path::new("."));
let _ = w.watch(parent, RecursiveMode::NonRecursive);
}
}
Self {
matcher: SkimMatcherV2::default(),
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ impl ClipboardPlugin {
)
.ok();
if let Some(w) = watcher.as_mut() {
let _ = w.watch(std::path::Path::new(&path), RecursiveMode::NonRecursive);
let p = std::path::Path::new(&path);
if w.watch(p, RecursiveMode::NonRecursive).is_err() {
let parent = p.parent().unwrap_or_else(|| std::path::Path::new("."));
let _ = w.watch(parent, RecursiveMode::NonRecursive);
}
}
Self {
max_entries,
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ impl FoldersPlugin {
)
.ok();
if let Some(w) = watcher.as_mut() {
let _ = w.watch(std::path::Path::new(&path), RecursiveMode::NonRecursive);
let p = std::path::Path::new(&path);
if w.watch(p, RecursiveMode::NonRecursive).is_err() {
let parent = p.parent().unwrap_or_else(|| std::path::Path::new("."));
let _ = w.watch(parent, RecursiveMode::NonRecursive);
}
}
Self {
matcher: SkimMatcherV2::default(),
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ impl NotesPlugin {
)
.ok();
if let Some(w) = watcher.as_mut() {
let _ = w.watch(std::path::Path::new(&path), RecursiveMode::NonRecursive);
let p = std::path::Path::new(&path);
if w.watch(p, RecursiveMode::NonRecursive).is_err() {
let parent = p.parent().unwrap_or_else(|| std::path::Path::new("."));
let _ = w.watch(parent, RecursiveMode::NonRecursive);
}
}
Self {
matcher: SkimMatcherV2::default(),
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ impl SnippetsPlugin {
)
.ok();
if let Some(w) = watcher.as_mut() {
let _ = w.watch(std::path::Path::new(&path), RecursiveMode::NonRecursive);
let p = std::path::Path::new(&path);
if w.watch(p, RecursiveMode::NonRecursive).is_err() {
let parent = p.parent().unwrap_or_else(|| std::path::Path::new("."));
let _ = w.watch(parent, RecursiveMode::NonRecursive);
}
}
Self {
matcher: SkimMatcherV2::default(),
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ impl TodoPlugin {
)
.ok();
if let Some(w) = watcher.as_mut() {
let _ = w.watch(std::path::Path::new(&path), RecursiveMode::NonRecursive);
let p = std::path::Path::new(&path);
if w.watch(p, RecursiveMode::NonRecursive).is_err() {
let parent = p.parent().unwrap_or_else(|| std::path::Path::new("."));
let _ = w.watch(parent, RecursiveMode::NonRecursive);
}
}
Self {
matcher: SkimMatcherV2::default(),
Expand Down