mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-16 07:42:39 +00:00
home: reveal from the keyboard, and run Reveal off the UI thread
The Menu key or Shift+F10 on a focused Recent row opens the Reveal menu. The existence check and file-manager call now run on a worker on every platform; a failure comes back to the UI thread and is shown as a notification. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5e6899341f
commit
0ac9f72d95
4 files changed
+77
-18
No files matched your search
@@ -152,6 +152,8 @@ impl PdfCraftApp {
|
||||
open_prepare_gate: None,
|
||||
pinned: Default::default(),
|
||||
recent_check: Default::default(),
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
reveal_errors: Default::default(),
|
||||
pinned_tools: Vec::new(),
|
||||
pin_tool_search: String::new(),
|
||||
toast: None,
|
||||
@@ -468,6 +470,7 @@ impl eframe::App for PdfCraftApp {
|
||||
worker.set_context(ctx);
|
||||
}
|
||||
self.poll_pending_opens();
|
||||
self.show_reveal_errors();
|
||||
}
|
||||
self.check_restored_recent();
|
||||
// Chinese wants its own faces before the Japanese ones (one baseline per line).
|
||||
|
||||
@@ -213,13 +213,23 @@ pub fn show(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 46.0), Sense::click());
|
||||
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &r.name));
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
resp.context_menu(|ui| {
|
||||
let label = if cfg!(target_os = "macos") { tl!("Reveal in Finder") } else { tl!("Reveal in File Explorer") };
|
||||
if ui.button(label).clicked() {
|
||||
reveal = Some(r.path.clone());
|
||||
ui.close();
|
||||
{
|
||||
// The Menu key or Shift+F10 on the focused row opens the same menu a secondary
|
||||
// click does, under the row, so Reveal works from the keyboard.
|
||||
let by_key =
|
||||
resp.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Menu) || (i.modifiers.shift && i.key_pressed(egui::Key::F10)));
|
||||
let mut popup = egui::Popup::context_menu(&resp);
|
||||
if by_key {
|
||||
popup = popup.open_memory(egui::SetOpenCommand::Bool(true)).anchor(rect).align(egui::RectAlign::BOTTOM_START);
|
||||
}
|
||||
});
|
||||
popup.show(|ui| {
|
||||
let label = if cfg!(target_os = "macos") { tl!("Reveal in Finder") } else { tl!("Reveal in File Explorer") };
|
||||
if ui.button(label).clicked() {
|
||||
reveal = Some(r.path.clone());
|
||||
ui.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
let press = theme::Press::track(ui, &resp);
|
||||
// The row, not its response: the pointer stays "in" the row over its remove button.
|
||||
let hovered = ui.rect_contains_pointer(rect);
|
||||
@@ -329,6 +339,23 @@ mod tests {
|
||||
assert!(h.state().pinned_tools.is_empty());
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_focused_recent_row_opens_its_reveal_menu_from_the_keyboard() {
|
||||
use egui_kittest::kittest::Queryable;
|
||||
for chord in [(egui::Modifiers::NONE, egui::Key::Menu), (egui::Modifiers::SHIFT, egui::Key::F10)] {
|
||||
let mut h = review_home_harness();
|
||||
h.state_mut().recent = vec![crate::RecentFile { name: "notes.pdf".into(), path: "/nowhere/notes.pdf".into(), pages: 2, size: 2048 }];
|
||||
h.run_steps(3);
|
||||
assert!(!egui::Popup::is_any_open(&h.ctx));
|
||||
h.get_by_label("notes.pdf").focus();
|
||||
h.run_steps(2);
|
||||
h.key_press_modifiers(chord.0, chord.1);
|
||||
h.run_steps(3);
|
||||
assert!(egui::Popup::is_any_open(&h.ctx), "{chord:?} did not open the Reveal menu");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translated_card_description_leaves_room_for_the_action() {
|
||||
if !crate::i18n::needs_catalogs() {
|
||||
|
||||
@@ -416,6 +416,9 @@ pub struct PdfCraftApp {
|
||||
pub pinned: folders_ui::PinnedFolders,
|
||||
/// The recent files restored from settings, checked off the UI thread (#5682).
|
||||
recent_check: persist::recent_check::RecentCheck,
|
||||
/// Failures of the Reveal worker, taken each frame and shown as a notification.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
reveal_errors: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
|
||||
/// Tools pinned to Home, in user-defined order.
|
||||
pub pinned_tools: Vec<String>,
|
||||
/// Search query for the Pin a tool selection dialog.
|
||||
|
||||
@@ -8,10 +8,35 @@ pub(crate) enum Action {
|
||||
}
|
||||
|
||||
impl crate::PdfCraftApp {
|
||||
/// Reveal a local file without opening it or changing document/Recent state.
|
||||
/// Reveal a local file without opening it or changing document/Recent state. The existence
|
||||
/// check and the file manager call can block (a disconnected network mount, a slow `dbus-send`
|
||||
/// or `explorer.exe`), so they run on a worker; a failure is shown by
|
||||
/// [`Self::show_reveal_errors`]. Tests run it inline ([`PdfCraftApp::run_inline`]).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub(crate) fn reveal_path(&mut self, path: &std::path::Path) {
|
||||
if let Err(e) = reveal(path) {
|
||||
let path = path.to_path_buf();
|
||||
let errors = self.reveal_errors.clone();
|
||||
let ctx = self.ctx.clone();
|
||||
let work = move || {
|
||||
if let Err(e) = reveal(&path) {
|
||||
errors.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(e);
|
||||
if let Some(ctx) = &ctx {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
if self.run_inline {
|
||||
work();
|
||||
} else if let Err(e) = std::thread::Builder::new().name("pdfcraft-reveal".into()).spawn(work) {
|
||||
self.notify_error(e);
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the failures the reveal worker has reported (called each frame).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub(crate) fn show_reveal_errors(&mut self) {
|
||||
let errors = std::mem::take(&mut *self.reveal_errors.lock().unwrap_or_else(std::sync::PoisonError::into_inner));
|
||||
for e in errors {
|
||||
self.notify_error(e);
|
||||
}
|
||||
}
|
||||
@@ -54,6 +79,7 @@ impl crate::PdfCraftApp {
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
/// Blocking: call it from a worker thread ([`crate::PdfCraftApp::reveal_path`]).
|
||||
fn reveal(path: &std::path::Path) -> Result<(), String> {
|
||||
// Absolute paths also prevent a filename starting with '-' becoming an option.
|
||||
let path = std::path::absolute(path).map_err(|e| e.to_string())?;
|
||||
@@ -63,16 +89,6 @@ fn reveal(path: &std::path::Path) -> Result<(), String> {
|
||||
let platform = std::env::consts::OS;
|
||||
let run =
|
||||
|program: &str, args: &[std::ffi::OsString]| reveal_process(program, args).output().map(|o| o.status.success()).map_err(|e| e.to_string());
|
||||
if matches!(platform, "linux" | "freebsd") {
|
||||
// `dbus-send --print-reply` waits up to 2 s for the file manager (then xdg-open may run),
|
||||
// so it runs off the UI thread; a failure there is only logged.
|
||||
let spawned = std::thread::Builder::new().name("pdfcraft-reveal".into()).spawn(move || {
|
||||
if let Err(e) = reveal_via(platform, &path, run) {
|
||||
log::warn!("reveal {}: {e}", path.display());
|
||||
}
|
||||
});
|
||||
return spawned.map(|_| ()).map_err(|e| e.to_string());
|
||||
}
|
||||
reveal_via(platform, &path, run)
|
||||
}
|
||||
|
||||
@@ -236,6 +252,16 @@ mod tests {
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_reveal_is_reported_back_to_the_ui_thread() {
|
||||
let mut app = crate::PdfCraftApp::new();
|
||||
app.run_inline = true;
|
||||
app.reveal_path(std::path::Path::new("/pdfcraft-no-such-dir/missing.pdf"));
|
||||
assert!(app.toast.is_none(), "nothing is shown until the UI thread takes the report");
|
||||
app.show_reveal_errors();
|
||||
assert_eq!(app.toast.as_ref().map(|t| t.0.as_str()), Some("The file no longer exists"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unavailable_file_manager_falls_back_safely_and_reports_failure() {
|
||||
let path = std::path::Path::new("/tmp/年度 & reports/file.pdf");
|
||||
|
||||
Reference in new issue
Block a user