Skip to content

Commit 9074e3b

Browse files
committed
add back hot reloading for dynamic app reload
1 parent c2919da commit 9074e3b

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub enum Message {
9292
HideTrayIcon,
9393
SwitchMode(String),
9494
ReloadConfig,
95+
UpdateApps,
9596
SetSender(ExtSender),
9697
SwitchToPage(Page),
9798
ClipboardHistory(ClipBoardContentType),

src/app/tile.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::app::{ArrowKey, Message, Move, Page};
77
use crate::clipboard::ClipBoardContentType;
88
use crate::config::Config;
99
use crate::debounce::Debouncer;
10+
use crate::platform::default_app_paths;
1011

1112
use arboard::Clipboard;
1213
use global_hotkey::hotkey::HotKey;
@@ -176,6 +177,7 @@ impl Tile {
176177
});
177178
Subscription::batch([
178179
Subscription::run(handle_hotkeys),
180+
Subscription::run(handle_hot_reloading),
179181
keyboard,
180182
Subscription::run(handle_recipient),
181183
Subscription::run(check_version),
@@ -389,6 +391,45 @@ async fn read_mdfind_results(
389391
}
390392
}
391393

394+
fn handle_hot_reloading() -> impl futures::Stream<Item = Message> {
395+
stream::channel(100, async |mut output| {
396+
let paths = default_app_paths();
397+
let mut total_files: usize = paths
398+
.par_iter()
399+
.map(|dir| count_dirs_in_dir(std::path::Path::new(dir)))
400+
.sum();
401+
402+
loop {
403+
let current_total_files: usize = paths
404+
.par_iter()
405+
.map(|dir| count_dirs_in_dir(std::path::Path::new(dir)))
406+
.sum();
407+
408+
if total_files != current_total_files {
409+
total_files = current_total_files;
410+
info!("App count was changed");
411+
let _ = output.send(Message::UpdateApps).await;
412+
}
413+
414+
tokio::time::sleep(Duration::from_millis(1000)).await;
415+
}
416+
})
417+
}
418+
419+
/// Helper fn for counting directories (since macos `.app`'s are directories) inside a directory
420+
fn count_dirs_in_dir(dir: impl AsRef<std::path::Path>) -> usize {
421+
// Read the directory; if it fails, treat as empty
422+
let entries = match std::fs::read_dir(dir) {
423+
Ok(e) => e,
424+
Err(_) => return 0,
425+
};
426+
427+
entries
428+
.filter_map(|entry| entry.ok())
429+
.filter(|entry| entry.file_type().map(|t| t.is_dir()).unwrap_or(false))
430+
.count()
431+
}
432+
392433
/// Async subscription that spawns `mdfind` for file search queries.
393434
///
394435
/// Uses a `watch` channel so the Tile can push new (query, dirs) pairs.

src/app/tile/update.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,8 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
276276
.ok();
277277
}
278278

279-
let mut new_options = get_installed_apps(new_config.theme.show_icons);
280-
new_options.extend(new_config.shells.iter().map(|x| x.to_app()));
281-
new_options.extend(new_config.modes.to_apps());
282-
new_options.extend(App::basic_apps());
283-
new_options.par_sort_by_key(|x| x.display_name.len());
284-
285279
tile.theme = new_config.theme.to_owned().into();
286280
tile.config = new_config;
287-
tile.options = AppIndex::from_apps(new_options);
288281
Task::none()
289282
}
290283

@@ -413,6 +406,17 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
413406
])
414407
}
415408

409+
Message::UpdateApps => {
410+
let mut new_options = get_installed_apps(tile.config.theme.show_icons);
411+
new_options.extend(tile.config.shells.iter().map(|x| x.to_app()));
412+
new_options.extend(tile.config.modes.to_apps());
413+
new_options.extend(App::basic_apps());
414+
new_options.par_sort_by_key(|x| x.display_name.len());
415+
tile.options = AppIndex::from_apps(new_options);
416+
417+
Task::none()
418+
}
419+
416420
Message::ClearSearchResults => {
417421
tile.results = Vec::new();
418422
Task::none()

0 commit comments

Comments
 (0)