mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-24 19:41:51 +01:00
Framework fwb8-c: port follow-ups (see runs/fwb8-c.md)
This commit is contained in:
8 files changed
+90
-14
No files matched your search
@@ -1,6 +1,6 @@
|
||||
//! `EditHistory<T>`: the undo and redo of a dialog, a tool session or the keymap editor, fed by edit events.
|
||||
|
||||
use crate::value::{EditEvent, GestureId, Value};
|
||||
use crate::value::{EditEvent, GestureId, Mixed, Value};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Most steps a history keeps.
|
||||
@@ -47,14 +47,15 @@ impl<T: Value> EditHistory<T> {
|
||||
EditHistory { undo: Vec::new(), redo: Vec::new(), settled: now.clone(), open: None }
|
||||
}
|
||||
|
||||
/// Takes in one event. Events of a gesture other than the open one are ignored.
|
||||
/// Takes in one event. Events of a gesture other than the open one are ignored. A concrete
|
||||
/// before value captures changes made outside history since the preceding edit.
|
||||
pub fn record(&mut self, event: &EditEvent<T>) {
|
||||
match event {
|
||||
EditEvent::Begin { id, label, .. } => self.open = Some(Open { id: *id, name: label.clone() }),
|
||||
EditEvent::Update { .. } => {}
|
||||
EditEvent::Commit { id, after, .. } => {
|
||||
EditEvent::Commit { id, before, after } => {
|
||||
if let Some(open) = self.open.take_if(|open| open.id == *id) {
|
||||
self.push(after, open.name);
|
||||
self.push(before, after, open.name);
|
||||
}
|
||||
}
|
||||
EditEvent::Cancel { id, .. } => {
|
||||
@@ -62,9 +63,9 @@ impl<T: Value> EditHistory<T> {
|
||||
self.open = None;
|
||||
}
|
||||
}
|
||||
EditEvent::Set { after, label, .. } => {
|
||||
EditEvent::Set { before, after, label, .. } => {
|
||||
self.open = None;
|
||||
self.push(after, label.clone());
|
||||
self.push(before, after, label.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,14 +75,16 @@ impl<T: Value> EditHistory<T> {
|
||||
self.open.is_some()
|
||||
}
|
||||
|
||||
fn push(&mut self, after: &T, name: Arc<str>) {
|
||||
if *after == self.settled {
|
||||
fn push(&mut self, before: &Mixed<T>, after: &T, name: Arc<str>) {
|
||||
let before = before.same().unwrap_or(&self.settled).clone();
|
||||
self.settled = after.clone();
|
||||
if *after == before {
|
||||
return;
|
||||
}
|
||||
if self.undo.len() >= LIMIT {
|
||||
self.undo.remove(0);
|
||||
}
|
||||
self.undo.push(Entry { value: std::mem::replace(&mut self.settled, after.clone()), name });
|
||||
self.undo.push(Entry { value: before, name });
|
||||
self.redo.clear();
|
||||
}
|
||||
|
||||
@@ -133,6 +136,17 @@ mod tests {
|
||||
EditEvent::set(Mixed::Same(before), after, "Fill".into(), None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undo_restores_selection_changed_between_edits() {
|
||||
let mut history = EditHistory::new(&(0, 0));
|
||||
history.record(&EditEvent::set(Mixed::Same((0, 1)), (1, 0), "Remove".into(), None));
|
||||
let mut now = (1, 0);
|
||||
assert!(history.step(&mut now, Step::Undo));
|
||||
assert_eq!(now, (0, 1));
|
||||
assert!(history.step(&mut now, Step::Redo));
|
||||
assert_eq!(now, (1, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_take_back_returns_the_earlier_value_keeps_redo_and_settles() {
|
||||
let mut history = EditHistory::new(&0);
|
||||
|
||||
@@ -60,6 +60,7 @@ struct Config {
|
||||
/// What the host keeps of the box and what the passes reported.
|
||||
struct Host {
|
||||
state: BoxState,
|
||||
shown: Option<BoxState>,
|
||||
before: Option<BoxState>,
|
||||
events: Vec<Seen>,
|
||||
applied: usize,
|
||||
@@ -116,6 +117,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
|
||||
let kept_config = Rc::clone(&shown_config);
|
||||
let host = Rc::new(RefCell::new(Host {
|
||||
state,
|
||||
shown: None,
|
||||
before: None,
|
||||
events: Vec::new(),
|
||||
applied: 0,
|
||||
@@ -187,6 +189,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
|
||||
});
|
||||
if let Some((output, guide_edits)) = canvas.inner {
|
||||
let mut host = shown_host.borrow_mut();
|
||||
host.shown = output.edit.shown.as_ref().and_then(|value| value.same()).map(|value| **value);
|
||||
host.guide_events += guide_edits;
|
||||
host.snapped = output.snapped.len();
|
||||
host.id = output.edit.response.id;
|
||||
@@ -471,6 +474,22 @@ fn a_text_frame_fed_back_through_an_f32_host_follows_every_move_to_the_commit()
|
||||
assert!((h.state().quad[2].y - 280.0).abs() < 0.01 && events.last() == Some(&Seen::Commit), "the commit holds the last move");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn transform_box_shows_each_drag_proposal_in_the_same_frame() {
|
||||
let mut h = upright();
|
||||
let start = h.screen(Point::new(300.0, 220.0));
|
||||
frame(&mut h, vec![pointer::moved(start)]);
|
||||
frame(&mut h, vec![pointer::press(start)]);
|
||||
let mut previous = h.state();
|
||||
for delta in [Vec2::new(30.0, 21.0), Vec2::new(60.0, 42.0)] {
|
||||
frame(&mut h, vec![pointer::moved(start + delta)]);
|
||||
let host = h.host.borrow();
|
||||
assert_ne!(host.state, previous, "the drag changed the box");
|
||||
assert_eq!(host.shown, Some(host.state), "the reported preview follows this frame's edit");
|
||||
previous = host.state;
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_scale_drag_reads_its_size_in_the_ruler_unit_and_percent_only_in_percent_rulers() {
|
||||
// A corner drag from 200 x 140 to 300 x 210 document units; the box follows the pointer.
|
||||
|
||||
@@ -16,7 +16,7 @@ use craft_font_catalog::Catalog;
|
||||
use craft_host::Section;
|
||||
use craft_ui::buttons::IconButton;
|
||||
use craft_ui::contract::{
|
||||
Bind, Common, Component, Cx, Edit as _, EditResponse, Focus as _, Input, MeasureCx, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx,
|
||||
Access, Bind, Common, Component, Cx, Edit as _, EditResponse, Focus as _, Input, MeasureCx, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx,
|
||||
Pointer as _, Props as _, Remember as _, Role, WordSetCx as _,
|
||||
};
|
||||
use craft_ui::fonts::FontRef;
|
||||
@@ -44,6 +44,8 @@ craft_ui::words! {
|
||||
clear_all: "fontPicker.clearAll" = "Clear All";
|
||||
sample: "fontPicker.sample" = "Sample";
|
||||
no_match: "fontPicker.noMatch" = "No fonts match";
|
||||
/// The family field's label when the selection has several families.
|
||||
mixed: "fontPicker.mixed" = "Mixed";
|
||||
/// The name of a row's star, with `{row}` for the row's label.
|
||||
favorite: template "fontPicker.favorite" = "Favorite {row}";
|
||||
list: "fontPicker.list" = "Fonts";
|
||||
@@ -646,6 +648,10 @@ impl Component for FontPicker<'_> {
|
||||
let family = cx.value().cloned().unwrap_or_default();
|
||||
let mut options = TextOptions::new();
|
||||
options.select_on_focus = true;
|
||||
options.mixed = cx.value().is_none();
|
||||
if options.mixed {
|
||||
options.placeholder = Some(cx.words::<FontPickerWords>().mixed.clone());
|
||||
}
|
||||
cx.text(text_key(), placed.text, &codec, &family, &options);
|
||||
}
|
||||
|
||||
@@ -653,6 +659,12 @@ impl Component for FontPicker<'_> {
|
||||
Some(self.family.reborrow())
|
||||
}
|
||||
|
||||
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
|
||||
if cx.value().is_none() && cx.text(&text_key()).is_none_or(|text| text.draft.as_deref().is_none_or(str::is_empty)) {
|
||||
access.value(cx.words::<FontPickerWords>().mixed.as_str());
|
||||
}
|
||||
}
|
||||
|
||||
/// A changed draft filters the list: it opens on the matches, and closes when the draft ended (committed,
|
||||
/// cancelled or replaced by a pick). A preview that the list closed on without a pick is cancelled. While the
|
||||
/// list is closed, the arrows and the wheel step through the host rows and the families.
|
||||
|
||||
@@ -89,6 +89,20 @@ fn present(fx: &Fixture, text: &str) -> bool {
|
||||
fx.by_label(text).is_some() || fx.texts().iter().any(|painted| painted.text == text)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn mixed_family_is_named_until_a_font_is_chosen() {
|
||||
let catalog = catalog_of(&[INTER_REGULAR]);
|
||||
let mut family = craft_behavior::Mixed::<String>::Mixed;
|
||||
let mut fx = Fixture::new(move |ui| {
|
||||
FontPicker::new("Font family", Bind::bound_mixed(&mut family), &catalog).show(ui);
|
||||
});
|
||||
fx.in_room(Room::Window);
|
||||
fx.settle();
|
||||
let field = fx.by_role(craft_ui::runtime::access::Role::COMBO_BOX).into_iter().next().expect("the family field");
|
||||
let label = fx.tree().value_of(field.node).filter(|value| !value.is_empty()).expect("the mixed family has an accessible label");
|
||||
assert!(present(&fx, label), "the accessible mixed label is also visible");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_style_row_under_an_expanded_family_picks_family_and_style_once() {
|
||||
let (mut fx, seen) = rig(catalog_of(&[INTER_REGULAR, INTER_SEMIBOLD]), vec![]);
|
||||
|
||||
@@ -618,7 +618,7 @@ impl Component for CropOverlay<'_> {
|
||||
let (lu, lv) = ((q[1] - q[0]).length(), (q[3] - q[0]).length());
|
||||
// A small frame keeps a movable middle: the reach inside is at most a quarter of the shortest side.
|
||||
let reach = cx.style.reach.min(lu.min(lv) / 4.0);
|
||||
let body = if self.default_frame { CursorIcon::Crosshair } else { CursorIcon::Move };
|
||||
let body = if self.default_frame { CursorIcon::Crosshair } else { CursorIcon::Grab };
|
||||
cx.part(FRAME.at(BoxPart::Inside.index()), HitShape::Polygon(q.to_vec()))
|
||||
.sense(Sense::DRAG | Sense::CLICK | Sense::CONTEXT_CLICK | Sense::TAKE_FOCUS)
|
||||
.priority(HitPriority::BODY)
|
||||
@@ -757,6 +757,9 @@ impl Component for CropOverlay<'_> {
|
||||
.find(|part| cx.hover_pos(craft_behavior::PartRef { key: FRAME, index: Some(part.index()) }).is_some())
|
||||
.or_else(|| [TURN, OUTSIDE].into_iter().find_map(|key| cx.hover_pos(key)).map(|_| BoxPart::Outside));
|
||||
self.run_keys(cx, &space, &frame, live.drag.is_some());
|
||||
if live.drag.as_ref().is_some_and(|drag| matches!(drag.kind, Kind::Move)) {
|
||||
cx.cursor(CursorIcon::Grabbing, CursorPriority::Drag);
|
||||
}
|
||||
self.report.scrolls_at = live.drag.as_ref().and_then(|drag| drag.scrolls_at(self.image_mode, self.auto_center));
|
||||
*cx.memory::<Live>() = live;
|
||||
}
|
||||
|
||||
@@ -117,6 +117,19 @@ struct Rig {
|
||||
rerun: Rc<Cell<bool>>,
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn crop_body_shows_grab_and_grabbing_during_move() {
|
||||
use craft_behavior::{Cursor, CursorIcon};
|
||||
let mut rig = Rig::upright();
|
||||
let at = rig.at(200.0, 150.0);
|
||||
rig.move_to(at);
|
||||
assert_eq!(craft_ui::runtime::cursor::current(&rig.frames.ctx), Some(Cursor::System(CursorIcon::Grab)));
|
||||
rig.press(at);
|
||||
rig.move_to(at + Vec2::new(30.0, 0.0));
|
||||
assert_eq!(craft_ui::runtime::cursor::current(&rig.frames.ctx), Some(Cursor::System(CursorIcon::Grabbing)));
|
||||
rig.release(at + Vec2::new(30.0, 0.0));
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
fn new(crop: CropBox, setup: Setup) -> Self {
|
||||
let (crop, setup, seen) = (Rc::new(RefCell::new(crop)), Rc::new(RefCell::new(setup)), Rc::new(RefCell::new(Seen::default())));
|
||||
|
||||
@@ -176,7 +176,7 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>
|
||||
keys::register_node(ctx, id, folded.inner, None, focusable);
|
||||
}
|
||||
if let Some(facts) = core.text_access.take() {
|
||||
crate::runtime::access::text::report_text(ctx, id, &facts);
|
||||
crate::runtime::access::text::report_text(ctx, id, &facts, access.value.as_deref());
|
||||
}
|
||||
if core.member_of.is_some() && flags.contains(Flags::FOCUSED) {
|
||||
access::set_focus(ctx, id);
|
||||
|
||||
@@ -88,7 +88,7 @@ pub(crate) fn exposed(ctx: &Context, widget: Id) -> bool {
|
||||
/// the widget's own node folds into it, so a component whose root edits its value through the text (a text field, a
|
||||
/// spin button, an editable combo box) is one node that takes focus and the text actions. Call it after the node's
|
||||
/// common facts.
|
||||
pub(crate) fn report_text(ctx: &Context, owner: Id, facts: &TextFacts) {
|
||||
pub(crate) fn report_text(ctx: &Context, owner: Id, facts: &TextFacts, value_override: Option<&str>) {
|
||||
adapter::fold_widget(ctx, facts.widget, owner);
|
||||
let mut entries = Vec::new();
|
||||
if !facts.masked {
|
||||
@@ -111,7 +111,8 @@ pub(crate) fn report_text(ctx: &Context, owner: Id, facts: &TextFacts) {
|
||||
let (focus_node, focus_at) = end_of(&entries, owner, focus)?;
|
||||
Some((anchor_node, anchor_at, focus_node, focus_at))
|
||||
});
|
||||
let value = (!facts.masked).then_some(facts.text.as_str());
|
||||
// An empty editor can name a mixed value without turning its label into editable text.
|
||||
let value = (!facts.masked).then_some(if facts.text.is_empty() { value_override.unwrap_or(&facts.text) } else { &facts.text });
|
||||
adapter::write_text(ctx, owner, &TextNode { value, editable: facts.editable, selection });
|
||||
Service::<Tables>::run(ctx, |tables| tables.0.insert(owner, entries));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user