draw: a running TransformFields gesture reads its values against the start box it began with, so a host that moves the start box with the gesture's own values does not cancel it as an outside change

This commit is contained in:
bflatastic
2026-11-23 17:28:06 +07:00
parent deb9ad9f07
commit a7f40c4474
2 changed files with 36 additions and 8 deletions
@@ -156,14 +156,15 @@ impl TransformRow {
}
}
/// What each field's running gesture began from, so every value it reports is made from that row.
/// What each field's running gesture began from, so every value it reports is made from that row and is read against
/// the start box of that moment: the row and the start box.
#[derive(Clone, Debug, Default)]
struct Bases([Option<(GestureId, TransformRow)>; 7]);
struct Bases([Option<(GestureId, TransformRow, Rect)>; 7]);
impl Bases {
/// The row the gesture `id` of `field` began from; `fallback` when it has no record.
fn origin(&self, field: TransformField, id: GestureId, fallback: &TransformRow) -> TransformRow {
self.0[field as usize].filter(|(of, _)| *of == id).map_or(*fallback, |(_, row)| row)
self.0[field as usize].filter(|(of, ..)| *of == id).map_or(*fallback, |(_, row, _)| row)
}
}
@@ -526,12 +527,15 @@ impl Cells<'_> {
fn field(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>, pass: &mut Pass, field: TransformField, form: Form) {
let (side, name) = self.side_and_name(field, form);
let sizing = form.sizing();
let conversion = &pass.conversion;
// A running gesture of this field is shown the value it began from, read against the start box it began with:
// the host's write-back of the gesture's own values, or a start box it moves with them, must not look like the
// app changing the field under it.
let running = pass.bases.0[field as usize];
let began = running.map(|(.., rect)| Conversion { rect, ..pass.conversion.clone() });
let conversion = began.as_ref().unwrap_or(&pass.conversion);
let (kind, decimals) = conversion.format(field);
let bound = conversion.bound(field);
// A running gesture of this field is shown the value it began from: the host's write-back of the gesture's own
// values must not look like the app changing the field under it.
let basis = pass.bases.0[field as usize].map_or(pass.before, |(_, row)| row);
let basis = running.map_or(pass.before, |(_, row, _)| row);
let current = conversion.shown(&basis.state, basis.skew_v, field);
let codec = conversion.codec(&self.units, field);
let unit = Label::from(self.units.symbol(kind));
@@ -806,7 +810,7 @@ fn lift(
let slot = field as usize;
match event {
EditEvent::Begin { id, label, doc_key, .. } => {
bases.0[slot] = Some((id, *before));
bases.0[slot] = Some((id, *before, conversion.rect));
Some(EditEvent::begin(id, Mixed::Same(*before), label, doc_key))
}
EditEvent::Update { id, value: Some(value), .. } => {
+24
View File
@@ -262,3 +262,27 @@ fn typing_50_into_w_of_a_distorted_box_equals_typing_50_once_from_the_start_box(
let (a, b) = (once.row(), by_digit.row());
assert!(a.state.quad.iter().zip(&b.state.quad).all(|(p, q)| p.distance(*q) < 1e-6), "{a:?} {b:?}");
}
/// A host that passes the box as it is now as the start box, so the start box moves with a scrub's own values, keeps
/// the scrub running to its release: the field reads it against the start box it began with. Guards against a field
/// that read the running scrub's value against each pass's start box, so the host's write-back looked like an outside
/// change and cancelled it (`HostChanged`).
#[trial]
fn a_start_box_that_moves_with_a_scrub_keeps_the_scrub_running() {
let host = Rc::new(RefCell::new((TransformRow::new(BoxState::from_rect(START), 0.0), Vec::new())));
let shown = Rc::clone(&host);
let mut linked = true;
let mut fixture = Fixture::new(move |ui| {
let (row, events) = &mut *shown.borrow_mut();
let start = Rect::from_points(row.state.quad[0], row.state.quad[2]);
events.extend(TransformFields::new("Transform", Bind::bound(row), start, Bind::bound(&mut linked)).show(ui).events);
});
fixture.record_paint().settle();
let glyph = fixture.texts().iter().find(|painted| painted.text == "W").map(|painted| painted.rect.center()).expect("W's caption shows");
fixture.drag_at(glyph, egui::vec2(40.0, 0.0));
let (row, events) = &*host.borrow();
assert!(events.iter().any(|event| matches!(event, EditEvent::Update { .. })), "the scrub ran: {events:?}");
assert!(!events.iter().any(|event| matches!(event, EditEvent::Cancel { .. })), "the scrub was not cancelled: {events:?}");
assert!(events.iter().any(|event| matches!(event, EditEvent::Commit { .. })), "the release committed it: {events:?}");
assert!(row.state.quad[0].distance(row.state.quad[1]) > START.width(), "the scrub widened the box: {row:?}");
}