mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-28 08:45:52 -06:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a9fa89fb2 | ||
|
|
8b293b64ad | ||
|
|
d1c9c8c034 | ||
|
|
06499e39aa |
@@ -3,18 +3,21 @@
|
||||
//! `styles.copyFrom`, `styles.delete`, `styles.rename`), so agents get the same result without
|
||||
//! the dialogs.
|
||||
|
||||
use craft::behavior::{EditEvent, Selection, SelectionChange, item_source::keyed};
|
||||
use craft::behavior::{ColumnWidth, EditEvent, Selection, SelectionChange, item_source::keyed};
|
||||
use craft::host::PickRequest;
|
||||
use craft::ui::choice::Checkbox;
|
||||
use craft::ui::contract::{Bind, CollectionEvent, Props as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _};
|
||||
use craft::ui::contract::{Bind, CollectionEvent, Props as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, request_rename};
|
||||
use craft::ui::dialog::{Dialog, DialogId, DialogResult, DialogStack, StackDialog, dialog_id};
|
||||
use craft::ui::display::Text;
|
||||
use craft::ui::form::Field;
|
||||
use craft::ui::layout_components::Row;
|
||||
use craft::ui::layout::CellAlign;
|
||||
use craft::ui::layout_components::{Grid, Stack};
|
||||
use craft::ui::list::{List, ListRow};
|
||||
use craft::ui::notices::Notice;
|
||||
use craft::ui::patterns::form_dialogs::{FormCx, PathRow};
|
||||
use craft::ui::runtime::env::{Env, Label};
|
||||
use craft::ui::sections::{DialogRole, FooterButton};
|
||||
use craft::ui::style::SurfaceKind;
|
||||
use egui::{Context, Ui};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
@@ -24,6 +27,15 @@ use super::styles::{action, label};
|
||||
use crate::WordApp;
|
||||
|
||||
const ORGANIZER_KEY: &str = "organizer";
|
||||
/// The Organizer's columns: each takes what it holds, and a list holds at least `LIST_WIDTH` (a fill column takes no more
|
||||
/// than its content in a dialog that sizes to its content, so an empty list was a few pixels wide).
|
||||
const COLUMNS: [ColumnWidth; 3] = [ColumnWidth::Content; 3];
|
||||
/// The least width of a list, in points.
|
||||
const LIST_WIDTH: f32 = 200.0;
|
||||
/// The rows each list shows before it scrolls.
|
||||
const LIST_ROWS: u16 = 12;
|
||||
/// The other file's list has its name above it, so it shows one row fewer to end level with the first.
|
||||
const OTHER_ROWS: u16 = LIST_ROWS - 1;
|
||||
|
||||
/// The Templates and Add-ins dialog's fields.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
@@ -236,50 +248,69 @@ impl OrganizerDialog {
|
||||
|
||||
fn body(&mut self, ui: &mut Ui) {
|
||||
let reason = browse_reason(ui);
|
||||
Row::new().show(ui, |ui, _| {
|
||||
let mut selection = Selection::single(self.left.clone());
|
||||
let mine = &self.mine;
|
||||
let source = keyed(mine, |st| text_of(st, "id"));
|
||||
let out = List::new(label(tl!("Document")))
|
||||
.select(SelectMode::Single)
|
||||
.selection(Bind::bound(&mut selection))
|
||||
.rename(true)
|
||||
.can_rename(|key: &String| {
|
||||
mine.iter().find(|x| text_of(x, "id") == *key).is_some_and(|x| !x.get("builtIn").and_then(Value::as_bool).unwrap_or(true))
|
||||
})
|
||||
.id_salt("mine")
|
||||
.show_items(ui, &source, &mut |_, icx| {
|
||||
let Some(st) = mine.get(icx.at) else { return ListRow::spacer() };
|
||||
ListRow::new(label(text_of(st, "name"))).lead_text(label(glyph(&text_of(st, "type"))), label("¶"))
|
||||
});
|
||||
for event in out.events {
|
||||
match event {
|
||||
CollectionEvent::Selection { change: SelectionChange::Set(keys), .. } => {
|
||||
if let Some(key) = keys.last() {
|
||||
self.left = key.clone();
|
||||
let mine_id = ui.scope_id().with("organizer_mine");
|
||||
// Three columns as the old dialog had: this document's styles, Copy / Delete / Rename, the other file's styles.
|
||||
Grid::new(&COLUMNS).cells(CellAlign::Top).show(ui, |ui, _| {
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
ui.set_min_width(LIST_WIDTH);
|
||||
let mut selection = Selection::single(self.left.clone());
|
||||
let mine = &self.mine;
|
||||
let source = keyed(mine, |st| text_of(st, "id"));
|
||||
let out = List::new(label(tl!("Document")))
|
||||
.surface(SurfaceKind::Frame)
|
||||
.select(SelectMode::Single)
|
||||
.selection(Bind::bound(&mut selection))
|
||||
.rename(true)
|
||||
.can_rename(|key: &String| {
|
||||
mine.iter().find(|x| text_of(x, "id") == *key).is_some_and(|x| !x.get("builtIn").and_then(Value::as_bool).unwrap_or(true))
|
||||
})
|
||||
.id(mine_id)
|
||||
.min_rows(LIST_ROWS)
|
||||
.max_rows(LIST_ROWS)
|
||||
.show_items(ui, &source, &mut |_, icx| {
|
||||
let Some(st) = mine.get(icx.at) else { return ListRow::spacer() };
|
||||
ListRow::new(label(text_of(st, "name"))).lead_text(label(glyph(&text_of(st, "type"))), label("¶"))
|
||||
});
|
||||
for event in out.events {
|
||||
match event {
|
||||
CollectionEvent::Selection { change: SelectionChange::Set(keys), .. } => {
|
||||
if let Some(key) = keys.last() {
|
||||
self.left = key.clone();
|
||||
}
|
||||
}
|
||||
CollectionEvent::Rename(EditEvent::Commit { after, .. }) => {
|
||||
self.requests.push(Request::Rename { style: after.key, name: after.name });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
CollectionEvent::Rename(EditEvent::Commit { after, .. }) => {
|
||||
self.requests.push(Request::Rename { style: after.key, name: after.name });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if action(ui, "copy", label(tl!("Copy")), !self.right.is_empty(), None) {
|
||||
self.requests.push(Request::Copy);
|
||||
}
|
||||
if action(ui, "delete", label(tl!("Delete")), !self.left_builtin(), None) {
|
||||
self.requests.push(Request::Delete(self.left.clone()));
|
||||
}
|
||||
let name = std::path::Path::new(&self.file).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "—".into());
|
||||
Field::new(label(name)).show(ui, |ui, _| {
|
||||
});
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
if action(ui, "copy", label(tl!("Copy")), !self.right.is_empty(), None) {
|
||||
self.requests.push(Request::Copy);
|
||||
}
|
||||
if action(ui, "delete", label(tl!("Delete")), !self.left_builtin(), None) {
|
||||
self.requests.push(Request::Delete(self.left.clone()));
|
||||
}
|
||||
// The list's own rename field takes the name in place.
|
||||
if action(ui, "rename", label(tl!("Rename…")), !self.left_builtin(), None) {
|
||||
request_rename(ui.ctx(), mine_id, &self.left);
|
||||
}
|
||||
});
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
ui.set_min_width(LIST_WIDTH);
|
||||
let name = std::path::Path::new(&self.file).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "—".into());
|
||||
Text::new(label(name)).show(ui);
|
||||
let mut selection = Selection::single(self.right.clone());
|
||||
let other = &self.other;
|
||||
let source = keyed(other, |(n, _)| n.clone());
|
||||
let out = List::new(label(tl!("Template")))
|
||||
.surface(SurfaceKind::Frame)
|
||||
.select(SelectMode::Single)
|
||||
.selection(Bind::bound(&mut selection))
|
||||
.id_salt("other")
|
||||
.min_rows(OTHER_ROWS)
|
||||
.max_rows(OTHER_ROWS)
|
||||
.show_items(ui, &source, &mut |_, icx| {
|
||||
let Some((n, ty)) = other.get(icx.at) else { return ListRow::spacer() };
|
||||
ListRow::new(label(n)).lead_text(label(glyph(ty)), label("¶"))
|
||||
@@ -291,12 +322,13 @@ impl OrganizerDialog {
|
||||
self.right = key.clone();
|
||||
}
|
||||
}
|
||||
let picked = template_row(label(tl!("Template")), Bind::bound(&mut self.file), reason.clone()).show(ui).picked;
|
||||
if let Some(path) = picked {
|
||||
self.requests.push(Request::Load(path));
|
||||
}
|
||||
});
|
||||
});
|
||||
// The other file's path and Browse stand under both lists, at the dialog's full width.
|
||||
let picked = template_row(label(tl!("Template")), Bind::bound(&mut self.file), reason.clone()).show(ui).picked;
|
||||
if let Some(path) = picked {
|
||||
self.requests.push(Request::Load(path));
|
||||
}
|
||||
if !self.message.is_empty() {
|
||||
Notice::new(craft::theme::variant::Tone::Bad, label(&self.message)).show(ui);
|
||||
}
|
||||
|
||||
@@ -191,11 +191,11 @@ const MARGIN_SIDES: [(&str, &str); 4] = [("top", "Top"), ("left", "Left"), ("bot
|
||||
fn open_cell_margins(app: &mut WordApp, ctx: &Context, parent: Option<DialogId>) -> Option<DialogId> {
|
||||
let margins = cell_margins_pt(app)?;
|
||||
let fields = DialogFields::from_pairs(MARGIN_SIDES.iter().zip(margins).map(|((key, _), v)| (*key, v)));
|
||||
let kind = crate::options::unit_kind(app.session.units());
|
||||
let units = crate::options::lengths(app.session.units());
|
||||
let dialog = HostedParamDialog::new(ctx, "tableCellMargins", fields, move |fields| {
|
||||
let params: Vec<Param> = MARGIN_SIDES
|
||||
.iter()
|
||||
.map(|(key, label)| Param::number(*key, tl!(label).to_string(), 0.0, 1.0, 2).bounds(Some(0.0), Some(144.0)).unit(kind))
|
||||
.map(|(key, label)| Param::number(*key, tl!(label).to_string(), 0.0, units.shown.step(), 2).bounds(Some(0.0), Some(144.0)).length(units))
|
||||
.collect();
|
||||
ParamDialog::command("tableCellMargins", tl!("Cell Margins").to_string(), params).values(Bind::bound(fields)).alt_resets(false)
|
||||
});
|
||||
|
||||
@@ -2283,3 +2283,109 @@ fn modal_dialog_with_open_popup_does_not_shake_on_inside_click_but_shakes_on_out
|
||||
let key2 = chrome::nudge_id(&h.ctx, id2);
|
||||
assert!(h.ctx.data(|d| d.get_temp::<f64>(key2)).is_none(), "reopened dialog starts still");
|
||||
}
|
||||
|
||||
/// #7970: Cell Margins shows the table's points in the interface unit (0.19 cm, not 5.4 cm), and OK without
|
||||
/// an edit leaves the table's margins as they were.
|
||||
#[test]
|
||||
fn cell_margins_dialog_shows_points_in_the_interface_unit_and_ok_changes_nothing() {
|
||||
let mut app = app_with("");
|
||||
let _ = app.run("insert.table", json!({"rows": 1, "cols": 1}));
|
||||
app.session.prefs.units = Some(wordcraft_geom::Unit::Centimeters);
|
||||
let mut h = app_harness(app);
|
||||
h.run_steps(2);
|
||||
let (tp, _, _) = h.state().session.sel.focus.path.cell().unwrap();
|
||||
let margins =
|
||||
|h: &egui_kittest::Harness<'_, WordApp>| h.state().session.doc.table(h.state().session.sel.focus.story, &tp).unwrap().props.cell_margins;
|
||||
let before = margins(&h);
|
||||
let _ = h.state_mut().run("ui.dialog", json!({"name": "tableCellMargins"}));
|
||||
h.run_steps(3);
|
||||
let left = h.get_by_role_and_label(egui::accesskit::Role::SpinButton, "Left").value().unwrap_or_default();
|
||||
let shown: f64 = left.trim_end_matches(|c: char| !c.is_ascii_digit()).replace(',', ".").parse().unwrap_or(f64::NAN);
|
||||
assert!(shown < 1.0, "5.4 pt is about 0.19 cm, not the point count: {left:?}");
|
||||
let id = id_of(h.state(), "tableCellMargins");
|
||||
let ctx = h.ctx.clone();
|
||||
let _ = h.state_mut().dialog_host.confirm_by_id(&ctx, id);
|
||||
h.run_steps(4);
|
||||
assert_eq!(margins(&h), before, "OK without an edit writes no margins");
|
||||
}
|
||||
|
||||
/// #7970: a width typed into Cell Margins in the interface unit reaches the table in points (1 cm is about 28 pt).
|
||||
#[test]
|
||||
fn cell_margins_dialog_converts_a_typed_length_back_to_points() {
|
||||
let mut app = app_with("");
|
||||
let _ = app.run("insert.table", json!({"rows": 1, "cols": 1}));
|
||||
app.session.prefs.units = Some(wordcraft_geom::Unit::Centimeters);
|
||||
let mut h = app_harness(app);
|
||||
h.run_steps(2);
|
||||
let (tp, _, _) = h.state().session.sel.focus.path.cell().unwrap();
|
||||
let _ = h.state_mut().run("ui.dialog", json!({"name": "tableCellMargins"}));
|
||||
h.run_steps(3);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::SpinButton, "Left").click();
|
||||
h.run_steps(3);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
h.event(egui::Event::Paste("1".into()));
|
||||
h.run_steps(3);
|
||||
h.key_press(egui::Key::Tab);
|
||||
h.run_steps(3);
|
||||
let id = id_of(h.state(), "tableCellMargins");
|
||||
let ctx = h.ctx.clone();
|
||||
let _ = h.state_mut().dialog_host.confirm_by_id(&ctx, id);
|
||||
h.run_steps(4);
|
||||
let left = h.state().session.doc.table(h.state().session.sel.focus.story, &tp).unwrap().props.cell_margins.map(|m| m[1]);
|
||||
assert!(left.is_some_and(|l| (l - 28.35).abs() < 0.5), "1 cm is about 28.35 pt: {left:?}");
|
||||
}
|
||||
|
||||
/// #7980: the Organizer shows this document's styles and the other file's side by side, with Copy, Delete and
|
||||
/// Rename between them, all inside the dialog.
|
||||
#[test]
|
||||
fn organizer_shows_both_lists_and_its_buttons() {
|
||||
use egui::accesskit::Role;
|
||||
let mut h = app_harness(app_with(""));
|
||||
h.run_steps(2);
|
||||
let _ = h.state_mut().run("ui.dialog", json!({"name": "organizer"}));
|
||||
h.run_steps(4);
|
||||
let rect = |h: &egui_kittest::Harness<'_, WordApp>, role, name: &str| h.get_by_role_and_label(role, name).rect();
|
||||
let mine = rect(&h, Role::List, "Document");
|
||||
let other = rect(&h, Role::List, "Template");
|
||||
assert!(mine.is_positive() && other.is_positive() && mine.right() <= other.left(), "the lists stand side by side: {mine:?} {other:?}");
|
||||
// Each list is wider than the button column between them, empty (no other file) or not.
|
||||
let rename = rect(&h, Role::Button, "Rename…");
|
||||
assert!(mine.width() > rename.width() && other.width() > rename.width(), "the lists are wider than the buttons: {mine:?} {other:?} {rename:?}");
|
||||
// The ribbon has its own Copy: the one that counts stands between the lists.
|
||||
for name in ["Copy", "Delete", "Rename…"] {
|
||||
let between = h
|
||||
.query_all(egui_kittest::kittest::By::new().role(Role::Button).label(name))
|
||||
.any(|b| b.rect().left() >= mine.right() - 1.0 && b.rect().right() <= other.left() + 1.0);
|
||||
assert!(between, "{name} stands between the lists {mine:?} {other:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Part of #7980: the Organizer's two lists have a visible frame, as before the port; without it an empty list is a
|
||||
/// blank patch.
|
||||
#[test]
|
||||
fn the_organizer_lists_are_framed() {
|
||||
use egui::accesskit::Role;
|
||||
fn rects<'a>(shape: &'a egui::Shape, out: &mut Vec<&'a egui::epaint::RectShape>) {
|
||||
match shape {
|
||||
egui::Shape::Vec(inner) => inner.iter().for_each(|s| rects(s, out)),
|
||||
egui::Shape::Rect(r) => out.push(r),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut h = app_harness(app_with(""));
|
||||
h.run_steps(2);
|
||||
let _ = h.state_mut().run("ui.dialog", json!({"name": "organizer"}));
|
||||
h.run_steps(4);
|
||||
let mut seen = Vec::new();
|
||||
h.output().shapes.iter().for_each(|c| rects(&c.shape, &mut seen));
|
||||
for label in ["Document", "Template"] {
|
||||
let list = h.get_by_role_and_label(Role::List, label).rect();
|
||||
assert!(
|
||||
seen.iter().any(|r| r.stroke.width > 0.0
|
||||
&& r.stroke.color.a() > 0
|
||||
&& r.rect.contains_rect(list.shrink(2.0))
|
||||
&& list.expand(2.0).contains_rect(r.rect)),
|
||||
"the list {label:?} {list:?} has a border"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user