mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-06 23:09:47 +00:00
Merge pull request #9379
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use craft::behavior::{ColumnWidth, EditEvent, IconId, Mixed};
|
||||
use craft::behavior::{ColumnWidth, EditEvent, EditEvents, IconId, Mixed};
|
||||
use craft::keymap::CommandRef;
|
||||
use craft::menu::ribbon::{RibbonGroup, RibbonRow};
|
||||
use craft::theme::ControlSize;
|
||||
@@ -153,28 +153,36 @@ fn cell_size_boxes(ui: &mut Ui, app: &mut WordApp) {
|
||||
// As the old ribbon and Layout › Paragraph: a visible label beside each field, two decimals ("0.20"). The fields are
|
||||
// as tall as the small buttons, so their rows line up with Distribute Rows and Distribute Columns beside them.
|
||||
let caption = |label: &'static str| Text::new(tl!(label)).role(TypeRole::Caption);
|
||||
let edit_field = |ui: &mut Ui, app: &WordApp, field: NumberField<'_>| -> EditEvents<Option<f64>> {
|
||||
let out = field
|
||||
.range(CELL_SIZE_RANGE)
|
||||
.edit_target((&app.session.sel.anchor, &app.session.sel.focus))
|
||||
.fixed_decimals(2)
|
||||
.size(ControlSize::Md)
|
||||
.show(ui);
|
||||
// Keep typing in the field until commit, while a scrub updates the table during the drag.
|
||||
let typing = focus::focused_rec(ui.ctx()).is_some_and(|rec| rec.stop.id == out.edit.response.id && rec.stop.text);
|
||||
out.edit
|
||||
.events
|
||||
.into_iter()
|
||||
.filter(|event| match event {
|
||||
EditEvent::Update { .. } => !typing,
|
||||
EditEvent::Set { before: Mixed::Same(before), after, .. } => before != after,
|
||||
_ => true,
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
Grid::new(&[ColumnWidth::Content; 2]).gap(Gap::Tight).show(ui, |ui, _| {
|
||||
caption("Height:").show(ui);
|
||||
let field = NumberField::length(tl!("Height:").to_string(), Bind::given(Some(f64::from(h0))), height_units).range(CELL_SIZE_RANGE);
|
||||
let out = field.fixed_decimals(2).size(ControlSize::Md).show(ui);
|
||||
apply_edits(app, out.edit.events, |app, v| {
|
||||
let events = edit_field(ui, app, NumberField::length(tl!("Height:").to_string(), Bind::given(Some(f64::from(h0))), height_units));
|
||||
apply_edits(app, events, |app, v| {
|
||||
if let Some(h) = v {
|
||||
let _ = app.run("table.rowHeight", json!({"height": h, "rule": if exact { "exact" } else { "atLeast" }}));
|
||||
}
|
||||
});
|
||||
caption("Width:").show(ui);
|
||||
let field = NumberField::length(tl!("Width:").to_string(), Bind::given(Some(f64::from(w0))), width_units)
|
||||
.range(CELL_SIZE_RANGE)
|
||||
.edit_target((&app.session.sel.anchor, &app.session.sel.focus));
|
||||
let out = field.fixed_decimals(2).size(ControlSize::Md).show(ui);
|
||||
// Keep typed numbers in the field until commit. A scrub still updates the table during the drag.
|
||||
let typing = focus::focused_rec(ui.ctx()).is_some_and(|rec| rec.stop.id == out.edit.response.id && rec.stop.text);
|
||||
let events = out.edit.events.into_iter().filter(|event| match event {
|
||||
EditEvent::Update { .. } => !typing,
|
||||
EditEvent::Set { before: Mixed::Same(before), after, .. } => before != after,
|
||||
_ => true,
|
||||
});
|
||||
apply_edits(app, events.collect(), |app, v| {
|
||||
let events = edit_field(ui, app, NumberField::length(tl!("Width:").to_string(), Bind::given(Some(f64::from(w0))), width_units));
|
||||
apply_edits(app, events, |app, v| {
|
||||
if let Some(w) = v {
|
||||
let _ = app.run("table.columnWidth", json!({"width": w}));
|
||||
}
|
||||
|
||||
@@ -1140,6 +1140,96 @@ fn the_width_box_for_a_merged_cell_commits_its_starting_column() {
|
||||
assert_eq!(grid(h.state()), before, "undo restores the width");
|
||||
}
|
||||
|
||||
/// #9377: Height preserves automatic rows until the user commits a change, and scrubs live as one step.
|
||||
#[test]
|
||||
fn the_height_box_commits_typing_and_scrubs_live() {
|
||||
use egui::accesskit::Role;
|
||||
use wordcraft_doc::props::HeightRule;
|
||||
|
||||
let mut app = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
|
||||
app.run("file.options", json!({"units": "points"})).unwrap();
|
||||
app.run("insert.table", json!({"rows": 3, "cols": 2})).unwrap();
|
||||
let index = app.session.doc.body.iter().position(|block| block.as_table().is_some()).expect("inserted table");
|
||||
let cell = |row| json!({"story": "body", "path": [index, row, 0, 0], "off": 0});
|
||||
let select = |app: &mut WordApp, row| {
|
||||
app.run("select.range", json!({"anchor": cell(row), "focus": cell(row)})).unwrap();
|
||||
};
|
||||
select(&mut app, 2);
|
||||
app.run("table.rowHeight", json!({"height": 40, "rule": "exact"})).unwrap();
|
||||
select(&mut app, 1);
|
||||
app.run("table.rowHeight", json!({"height": 30, "rule": "atLeast"})).unwrap();
|
||||
|
||||
let mut h = tab_harness(app, "Table Layout");
|
||||
let rows = |app: &WordApp| {
|
||||
app.session.doc.body[index].as_table().unwrap().rows.iter().map(|r| (r.props.height, r.props.height_rule)).collect::<Vec<_>>()
|
||||
};
|
||||
let before = rows(h.state());
|
||||
let undo = h.state().session.undo_depth();
|
||||
assert_eq!(before, [(None, HeightRule::Auto), (Some(30.0), HeightRule::AtLeast), (Some(40.0), HeightRule::Exact)]);
|
||||
|
||||
let at = h.get_by_role_and_label(Role::SpinButton, "Height:").rect().center();
|
||||
let pointer = |pos, pressed| egui::Event::PointerButton { pos, pressed, button: egui::PointerButton::Primary, modifiers: egui::Modifiers::NONE };
|
||||
h.event(egui::Event::PointerMoved(at));
|
||||
h.event(pointer(at, true));
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
let mut last = 30.0;
|
||||
for distance in [12.0, 24.0] {
|
||||
h.event(egui::Event::PointerMoved(at + vec2(distance, 0.0)));
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
let after = rows(h.state());
|
||||
let height = after[1].0.expect("scrubbed height");
|
||||
assert!(height > last, "each move changes the height before release");
|
||||
assert_eq!(after[1].1, HeightRule::AtLeast);
|
||||
assert_eq!([after[0], after[2]], [before[0], before[2]]);
|
||||
last = height;
|
||||
}
|
||||
h.event(pointer(at + vec2(24.0, 0.0), false));
|
||||
crate::test_harness::frames(&mut h, 3);
|
||||
assert_eq!(rows(h.state())[1], (Some(last), HeightRule::AtLeast));
|
||||
assert_eq!(h.state().session.undo_depth(), undo + 1, "one scrub, one undo step");
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(rows(h.state()), before);
|
||||
|
||||
for (row, rule) in [(0, HeightRule::AtLeast), (1, HeightRule::AtLeast), (2, HeightRule::Exact)] {
|
||||
select(h.state_mut(), row);
|
||||
h.state_mut().run("table.selectRow", json!({})).unwrap();
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
h.get_by_role_and_label(Role::SpinButton, "Height:").click();
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
h.key_press(egui::Key::End);
|
||||
h.key_press(egui::Key::Delete);
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
assert_eq!((rows(h.state()), h.state().session.undo_depth()), (before.clone(), undo), "Delete at the end changes nothing");
|
||||
|
||||
let type_height = |h: &mut Harness| {
|
||||
let focused = h.ctx.memory(|m| m.focused()).expect("Height took keyboard focus");
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
crate::test_harness::frames(h, 2);
|
||||
for digit in ["5", "6"] {
|
||||
h.event(egui::Event::Text(digit.into()));
|
||||
crate::test_harness::frames(h, 2);
|
||||
assert_eq!((rows(h.state()), h.state().session.undo_depth()), (before.clone(), undo), "typing stays in the draft");
|
||||
assert_eq!(h.ctx.memory(|m| m.focused()), Some(focused), "Height keeps the keyboard while typing");
|
||||
}
|
||||
};
|
||||
type_height(&mut h);
|
||||
h.key_press(egui::Key::Escape);
|
||||
crate::test_harness::frames(&mut h, 3);
|
||||
assert_eq!((rows(h.state()), h.state().session.undo_depth()), (before.clone(), undo), "Escape discards the draft");
|
||||
|
||||
h.get_by_role_and_label(Role::SpinButton, "Height:").click();
|
||||
crate::test_harness::frames(&mut h, 2);
|
||||
type_height(&mut h);
|
||||
h.key_press(egui::Key::Enter);
|
||||
crate::test_harness::frames(&mut h, 3);
|
||||
let mut expected = before.clone();
|
||||
expected[row] = (Some(56.0), rule);
|
||||
assert_eq!((rows(h.state()), h.state().session.undo_depth()), (expected, undo + 1), "only the selected row changes");
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(rows(h.state()), before, "one Undo restores the original row height and rule");
|
||||
}
|
||||
}
|
||||
|
||||
/// #4017: Home's small buttons, icon-only or labelled, are at least 24 points square: the least target size the
|
||||
/// accessibility standard asks for (WCAG 2.5.8).
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user