diff --git a/crates/ui-egui/src/ribbon/table.rs b/crates/ui-egui/src/ribbon/table.rs index 22fca7ca6a..e2b6cfcbc7 100644 --- a/crates/ui-egui/src/ribbon/table.rs +++ b/crates/ui-egui/src/ribbon/table.rs @@ -1,4 +1,4 @@ -use craft::behavior::{ColumnWidth, IconId}; +use craft::behavior::{ColumnWidth, EditEvent, IconId, Mixed}; use craft::keymap::CommandRef; use craft::menu::ribbon::{RibbonGroup, RibbonRow}; use craft::theme::ControlSize; @@ -9,6 +9,7 @@ use craft::ui::display::Text; use craft::ui::layout::{GridFit, empty_cell}; use craft::ui::layout_components::{Grid, Row, Stack}; use craft::ui::menu::MenuItem; +use craft::ui::runtime::focus; use craft::ui::style::TypeRole; use craft::ui::text::NumberField; @@ -113,21 +114,26 @@ pub(super) fn table_design(row: &mut RibbonRow<'_>, app: &mut WordApp) { }); } -/// Table Layout › Cell Size: the caret cell's row height and column width. They show the size on the -/// page (a row without a set height shows the height its text gives it) and editing runs -/// `table.rowHeight` / `table.columnWidth`; a drag is one undo step. +/// Table Layout › Cell Size: edit the caret cell's row height and starting grid column width. An +/// automatic row shows the height its text gives it. A drag is one undo step. fn cell_size_boxes(ui: &mut Ui, app: &mut WordApp) { let story = app.session.sel.focus.story; let Some((tp, r, c)) = app.session.sel.focus.path.cell() else { return }; let Some(t) = app.session.doc.table(story, &tp) else { return }; let row = t.rows.get(r).map(|x| x.props.clone()).unwrap_or_default(); - let stored_w = t.grid.get(t.grid_col(r, c)).copied(); - let laid = app.session.layout().pages.iter().flat_map(|pg| pg.items.iter()).find_map(|it| match it { - wordcraft_layout::Placed::Cell { rect, table, row, cell, story: st, .. } if *table == tp && *row == r && *cell == c && *st == story => { - Some(*rect) - } - _ => None, - }); + let column_w = t.grid.get(t.grid_col(r, c)).copied(); + let w0 = column_w + .or_else(|| { + app.session.layout().pages.iter().flat_map(|pg| pg.items.iter()).find_map(|it| match it { + wordcraft_layout::Placed::Cell { rect, table, row, cell, story: st, .. } + if *table == tp && *row == r && *cell == c && *st == story => + { + Some(rect.w) + } + _ => None, + }) + }) + .unwrap_or(0.0); // The row's own height: a vertically merged cell's box spans several rows, so take the // shortest box in this row. let row_h = app @@ -142,7 +148,6 @@ fn cell_size_boxes(ui: &mut Ui, app: &mut WordApp) { }) .reduce(f32::min); let h0 = row.height.or(row_h).unwrap_or(0.0); - let w0 = laid.map(|x| x.w).or(stored_w).unwrap_or(0.0); let exact = row.height_rule == wordcraft_doc::props::HeightRule::Exact; let (height_units, width_units) = (crate::options::lengths(app.session.units()), crate::options::lengths(app.session.units())); // As the old ribbon and Layout › Paragraph: a visible label beside each field, two decimals ("0.20"). The fields are @@ -158,9 +163,18 @@ fn cell_size_boxes(ui: &mut Ui, app: &mut WordApp) { } }); caption("Width:").show(ui); - let field = NumberField::length(tl!("Width:").to_string(), Bind::given(Some(f64::from(w0))), width_units).range(CELL_SIZE_RANGE); + 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); - apply_edits(app, out.edit.events, |app, v| { + // 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| { if let Some(w) = v { let _ = app.run("table.columnWidth", json!({"width": w})); } diff --git a/crates/ui-egui/src/ribbon/tests.rs b/crates/ui-egui/src/ribbon/tests.rs index 00d8d1975c..d5da410012 100644 --- a/crates/ui-egui/src/ribbon/tests.rs +++ b/crates/ui-egui/src/ribbon/tests.rs @@ -1096,6 +1096,50 @@ fn table_cell_size_fields_line_up_with_the_distribute_buttons() { ); } +#[test] +fn the_width_box_for_a_merged_cell_commits_its_starting_column() { + use egui::accesskit::Role; + + 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": 2, "cols": 3})).unwrap(); + let table = |app: &WordApp| app.session.doc.body.iter().position(|block| block.as_table().is_some()).expect("inserted table"); + let index = table(&app); + let grid = |app: &WordApp| app.session.doc.body[index].as_table().expect("inserted table").grid.clone(); + let cell = |col| json!({"story": "body", "path": [index, 0, col, 0], "off": 0}); + app.run("select.range", json!({"anchor": cell(0), "focus": cell(1)})).unwrap(); + app.run("table.merge", json!({})).unwrap(); + + let mut h = tab_harness(app, "Table Layout"); + let before = grid(h.state()); + let undo = h.state().session.undo_depth(); + let displayed = h.get_by_role_and_label(Role::SpinButton, "Width:").accesskit_node().numeric_value().expect("Width value"); + assert!((displayed - f64::from(before[0])).abs() < 0.01, "the field shows the column it will edit"); + h.get_by_role_and_label(Role::SpinButton, "Width:").click(); + crate::test_harness::frames(&mut h, 2); + let focused = h.ctx.memory(|m| m.focused()).expect("Width took keyboard focus"); + h.key_press(egui::Key::End); + h.key_press(egui::Key::Delete); + crate::test_harness::frames(&mut h, 2); + assert_eq!((grid(h.state()), h.state().session.undo_depth()), (before.clone(), undo), "Delete at the end changes nothing"); + + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + crate::test_harness::frames(&mut h, 2); + for digit in ["5", "0"] { + h.event(egui::Event::Text(digit.into())); + crate::test_harness::frames(&mut h, 2); + assert_eq!((grid(h.state()), h.state().session.undo_depth()), (before.clone(), undo), "typing changes only the draft"); + assert_eq!(h.ctx.memory(|m| m.focused()), Some(focused), "Width keeps the keyboard while typing"); + } + h.key_press(egui::Key::Enter); + crate::test_harness::frames(&mut h, 3); + let mut expected = before.clone(); + expected[0] = 50.0; + assert_eq!((grid(h.state()), h.state().session.undo_depth()), (expected, undo + 1), "commit changes the starting column in one step"); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(grid(h.state()), before, "undo restores the width"); +} + /// #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]