diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 3ee8c24ce5..6194f28085 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -29,6 +29,8 @@ pub struct PropertiesUi { quick_actions: Option<(u64, Vec)>, /// The [`content_key`] the sections were last shown for. content: Option, + /// The document (uid, revision) the sections were last shown for. + revision: Option<(u64, u64)>, } /// The key of [`PropertiesUi::edited_profiled`]: (document uid, revision, active Appearance row). @@ -66,7 +68,13 @@ 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. + let dragging = app.session.active().is_some_and(|st| st.interaction.is_some() || st.undo_group.is_some()); + let changed = content_changed || (edited && !dragging); 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 +95,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 +233,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 = 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 = const { std::cell::Cell::new(0) }; } /// [`quick_actions`], listed again only when [`quick_actions_key`] changed. diff --git a/crates/ui-egui/src/panels/properties/tests.rs b/crates/ui-egui/src/panels/properties/tests.rs index 97c9f1e9bb..c46bcbf6e4 100644 --- a/crates/ui-egui/src/panels/properties/tests.rs +++ b/crates/ui-egui/src/panels/properties/tests.rs @@ -190,6 +190,57 @@ 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| { + 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"); +} + /// 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]