diff --git a/crates/ui-egui/src/panels/tabs.rs b/crates/ui-egui/src/panels/tabs.rs index a09c634967..a927c5a6bf 100644 --- a/crates/ui-egui/src/panels/tabs.rs +++ b/crates/ui-egui/src/panels/tabs.rs @@ -1,4 +1,5 @@ -//! Tabs panel: tab-stop alignment, X position (Y in vertical type), leader and "align on", and a +//! Tabs panel: tab-stop alignment, X position (Y in vertical type; a position typed there for a +//! paragraph with no stops adds a stop at it), leader and "align on", and a //! ruler numbered in the General unit on which a click adds a stop, dragging moves one and dragging //! it off the ruler deletes it. Ctrl-dragging a stop (Command on macOS) moves all the stops by the //! same distance. A stop placed or dragged on the ruler lands on the nearest whole point. With Snap @@ -462,6 +463,32 @@ fn apply_moved(app: &mut VectorcraftApp, ctx: &egui::Context, stops: &mut Vec, position: f64, align: TabAlign) -> Option { + let mut added = stops.clone(); + if !added.iter().any(|s| s.position == position) { + let max = TabStop::MAX_STOPS; + if added.len() >= max { + app.status(format!("at most {max} tab stops")); + return None; + } + added.push(TabStop { position, align, leader: String::new(), align_on: '.' }); + added.sort_by(|a, b| a.position.total_cmp(&b.position)); + if !apply(app, &added) { + return None; + } + } + let i = added.iter().position(|s| s.position == position).unwrap_or(0); + *stops = added; + set_pstate::>(ctx, "tabs-marker-sel", None); + set_pstate(ctx, "tabs-sel", i); + Some(i) +} + /// `stops` with every stop moved by the distance that takes stop `i` to `position`, or by less /// when that would put a stop below 0 or past [`MAX_STOP`], so the leftmost stop ends at 0 or the /// rightmost at [`MAX_STOP`] and the stops keep their spacing. The stops keep their order, @@ -622,6 +649,11 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { if let Some(v) = widgets::spin_field(ui, "tabs-x", x, app.session.general_unit(), 90.0, 1.0, 0.0, &[]) { if let Some(marker) = selected_marker { set_marker(app, marker, v, ¶, span, &targets, false); + } else if stops.is_empty() { + // With no stops, the typed position adds the first one, as a click on the ruler does. + if let Some(i) = add_stop(app, ui.ctx(), &mut stops, v.max(0.0), ALIGNS[align_idx.min(3)].0) { + sel = i; + } } else if let Some(i) = apply_moved(app, ui.ctx(), &mut stops, sel, v.max(0.0)) { sel = i; } @@ -800,27 +832,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { set_pstate(ui.ctx(), "tabs-sel", i); } None => { - let position = place(pos.x); // A click that lands on a stop's whole point or tick selects that stop and records no - // undo step. - let added = !stops.iter().any(|s| s.position == position); - let max = TabStop::MAX_STOPS; - if added && stops.len() >= max { - // A full list takes no more stops: the click leaves the stops and the - // selection as they are and shows the limit. - app.status(format!("at most {max} tab stops")); - } else { - if added { - stops.push(TabStop { position, align: ALIGNS[align_idx.min(3)].0, leader: String::new(), align_on: '.' }); - stops.sort_by(|a, b| a.position.total_cmp(&b.position)); - } - let i = stops.iter().position(|s| s.position == position).unwrap_or(0); - set_pstate::>(ui.ctx(), "tabs-marker-sel", None); - set_pstate(ui.ctx(), "tabs-sel", i); - if added { - apply(app, &stops); - } - } + // undo step. A full list takes no more stops: the click leaves the stops and the + // selection as they are and shows the limit. + add_stop(app, ui.ctx(), &mut stops, place(pos.x), ALIGNS[align_idx.min(3)].0); } } } diff --git a/crates/ui-egui/src/panels/tabs/tests.rs b/crates/ui-egui/src/panels/tabs/tests.rs index 37d2a4d93d..6edba53c3a 100644 --- a/crates/ui-egui/src/panels/tabs/tests.rs +++ b/crates/ui-egui/src/panels/tabs/tests.rs @@ -1577,6 +1577,116 @@ fn a_refused_x_leaves_the_ruler_as_it_was() { assert_eq!(committed, before); } +/// Click the position field to the right of the label `label` ("X:" or "Y:"), which is blank +/// while the paragraph has no stops, type `typed` and press Enter → the texts painted next. +fn type_into_blank_position(ctx: &egui::Context, app: &mut VectorcraftApp, label: &str, typed: &str) -> Vec<(String, egui::Rect)> { + let texts = panel_frame(ctx, app, vec![]); + let rect_of = |name: &str| texts.iter().find(|(t, _)| t == name).unwrap_or_else(|| panic!("{name} in {texts:?}")).1; + // The fields' column starts after the widest label, "Align On:". + let at = egui::pos2(rect_of("Align On:").right() + 6.0 + 45.0, rect_of(label).center().y); + let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }; + let key = |key, modifiers| egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }; + panel_frame(ctx, app, vec![egui::Event::PointerMoved(at), button(true)]); + panel_frame(ctx, app, vec![button(false)]); + panel_frame(ctx, app, vec![key(egui::Key::A, egui::Modifiers::COMMAND)]); + panel_frame(ctx, app, vec![egui::Event::Text(typed.into())]); + panel_frame(ctx, app, vec![key(egui::Key::Enter, egui::Modifiers::NONE)]); + panel_frame(ctx, app, vec![]) +} + +/// A position typed in the X field of a paragraph with no stops adds a stop there with the chosen +/// alignment, keeps its hundredths of a point, selects it, and is one undo step. +#[test] +fn typing_x_into_a_paragraph_without_stops_adds_and_selects_a_stop() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({})).unwrap(); + app.run("text.create", json!({"x": 10, "y": 20, "text": "a\tb", "area": {"width": 300, "height": 100}})).unwrap(); + assert!(current(&app).unwrap().1.is_empty()); + let ctx = egui::Context::default(); + set_pstate(&ctx, "tabs-align", 2usize); + let before = app.session.active().unwrap().history.undo.len(); + let texts = type_into_blank_position(&ctx, &mut app, "X:", "36.47"); + let stops = current(&app).unwrap().1; + assert_eq!(stops.iter().map(|s| (s.position, s.align)).collect::>(), [(36.47, TabAlign::Right)]); + assert_eq!(pstate::(&ctx, "tabs-sel"), 0); + assert!(texts.iter().any(|(t, _)| t == "36.47 pt"), "36.47 pt in {texts:?}"); + assert_eq!(app.session.active().unwrap().history.undo.len(), before + 1); + app.run("edit.undo", json!({})).unwrap(); + assert!(current(&app).unwrap().1.is_empty()); +} + +/// In vertical type the position field is Y, and a position typed there for a paragraph with no +/// stops adds a stop. +#[test] +fn typing_y_into_vertical_type_without_stops_adds_a_stop() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({})).unwrap(); + app.run("text.create", json!({"x": 10, "y": 20, "text": "a\tb", "vertical": true})).unwrap(); + let ctx = egui::Context::default(); + type_into_blank_position(&ctx, &mut app, "Y:", "48"); + let stops = current(&app).unwrap().1; + assert_eq!(stops.iter().map(|s| (s.position, s.align)).collect::>(), [(48.0, TabAlign::Left)]); +} + +/// A first stop typed with a unit other than the document's lands at that distance in points, and +/// one typed without a unit is in the document's unit. +#[test] +fn a_first_stop_typed_in_another_unit_keeps_its_point_position() { + for typed in ["1 in", "25.4"] { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({})).unwrap(); + app.run("document.setUnits", json!({"units": "mm"})).unwrap(); + app.run("text.create", json!({"x": 10, "y": 20, "text": "a\tb", "area": {"width": 300, "height": 100}})).unwrap(); + let ctx = egui::Context::default(); + type_into_blank_position(&ctx, &mut app, "X:", typed); + let positions = current(&app).unwrap().1.iter().map(|s| s.position).collect::>(); + assert_eq!(positions.len(), 1, "{typed}: {positions:?}"); + assert!((positions[0] - 72.0).abs() < 1e-9, "{typed}: {positions:?}"); + } +} + +/// A first stop past 100,000 pt or text that is not a number adds no stop and records no undo +/// step. A negative position adds the stop at 0 pt. +#[test] +fn a_refused_first_stop_adds_nothing_and_records_no_undo() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({})).unwrap(); + app.run("text.create", json!({"x": 10, "y": 20, "text": "a\tb", "area": {"width": 300, "height": 100}})).unwrap(); + let ctx = egui::Context::default(); + let steps = |app: &VectorcraftApp| app.session.active().unwrap().history.undo.len(); + let before = steps(&app); + for typed in ["200000", "abc"] { + type_into_blank_position(&ctx, &mut app, "X:", typed); + assert!(current(&app).unwrap().1.is_empty(), "{typed}"); + assert_eq!(steps(&app), before, "{typed}"); + } + type_into_blank_position(&ctx, &mut app, "X:", "-5"); + assert_eq!(current(&app).unwrap().1.iter().map(|s| s.position).collect::>(), [0.0]); + assert_eq!(steps(&app), before + 1); +} + +/// With the Selection tool on two texts, a first stop typed for the shown text, which has no +/// stops, becomes every selected text's stops in one undo step, replacing the other text's stop. +#[test] +fn typing_the_first_stop_over_mixed_paragraphs_uses_the_displayed_list() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({})).unwrap(); + let id = |v: serde_json::Value| v["id"].as_u64().unwrap(); + let a = id(app.run("text.create", json!({"x": 10, "y": 20, "text": "a\tb", "area": {"width": 300, "height": 60}})).unwrap()); + let b = id(app.run("text.create", json!({"x": 10, "y": 100, "text": "c\td", "area": {"width": 250, "height": 60}})).unwrap()); + app.run("select.set", json!({"ids": [b]})).unwrap(); + app.run("text.tabs.set", json!({"stops": [{"position": 90}]})).unwrap(); + app.run("select.set", json!({"ids": [a, b]})).unwrap(); + assert_eq!(current(&app).unwrap().0.id.0, a); + let ctx = egui::Context::default(); + let before = app.session.active().unwrap().history.undo.len(); + type_into_blank_position(&ctx, &mut app, "X:", "25"); + for text in [a, b] { + assert_eq!(text_of(&app, text).para_at(0).tabs.iter().map(|s| s.position).collect::>(), [25.0], "text {text}"); + } + assert_eq!(app.session.active().unwrap().history.undo.len(), before + 1); +} + /// One frame of the panel menu with `events` → the rows' texts and where they are painted. fn menu_frame(ctx: &egui::Context, app: &mut VectorcraftApp, events: Vec) -> Vec<(String, egui::Rect)> { let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(320.0, 400.0)); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 57ac87a396..f43060eb9f 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1829,7 +1829,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur The Character panel menu's Character Direction (with the Indic options; `text.setFormat {charDirection}`, `text.setRangeStyle {charDirection}`) sets the selected characters left to right or right to left inside their paragraph, as a Unicode bidi embedding: a date set Left to Right keeps its order in right-to-left text, and a word set Right to Left keeps the punctuation after it on its left. Default (the default) leaves the characters to the bidi algorithm; character styles and the native file keep the setting. In vertical type, the Tabs panel names its alignment buttons Top-Justified Tab, Center-Justified Tab, Bottom-Justified Tab and Decimal-Justified Tab, in their tool tips and for screen readers, and labels the position field Y. In horizontal type the buttons keep the names Left-Justified Tab and Right-Justified Tab, and the field keeps the label X. The Tabs panel's ruler is numbered in the General unit with the canvas rulers' label steps and a tick every tenth of a step. With the panel menu's Snap to Unit on, or Shift held, a tab stop placed by a click on the ruler or dragged on it lands on the nearest tick (`text.tabs.set`). - With Snap to Unit off and Shift not held, a tab stop placed by a click on the Tabs panel's ruler or dragged on it lands on the nearest whole point. A position typed in the X or Y field keeps its hundredths of a point. + With Snap to Unit off and Shift not held, a tab stop placed by a click on the Tabs panel's ruler or dragged on it lands on the nearest whole point. A position typed in the X or Y field keeps its hundredths of a point. When the shown paragraph has no tab stops and no indent marker is selected, a position typed in the X or Y field and committed with Enter adds a stop there with the chosen alignment and selects it, in one undo step, as a click on the ruler does; the [archived user guide](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=521) gives typing a position as another way to set a new stop. A negative position becomes 0 pt, and a position past 100,000 pt adds nothing (VectorCraft's own limits). Tests `panels::tabs::tests::typing_x_into_a_paragraph_without_stops_adds_and_selects_a_stop`, `typing_y_into_vertical_type_without_stops_adds_a_stop`, `a_first_stop_typed_in_another_unit_keeps_its_point_position`, `a_refused_first_stop_adds_nothing_and_records_no_undo` and `typing_the_first_stop_over_mixed_paragraphs_uses_the_displayed_list`. The Tabs ruler's upper first-line marker and lower left-indent marker show absolute positions and set paragraph indents (`text.setFormat`). Dragging the lower marker keeps the upper position, while Ctrl-dragging it (Command on macOS) moves both; marker drags snap like tab stops, and each release is one undo step. With differing left indents, only the upper marker is shown and its absolute target applies to every touched paragraph. When a drag on the Tabs panel's ruler or a value typed in its X field moves a tab stop past another stop, the moved stop stays selected. With the Selection tool, the Tabs panel shows the ruler and stops of the first paragraph of the first selected text object, also when the text is inside a selected group or a shape is selected before it; a selection without text shows "No text selected". A stop set on that ruler is set on every selected text object and the text in selected groups, and other objects are left as they were. This is VectorCraft's own rule. Tests `panels::tabs::tests::the_ruler_shows_text_selected_after_an_object_that_is_not_text`, `the_ruler_shows_text_inside_a_selected_group`, `a_stop_added_on_a_mixed_selection_sets_every_selected_text` and `a_selection_without_text_shows_no_ruler`.