Drive Type increment regressions through the full eframe input route

This commit is contained in:
Terra854 committed 2026-12-02 22:12:42 +08:00
1 parent d890c73b4a
commit e8fa3a331f
1 file changed
+41 -18
@@ -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<PhotocraftApp>, LayerId) {
(app, id)
}
type H = Harness<'static, Box<PhotocraftApp>>;
/// 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<CharStyle> {
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));
}
}