PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Dialogs: Tab walks the text fields only, as in Photoshop (#1285)
texjer78f32cac2633
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 4e12f58ae..c4a700aef 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -129,6 +129,8 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {if color_picker {modal = modal.frame(egui::Frame::popup(&ctx.global_style()).inner_margin(16));}+ // Tab / ⇧Tab walk the topmost dialog's text fields, not every widget (field_tab.rs).+ let tab = if top == Some(d.id) && !egui::Popup::is_any_open(ctx) { crate::field_tab::take_step(ctx) } else { 0 };// Photoshop doesn't dim the window behind dialogs: previews must be judged at true contrast.let modal = modal.show(ctx, |ui| {if !interactive {@@ -173,6 +175,7 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {ui.add_space(4.0);crate::widgets::hairline(ui);ui.add_space(8.0);+ crate::field_tab::begin(ui.ctx());match d.kind {DialogKind::NewDocument => {if crate::new_doc_ui::body(app, ui, &mut fields) {@@ -243,6 +246,7 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {ui.label(fields.get("message").and_then(Value::as_str).unwrap_or("Error"));}}+ crate::field_tab::end(ui.ctx(), tab);if crate::color_picker_ui::owns(&fields) {if interactive && outcome.is_none() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {outcome = Some(true);diff --git a/crates/ui-egui/src/field_tab.rs b/crates/ui-egui/src/field_tab.rsnew file mode 100644index 000000000..467c7d8b5--- /dev/null+++ b/crates/ui-egui/src/field_tab.rs@@ -0,0 +1,228 @@+//! Photoshop-style Tab in dialogs: Tab and ⇧Tab cycle a dialog's text and number fields only+//! (Name → Width → Height → Resolution in New Document, Hue → Saturation → Lightness in+//! Hue/Saturation), never its sliders, dropdowns, checkboxes or buttons, and a field reached this+//! way has its text selected so typing replaces it.+//!+//! egui's own Tab walks every focusable widget in creation order (the title bar, the preset+//! cards, each slider…). So the dialog frame (`dialogs::show`) takes the key before egui's focus+//! machinery acts on it ([`take_step`]), the fields register themselves as the body is laid out+//! ([`register`], from `widgets::number_edit` and the dialogs' text fields), and the frame then+//! hands focus to the next one ([`end`]).++use egui::{Context, Id, Key, Modifiers};++fn list_id() -> Id {+ Id::new("pc-field-tab-order")+}++fn collecting_id() -> Id {+ Id::new("pc-field-tab-collecting")+}++fn pending_id() -> Id {+ Id::new("pc-field-tab-pending")+}++/// Give `id` the focus from the next frame on. Focusing a widget after it was laid out would+/// never read as "gained focus" to it (egui compares with the focus at the start of the frame),+/// and that is what selects a field's text; so the move waits for [`take_step`] next frame.+pub fn focus(ctx: &Context, id: Id) {+ ctx.data_mut(|d| d.insert_temp(pending_id(), Some(id)));+}++/// Tab (+1) or ⇧Tab (−1) pressed this frame, taken from the input so nothing else acts on it;+/// 0 when neither was. Also applies a pending [`focus`]. Call before the dialog is laid out.+pub fn take_step(ctx: &Context) -> i32 {+ if let Some(id) = ctx.data_mut(|d| d.remove_temp::<Option<Id>>(pending_id())).flatten() {+ ctx.memory_mut(|m| m.request_focus(id));+ }+ let step = ctx.input_mut(|i| {+ if i.consume_key(Modifiers::SHIFT, Key::Tab) {+ -1+ } else if i.consume_key(Modifiers::NONE, Key::Tab) {+ 1+ } else {+ 0+ }+ });+ if step != 0 {+ // egui read the key before this frame's widgets were laid out: cancel its own focus move.+ ctx.memory_mut(|m| m.move_focus(egui::FocusDirection::None));+ }+ step+}++/// Start collecting the fields of a dialog body.+pub fn begin(ctx: &Context) {+ ctx.data_mut(|d| {+ d.insert_temp(collecting_id(), true);+ d.insert_temp(list_id(), Vec::<Id>::new());+ });+}++/// A text or number field, in layout order. Outside a dialog body this does nothing.+pub fn register(ctx: &Context, id: Id) {+ ctx.data_mut(|d| {+ if d.get_temp::<bool>(collecting_id()).unwrap_or(false) {+ d.get_temp_mut_or_default::<Vec<Id>>(list_id()).push(id);+ }+ });+}++/// End of the body: move focus by `step` through the registered fields, wrapping around (from+/// nothing focused, Tab starts at the first and ⇧Tab at the last). Returns the field focused.+pub fn end(ctx: &Context, step: i32) -> Option<Id> {+ let fields: Vec<Id> = ctx.data_mut(|d| {+ d.insert_temp(collecting_id(), false);+ d.get_temp(list_id()).unwrap_or_default()+ });+ if step == 0 || fields.is_empty() {+ return None;+ }+ let n = fields.len() as i32;+ let at = ctx.memory(|m| m.focused()).and_then(|f| fields.iter().position(|&id| id == f)).map(|i| i as i32);+ let next = match at {+ Some(i) => (i + step).rem_euclid(n),+ None if step > 0 => 0,+ None => n - 1,+ };+ let id = fields.get(next as usize).copied()?;+ focus(ctx, id);+ Some(id)+}++/// Select all of a text field's text (so typing replaces it), as Photoshop does when a field is+/// reached with Tab or stepped with the arrow keys.+pub fn select_all(ctx: &Context, id: Id, text: &str) {+ let mut state = egui::TextEdit::load_state(ctx, id).unwrap_or_default();+ state.cursor.set_char_range(Some(egui::text::CCursorRange::two(egui::text::CCursor::default(), egui::text::CCursor::new(text.chars().count()))));+ state.store(ctx, id);+}++#[cfg(test)]+mod tests {+ use egui::accesskit::Role;+ use egui_kittest::{Harness, kittest::Queryable};+ use serde_json::{Value, json};++ use crate::PhotocraftApp;+ use crate::state::{DialogKind, UiState};++ fn harness(app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {+ let 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]);+ h+ }++ fn fields(h: &Harness<'static, PhotocraftApp>) -> serde_json::Map<String, Value> {+ h.state().ui.dialogs.first().map(|d| d.fields.clone()).expect("the dialog is open")+ }++ fn num(h: &Harness<'static, PhotocraftApp>, key: &str) -> f64 {+ fields(h).get(key).and_then(Value::as_f64).unwrap_or(f64::NAN)+ }++ fn tab(h: &mut Harness<'static, PhotocraftApp>) {+ h.key_press(egui::Key::Tab);+ h.run_steps(2);+ }++ fn arrow(h: &mut Harness<'static, PhotocraftApp>, key: egui::Key, m: egui::Modifiers) {+ h.key_press_modifiers(m, key);+ h.run_steps(2);+ }++ fn type_text(h: &mut Harness<'static, PhotocraftApp>, text: &str) {+ for c in text.chars() {+ h.event(egui::Event::Text(c.to_string()));+ h.run_steps(1);+ }+ }++ /// Hue/Saturation: Tab goes Hue → Saturation → Lightness (never the dropdown or the+ /// sliders), ⇧Tab back, and ↑ / ↓ step the focused field by 1 (⇧: 10).+ #[test]+ fn hue_saturation_tabs_through_its_fields_and_arrows_step_them() {+ let mut s = photocraft_engine::Session::new();+ s.execute("file.new", json!({"width": 64, "height": 48})).unwrap();+ let mut h = harness(PhotocraftApp::new(s, crate::Services::default()));+ crate::adjust_dialog::open(h.state_mut(), "image.adjustments.hueSaturation").unwrap();+ h.run_steps(3);+ assert_eq!(h.ctx.memory(|m| m.focused()), None, "nothing is focused to begin with");+ tab(&mut h);+ assert!(h.query_all_by_role(Role::SpinButton).next().is_some_and(|n| n.is_focused()), "the first Tab lands on Hue");+ arrow(&mut h, egui::Key::ArrowUp, egui::Modifiers::NONE);+ assert_eq!(num(&h, "hue"), 1.0);+ arrow(&mut h, egui::Key::ArrowUp, egui::Modifiers::SHIFT);+ assert_eq!(num(&h, "hue"), 11.0);+ arrow(&mut h, egui::Key::ArrowDown, egui::Modifiers::NONE);+ assert_eq!(num(&h, "hue"), 10.0);+ // The text stays selected: typing replaces it.+ type_text(&mut h, "-5");+ assert_eq!(num(&h, "hue"), -5.0);+ tab(&mut h);+ arrow(&mut h, egui::Key::ArrowUp, egui::Modifiers::NONE);+ assert_eq!((num(&h, "hue"), num(&h, "saturation")), (-5.0, 1.0), "the second Tab is Saturation");+ tab(&mut h);+ arrow(&mut h, egui::Key::ArrowDown, egui::Modifiers::SHIFT);+ assert_eq!(num(&h, "lightness"), -10.0, "the third is Lightness");+ tab(&mut h);+ arrow(&mut h, egui::Key::ArrowUp, egui::Modifiers::NONE);+ assert_eq!(num(&h, "hue"), -4.0, "Tab wraps around to Hue");+ h.key_press_modifiers(egui::Modifiers::SHIFT, egui::Key::Tab);+ h.run_steps(2);+ arrow(&mut h, egui::Key::ArrowUp, egui::Modifiers::NONE);+ assert_eq!(num(&h, "lightness"), -9.0, "⇧Tab goes back to Lightness");+ // The range clamps.+ for _ in 0..12 {+ arrow(&mut h, egui::Key::ArrowDown, egui::Modifiers::SHIFT);+ }+ assert_eq!(num(&h, "lightness"), -100.0);+ assert!(h.state().ui.dialogs.len() == 1, "Tab never closed the dialog");+ }++ /// New Document opens with the name selected; Tab goes Width → Height → Resolution and wraps.+ #[test]+ fn new_document_opens_on_the_name_and_tabs_to_width_then_height() {+ let mut h = harness(PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()));+ h.state_mut().ui.open_dialog(DialogKind::NewDocument, UiState::new_document_fields());+ h.run_steps(3);+ assert!(h.get_by_role(Role::TextInput).is_focused(), "the name field has focus");+ type_text(&mut h, "Poster");+ assert_eq!(fields(&h)["name"], "Poster", "typing replaced the selected name");+ tab(&mut h);+ type_text(&mut h, "512");+ tab(&mut h);+ type_text(&mut h, "300");+ tab(&mut h);+ type_text(&mut h, "150");+ let f = fields(&h);+ assert_eq!((f["width"].as_u64(), f["height"].as_u64(), f["resolution"].as_f64()), (Some(512), Some(300), Some(150.0)), "{f:?}");+ // Arrows step a size field too.+ h.key_press_modifiers(egui::Modifiers::SHIFT, egui::Key::Tab);+ h.run_steps(2);+ arrow(&mut h, egui::Key::ArrowUp, egui::Modifiers::SHIFT);+ assert_eq!(fields(&h)["height"].as_u64(), Some(310));+ // Tab from the last field wraps to the name.+ tab(&mut h);+ tab(&mut h);+ assert!(h.get_by_role(Role::TextInput).is_focused(), "Tab wrapped to the name");+ type_text(&mut h, "Flyer");+ assert_eq!(fields(&h)["name"], "Flyer");+ h.key_press(egui::Key::Enter);+ h.run_steps(3);+ let d = &h.state().session.active().expect("a new document").doc;+ assert_eq!((d.name.as_str(), d.size.width, d.size.height), ("Flyer", 512, 310));+ }++ /// Tab is the dialog's: the modal underneath never sees it, and a dialog with no fields swallows it.+ #[test]+ fn tab_in_a_dialog_without_fields_does_nothing() {+ let mut h = harness(PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()));+ h.state_mut().ui.open_dialog(DialogKind::About, Default::default());+ h.run_steps(3);+ tab(&mut h);+ assert_eq!(h.ctx.memory(|m| m.focused()), None);+ assert_eq!(h.state().ui.dialogs.len(), 1);+ }+}diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 2395cf636..036fc0b68 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -63,6 +63,7 @@ pub mod enable_rules;pub mod eraser_ui;pub mod export_dialog;pub mod eyedropper_ui;+pub mod field_tab;pub mod file_dialog;pub mod file_open;pub mod file_ui;diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rsindex f43c1e50c..87ca90063 100644--- a/crates/ui-egui/src/new_doc_ui.rs+++ b/crates/ui-egui/src/new_doc_ui.rs@@ -422,9 +422,20 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strinui.label(RichText::new(tl!("PRESET DETAILS")).size(11.0).color(t.text_faint));ui.add_space(4.0);let mut name = get_s(f, "name", tl!("Untitled-1"));- if ui.add(egui::TextEdit::singleline(&mut name).desired_width(250.0).font(egui::FontId::proportional(15.0))).changed() {+ let r = ui.add(egui::TextEdit::singleline(&mut name).desired_width(250.0).font(egui::FontId::proportional(15.0)));+ if r.changed() {f.insert("name".into(), json!(name));}+ crate::field_tab::register(ui.ctx(), r.id);+ // Photoshop opens the dialog with the name selected, so typing replaces it and Tab goes+ // straight on to Width. (`__focused` marks that this instance has had its turn.)+ if !f.contains_key("__focused") {+ f.insert("__focused".into(), json!(true));+ crate::field_tab::focus(ui.ctx(), r.id);+ }+ if r.gained_focus() {+ crate::field_tab::select_all(ui.ctx(), r.id, &name);+ }ui.add_space(8.0);let ppi = get_f(f, "resolution", 72.0);let mut unit = get_s(f, "__unit", "px");diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rsindex 1600ae10a..0a496d2a0 100644--- a/crates/ui-egui/src/widgets.rs+++ b/crates/ui-egui/src/widgets.rs@@ -259,6 +259,8 @@ fn value_field_in(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<// `f32::clamp` panics on a reversed or NaN range, which `DragValue::range` accepts.*value = if lo <= hi { v.clamp(lo, hi) } else { v };ui.memory_mut(|m| m.request_focus(resp.id));+ // The text stays selected, as in Photoshop: typing after a step replaces the value.+ crate::field_tab::select_all(ui.ctx(), resp.id, &if fine { fmt_num2(f64::from(*value)) } else { fmt_num(f64::from(*value)) });resp.mark_changed();}(resp, rect)@@ -386,6 +388,8 @@ fn number_edit(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<f32if ui.data_mut(|d| d.remove_temp::<bool>(first_field_id())).unwrap_or(false) {ui.memory_mut(|m| m.request_focus(id));}+ // Tab in a dialog goes from one of these to the next (field_tab.rs).+ crate::field_tab::register(&ctx, id);let held = id.with("arithmetic");let math = ui.memory(|m| m.has_focus(id)) && ui.data(|d| d.get_temp(held)).unwrap_or(false);ui.data_mut(|d| d.insert_temp(held, math));