PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Dialogs: a close box in the title bar cancels like Cancel (#5496) (#5505)
David Mann8987980d1614
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 2930889ba..013c79e82 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -135,6 +135,26 @@ fn show_modal<T>(})}+/// Whether dialogs have a close box (×) in their title bar (#5496). Photoshop's modal dialogs have+/// one on Windows, where clicking it is Cancel, and none on macOS; Linux and the web follow Windows.+pub(crate) const CLOSE_BOX: bool = !cfg!(target_os = "macos");++/// The side of the close box's square, in points.+const CLOSE_BOX_SIZE: f32 = 20.0;++/// The close box's square: flush with the title band's right end `right`, centred on the title+/// row at height `centre_y`.+fn close_box_rect(right: f32, centre_y: f32) -> egui::Rect {+ egui::Rect::from_min_size(egui::pos2(right - CLOSE_BOX_SIZE, centre_y - CLOSE_BOX_SIZE / 2.0), egui::vec2(CLOSE_BOX_SIZE, CLOSE_BOX_SIZE))+}++/// Draws a title bar's close box in `rect`, the same × icon button the floating panels close with,+/// and says whether it was clicked. Its own child `Ui`, so the title row's layout is unchanged.+pub(crate) fn close_box(ui: &mut egui::Ui, rect: egui::Rect) -> bool {+ let mut child = ui.new_child(egui::UiBuilder::new().max_rect(rect).layout(egui::Layout::right_to_left(egui::Align::Center)));+ crate::icons::button(&mut child, "x", CLOSE_BOX_SIZE, false, tl!("Close")).clicked()+}+pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {crate::layer_style::color_picker::prune(app);crate::type_panels_ui::color_picker::prune(app);@@ -163,6 +183,7 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {let place = place_id(&d);let start = pinned.or_else(|| ctx.data_mut(|m| m.get_persisted::<egui::Vec2>(place))).or_else(|| crate::blur_pins::first_place(&d.fields, ctx));let mut drag = egui::Vec2::ZERO;+ let mut close_clicked = false;let mut sizing = false;let area = match start {Some(offset) => egui::Modal::default_area(id).anchor(egui::Align2::LEFT_TOP, offset),@@ -242,8 +263,15 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {let t = if crate::color_picker_ui::owns(&fields) { ui.add_sized(egui::vec2(ui.available_width(), 22.0), label).rect } else { ui.add(label).rect };// The whole title band drags (#1921): the full width plus a little of the popup's// padding around it and the gap above the hairline, but none of the controls below.- let bar = egui::Rect::from_min_max(t.min - egui::vec2(7.0, 7.0), egui::pos2(ui.max_rect().right() + 7.0, t.bottom() + 4.0));+ // The close box (#5496) sits at the band's right end and is left out of the drag area:+ // egui would let a drag that starts on a button move the dialog.+ let close_rect = CLOSE_BOX.then(|| close_box_rect(ui.max_rect().right(), t.center().y));+ let bar_right = close_rect.map_or(ui.max_rect().right() + 7.0, |r| r.left());+ let bar = egui::Rect::from_min_max(t.min - egui::vec2(7.0, 7.0), egui::pos2(bar_right, t.bottom() + 4.0));drag = ui.interact(bar, id.with("title"), egui::Sense::drag()).drag_delta();+ if let Some(r) = close_rect {+ close_clicked = close_box(ui, r);+ }ui.add_space(4.0);crate::widgets::hairline(ui);ui.add_space(8.0);@@ -458,11 +486,13 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {ctx.data_mut(|m| m.insert_persisted(place, offset));}}- // Esc cancels (topmost dialog, no popup open). A click outside does nothing: Photoshop keeps- // the dialog, and the pointer may be panning or zooming the canvas under it.+ // Esc and the title bar's close box cancel (topmost dialog, no popup open for Esc). A click+ // outside does nothing: Photoshop keeps the dialog, and the pointer may be panning or+ // zooming the canvas under it.if interactive&& outcome.is_none()- && (modal.response.should_close()+ && (close_clicked+ || modal.response.should_close()|| (modal.is_top_modal && !modal.any_popup_open && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)))){outcome = Some(false);@@ -735,6 +765,83 @@ mod tests {assert_eq!(crate::canvas::committed_filter_preview(&mut app, 0), None);}+ /// #5496: a pointer click on the title bar's close box does what Cancel does: the dialog+ /// closes, the live preview goes and nothing reaches the document or the journal. On macOS+ /// dialogs have no close box.+ #[test]+ fn the_title_bar_close_box_cancels_like_cancel() {+ use egui_kittest::kittest::Queryable;+ let close = |button: &str| {+ let (mut app, _) = filter_dialog_with_preview(false);+ let revision = app.session.active().unwrap().revision;+ let journal = app.session.journal.len();+ // The fonts are set up after the harness's first frame: the dialog appears after it.+ let dialogs = std::mem::take(&mut app.ui.dialogs);+ let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1280.0, 800.0)).build_ui_state(|ui, app| show(app, ui.ctx()), app);+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);+ h.state_mut().ui.dialogs = dialogs;+ h.run_steps(4);+ assert!(h.state().filter_preview.is_some(), "the preview is on screen");+ if !CLOSE_BOX {+ assert_eq!(h.query_all_by_label("Close").count(), 0, "no close box on macOS");+ }+ let target = h.query_by_label(button).map(|n| n.rect().center())?;+ h.hover_at(target);+ h.drag_at(target);+ h.drop_at(target);+ h.run_steps(3);+ let app = h.state();+ Some((+ app.ui.dialogs.is_empty(),+ app.filter_preview.is_none(),+ app.session.active().unwrap().revision == revision,+ app.session.journal.len() == journal,+ ))+ };+ let cancel = close("Cancel");+ assert_eq!(cancel, Some((true, true, true, true)), "Cancel closes, drops the preview and changes nothing");+ if CLOSE_BOX {+ assert_eq!(close("Close"), cancel, "the × is Cancel");+ }+ }++ /// #5496: the close box sits at the title band's right end, and the band stays a drag handle+ /// around it: a drag that starts on the × neither moves nor closes the dialog, and the band+ /// just left of the × still moves it.+ #[test]+ fn the_close_box_and_the_title_drag_keep_apart() {+ use egui_kittest::kittest::Queryable;+ if !CLOSE_BOX {+ return;+ }+ let mut h = dialog_harness(egui::vec2(1280.0, 800.0), "filter.blur.gaussianBlur");+ let title = h.get_by_label("Gaussian Blur").rect();+ let x = h.get_by_label("Close").rect();+ let frame = rects(&h.ctx)[0];+ assert!((x.center().y - title.center().y).abs() < 2.0 && x.left() > title.right(), "the × is on the title row: {x:?}, {title:?}");+ assert!(frame.right() - x.right() < 16.0, "at its right end: {x:?} in {frame:?}");+ let drag = |h: &mut egui_kittest::Harness<'static, PhotocraftApp>, from: egui::Pos2, to: egui::Pos2| {+ h.hover_at(from);+ h.drag_at(from);+ h.run_steps(2);+ for i in 1..=8 {+ h.hover_at(from + (to - from) * (i as f32 / 8.0));+ h.run_steps(1);+ }+ h.drop_at(to);+ h.run_steps(3);+ };+ drag(&mut h, x.center(), x.center() + egui::vec2(-80.0, 60.0));+ assert_eq!(h.state().ui.dialogs.len(), 1, "a drag from the × doesn't close the dialog");+ assert!((h.get_by_label("Gaussian Blur").rect().min - title.min).length() < 1.0, "nor move it");+ let x = h.get_by_label("Close").rect();+ let beside = x.left_center() - egui::vec2(4.0, 0.0);+ drag(&mut h, beside, beside + egui::vec2(-120.0, 40.0));+ assert_eq!(h.state().ui.dialogs.len(), 1, "a title drag doesn't close the dialog");+ let moved = h.get_by_label("Gaussian Blur").rect().min - title.min;+ assert!((moved - egui::vec2(-120.0, 40.0)).length() < 2.0, "the band beside the × moved the dialog: {moved:?}");+ }+#[test]fn color_picker_has_inset_content_and_actions_at_the_right_edge() {use egui_kittest::{Harness, kittest::Queryable};diff --git a/crates/ui-egui/src/type_panels_ui.rs b/crates/ui-egui/src/type_panels_ui.rsindex 499e8eac4..d97a649fb 100644--- a/crates/ui-egui/src/type_panels_ui.rs+++ b/crates/ui-egui/src/type_panels_ui.rs@@ -768,7 +768,17 @@ fn spelling_window(app: &mut PhotocraftApp, ctx: &egui::Context) {.anchor(Align2::CENTER_CENTER, vec2(0.0, -40.0)).show(ctx, |ui| {ui.set_width(380.0);- ui.label(RichText::new(tl!("Check Spelling")).color(t.text).size(13.0).strong());+ ui.horizontal(|ui| {+ ui.label(RichText::new(tl!("Check Spelling")).color(t.text).size(13.0).strong());+ // The close box (#5496) is Done, as in Photoshop: every change is already made.+ if crate::dialogs::CLOSE_BOX {+ ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {+ if crate::icons::button(ui, "x", 20.0, false, tl!("Close")).clicked() {+ act = Some("done");+ }+ });+ }+ });ui.add_space(6.0);ui.horizontal_top(|ui| {ui.vertical(|ui| {@@ -930,4 +940,33 @@ mod tests {// Headless calls with an action go to the engine.assert!(menu(&mut app, "edit.checkSpelling", &json!({"action": "list"})).is_none());}++ /// #5496: Check Spelling's close box is Done, as in Photoshop on Windows: the window closes+ /// and the text keeps what it had. None on macOS.+ #[test]+ fn check_spellings_close_box_is_done() {+ use egui_kittest::kittest::Queryable;+ let mut app = app_with_text("Teh cat");+ menu(&mut app, "edit.checkSpelling", &json!({})).unwrap().unwrap();+ let revision = app.session.active().unwrap().revision;+ // The fonts are set up after the harness's first frame: the window appears after it.+ let spell = app.ui.type_panels.spell.take();+ let mut h = egui_kittest::Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(|ui, app| windows(app, ui.ctx()), app);+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);+ h.state_mut().ui.type_panels.spell = spell;+ h.run_steps(3);+ if !crate::dialogs::CLOSE_BOX {+ assert_eq!(h.query_all_by_label("Close").count(), 0, "no close box on macOS");+ return;+ }+ let x = h.get_by_label("Close").rect();+ let done = h.get_by_label("Done").rect();+ assert!(x.bottom() <= done.top() && x.right() >= done.right(), "the × ends the title row: {x:?} over {done:?}");+ h.hover_at(x.center());+ h.drag_at(x.center());+ h.drop_at(x.center());+ h.run_steps(2);+ assert!(h.state().ui.type_panels.spell.is_none(), "the × closed Check Spelling");+ assert_eq!(h.state().session.active().unwrap().revision, revision, "and changed nothing");+ }}