mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-27 06:58:35 +00:00
* Ribbon: align Table Style Options checkboxes in two columns (#7691) * Ribbon: widen Table Style Options column gutter while keeping tight rows
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
use craft::behavior::IconId;
|
||||
use craft::behavior::{ColumnWidth, IconId};
|
||||
use craft::keymap::CommandRef;
|
||||
use craft::menu::ribbon::{RibbonGroup, RibbonRow};
|
||||
use craft::ui::buttons::{Arrangement, Button, IconButton, ToggleButton};
|
||||
use craft::ui::choice::Checkbox;
|
||||
use craft::ui::contract::{Bind, Gap, Props as _, Show as _, ShowContainer as _};
|
||||
use craft::ui::layout_components::{Row, Stack};
|
||||
use craft::ui::layout::{GridFit, empty_cell};
|
||||
use craft::ui::layout_components::{Grid, Row, Stack};
|
||||
use craft::ui::menu::MenuItem;
|
||||
use craft::ui::text::NumberField;
|
||||
|
||||
@@ -17,25 +18,33 @@ use crate::undo_marks::apply_edits;
|
||||
/// The row height and column width a Cell Size field accepts, in points (0.02 to 22 inches).
|
||||
const CELL_SIZE_RANGE: std::ops::RangeInclusive<f64> = 1.44..=1584.0;
|
||||
|
||||
/// Two checkbox columns separated by two empty gutter cells: `Grid::gap` sets both axes, so the
|
||||
/// empty gutter widens the column gap to `3 * Gap::Tight` (at least `Gap::Stack`) while keeping
|
||||
/// rows `Gap::Tight` apart inside the ribbon group.
|
||||
const TABLE_STYLE_OPTION_COLUMNS: [ColumnWidth; 4] = [ColumnWidth::Content; 4];
|
||||
|
||||
pub(super) fn table_design(row: &mut RibbonRow<'_>, app: &mut WordApp) {
|
||||
let look = app.session.sel.focus.path.cell().and_then(|(tp, _, _)| app.session.doc.table(app.session.sel.focus.story, &tp).map(|t| t.props.look));
|
||||
if let Some(l) = look {
|
||||
// Three rows of two, so Tab runs Header Row, First Column, Total Row, Last Column, Banded Rows, Banded Columns.
|
||||
// Row-major pairs, so Tab runs Header Row, First Column, Total Row, Last Column, Banded Rows, Banded Columns.
|
||||
let options = [
|
||||
[("Header Row", "headerRow", l.header_row), ("First Column", "firstColumn", l.first_column)],
|
||||
[("Total Row", "totalRow", l.total_row), ("Last Column", "lastColumn", l.last_column)],
|
||||
[("Banded Rows", "bandedRows", l.banded_rows), ("Banded Columns", "bandedColumns", l.banded_columns)],
|
||||
("Header Row", "headerRow", l.header_row),
|
||||
("First Column", "firstColumn", l.first_column),
|
||||
("Total Row", "totalRow", l.total_row),
|
||||
("Last Column", "lastColumn", l.last_column),
|
||||
("Banded Rows", "bandedRows", l.banded_rows),
|
||||
("Banded Columns", "bandedColumns", l.banded_columns),
|
||||
];
|
||||
row.group(RibbonGroup::new("tableStyleOptions", tl!("Table Style Options").to_string(), IconId::new("tableStyleOptions").into()), |ui| {
|
||||
Stack::new().gap(Gap::Tight).show(ui, |ui, _| {
|
||||
for line in options {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
for (label, key, value) in line {
|
||||
let out = Checkbox::new(Bind::given(value)).label(tl!(label).to_string()).show(ui);
|
||||
apply_edits(app, out.events, |app, v| {
|
||||
let _ = app.run("table.look", json!({ key: v }));
|
||||
});
|
||||
}
|
||||
Grid::new(&TABLE_STYLE_OPTION_COLUMNS).fit(GridFit::Fixed).gap(Gap::Tight).show(ui, |ui, _| {
|
||||
for (index, (label, key, value)) in options.into_iter().enumerate() {
|
||||
if index % 2 == 1 {
|
||||
empty_cell(ui);
|
||||
empty_cell(ui);
|
||||
}
|
||||
let out = Checkbox::new(Bind::given(value)).label(tl!(label).to_string()).show(ui);
|
||||
apply_edits(app, out.events, |app, v| {
|
||||
let _ = app.run("table.look", json!({ key: v }));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -994,3 +994,66 @@ fn decorative_text_dropdown_renders_compact_wordart_tiles() {
|
||||
rect.height()
|
||||
);
|
||||
}
|
||||
|
||||
/// #7691: Table Design › Table Style Options aligns its six checkboxes in two columns with a gap
|
||||
/// between them and keeps all three rows above the group caption.
|
||||
#[test]
|
||||
fn table_style_options_align_in_two_columns() {
|
||||
use egui::accesskit::Role;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
|
||||
let mut app = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
|
||||
app.run("insert.table", json!({"rows": 2, "cols": 2})).unwrap();
|
||||
let h = tab_harness(app, "Table Design");
|
||||
let rect = |label: &str| h.get_by_role_and_label(Role::CheckBox, label).rect();
|
||||
let header = rect("Header Row");
|
||||
let first_col = rect("First Column");
|
||||
let total = rect("Total Row");
|
||||
let last_col = rect("Last Column");
|
||||
let banded = rect("Banded Rows");
|
||||
let banded_col = rect("Banded Columns");
|
||||
// Left column checkboxes share one left edge, and right column checkboxes share another.
|
||||
assert!(
|
||||
(header.left() - total.left()).abs() < 0.5 && (header.left() - banded.left()).abs() < 0.5,
|
||||
"left column misaligned: {header:?}, {total:?}, {banded:?}"
|
||||
);
|
||||
assert!(
|
||||
(first_col.left() - last_col.left()).abs() < 0.5 && (first_col.left() - banded_col.left()).abs() < 0.5,
|
||||
"right column misaligned: {first_col:?}, {last_col:?}, {banded_col:?}"
|
||||
);
|
||||
// Each row's pair shares the same top edge and stacks vertically with tight spacing.
|
||||
let env = craft::ui::runtime::env::Env::of_root(&h.ctx);
|
||||
let tight_gap = env.tokens.space.gap.tight;
|
||||
let min_col_gap = env.tokens.space.gap.stack;
|
||||
assert!((header.top() - first_col.top()).abs() < 0.5, "row 1 misaligned: {header:?}, {first_col:?}");
|
||||
assert!((total.top() - last_col.top()).abs() < 0.5, "row 2 misaligned: {total:?}, {last_col:?}");
|
||||
assert!((banded.top() - banded_col.top()).abs() < 0.5, "row 3 misaligned: {banded:?}, {banded_col:?}");
|
||||
assert!(header.bottom() <= total.top() && total.bottom() <= banded.top(), "rows overlap: {header:?}, {total:?}, {banded:?}");
|
||||
let row_gap_1 = total.top() - header.bottom();
|
||||
let row_gap_2 = banded.top() - total.bottom();
|
||||
assert!(
|
||||
(row_gap_1 - tight_gap).abs() <= 0.5 && (row_gap_2 - tight_gap).abs() <= 0.5,
|
||||
"rows should remain Gap::Tight ({tight_gap} pt) apart, got {row_gap_1} and {row_gap_2}"
|
||||
);
|
||||
// A horizontal gap at least `space.gap.stack` separates the widest left-column label from the second column.
|
||||
let text_rect = |label: &str| {
|
||||
h.output()
|
||||
.shapes
|
||||
.iter()
|
||||
.find_map(|s| match &s.shape {
|
||||
egui::Shape::Text(t) if t.galley.text() == label => Some(egui::Rect::from_min_size(t.pos, t.galley.size())),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_else(|| panic!("painted text for {label:?}"))
|
||||
};
|
||||
let left_label_max_right =
|
||||
["Header Row", "Total Row", "Banded Rows"].into_iter().map(|label| text_rect(label).right()).fold(f32::NEG_INFINITY, f32::max);
|
||||
let col_gap = first_col.left() - left_label_max_right;
|
||||
assert!(
|
||||
col_gap >= min_col_gap,
|
||||
"column gap between widest left label and right column too narrow: got {col_gap:.2} pt, expected >= {min_col_gap:.2} pt (left_label_max_right={left_label_max_right}, first_col={first_col:?})"
|
||||
);
|
||||
// All three rows stay above the group's caption.
|
||||
let caption = text_rect("Table Style Options");
|
||||
assert!(banded.bottom() <= caption.top(), "checkboxes collide with group caption: {banded:?} vs {caption:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user