PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Double-clicking the empty workspace shows File › Open (#5367) (#5429)
Daktyl051959ea7b41
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 7e2e66ff4..09d7dcc8e 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1993,7 +1993,9 @@ fn documents(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}if n == 0 && !opening {// Auto show the Home Screen is off: an empty workspace, like Photoshop.- paint_dots(ui, ui.available_rect_before_wrap(), app.session.prefs().interface.show_canvas_dots);+ let area = ui.available_rect_before_wrap();+ paint_dots(ui, area, app.session.prefs().interface.show_canvas_dots);+ open_on_double_click(app, ui, area);return;}if !app.ui.view.hides_tabs() || opening {@@ -2628,10 +2630,20 @@ pub fn mode_label(doc: &Document) -> &'static str {}}+/// With no document open, double-clicking the empty workspace shows File › Open, as in Photoshop+/// (#5367), and so does the Home screen's blank background. Registered before anything drawn over+/// `area`, so the buttons and links there keep their own clicks; not a keyboard focus stop.+fn open_on_double_click(app: &mut PhotocraftApp, ui: &egui::Ui, area: Rect) {+ if app.session.documents().is_empty() && ui.interact(area, ui.id().with("empty-workspace"), Sense::CLICK).double_clicked() {+ let _ = app.open_dialog_file();+ }+}+fn start_screen(app: &mut PhotocraftApp, ui: &mut egui::Ui) {let t = crate::theme::Tokens::get(ui.ctx());let area = ui.available_rect_before_wrap();paint_dots(ui, area, app.session.prefs().interface.show_canvas_dots);+ open_on_double_click(app, ui, area);// File › Open Recent, newest first (on the web there are no paths to reopen).let show_recent = app.session.prefs().interface.show_home_recent_files;let recent: Vec<String> =diff --git a/crates/ui-egui/src/empty_workspace_tests.rs b/crates/ui-egui/src/empty_workspace_tests.rsnew file mode 100644index 000000000..f7473dbcc--- /dev/null+++ b/crates/ui-egui/src/empty_workspace_tests.rs@@ -0,0 +1,89 @@+//! With no document open, double-clicking the empty workspace shows File › Open, as in Photoshop+//! (#5367): with Auto Show the Home Screen on (the Home screen's blank background) and off (the+//! plain workspace). A single click does nothing, and the Home screen's own buttons keep theirs.++use std::cell::RefCell;+use std::rc::Rc;++use egui::{Event, Modifiers, PointerButton, Pos2, pos2, vec2};+use egui_kittest::Harness;+use egui_kittest::kittest::Queryable;++use crate::file_dialog::{FileDialogReply, FileDialogRequest};+use crate::{PhotocraftApp, Services};++type Requests = Rc<RefCell<Vec<(FileDialogRequest, FileDialogReply)>>>;++fn harness(home: bool) -> (Harness<'static, PhotocraftApp>, Requests) {+ let requests: Requests = Default::default();+ let held = requests.clone();+ let services = Services { file_dialog: Some(Box::new(move |request, _parent, reply| held.borrow_mut().push((request, reply)))), ..Default::default() };+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services);+ app.session.edit_prefs(|p| p.general.auto_show_home_screen = home);+ let mut h = Harness::builder().with_size(vec2(1200.0, 800.0)).with_step_dt(1.0 / 60.0).build_ui_state(+ |ui, app: &mut PhotocraftApp| {+ let ctx = ui.ctx().clone();+ // Fonts set up after the first frame only apply from the next one.+ if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {+ return;+ }+ egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));+ app.poll_file_dialog(&ctx, None);+ },+ app,+ );+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);+ h.run_steps(4);+ (h, requests)+}++fn button(h: &mut Harness<'static, PhotocraftApp>, at: Pos2, pressed: bool) {+ h.event(Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });+}++/// `count` clicks two frames apart at `at`, after letting the clock move past earlier clicks.+fn clicks(h: &mut Harness<'static, PhotocraftApp>, at: Pos2, count: usize) {+ h.hover_at(at);+ for _ in 0..40 {+ h.step();+ }+ for _ in 0..count {+ button(h, at, true);+ h.step();+ button(h, at, false);+ h.step();+ }+ h.run_steps(3);+}++fn opens(requests: &Requests) -> usize {+ requests.borrow().iter().filter(|(r, _)| matches!(r, FileDialogRequest::Open { multiple: true, .. })).count()+}++#[test]+fn double_clicking_the_empty_workspace_shows_file_open() {+ for home in [false, true] {+ let (mut h, requests) = harness(home);+ assert!(h.state().session.documents().is_empty());+ // Well away from the Home screen's centred card.+ let blank = pos2(40.0, 760.0);+ clicks(&mut h, blank, 1);+ assert_eq!(opens(&requests), 0, "home {home}: a single click opens nothing");+ clicks(&mut h, blank, 2);+ assert_eq!(opens(&requests), 1, "home {home}: a double-click shows File › Open");+ }+}++#[test]+fn the_home_screens_own_widgets_keep_their_double_clicks() {+ let (mut h, requests) = harness(true);+ // The subtitle is selectable text: a double-click there selects a word, not File › Open.+ let at = h.get_by_label_contains("Create a new document or open an existing file").rect().center();+ clicks(&mut h, at, 2);+ assert_eq!(opens(&requests), 0, "the label took the double-click");+ // The New document button opens the New Document dialog, not File › Open.+ let at = h.get_by_label_contains("New document").rect().center();+ clicks(&mut h, at, 1);+ assert_eq!(opens(&requests), 0);+ assert_eq!(h.state().ui.dialogs.len(), 1, "the New Document dialog");+}diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex d8d091314..f8cc8f21a 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2601,6 +2601,9 @@ mod profile_prompt_tests;#[cfg(test)]mod input_tests;+#[cfg(test)]+mod empty_workspace_tests;+#[cfg(test)]mod pen_variants_tests;