Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Symmetry: Transform Symmetry gets commit/cancel buttons and its own cursors (#4495) (#4508)

Dan Hiornsad62f350e5bf
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index febb6571e..7beaa0893 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3563,6 +3563,8 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
let toward = xf.to_screen((d[0] + k[0] * 16.0) as f32, (d[1] + k[1] * 16.0) as f32) - p;
crate::crop_ui::draw_turn_cursor(&ctx, p, toward);
}
+ } else if let Some(c) = response.hover_pos().and_then(|p| crate::symmetry_ui::cursor(app, &xf, xf.to_doc(p))) {
+ ui.ctx().set_cursor_icon(c);
} else if let Some(c) = response.hover_pos().and_then(|p| crate::cam_transform::cursor(app, &ctx, &xf, p)) {
ui.ctx().set_cursor_icon(c);
} else if let Some((r, p, c)) =
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 17b4f43cb..2aae1c55e 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -964,7 +964,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
crate::transform_tool::options_bar(app, ui);
return;
}
- if crate::distort_ui::options_bar(app, ui) {
+ if crate::symmetry_ui::options_bar(app, ui) || crate::distort_ui::options_bar(app, ui) {
return;
}
let tool = app.ui.tool;
diff --git a/crates/ui-egui/src/symmetry_ui.rs b/crates/ui-egui/src/symmetry_ui.rs
index 5441f8aa9..4ccdc2277 100644
--- a/crates/ui-egui/src/symmetry_ui.rs
+++ b/crates/ui-egui/src/symmetry_ui.rs
@@ -83,6 +83,44 @@ pub fn commit(app: &mut PhotocraftApp) {
let _ = app.run("paint.setSymmetry", json!({"mode":t.preview.mode.id(), "center":t.preview.center,"rotation":t.preview.rotation}));
}
}
+/// While the symmetry is being transformed, the options bar names the edit and gives it ✓ and ⊘
+/// buttons, as Free Transform's bar does: Enter and Esc aren't the only ways out, which a pen or
+/// touch user may have no key for (#4495). False (nothing drawn) when no transform is open.
+pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> bool {
+ if app.ui.symmetry_transform.is_none() {
+ return false;
+ }
+ ui.label(egui::RichText::new(tl!("Transform Symmetry")).color(Tokens::get(ui.ctx()).text_dim));
+ widgets::vline(ui, 22.0);
+ let tip = crate::i18n::fmt(tl!("Commit transform ({key})"), &[("key", &crate::shortcuts::pretty("Enter"))]);
+ let commit_btn = icons::button(ui, "check", 26.0, false, &tip);
+ commit_btn.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Commit transform"));
+ if commit_btn.clicked() {
+ commit(app);
+ }
+ let cancel = icons::button(ui, "ban", 26.0, false, tl!("Cancel transform (Esc)"));
+ cancel.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Cancel transform"));
+ if cancel.clicked() {
+ app.ui.symmetry_transform = None;
+ }
+ true
+}
+/// The canvas cursor while the symmetry is being transformed: the move cursor over the centre,
+/// the rotate cursor over the rotation handle and the arrow elsewhere, never the brush outline,
+/// since a press there doesn't paint (#4495). `None` when no transform is open on this document.
+pub fn cursor(app: &PhotocraftApp, xf: &ViewXform, p: [f64; 2]) -> Option<egui::CursorIcon> {
+ let t = app.ui.symmetry_transform.as_ref().filter(|t| app.session.active_index() == Some(t.document))?;
+ let size = app.session.active().map(|s| [f64::from(s.doc.size.width), f64::from(s.doc.size.height)]).unwrap_or([1.0, 1.0]);
+ let tol = 8.0 / f64::from(xf.zoom.max(0.001));
+ let distance = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).hypot(a[1] - b[1]);
+ Some(match t.gesture {
+ Some(Gesture::Move { .. }) => egui::CursorIcon::Move,
+ Some(Gesture::Rotate { .. }) => egui::CursorIcon::Alias,
+ None if distance(p, t.preview.center) <= tol => egui::CursorIcon::Move,
+ None if distance(p, rotation_handle(t.preview, size)) <= tol => egui::CursorIcon::Alias,
+ None => egui::CursorIcon::Default,
+ })
+}
fn valid(app: &PhotocraftApp, t: &Transform) -> bool {
app.session.active_index() == Some(t.document)
&& app.ui.tool == t.tool
diff --git a/crates/ui-egui/src/symmetry_ui/tests.rs b/crates/ui-egui/src/symmetry_ui/tests.rs
index 3510e4c83..9f6644957 100644
--- a/crates/ui-egui/src/symmetry_ui/tests.rs
+++ b/crates/ui-egui/src/symmetry_ui/tests.rs
@@ -188,3 +188,56 @@ fn overlays_follow_document_view_transforms_in_every_theme() {
assert_eq!(crate::i18n::tr(de, "Dual Axis"), "Duale Achse");
assert_eq!(crate::i18n::tr(de, "Transform Symmetry"), "Symmetrie transformieren");
}
+/// #4495: while the symmetry is transformed the options bar has ✓ and ⊘ (Enter and Esc were the
+/// only ways out), and the canvas shows the move, rotate or arrow cursor instead of the brush.
+#[test]
+fn transform_has_commit_and_cancel_buttons_and_its_own_cursors() {
+ let mut h = Harness::builder().with_size(vec2(1280.0, 800.0)).with_max_steps(64).build_eframe(|cc| {
+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
+ PhotocraftApp::new(Session::new(), crate::Services::default())
+ });
+ h.state_mut().run("file.new", json!({"width":400,"height":300})).unwrap();
+ h.state_mut().ui.tool = Tool::Brush;
+ h.state_mut().run("paint.setSymmetry", json!({"mode":"dual"})).unwrap();
+ h.run_steps(4);
+ let cursor = |h: &Harness<'_, PhotocraftApp>| h.output().platform_output.cursor_icon;
+ let begin_transform = |h: &mut Harness<'_, PhotocraftApp>| {
+ let ctx = h.ctx.clone();
+ crate::menus::invoke(h.state_mut(), &ctx, "ui.symmetryTransform", json!({})).unwrap();
+ h.run_steps(3);
+ };
+ let xf = ViewXform::active(h.state()).expect("a canvas");
+ let off_handles = xf.to_screen(40.0, 260.0);
+ // Painting: the brush outline replaces the pointer.
+ h.hover_at(off_handles);
+ h.run_steps(2);
+ assert_ne!(cursor(&h), egui::CursorIcon::Default, "the brush cursor while painting");
+ assert!(h.query_by_label("Commit transform").is_none(), "no ✓ outside the transform");
+
+ begin_transform(&mut h);
+ h.hover_at(off_handles);
+ h.run_steps(2);
+ assert_eq!(cursor(&h), egui::CursorIcon::Default, "an arrow, not the brush, away from the handles");
+ let p = h.state().ui.symmetry_transform.as_ref().unwrap().preview;
+ h.hover_at(xf.to_screen(p.center[0] as f32, p.center[1] as f32));
+ h.run_steps(2);
+ assert_eq!(cursor(&h), egui::CursorIcon::Move, "the centre moves");
+ let r = rotation_handle(p, [400.0, 300.0]);
+ h.hover_at(xf.to_screen(r[0] as f32, r[1] as f32));
+ h.run_steps(2);
+ assert_eq!(cursor(&h), egui::CursorIcon::Alias, "the handle rotates");
+
+ // ⊘ drops the preview; ✓ applies it.
+ h.state_mut().ui.symmetry_transform.as_mut().unwrap().preview = PresetSymmetry::new(p.mode, [120.0, 90.0], 30.0).unwrap();
+ h.get_by_label("Cancel transform").click();
+ h.run_steps(3);
+ assert!(h.state().ui.symmetry_transform.is_none());
+ assert_eq!(preset(h.state()).unwrap().center, [200.0, 150.0], "cancel keeps the original");
+ begin_transform(&mut h);
+ h.state_mut().ui.symmetry_transform.as_mut().unwrap().preview = PresetSymmetry::new(p.mode, [120.0, 90.0], 30.0).unwrap();
+ h.get_by_label("Commit transform").click();
+ h.run_steps(3);
+ assert!(h.state().ui.symmetry_transform.is_none());
+ let done = preset(h.state()).unwrap();
+ assert_eq!((done.center, done.rotation), ([120.0, 90.0], 30.0), "commit applies the preview");
+}