Skip to content

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

Image Size and Canvas Size open on Width, selected (#2329) (#2357)

alexanderalber1c4a0d12dabc
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index dc64c2522..da372d806 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -67,6 +67,22 @@ pub fn free_press(ctx: &egui::Context, canvas: egui::Rect) -> Option<egui::Pos2>
/// scrolls.
const DIALOG_CHROME: f32 = 150.0;
+/// Draw a dialog body that opens on its first number, selected, as Photoshop's value dialogs do:
+/// typing replaces it and Enter applies it (#1757). Once per dialog `id`, on the first laid-out
+/// frame.
+fn first_field_focused<R>(ui: &mut egui::Ui, id: egui::Id, body: impl FnOnce(&mut egui::Ui) -> R) -> R {
+ let ctx = ui.ctx().clone();
+ let focused = id.with("first-field");
+ let first = !ui.is_sizing_pass() && !ctx.data(|m| m.get_temp::<bool>(focused).unwrap_or(false));
+ if first {
+ ctx.data_mut(|m| m.insert_temp(focused, true));
+ }
+ crate::widgets::focus_first_field(&ctx, first);
+ let r = body(ui);
+ crate::widgets::focus_first_field(&ctx, false);
+ r
+}
+
/// Where a dialog's last position is remembered: per command for command dialogs (each filter has
/// its own), per kind for the rest.
fn place_id(d: &Dialog) -> egui::Id {
@@ -201,7 +217,8 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
DialogKind::Command if crate::color_range_ui::owns(&fields) => crate::color_range_ui::body(app, ui, &mut fields),
DialogKind::Command if crate::prefs_ui::owns(&fields) => crate::prefs_ui::body(app, ui, &mut fields),
DialogKind::Command if fields.contains_key("__export") => crate::export_dialog::body(app, ui, &mut fields),
- DialogKind::Command if fields.contains_key("__sizing") => crate::sizing::body(ui, &mut fields),
+ // Image Size and Canvas Size open on Width, selected (#2329).
+ DialogKind::Command if fields.contains_key("__sizing") => first_field_focused(ui, id, |ui| crate::sizing::body(ui, &mut fields)),
DialogKind::Command if crate::adjust_dialog::owns(&fields) => crate::adjust_dialog::body(app, ui, &mut fields),
DialogKind::Command if fields.contains_key("__filter") => {
// Color Settings: the monitor profile in use can change while it is open.
@@ -211,16 +228,9 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
// Long parameter lists (Flame, Lighting Effects) scroll, so the title and the
// OK / Cancel buttons stay inside a small window.
let room = (ctx.content_rect().height() - DIALOG_CHROME).max(120.0);
- // Photoshop opens a value dialog on its first number, selected: typing
- // replaces it and Enter applies it (#1757). Once, on the first laid-out frame.
- let focused = id.with("first-field");
- let first = !ui.is_sizing_pass() && !ctx.data(|m| m.get_temp::<bool>(focused).unwrap_or(false));
- if first {
- ctx.data_mut(|m| m.insert_temp(focused, true));
- }
- crate::widgets::focus_first_field(ctx, first);
- egui::ScrollArea::vertical().id_salt(id.with("body")).max_height(room).show(ui, |ui| crate::filter_dialog::body(ui, &mut fields));
- crate::widgets::focus_first_field(ctx, false);
+ first_field_focused(ui, id, |ui| {
+ egui::ScrollArea::vertical().id_salt(id.with("body")).max_height(room).show(ui, |ui| crate::filter_dialog::body(ui, &mut fields));
+ });
}
DialogKind::Command if fields.contains_key("__form") => crate::view_cmds::form_body(ui, &mut fields),
DialogKind::Command => {}
diff --git a/crates/ui-egui/src/sizing.rs b/crates/ui-egui/src/sizing.rs
index ae3e94007..61cebf7f9 100644
--- a/crates/ui-egui/src/sizing.rs
+++ b/crates/ui-egui/src/sizing.rs
@@ -419,13 +419,8 @@ mod tests {
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
let id = open(h.state_mut(), "image.imageSize").unwrap();
h.run_steps(3);
- let width = h.query_all_by_role(Role::SpinButton).next().map(|n| n.rect()).expect("the Width field");
- h.hover_at(width.center());
- h.run_steps(1);
- h.drag_at(width.center());
- h.run_steps(1);
- h.drop_at(width.center());
- h.run_steps(2);
+ // The dialog opens on Width, selected (#2329): the typed 0 replaces the 4.
+ assert!(h.query_all_by_role(Role::SpinButton).next().expect("the Width field").is_focused());
h.event(egui::Event::Text("0".into()));
h.run_steps(1);
h.key_press(egui::Key::Tab);
@@ -435,4 +430,29 @@ mod tests {
crate::dialogs::confirm(h.state_mut(), id).unwrap();
assert_eq!(h.state().session.active().unwrap().doc.size, photocraft_doc::Size::new(1, 1));
}
+
+ /// #2329: Image Size and Canvas Size open on Width with its value selected, as in Photoshop:
+ /// typing replaces it and Enter applies it.
+ #[test]
+ fn sizing_dialogs_open_on_width_selected() {
+ use egui::accesskit::Role;
+ use egui_kittest::{Harness, kittest::Queryable};
+ for command in ["image.imageSize", "image.canvasSize"] {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.session.execute("file.new", json!({"width": 200, "height": 100})).unwrap();
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app);
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
+ open(h.state_mut(), command).unwrap();
+ h.run_steps(3);
+ assert!(h.query_all_by_role(Role::SpinButton).next().expect("the Width field").is_focused(), "{command}: Width has focus");
+ h.event(egui::Event::Text("50".into()));
+ h.run_steps(1);
+ let f = h.state().ui.dialogs.first().map(|d| d.fields.clone()).expect("the dialog is open");
+ assert_eq!(num(&f, "width"), 50.0, "{command}: typing replaced the selected 200");
+ h.key_press(egui::Key::Enter);
+ h.run_steps(3);
+ assert!(h.state().ui.dialogs.is_empty(), "{command}: Enter is OK");
+ assert_eq!(h.state().session.active().unwrap().doc.size.width, 50, "{command}");
+ }
+ }
}