mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-02 20:18:38 -05:00
Merge pull request #10509 from dotCooCoo/fix/drag-frame-ui-cost
Properties: a drag does not measure the panel's sections on each move
This commit is contained in:
2 files changed
+108
-3
No files matched your search
@@ -29,6 +29,10 @@ pub struct PropertiesUi {
|
||||
quick_actions: Option<(u64, Vec<QuickAction>)>,
|
||||
/// The [`content_key`] the sections were last shown for.
|
||||
content: Option<u64>,
|
||||
/// The document (uid, revision) the sections were last shown for.
|
||||
revision: Option<(u64, u64)>,
|
||||
/// An edit made during a drag whose sections are not measured yet.
|
||||
measure_pending: bool,
|
||||
}
|
||||
|
||||
/// The key of [`PropertiesUi::edited_profiled`]: (document uid, revision, active Appearance row).
|
||||
@@ -66,7 +70,16 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let guides = super::transform::selected_guides(app);
|
||||
let header = heading(first.as_deref(), n_sel, editing_artboards, guides.is_some());
|
||||
let content = content_key(app);
|
||||
let changed = app.ui.properties_panel.content.replace(content) != Some(content);
|
||||
let revision = app.session.active().map(|st| (st.uid, st.revision));
|
||||
let content_changed = app.ui.properties_panel.content.replace(content) != Some(content);
|
||||
let edited = std::mem::replace(&mut app.ui.properties_panel.revision, revision) != revision;
|
||||
// A drag on the canvas, or a scrubbed field, moves the revision on every pointer move without changing which
|
||||
// sections show: the sections are measured again for an edit once the drag ends, not on each move. The edit stays
|
||||
// pending until then, also when the release itself does not move the revision.
|
||||
let dragging = app.session.active().is_some_and(|st| st.interaction.is_some() || st.undo_group.is_some());
|
||||
let pending = app.ui.properties_panel.measure_pending || edited;
|
||||
let changed = content_changed || (pending && !dragging);
|
||||
app.ui.properties_panel.measure_pending = pending && !changed;
|
||||
let mut sections = |ui: &mut Ui, _: &mut BodyCx<'_>| {
|
||||
// The reference panel's labels end in a colon beside fields that keep their own width.
|
||||
Stack::new().fill_width().labels(LabelScheme::Form).show(ui, |ui, _| {
|
||||
@@ -87,19 +100,21 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let mut panel = Panel::new(header).titled().strength(HeaderStrength::Quiet).labels(LabelScheme::Inspector).pad(Pad::None);
|
||||
panel = panel.id_salt(egui::IdSalt::new(key));
|
||||
if changed {
|
||||
#[cfg(test)]
|
||||
SECTION_MEASURES.with(|n| n.set(n.get() + 1));
|
||||
let _ = panel.measure_in(ui, Proposal::Size(ui.available_size()), &mut sections);
|
||||
}
|
||||
panel.show(ui, sections);
|
||||
}
|
||||
|
||||
/// What the sections show: the document and its revision, the selection, the tool and isolation mode.
|
||||
/// What the sections show besides the document's revision: the document, the selection, the tool and isolation mode.
|
||||
fn content_key(app: &VectorcraftApp) -> u64 {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
app.session.tool_id().hash(&mut h);
|
||||
if let Some(st) = app.session.active() {
|
||||
let selection = &st.selection;
|
||||
(st.uid, st.revision, st.isolation, &selection.objects, selection.key, selection.target, &selection.slices).hash(&mut h);
|
||||
(st.uid, st.isolation, &selection.objects, selection.key, selection.target, &selection.slices).hash(&mut h);
|
||||
for (id, anchors) in &selection.anchors {
|
||||
(id, anchors.len()).hash(&mut h);
|
||||
}
|
||||
@@ -223,6 +238,8 @@ fn quick_actions_key(app: &VectorcraftApp) -> u64 {
|
||||
thread_local! {
|
||||
/// How many times this thread has listed the Quick Actions.
|
||||
pub(crate) static QUICK_ACTION_LISTS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
/// How many times this thread has measured the panel's sections before showing them.
|
||||
pub(crate) static SECTION_MEASURES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// [`quick_actions`], listed again only when [`quick_actions_key`] changed.
|
||||
|
||||
@@ -190,6 +190,94 @@ fn a_selection_change_draws_the_window_in_one_pass() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Dragging the selected object with the Selection tool, with the Properties panel docked, does not measure the
|
||||
/// panel's sections on each pointer move, and each frame of the drag takes one pass. The release measures them once
|
||||
/// for the moved object, and so does an edit made without a drag.
|
||||
#[test]
|
||||
fn a_drag_does_not_measure_the_sections_on_each_move() {
|
||||
let measures = || SECTION_MEASURES.with(std::cell::Cell::get);
|
||||
let mut app = new_app();
|
||||
let a = rect(&mut app, 100.0, 100.0, 80.0, 80.0);
|
||||
app.run("select.set", json!({"ids": [a]})).unwrap();
|
||||
app.run("window.panel", json!({"panel": "properties"})).unwrap();
|
||||
app.select_tool("selection");
|
||||
let ctx = crate::testing::ui_context();
|
||||
let mut time = 0.0;
|
||||
// One frame with `events` → (the passes it took, the section measures it made).
|
||||
let mut frame = |app: &mut VectorcraftApp, events: Vec<Event>| {
|
||||
time += 1.0 / 60.0;
|
||||
let raw = egui::RawInput {
|
||||
time: Some(time),
|
||||
screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 1000.0))),
|
||||
focused: true,
|
||||
events,
|
||||
..Default::default()
|
||||
};
|
||||
let (pass, measured) = (ctx.cumulative_pass_nr(), measures());
|
||||
let _ = crate::testing::app_frame(app, &ctx, raw);
|
||||
(ctx.cumulative_pass_nr() - pass, measures() - measured)
|
||||
};
|
||||
for _ in 0..4 {
|
||||
frame(&mut app, vec![]);
|
||||
}
|
||||
let x = |app: &VectorcraftApp| {
|
||||
let st = app.session.active().unwrap();
|
||||
st.selection.objects.first().and_then(|id| st.doc.node(*id)).and_then(|n| n.geometric_bounds()).unwrap().x0
|
||||
};
|
||||
let start_x = x(&app);
|
||||
let start = app.canvas_view().unwrap().to_screen(craft::behavior::kurbo::Point::new(140.0, 140.0));
|
||||
let button = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
|
||||
frame(&mut app, vec![Event::PointerMoved(start)]);
|
||||
assert_eq!(frame(&mut app, vec![button(start, true)]), (1, 0), "the press");
|
||||
let mut at = start;
|
||||
for step in 1..=6 {
|
||||
at = start + egui::vec2(8.0 * step as f32, 5.0 * step as f32);
|
||||
assert_eq!(frame(&mut app, vec![Event::PointerMoved(at)]), (1, 0), "move {step} of the drag");
|
||||
}
|
||||
assert_eq!(frame(&mut app, vec![button(at, false)]), (1, 1), "the release measures the moved object's sections once");
|
||||
assert!(x(&app) > start_x, "the drag moved the object");
|
||||
assert_eq!(frame(&mut app, vec![]).1, 0, "a settled frame");
|
||||
app.run("object.move", json!({"dx": 5, "dy": 0})).unwrap();
|
||||
assert_eq!(frame(&mut app, vec![]).1, 1, "an edit without a drag measures the sections");
|
||||
}
|
||||
|
||||
/// An edit made while an undo group is open, as a scrubbed field's drag makes, is measured once the group ends, also
|
||||
/// when ending it does not move the document's revision.
|
||||
#[test]
|
||||
fn an_edit_during_a_drag_is_measured_when_the_drag_ends_without_a_new_revision() {
|
||||
let measures = || SECTION_MEASURES.with(std::cell::Cell::get);
|
||||
let mut app = new_app();
|
||||
let a = rect(&mut app, 100.0, 100.0, 80.0, 80.0);
|
||||
app.run("select.set", json!({"ids": [a]})).unwrap();
|
||||
app.run("window.panel", json!({"panel": "properties"})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
let mut time = 0.0;
|
||||
let mut frame = |app: &mut VectorcraftApp| {
|
||||
time += 1.0 / 60.0;
|
||||
let raw = egui::RawInput {
|
||||
time: Some(time),
|
||||
screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 1000.0))),
|
||||
focused: true,
|
||||
..Default::default()
|
||||
};
|
||||
let measured = measures();
|
||||
let _ = crate::testing::app_frame(app, &ctx, raw);
|
||||
measures() - measured
|
||||
};
|
||||
for _ in 0..4 {
|
||||
frame(&mut app);
|
||||
}
|
||||
app.run("edit.undoGroup.begin", json!({})).unwrap();
|
||||
assert_eq!(frame(&mut app), 0, "opening the group");
|
||||
app.run("object.move", json!({"dx": 5, "dy": 0})).unwrap();
|
||||
assert_eq!(frame(&mut app), 0, "an edit inside the group");
|
||||
let revision = app.session.active().unwrap().revision;
|
||||
app.run("edit.undoGroup.end", json!({})).unwrap();
|
||||
assert_eq!(app.session.active().unwrap().revision, revision, "ending the group keeps the revision");
|
||||
assert_eq!(frame(&mut app), 1, "the end of the group measures the edit");
|
||||
assert_eq!(frame(&mut app), 0, "a settled frame");
|
||||
}
|
||||
|
||||
/// Start Global Edit in Quick Actions starts it on the one selected path, the button then reads
|
||||
/// Stop Global Edit, and Escape stops it. Selected type has no Start Global Edit button.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user