mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-18 11:54:38 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0360a71277 | ||
|
|
395a0c9895 |
+3
-1
File diff suppressed because one or more lines are too long
@@ -154,18 +154,14 @@ pub(super) fn layout(app: &mut WordApp, ui: &mut Ui) {
|
||||
t.row(|r| {
|
||||
r.cell(|ui| ui.label(egui::RichText::new(tl!("Left:")).small()));
|
||||
r.cell(|ui| {
|
||||
if ui
|
||||
.add_sized(FIELD, egui::DragValue::new(&mut l).speed(3.6 / k).range(-792.0 / k..=1584.0 / k).measure(u).max_decimals(2))
|
||||
.changed()
|
||||
{
|
||||
let _ = app.run("para.indents", json!({"left": l * k}));
|
||||
}
|
||||
let resp =
|
||||
ui.add_sized(FIELD, egui::DragValue::new(&mut l).speed(3.6 / k).range(-792.0 / k..=1584.0 / k).measure(u).max_decimals(2));
|
||||
apply_box(app, ui, &resp, "para.indents", json!({"left": l * k}));
|
||||
});
|
||||
r.cell(|ui| spaced(ui, egui::RichText::new(tl!("Before:")).small()));
|
||||
r.cell(|ui| {
|
||||
if ui.add_sized(FIELD, egui::DragValue::new(&mut b).speed(1.0).range(0.0..=1584.0).suffix(format!(" {}", tl!("pt")))).changed() {
|
||||
let _ = app.run("para.spacing", json!({"before": b}));
|
||||
}
|
||||
let resp = ui.add_sized(FIELD, egui::DragValue::new(&mut b).speed(1.0).range(0.0..=1584.0).suffix(format!(" {}", tl!("pt"))));
|
||||
apply_box(app, ui, &resp, "para.spacing", json!({"before": b}));
|
||||
});
|
||||
});
|
||||
let mut right = rp.indent_right / k;
|
||||
@@ -173,18 +169,14 @@ pub(super) fn layout(app: &mut WordApp, ui: &mut Ui) {
|
||||
t.row(|r| {
|
||||
r.cell(|ui| ui.label(egui::RichText::new(tl!("Right:")).small()));
|
||||
r.cell(|ui| {
|
||||
if ui
|
||||
.add_sized(FIELD, egui::DragValue::new(&mut right).speed(3.6 / k).range(-792.0 / k..=1584.0 / k).measure(u).max_decimals(2))
|
||||
.changed()
|
||||
{
|
||||
let _ = app.run("para.indents", json!({"right": right * k}));
|
||||
}
|
||||
let resp = ui
|
||||
.add_sized(FIELD, egui::DragValue::new(&mut right).speed(3.6 / k).range(-792.0 / k..=1584.0 / k).measure(u).max_decimals(2));
|
||||
apply_box(app, ui, &resp, "para.indents", json!({"right": right * k}));
|
||||
});
|
||||
r.cell(|ui| spaced(ui, egui::RichText::new(tl!("After:")).small()));
|
||||
r.cell(|ui| {
|
||||
if ui.add_sized(FIELD, egui::DragValue::new(&mut a).speed(1.0).range(0.0..=1584.0).suffix(format!(" {}", tl!("pt")))).changed() {
|
||||
let _ = app.run("para.spacing", json!({"after": a}));
|
||||
}
|
||||
let resp = ui.add_sized(FIELD, egui::DragValue::new(&mut a).speed(1.0).range(0.0..=1584.0).suffix(format!(" {}", tl!("pt"))));
|
||||
apply_box(app, ui, &resp, "para.spacing", json!({"after": a}));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -225,3 +217,35 @@ pub(super) fn layout(app: &mut WordApp, ui: &mut Ui) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Apply the new value of a Layout › Paragraph box (Indent Left/Right, Spacing Before/After)
|
||||
/// with `cmd` and `params`. A drag changes it frame by frame: every change after the drag's
|
||||
/// first joins the undo step before it, so one Undo takes the whole drag back (#5171), not 0.1 cm
|
||||
/// of it. The first change starts its own step, whatever frame it comes on. A change joins only
|
||||
/// while the document stands where the drag's last change left it: the undo mark and the
|
||||
/// revision both match. An edit that came in between (a control-channel command, say) keeps its
|
||||
/// own step, and the drag goes on in a new one. The revision is what catches an Undo followed by
|
||||
/// an edit: the undo mark comes back to the same number, but each of them bumps the revision.
|
||||
fn apply_box(app: &mut WordApp, ui: &Ui, r: &egui::Response, cmd: &str, params: serde_json::Value) {
|
||||
let applied = r.id.with("drag_applied");
|
||||
if !r.dragged() {
|
||||
ui.data_mut(|d| d.remove::<(u64, u64)>(applied));
|
||||
}
|
||||
if !r.changed() {
|
||||
return;
|
||||
}
|
||||
let drag = r.dragged();
|
||||
let before = app.session.undo_mark();
|
||||
let join = drag && ui.data(|d| d.get_temp::<(u64, u64)>(applied)) == Some((before, app.session.rev()));
|
||||
if join {
|
||||
app.session.join_next_undo();
|
||||
}
|
||||
let _ = app.run(cmd, params);
|
||||
let after = app.session.undo_mark();
|
||||
// The newest step is the drag's when this change joined it or made it; a change that made
|
||||
// none leaves the next one to start the drag's step.
|
||||
if drag && (join || after != before) {
|
||||
let rev = app.session.rev();
|
||||
ui.data_mut(|d| d.insert_temp(applied, (after, rev)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1092,3 +1092,99 @@ fn shape_and_chart_formats_leave_out_of_range_sizes_alone() {
|
||||
assert_eq!(before.0, Some((20.0, 2500.0)));
|
||||
assert_eq!(shown(a), before, "Chart Design shown, the chart untouched");
|
||||
}
|
||||
|
||||
type LayoutH = egui_kittest::Harness<'static, WordApp>;
|
||||
|
||||
/// A window on the Layout tab with "Indented" typed, and the Left indent box drag, 60 points
|
||||
/// right in ten moves: `between(k)` runs before the k-th move (an edit coming in mid-drag).
|
||||
fn drag_left_indent(between: impl Fn(&mut LayoutH, usize)) -> (LayoutH, usize) {
|
||||
use crate::table_resize::tests::{button, steps};
|
||||
use egui_kittest::kittest::Queryable;
|
||||
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::new()), Default::default());
|
||||
a.run("text.insert", json!({"text": "Indented"})).unwrap();
|
||||
a.ui.tab = "Layout".into();
|
||||
let mut h = egui_kittest::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,
|
||||
);
|
||||
steps(&mut h, 4);
|
||||
let depth = h.state().session.undo_depth();
|
||||
let label = h.get_by_label("Left:").rect();
|
||||
// The box is the cell right of the label.
|
||||
let from = egui::pos2(label.right() + 30.0, label.center().y);
|
||||
h.event(egui::Event::PointerMoved(from));
|
||||
h.step();
|
||||
button(&h, from, true);
|
||||
h.step();
|
||||
for k in 1..=10 {
|
||||
between(&mut h, k);
|
||||
h.event(egui::Event::PointerMoved(from + egui::vec2(6.0 * k as f32, 0.0)));
|
||||
h.step();
|
||||
}
|
||||
button(&h, from + egui::vec2(60.0, 0.0), false);
|
||||
steps(&mut h, 3);
|
||||
(h, depth)
|
||||
}
|
||||
|
||||
fn left_indent(h: &LayoutH) -> f32 {
|
||||
let s = &h.state().session;
|
||||
s.doc.para_at(&s.sel.focus).map(|p| s.doc.styles.resolve_para(&p.props).indent_left).unwrap()
|
||||
}
|
||||
|
||||
/// #5171: dragging Layout › Paragraph › Indent Left changes the indent as it goes, in one undo
|
||||
/// step: one Undo takes the whole drag back, not 0.1 cm of it. The text typed before it keeps
|
||||
/// its own step.
|
||||
#[test]
|
||||
fn dragging_a_layout_indent_box_is_one_undo_step() {
|
||||
let (mut h, depth) = drag_left_indent(|_, _| {});
|
||||
let dragged = left_indent(&h);
|
||||
assert!(dragged > 20.0, "the drag indents the paragraph: {dragged}");
|
||||
assert_eq!(h.state().session.undo_depth(), depth + 1, "one undo step for the drag");
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(left_indent(&h), 0.0, "one Undo takes the whole drag back");
|
||||
assert!(h.state().session.doc.plain_text(wordcraft_doc::StoryRef::Body).starts_with("Indented"), "the typing keeps its own step");
|
||||
}
|
||||
|
||||
/// #5171 review: an edit that comes in during the drag (a control-channel command) keeps its own
|
||||
/// undo step: the drag's changes after it make a step of their own instead of joining it.
|
||||
#[test]
|
||||
fn an_edit_in_the_middle_of_a_box_drag_keeps_its_own_undo_step() {
|
||||
let (mut h, depth) = drag_left_indent(|h, k| {
|
||||
if k == 5 {
|
||||
h.state_mut().run("text.insert", json!({"text": " now"})).unwrap();
|
||||
}
|
||||
});
|
||||
let text = |h: &LayoutH| h.state().session.doc.plain_text(wordcraft_doc::StoryRef::Body);
|
||||
assert!(text(&h).contains(" now"), "{}", text(&h));
|
||||
assert_eq!(h.state().session.undo_depth(), depth + 3, "the drag before, the edit, the drag after");
|
||||
let full = left_indent(&h);
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
let mid = left_indent(&h);
|
||||
assert!(mid > 0.0 && mid < full, "one Undo takes back the drag after the edit: {mid} of {full}");
|
||||
assert!(text(&h).contains(" now"), "and leaves the edit");
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
assert!(!text(&h).contains(" now") && left_indent(&h) == mid, "the edit is its own step");
|
||||
}
|
||||
|
||||
/// #5171 review: an Undo and then an edit in the middle of a drag bring the undo mark back to
|
||||
/// where the drag left it. The drag's next change still doesn't join that edit: one Undo takes
|
||||
/// back the rest of the drag and leaves the edit.
|
||||
#[test]
|
||||
fn an_undo_then_an_edit_in_the_middle_of_a_box_drag_keeps_the_edit_apart() {
|
||||
let (mut h, _) = drag_left_indent(|h, k| {
|
||||
if k == 5 {
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
h.state_mut().run("text.insert", json!({"text": " now"})).unwrap();
|
||||
}
|
||||
});
|
||||
let text = |h: &LayoutH| h.state().session.doc.plain_text(wordcraft_doc::StoryRef::Body);
|
||||
assert!(text(&h).contains(" now"), "{}", text(&h));
|
||||
assert!(left_indent(&h) > 0.0, "the drag goes on after the edit");
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(left_indent(&h), 0.0, "one Undo takes back the drag after the edit");
|
||||
assert!(text(&h).contains(" now"), "and leaves the edit: {}", text(&h));
|
||||
}
|
||||
|
||||
+2
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user