mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-01 18:24:54 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af6da289d1 | ||
|
|
f644f93a4b |
@@ -716,8 +716,17 @@ fn show_reading(app: &mut WordApp, ui: &mut Ui, layout: &Arc<DocLayout>, full: R
|
||||
let mut state = std::mem::take(&mut app.canvas.reading);
|
||||
let source = source::WordPages::new(app, layout, false, None);
|
||||
let out = ReadingView::new(tl!("Document"), &source, &mut state, paper).zoom(zoom).show_at(ui, columns);
|
||||
// The page-turn row sits at the bottom right, as in v0.19.1 (the far end, so the bottom left in a right-to-left UI),
|
||||
// the page view's side margin in from the edge.
|
||||
let margin = Env::of(ui).tokens.space.inset.page_side;
|
||||
let footer = footer.shrink2(egui::vec2(margin, 0.0));
|
||||
let far = if crate::i18n::current().rtl() {
|
||||
egui::Layout::left_to_right(egui::Align::Center)
|
||||
} else {
|
||||
egui::Layout::right_to_left(egui::Align::Center)
|
||||
};
|
||||
let nav = ui
|
||||
.scope_builder(egui::UiBuilder::new().max_rect(footer), |ui| {
|
||||
.scope_builder(egui::UiBuilder::new().max_rect(footer).layout(far), |ui| {
|
||||
ReadingNav::new(&mut state, layout.pages.len()).position(true).action(tl!("Edit Document")).show(ui)
|
||||
})
|
||||
.inner;
|
||||
|
||||
@@ -56,7 +56,7 @@ impl Component for ReadingProbe {
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> egui::Vec2 {
|
||||
let measure = ReadingMeasure::of(TypeRole::Body, |run| cx.layout(run));
|
||||
self.measured = Some(ReadingMetrics::new(cx.style.column_gap, cx.style.nav_bar, measure));
|
||||
self.measured = Some(metrics(cx.style.column_gap, cx.style.nav_bar, measure));
|
||||
egui::Vec2::ZERO
|
||||
}
|
||||
|
||||
@@ -69,6 +69,19 @@ impl Component for ReadingProbe {
|
||||
}
|
||||
}
|
||||
|
||||
/// Column widths Read Mode has always used (about one column per 295 screen points, none under 192), as v0.19.1 laid out 4
|
||||
/// columns at 1440 screen points. craft's body-text measure (66 characters) gives 2 there, so only the gaps come from it.
|
||||
const COLUMN_TARGET: f32 = 295.0;
|
||||
const COLUMN_MIN: f32 = 192.0;
|
||||
|
||||
fn metrics(column_gap: f32, nav_bar: f32, measure: ReadingMeasure) -> ReadingMetrics {
|
||||
let mut m = ReadingMetrics::new(column_gap, nav_bar, measure);
|
||||
// lean: fixed px, not the text size; follow the body text again if Read Mode should scale with it.
|
||||
m.column_target = COLUMN_TARGET;
|
||||
m.column_min = COLUMN_MIN;
|
||||
m
|
||||
}
|
||||
|
||||
/// The reading metrics under the current theme, or `None` when the probe did not measure this pass.
|
||||
pub(super) fn read(ui: &mut Ui) -> Option<ReadingMetrics> {
|
||||
ReadingProbe { common: Common::new(), measured: None }.label("Reading metrics").show(ui).metrics
|
||||
|
||||
@@ -253,6 +253,48 @@ fn drawing_over_the_pages_follows_the_read_mode_page_color() {
|
||||
assert_eq!(blank(&a), Rgb::WHITE, "the paper under the raster, not the page color");
|
||||
}
|
||||
|
||||
/// #8019: at 1440 x 900 Read Mode lays out several narrow columns (v0.19.1: 4), not the two the body
|
||||
/// text's measure gave after the port.
|
||||
#[test]
|
||||
fn read_mode_lays_out_several_columns_at_1440() {
|
||||
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Hello WordCraft")), Default::default());
|
||||
a.run("view.readMode", json!({})).unwrap();
|
||||
let mut h = crate::test_harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
|
||||
|ui, app: &mut WordApp| {
|
||||
let ctx = ui.ctx().clone();
|
||||
app.logic(&ctx);
|
||||
app.ui(ui);
|
||||
},
|
||||
a,
|
||||
);
|
||||
h.run_steps(4);
|
||||
let px = ReadingView::screen_scale(1.0) * h.state().session.view.read_width;
|
||||
assert!(px > 0.0 && 4.0 * px < 1440.0, "columns {px} px wide: four do not fit");
|
||||
}
|
||||
|
||||
/// #8019: the page-turn row (position and Edit Document) sits in the bottom-right quadrant, as in v0.19.1,
|
||||
/// not at the bottom left where craft's row starts.
|
||||
#[test]
|
||||
fn read_mode_pager_is_at_the_bottom_right() {
|
||||
use egui_kittest::kittest::Queryable;
|
||||
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Hello WordCraft")), Default::default());
|
||||
a.run("view.readMode", json!({})).unwrap();
|
||||
let mut h = crate::test_harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
|
||||
|ui, app: &mut WordApp| {
|
||||
let ctx = ui.ctx().clone();
|
||||
app.logic(&ctx);
|
||||
app.ui(ui);
|
||||
},
|
||||
a,
|
||||
);
|
||||
h.run_steps(4);
|
||||
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1440.0, 900.0));
|
||||
for label in ["Edit Document", "1 / 1"] {
|
||||
let r = h.get_all_by_label(label).last().unwrap().rect();
|
||||
assert!(r.center().x > screen.center().x && r.center().y > screen.center().y, "{label} at {r:?}: not bottom right");
|
||||
}
|
||||
}
|
||||
|
||||
/// #299: the caret's colour is worked out only when the caret is drawn: with text selected
|
||||
/// there is no caret, so the page's draw list isn't built for it.
|
||||
#[test]
|
||||
|
||||
@@ -3,21 +3,18 @@
|
||||
//! `styles.copyFrom`, `styles.delete`, `styles.rename`), so agents get the same result without
|
||||
//! the dialogs.
|
||||
|
||||
use craft::behavior::{ColumnWidth, EditEvent, Selection, SelectionChange, item_source::keyed};
|
||||
use craft::behavior::{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 _, request_rename};
|
||||
use craft::ui::contract::{Bind, CollectionEvent, Props as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _};
|
||||
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::CellAlign;
|
||||
use craft::ui::layout_components::{Grid, Stack};
|
||||
use craft::ui::layout_components::Row;
|
||||
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};
|
||||
@@ -27,15 +24,6 @@ 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)]
|
||||
@@ -248,69 +236,50 @@ impl OrganizerDialog {
|
||||
|
||||
fn body(&mut self, ui: &mut Ui) {
|
||||
let reason = browse_reason(ui);
|
||||
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();
|
||||
}
|
||||
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();
|
||||
}
|
||||
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 });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
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);
|
||||
}
|
||||
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, _| {
|
||||
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("¶"))
|
||||
@@ -322,13 +291,12 @@ 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 units = crate::options::lengths(app.session.units());
|
||||
let kind = crate::options::unit_kind(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, units.shown.step(), 2).bounds(Some(0.0), Some(144.0)).length(units))
|
||||
.map(|(key, label)| Param::number(*key, tl!(label).to_string(), 0.0, 1.0, 2).bounds(Some(0.0), Some(144.0)).unit(kind))
|
||||
.collect();
|
||||
ParamDialog::command("tableCellMargins", tl!("Cell Margins").to_string(), params).values(Bind::bound(fields)).alt_resets(false)
|
||||
});
|
||||
|
||||
@@ -2283,109 +2283,3 @@ 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