Files
craft-libs/crates/draw/tests/it/stroke_editor.rs
T
dotCooCoo c34f5d15a0 draw: StrokeEditor::host_labels lines its rows up with the host's field group
A host that shows the stroke editor inside its own FieldGroup, next to
rows of its own, got two label columns. The editor put its Alignment,
Caps, Corners, Miter limit and Opacity rows in a FieldGroup of its own,
and the Dash offset row in another, so their controls started at the
editor's widest label instead of the host group's.

StrokeEditor::host_labels(true) shows those rows in a stack that fills
the width, which passes the host group's label column through. The
controls of the editor's rows, the app's choice rows and the Align
dashes row then start where the controls of the host's rows start. The
default is unchanged: without the option each group keeps its own label
column.

Test: host_labels_start_the_editor_rows_at_the_host_groups_control_edge
in crates/draw/tests/it/stroke_editor.rs.
2026-11-28 20:54:53 -08:00

155 lines
7.1 KiB
Rust

//! The stroke editor's dash fields: typing in the blank after the pattern adds an entry, clearing a later field ends
//! the pattern there, and the first field never clears. Its Caps row on a solid closed path: disabled by default,
//! enabled with `caps_on_closed`. Its labeled rows in a host's group: a label column of their own by default, the
//! host group's with `host_labels`.
use craft_draw::ui::stroke::{LineCap, StrokeEditor, StrokeSections, StrokeStyle};
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
use craft_ui::form::{Field, FieldGroup};
use craft_ui::testing::{Fixture, trial};
use craft_ui::text::NumberField;
use egui::{Key, Modifiers};
use std::cell::RefCell;
use std::rc::Rc;
/// The panel editor over a style with `dashes`, and the style it writes through to.
fn editor(dashes: Vec<f32>) -> (Fixture, Rc<RefCell<StrokeStyle>>) {
let style = Rc::new(RefCell::new(StrokeStyle { dashes, ..StrokeStyle::default() }));
let shown = Rc::clone(&style);
let mut fixture = Fixture::new(move |ui| {
let mut style = shown.borrow_mut();
let _ = StrokeEditor::new("Stroke", Bind::bound(&mut *style)).sections(StrokeSections::PANEL).show(ui);
});
fixture.settle();
(fixture, style)
}
/// Replaces the text of the field named `label` with `text` (empty clears it) and commits it with Enter.
fn type_into(fixture: &mut Fixture, label: &str, text: &str) {
let field = fixture.by_label(label).unwrap_or_else(|| panic!("the editor shows {label}"));
fixture.click(&field);
fixture.key(Key::A, Modifiers::COMMAND);
if text.is_empty() {
fixture.key(Key::Backspace, Modifiers::NONE);
} else {
fixture.type_text(text);
}
fixture.key(Key::Enter, Modifiers::NONE);
fixture.idle(3);
}
/// The panel editor over a solid closed path, with `caps_on_closed` as given, and the style it writes through to.
fn closed_editor(caps_on_closed: bool) -> (Fixture, Rc<RefCell<StrokeStyle>>) {
let style = Rc::new(RefCell::new(StrokeStyle::default()));
let shown = Rc::clone(&style);
let mut fixture = Fixture::new(move |ui| {
let mut style = shown.borrow_mut();
let _ = StrokeEditor::new("Stroke", Bind::bound(&mut *style))
.sections(StrokeSections::PANEL)
.closed(true)
.caps_on_closed(caps_on_closed)
.show(ui);
});
fixture.settle();
(fixture, style)
}
#[trial]
fn caps_on_closed_keeps_the_cap_buttons_enabled_on_a_solid_closed_path() {
let (mut fixture, style) = closed_editor(false);
let square = fixture.by_label("Square").expect("the editor shows Square");
assert!(fixture.tree().is_disabled_key(square.node), "by default Caps is disabled for a solid closed path");
fixture.click(&square);
fixture.idle(3);
assert_ne!(style.borrow().cap, LineCap::Square, "a disabled cap button does not change the cap");
let (mut fixture, style) = closed_editor(true);
let square = fixture.by_label("Square").expect("the editor shows Square");
assert!(!fixture.tree().is_disabled_key(square.node), "with caps_on_closed Caps is enabled");
fixture.click(&square);
fixture.idle(3);
assert_eq!(style.borrow().cap, LineCap::Square, "the cap button sets the cap");
}
/// The label of the host group's own row: longer than every label of the editor.
const HOST_ROW: &str = "Arrowheads and profile";
/// A host group with one long-labeled row, [`HOST_ROW`], over the editor with `sections` and `host_labels` as given,
/// on a dashed path.
fn hosted_editor(sections: StrokeSections, host_labels: bool) -> Fixture {
let style = Rc::new(RefCell::new(StrokeStyle { dashes: vec![4.0, 2.0], ..StrokeStyle::default() }));
let mut fixture = Fixture::new(move |ui| {
let mut style = style.borrow_mut();
FieldGroup::new("Host").show(ui, |ui, _| {
Field::new(HOST_ROW).show(ui, |ui, _| {
NumberField::new(HOST_ROW, Bind::given(Some(1.0))).show(ui);
});
let _ = StrokeEditor::new("Stroke", Bind::bound(&mut *style)).sections(sections).host_labels(host_labels).show(ui);
});
});
fixture.settle();
fixture
}
/// The x where the control named `label` starts.
fn control_start(fixture: &Fixture, label: &str) -> f32 {
fixture.by_label(label).unwrap_or_else(|| panic!("{label:?} names its control")).rect.min.x
}
#[trial]
fn host_labels_start_the_editor_rows_at_the_host_groups_control_edge() {
/// How far two edges may differ and still line up, in points.
const SLACK: f32 = 0.5;
let selects = StrokeSections::DASHES | StrokeSections::DASH_ALIGN | StrokeSections::OPACITY;
let fixture = hosted_editor(selects, true);
let edge = control_start(&fixture, HOST_ROW);
for name in ["Alignment", "Caps", "Corners", "Miter limit", "Opacity", "Dash offset", "Align dashes"] {
let at = control_start(&fixture, name);
assert!((at - edge).abs() <= SLACK, "{name:?} starts at the host group's control edge: {at} {edge}");
}
let fixture = hosted_editor(StrokeSections::PANEL, true);
let edge = control_start(&fixture, HOST_ROW);
for name in ["Miter limit", "Dash offset"] {
let at = control_start(&fixture, name);
assert!((at - edge).abs() <= SLACK, "{name:?} starts at the host group's control edge with icon rows: {at} {edge}");
}
let fixture = hosted_editor(selects, false);
let edge = control_start(&fixture, HOST_ROW);
for name in ["Alignment", "Caps", "Corners", "Miter limit", "Opacity", "Dash offset"] {
let at = control_start(&fixture, name);
assert!(at < edge - SLACK, "by default {name:?} keeps the editor's own label column: {at} {edge}");
}
}
#[trial]
fn dash_fields_add_after_the_pattern_and_a_cleared_field_ends_it() {
let (mut fixture, style) = editor(vec![4.0, 2.0]);
type_into(&mut fixture, "Dash 2", "3");
assert_eq!(style.borrow().dashes, [4.0, 2.0, 3.0], "the blank after the pattern adds an entry");
type_into(&mut fixture, "Gap 1", "");
assert_eq!(style.borrow().dashes, [4.0], "clearing the second field ends the pattern there");
type_into(&mut fixture, "Dash 1", "");
assert_eq!(style.borrow().dashes, [4.0], "the first field never clears");
}
#[trial]
fn round_cap_and_round_join_names_lead_to_their_own_actions() {
use craft_draw::ui::stroke::{LineCap, LineJoin};
use craft_ui::testing::{access::focused_key, focus::AccessActionKind};
let (mut fixture, style) = editor(vec![4.0, 2.0]);
let cap = fixture.by_label("Round Cap").expect("the cap has its own accessible name");
let join = fixture.by_label("Round Join").expect("the join has its own accessible name");
assert_ne!(cap.node, join.node, "the names find independent choices");
fixture.click(&cap).settle();
assert_eq!(style.borrow().cap, LineCap::Round);
assert_eq!(style.borrow().join, LineJoin::Miter);
fixture.access(AccessActionKind::Focus, &join).settle();
assert_eq!(focused_key(fixture.tree()), join.node);
fixture.key(Key::Enter, Modifiers::NONE).settle();
assert_eq!(style.borrow().join, LineJoin::Round);
assert_eq!(style.borrow().cap, LineCap::Round);
}