From d742a9513819c3327466599d6dd95b75a2637c66 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Wed, 2 Dec 2026 20:54:45 +0800 Subject: [PATCH 1/3] Use one-point Type leading and baseline shortcut steps --- crates/ui-egui/src/tools/type_tool/host.rs | 50 +++++++- .../type_tool/leading_baseline_step_tests.rs | 120 ++++++++++++++++++ 2 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs diff --git a/crates/ui-egui/src/tools/type_tool/host.rs b/crates/ui-egui/src/tools/type_tool/host.rs index fde60ed8ab9..efd8d0c75ba 100644 --- a/crates/ui-egui/src/tools/type_tool/host.rs +++ b/crates/ui-egui/src/tools/type_tool/host.rs @@ -7,9 +7,9 @@ use craft::text::ui::editor::{CaretAffinity, CaretLine, EditKind, HostLine, Host use photocraft_doc::LayerId; use photocraft_text::warp::{Placement, Warp}; -use super::editing::{kern_pair, step_char_metric, toggle_char_style, toggle_hyphenate}; +use super::editing::{kern_pair, selected_styles, step_char_metric, toggle_char_style, toggle_hyphenate}; use super::layout::{doc_point, hit_at, is_vertical, layout, path_warp}; -use super::{Affine, Arc, PhotocraftApp, TextLayout, byte_of, insertion_style, text_layer}; +use super::{Affine, Arc, PhotocraftApp, TextLayer, TextLayout, byte_of, byte_range, insertion_style, text_layer}; /// A type layer's text, layout and formatting for one editing session. Geometry is in document units. pub(crate) struct LayerHost<'a> { @@ -23,6 +23,13 @@ impl<'a> LayerHost<'a> { Self { app, layer } } + /// PhotoCraft's measured horizontal selection shortcuts take 1/5 pt, rather than the shared editor's 2/10. + /// Keep its existing behavior for mixed styles, vertical text and the empty-text typing style until measured. + fn point_metric_step(&self, selection: TextSelection, by: f32) -> f32 { + let Some(text) = self.app.session.active().and_then(|st| text_layer(&st.doc, self.layer)) else { return by }; + if is_vertical(self.app, self.layer) || !uniform_selection(text, selection.range()) { by } else { by * 0.5 } + } + fn laid_out(&mut self) -> Option { let (l, aff, text) = layout(self.app, self.layer)?; let warp = path_warp(self.app, self.layer, &l); @@ -30,6 +37,31 @@ impl<'a> LayerHost<'a> { } } +/// Only the selected runs decide whether values are mixed; unrelated text outside the selection does not. +fn uniform_selection(text: &TextLayer, range: Range) -> bool { + if range.is_empty() { + return false; + } + let styles = selected_styles(text, range.start, range.end); + if styles.first().is_none_or(|first| styles.iter().any(|style| style != first)) { + return false; + } + let (a, b) = byte_range(&text.text, range.start, range.end); + let mut at = 0usize; + let mut first = None; + for run in text.paragraph_runs() { + let end = at.saturating_add(run.len); + if at < b && end > a { + if first.as_ref().is_some_and(|style| style != &run.style) { + return false; + } + first = Some(run.style); + } + at = end; + } + first.is_some() +} + /// The layer's layout with what maps its line space to the document. struct Laid { l: Arc, @@ -238,8 +270,14 @@ impl TextEditorHost for LayerHost<'_> { match key { StyleKey::Toggle(which) => toggle_char_style(self.app, which), StyleKey::Hyphenate => toggle_hyphenate(self.app), - StyleKey::Leading(v) => step_char_metric(self.app, "leadingBy", v), - StyleKey::BaselineShift(v) => step_char_metric(self.app, "baselineShiftBy", v), + StyleKey::Leading(v) => { + let by = self.point_metric_step(selection, v); + step_char_metric(self.app, "leadingBy", by); + } + StyleKey::BaselineShift(v) => { + let by = self.point_metric_step(selection, v); + step_char_metric(self.app, "baselineShiftBy", by); + } StyleKey::Tracking(v) => step_char_metric(self.app, "trackingBy", v), StyleKey::Kern(v) => kern_pair(self.app, self.layer, selection.caret, v), _ => return false, @@ -258,3 +296,7 @@ pub(crate) fn type_text(app: &mut PhotocraftApp, s: &str) { } LayerHost::new(app, layer).replace(range, s, EditKind::Typed); } + +#[cfg(test)] +#[path = "leading_baseline_step_tests.rs"] +mod leading_baseline_step_tests; diff --git a/crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs b/crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs new file mode 100644 index 00000000000..a81d2a46882 --- /dev/null +++ b/crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs @@ -0,0 +1,120 @@ +//! #17902: the real native Type editor chords take 1/5 pt for leading and baseline, retaining 20/100 tracking/kern. +use craft::text::ui::editor::{StyleKey, TextEditorHost, TextSelection}; +use egui::{Key, Modifiers}; +use photocraft_doc::LayerId; +use photocraft_doc::text::CharStyle; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::testing; +use crate::tools::type_tool::{self, text_layer}; + +const TEXT: &str = "ABCDEFGH\nIJKLMNOP"; + +fn editing(vertical: bool, auto: f32) -> (Box, LayerId) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 640, "height": 360})).unwrap(); + let made = app + .run( + "type.create", + json!({"text": TEXT, "x": 50, "y": 100, "size": 48, + "orientation": if vertical { "vertical" } else { "horizontal" }}), + ) + .unwrap(); + let id = LayerId(made["layer"].as_u64().unwrap()); + app.run("type.setStyle", json!({"layer": id.0, "autoLeading": auto})).unwrap(); + app.sync_views(); + type_tool::commit_tests::edit_active(&mut app); + (app, id) +} + +fn styles(app: &PhotocraftApp, id: LayerId) -> Vec { + text_layer(&app.session.active().unwrap().doc, id).unwrap().char_runs().into_iter().map(|r| r.style).collect() +} + +fn close(actual: f32, expected: f32) { + assert!((actual - expected).abs() < 0.0001, "{actual} != {expected}"); +} + +#[test] +fn native_leading_chords_start_from_effective_auto_and_take_one_or_five_points() { + // A non-default paragraph factor catches implementations that hard-code 120% or the initial 57.6 pt. + for auto in [120.0, 150.0] { + for (key, far, delta) in [(Key::ArrowUp, false, -1.0), (Key::ArrowDown, false, 1.0), (Key::ArrowUp, true, -5.0), (Key::ArrowDown, true, 5.0)] { + let (mut app, id) = editing(false, auto); + let t = text_layer(&app.session.active().unwrap().doc, id).unwrap(); + let effective = t.char_runs()[0].style.size_pt * t.paragraph_runs()[0].style.auto_leading; + assert!(styles(&app, id).iter().all(|s| s.leading_pt.is_none())); + let mods = Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }; + testing::key(&mut app, key, mods); + for style in styles(&app, id) { + close(style.leading_pt.expect("the key switches auto leading off"), effective + delta); + assert_eq!((style.tracking, style.baseline_shift_pt), (0.0, 0.0)); + } + assert_eq!(app.ui.text_edit.as_ref().unwrap().caret.max(app.ui.text_edit.as_ref().unwrap().anchor), TEXT.chars().count()); + } + } +} + +#[test] +fn native_baseline_chords_raise_and_lower_by_one_or_five_points() { + let (mut app, id) = editing(false, 120.0); + for (key, far, expected) in [(Key::ArrowUp, false, 1.0), (Key::ArrowDown, false, 0.0), (Key::ArrowUp, true, 5.0), (Key::ArrowDown, true, 0.0)] { + testing::key(&mut app, key, Modifiers::ALT | Modifiers::SHIFT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); + for style in styles(&app, id) { + close(style.baseline_shift_pt, expected); + assert!(style.leading_pt.is_none()); + } + } +} + +#[test] +fn native_tracking_and_caret_kerning_keep_twenty_and_one_hundred() { + let (mut app, id) = editing(false, 120.0); + for (key, far, expected) in [(Key::ArrowRight, false, 20.0), (Key::ArrowLeft, false, 0.0), (Key::ArrowRight, true, 100.0), (Key::ArrowLeft, true, 0.0)] { + testing::key(&mut app, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); + assert!(styles(&app, id).iter().all(|style| style.tracking == expected)); + } + let ed = app.ui.text_edit.as_mut().unwrap(); + ed.caret = 1; + ed.anchor = 1; + testing::frame(&mut app); + for (key, far, expected) in [(Key::ArrowRight, false, 20.0), (Key::ArrowLeft, false, 0.0), (Key::ArrowRight, true, 100.0), (Key::ArrowLeft, true, 0.0)] { + testing::key(&mut app, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); + close(styles(&app, id)[0].kern, expected); + assert!(styles(&app, id).iter().all(|style| style.tracking == 0.0)); + } +} + +#[test] +fn host_keeps_unmeasured_vertical_and_mixed_selection_steps() { + let selection = TextSelection::new(0, TEXT.chars().count()); + let (mut app, id) = editing(true, 120.0); + let effective = 48.0 * 1.2; + assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::Leading(2.0), selection)); + assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::BaselineShift(10.0), selection)); + for style in styles(&app, id) { + close(style.leading_pt.unwrap(), effective + 2.0); + close(style.baseline_shift_pt, 10.0); + } + + let (mut app, id) = editing(false, 120.0); + app.run("type.setStyle", json!({"layer": id.0, "range": [0, 3], "leading": 60, "baselineShift": 7})).unwrap(); + assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::Leading(2.0), selection)); + assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::BaselineShift(10.0), selection)); + let runs = styles(&app, id); + assert_eq!((runs[0].leading_pt, runs[0].baseline_shift_pt), (Some(62.0), 17.0)); + close(runs.last().unwrap().leading_pt.unwrap(), effective + 2.0); + close(runs.last().unwrap().baseline_shift_pt, 10.0); +} + +#[test] +fn only_runs_within_the_selection_decide_whether_the_metric_step_is_mixed() { + let (mut app, id) = editing(false, 120.0); + app.run("type.setStyle", json!({"layer": id.0, "range": [9, 17], "leading": 80, "autoLeading": 150})).unwrap(); + let first_line = TextSelection::new(0, 8); + assert!(super::LayerHost::new(&mut app, id).style_key(StyleKey::Leading(2.0), first_line)); + let runs = styles(&app, id); + close(runs[0].leading_pt.unwrap(), 48.0 * 1.2 + 1.0); + assert_eq!(runs.last().unwrap().leading_pt, Some(80.0)); +} From d890c73b4ad44a17b39dfb7ecc51f6c8a98605ba Mon Sep 17 00:00:00 2001 From: Terra854 Date: Wed, 2 Dec 2026 21:28:13 +0800 Subject: [PATCH 2/3] Update existing Type shortcut assertions for one-point steps --- crates/ui-egui/src/input_tests.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/input_tests.rs b/crates/ui-egui/src/input_tests.rs index 09bca121d5f..5c3ca3bc9ba 100644 --- a/crates/ui-egui/src/input_tests.rs +++ b/crates/ui-egui/src/input_tests.rs @@ -519,7 +519,7 @@ fn global_cmd_shift_keys_still_work_outside_type_editing() { assert!(!char_style(&h, id, 0).faux_italic && char_style(&h, id, 0).caps == photocraft_doc::text::Caps::Normal); } -/// ⌥↑/↓ leading (2 pt, 10 with ⌘), ⇧⌥↑/↓ baseline shift and ⌥←/→ tracking over a selection +/// ⌥↑/↓ leading (1 pt, 5 with ⌘), ⇧⌥↑/↓ baseline shift and ⌥←/→ tracking over a selection /// (20, 100 with ⌘): one step per press, from each character's own value, the selection kept. #[test] fn type_tool_format_keys_step_leading_baseline_and_tracking() { @@ -531,17 +531,17 @@ fn type_tool_format_keys_step_leading_baseline_and_tracking() { h.key_press_modifiers(m, k); h.run_steps(2); }; - // Auto leading is 120% of 10 pt: ⌥↓ makes it 14. + // Auto leading is 120% of 10 pt: ⌥↓ makes it 13. press(&mut h, Modifiers::ALT, Key::ArrowDown); - assert_eq!(char_style(&h, id, 0).leading_pt, Some(14.0)); + assert_eq!(char_style(&h, id, 0).leading_pt, Some(13.0)); press(&mut h, Modifiers::ALT, Key::ArrowUp); assert_eq!(char_style(&h, id, 4).leading_pt, Some(12.0)); press(&mut h, Modifiers::ALT | Modifiers::COMMAND, Key::ArrowDown); - assert_eq!(char_style(&h, id, 0).leading_pt, Some(22.0)); + assert_eq!(char_style(&h, id, 0).leading_pt, Some(17.0)); press(&mut h, Modifiers::ALT | Modifiers::SHIFT, Key::ArrowUp); - assert_eq!(char_style(&h, id, 0).baseline_shift_pt, 2.0); + assert_eq!(char_style(&h, id, 0).baseline_shift_pt, 1.0); press(&mut h, Modifiers::ALT | Modifiers::SHIFT | Modifiers::COMMAND, Key::ArrowDown); - assert_eq!(char_style(&h, id, 0).baseline_shift_pt, -8.0); + assert_eq!(char_style(&h, id, 0).baseline_shift_pt, -4.0); press(&mut h, Modifiers::ALT, Key::ArrowRight); assert_eq!(char_style(&h, id, 0).tracking, 20.0); press(&mut h, Modifiers::ALT | Modifiers::COMMAND, Key::ArrowLeft); From e8fa3a331fa8014fd53f25dd8bb1b24b6320892e Mon Sep 17 00:00:00 2001 From: Terra854 Date: Wed, 2 Dec 2026 22:12:42 +0800 Subject: [PATCH 3/3] Drive Type increment regressions through the full eframe input route --- .../type_tool/leading_baseline_step_tests.rs | 59 +++++++++++++------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs b/crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs index a81d2a46882..58da5953cf2 100644 --- a/crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs +++ b/crates/ui-egui/src/tools/type_tool/leading_baseline_step_tests.rs @@ -1,12 +1,12 @@ //! #17902: the real native Type editor chords take 1/5 pt for leading and baseline, retaining 20/100 tracking/kern. use craft::text::ui::editor::{StyleKey, TextEditorHost, TextSelection}; use egui::{Key, Modifiers}; +use egui_kittest::Harness; use photocraft_doc::LayerId; use photocraft_doc::text::CharStyle; use serde_json::json; use crate::PhotocraftApp; -use crate::canvas::testing; use crate::tools::type_tool::{self, text_layer}; const TEXT: &str = "ABCDEFGH\nIJKLMNOP"; @@ -28,6 +28,29 @@ fn editing(vertical: bool, auto: f32) -> (Box, LayerId) { (app, id) } +type H = Harness<'static, Box>; + +/// Match input_tests' working eframe route: the full app hook owns native key input and editor focus. +fn native_editing(auto: f32) -> (H, LayerId) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 640, "height": 360})).unwrap(); + app.sync_views(); + let ctx = app.ctx().clone(); + let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_context(ctx.clone()).with_max_steps(64).build_eframe(move |_cc| app); + h.run_steps(4); + let made = h.state_mut().run("type.create", json!({"text": TEXT, "x": 50, "y": 100, "size": 48})).unwrap(); + let id = LayerId(made["layer"].as_u64().unwrap()); + h.state_mut().run("type.setStyle", json!({"layer": id.0, "autoLeading": auto})).unwrap(); + type_tool::edit_active(h.state_mut(), &ctx).unwrap(); + h.run_steps(2); + (h, id) +} + +fn key(h: &mut H, key: Key, modifiers: Modifiers) { + h.key_press_modifiers(modifiers, key); + h.run_steps(2); +} + fn styles(app: &PhotocraftApp, id: LayerId) -> Vec { text_layer(&app.session.active().unwrap().doc, id).unwrap().char_runs().into_iter().map(|r| r.style).collect() } @@ -41,27 +64,27 @@ fn native_leading_chords_start_from_effective_auto_and_take_one_or_five_points() // A non-default paragraph factor catches implementations that hard-code 120% or the initial 57.6 pt. for auto in [120.0, 150.0] { for (key, far, delta) in [(Key::ArrowUp, false, -1.0), (Key::ArrowDown, false, 1.0), (Key::ArrowUp, true, -5.0), (Key::ArrowDown, true, 5.0)] { - let (mut app, id) = editing(false, auto); - let t = text_layer(&app.session.active().unwrap().doc, id).unwrap(); + let (mut h, id) = native_editing(auto); + let t = text_layer(&h.state().session.active().unwrap().doc, id).unwrap(); let effective = t.char_runs()[0].style.size_pt * t.paragraph_runs()[0].style.auto_leading; - assert!(styles(&app, id).iter().all(|s| s.leading_pt.is_none())); + assert!(styles(h.state(), id).iter().all(|s| s.leading_pt.is_none())); let mods = Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }; - testing::key(&mut app, key, mods); - for style in styles(&app, id) { + self::key(&mut h, key, mods); + for style in styles(h.state(), id) { close(style.leading_pt.expect("the key switches auto leading off"), effective + delta); assert_eq!((style.tracking, style.baseline_shift_pt), (0.0, 0.0)); } - assert_eq!(app.ui.text_edit.as_ref().unwrap().caret.max(app.ui.text_edit.as_ref().unwrap().anchor), TEXT.chars().count()); + assert_eq!(h.state().ui.text_edit.as_ref().unwrap().caret.max(h.state().ui.text_edit.as_ref().unwrap().anchor), TEXT.chars().count()); } } } #[test] fn native_baseline_chords_raise_and_lower_by_one_or_five_points() { - let (mut app, id) = editing(false, 120.0); + let (mut h, id) = native_editing(120.0); for (key, far, expected) in [(Key::ArrowUp, false, 1.0), (Key::ArrowDown, false, 0.0), (Key::ArrowUp, true, 5.0), (Key::ArrowDown, true, 0.0)] { - testing::key(&mut app, key, Modifiers::ALT | Modifiers::SHIFT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); - for style in styles(&app, id) { + self::key(&mut h, key, Modifiers::ALT | Modifiers::SHIFT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); + for style in styles(h.state(), id) { close(style.baseline_shift_pt, expected); assert!(style.leading_pt.is_none()); } @@ -70,19 +93,19 @@ fn native_baseline_chords_raise_and_lower_by_one_or_five_points() { #[test] fn native_tracking_and_caret_kerning_keep_twenty_and_one_hundred() { - let (mut app, id) = editing(false, 120.0); + let (mut h, id) = native_editing(120.0); for (key, far, expected) in [(Key::ArrowRight, false, 20.0), (Key::ArrowLeft, false, 0.0), (Key::ArrowRight, true, 100.0), (Key::ArrowLeft, true, 0.0)] { - testing::key(&mut app, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); - assert!(styles(&app, id).iter().all(|style| style.tracking == expected)); + self::key(&mut h, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); + assert!(styles(h.state(), id).iter().all(|style| style.tracking == expected)); } - let ed = app.ui.text_edit.as_mut().unwrap(); + let ed = h.state_mut().ui.text_edit.as_mut().unwrap(); ed.caret = 1; ed.anchor = 1; - testing::frame(&mut app); + h.run_steps(2); for (key, far, expected) in [(Key::ArrowRight, false, 20.0), (Key::ArrowLeft, false, 0.0), (Key::ArrowRight, true, 100.0), (Key::ArrowLeft, true, 0.0)] { - testing::key(&mut app, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); - close(styles(&app, id)[0].kern, expected); - assert!(styles(&app, id).iter().all(|style| style.tracking == 0.0)); + self::key(&mut h, key, Modifiers::ALT | if far { Modifiers::COMMAND } else { Modifiers::NONE }); + close(styles(h.state(), id)[0].kern, expected); + assert!(styles(h.state(), id).iter().all(|style| style.tracking == 0.0)); } }