diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 538e681765..12cc42686c 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -11,6 +11,7 @@ use craft::ui::docking::IconGroupSeparator; use craft::ui::list::{CommandHighlight, ThumbRows}; use craft::ui::menu::{MenuTitleLook, MenuTitlePad}; use craft::ui::patterns::form_dialogs::PresetCardLook; +use craft::ui::runtime::drag::{self, OsFiles}; use craft::ui::runtime::env::{AltCodeKeys, RootEnv, Scale, ScrubDrags, WindowKey, WordsOverride, with_env}; use craft::ui::runtime::frame::{FrameInput, FrameOutput}; use craft::ui::runtime::toast::{self, Toast}; @@ -149,7 +150,6 @@ impl VectorcraftApp { spelling: Default::default(), text_nodes: Default::default(), momentary: None, - momentary_keys: vec![], hand: None, zoom_tool: None, selection_box: None, @@ -368,18 +368,26 @@ impl VectorcraftApp { } /// The folders this pass may read and write: the automation roots while the pass carries - /// input the control channel injected, or delivers files the OS dropped in such a pass; - /// anywhere otherwise. + /// input the control channel injected, or while the drag session still holds files the OS + /// dropped in such a pass; anywhere otherwise. fn automation_scope(&self, ctx: &egui::Context) -> Option> { - let delivers = self.confined_drop.is_some_and(|last| ctx.cumulative_frame_nr_for(egui::ViewportId::ROOT) <= last); - (WindowHost::of(ctx).injected_pass() || delivers).then(|| self.automation_roots.clone()).flatten() + (WindowHost::of(ctx).injected_pass() || self.confined_drop.is_some()).then(|| self.automation_roots.clone()).flatten() } - /// Notes files the OS dropped in a pass that carries injected input: the drag session delivers - /// them in the next root frame, which [`Self::automation_scope`] confines too. + /// Keeps the files the OS dropped in a pass that carries injected input until the drag session + /// lets them go: it delivers them in a later pass, after their target is resolved, and + /// [`Self::automation_scope`] confines every pass until then. fn note_confined_drop(&mut self, ctx: &egui::Context) { + if self.automation_roots.is_none() { + return; + } + let held = || drag::active::(ctx); if WindowHost::of(ctx).injected_pass() && ctx.input(|i| !i.raw.dropped_files.is_empty()) { - self.confined_drop = Some(ctx.cumulative_frame_nr_for(egui::ViewportId::ROOT) + 1); + self.confined_drop = held(); + } else if let Some(dropped) = &self.confined_drop + && !held().is_some_and(|now| Arc::ptr_eq(&now, dropped)) + { + self.confined_drop = None; } } } diff --git a/crates/ui-egui/src/caches.rs b/crates/ui-egui/src/caches.rs index b98eeb4606..ff93fc733c 100644 --- a/crates/ui-egui/src/caches.rs +++ b/crates/ui-egui/src/caches.rs @@ -81,9 +81,6 @@ pub struct CanvasCache { /// The Hand, Zoom and Rotate View tools the Space key and its chords lend the canvas, between /// passes; the tool adapter takes it out for its call and puts it back. pub momentary: Option, - /// The keys down when the held momentary command started; empty while none is held or when none started it. - /// Temporary, with `tool_adapter::end_missed_momentary` (craft-libs#5056). - pub momentary_keys: Vec, pub hand: Option, /// The selected Zoom tool keeps its press and drag rectangle between canvas passes. pub zoom_tool: Option, diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index 4edaa65945..67b2b51db8 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -9,8 +9,8 @@ use craft::behavior::{CancelReason, CanvasView, Cursor, PointerKind as Device, P use craft::keymap::{Chord, Key}; use craft::ui::canvas::ToolPath; use craft::ui::canvas::{ - Autoscroll, CanvasTool, Momentary, MomentaryCommands, MomentaryShift, ToolAction, ToolClaims, ToolContext, ToolHost, ToolHostResponse, ToolKey, - ToolPass, ToolPointer, + Autoscroll, CanvasTool, Momentary, MomentaryShift, ToolAction, ToolClaims, ToolContext, ToolHost, ToolHostResponse, ToolKey, ToolPass, + ToolPointer, }; use craft::ui::contract::DragPreview; use craft::ui::runtime::drag; @@ -656,37 +656,6 @@ impl CanvasTool for SessionTool<'_> { } } -/// Ends the held momentary command (Space's Hand, Cmd+Space's Zoom...) once none of the keys that were down when it -/// started is down, and returns the command still held. The shortcut board drops a queued release when the canvas -/// does not run on the frame the key goes up (a dialog or another tab in front). A hold that started with no key down -/// (automation) lasts until its release. -/// -/// Temporary: craft-libs#5056 ends the hold inside the tool host. Remove this function, [`note_momentary_keys`], -/// their calls and `CanvasCache::momentary_keys` once the craft-libs pin includes that change. -fn end_missed_momentary(ctx: &egui::Context, momentary: &mut Momentary, keys: &mut Vec) -> Option { - let held = momentary.held()?; - if keys.is_empty() { - return Some(held); - } - let down = craft::ui::runtime::keys::held(ctx); - if keys.iter().any(|key| down.contains(key)) { - return Some(held); - } - momentary.fired(held, craft::ui::runtime::commands::FirePhase::Release); - keys.clear(); - None -} - -/// Notes the keys down when a momentary command starts (or changes), for [`end_missed_momentary`]; `before` is the -/// command held before the tool host's pass. -fn note_momentary_keys(ctx: &egui::Context, before: Option, momentary: &Momentary, keys: &mut Vec) { - match momentary.held() { - None => keys.clear(), - Some(held) if before != Some(held) => *keys = craft::ui::runtime::keys::held(ctx), - Some(_) => {} - } -} - /// Runs the tool host on `tool` with the app's momentary set, which the app keeps across passes. A finger, once a pen /// has been seen, only navigates (palm rejection). fn host_run(app: &mut VectorcraftApp, ui: &Ui, pass: &ToolPass, tool: &mut dyn CanvasTool) -> ToolHostResponse { @@ -694,9 +663,7 @@ fn host_run(app: &mut VectorcraftApp, ui: &Ui, pass: &ToolPass, tool: &mut dyn C let mut momentary = app.canvas.momentary.take().unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); momentary.set_options(options); momentary.hand_selected(app.session.tool_id() == "hand"); - let before = end_missed_momentary(ui.ctx(), &mut momentary, &mut app.canvas.momentary_keys); let response = ToolHost::new().momentary(&mut momentary).navigates(crate::touch::finger_navigates_only()).run(ui, pass, tool); - note_momentary_keys(ui.ctx(), before, &momentary, &mut app.canvas.momentary_keys); app.canvas.momentary = Some(momentary); response } @@ -855,14 +822,9 @@ fn host_run_session(tool: &mut SessionTool<'_>, ui: &Ui, pass: &ToolPass) -> Too let mut momentary = app.canvas.momentary.take().unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); momentary.set_options(options); momentary.hand_selected(app.session.tool_id() == "hand"); - let mut keys = std::mem::take(&mut app.canvas.momentary_keys); drop(app); - let before = end_missed_momentary(ui.ctx(), &mut momentary, &mut keys); let response = ToolHost::new().momentary(&mut momentary).navigates(crate::touch::finger_navigates_only()).run(ui, pass, tool); - note_momentary_keys(ui.ctx(), before, &momentary, &mut keys); - let mut app = tool.app(); - app.canvas.momentary = Some(momentary); - app.canvas.momentary_keys = keys; + tool.app().canvas.momentary = Some(momentary); response } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 333d22209d..6c41d58e2d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -356,9 +356,10 @@ pub struct VectorcraftApp { /// (`--automation-read-root`, `--automation-write-root`); `None`: anywhere. See /// [`Self::with_automation_roots`]. automation_roots: Option>, - /// The last root frame of a drop of files the OS made in a pass that carried injected input: the drag session - /// delivers that drop in the next frame, which [`Self::ui`] confines to the automation roots as well. - confined_drop: Option, + /// The files the OS dropped in a pass that carried injected input, while the drag session holds them: every pass + /// until the session lets them go, the one that delivers them included, is confined to the automation roots as well + /// ([`Self::ui`]). + confined_drop: Option>, /// The agents link while the ArtCraft Launcher's switch may let agents in ([`Self::with_agents`]). #[cfg(not(target_arch = "wasm32"))] pub(crate) agents: Option>, diff --git a/crates/ui-egui/src/panels/gradient.rs b/crates/ui-egui/src/panels/gradient.rs index 68243adb08..db8df536e6 100644 --- a/crates/ui-egui/src/panels/gradient.rs +++ b/crates/ui-egui/src/panels/gradient.rs @@ -102,13 +102,6 @@ pub struct GradientUi { /// `paint.editGradient` sorts the stops, so the gradient shown mid-gesture is the last preview /// re-sorted: the editor's tags name stops of this one. pub(crate) begun: Option>, - /// The press of a double click that asked for a stop's color is still held. Until craft-libs#5131 reaches the - /// pin, the slider also drags the stop when the pointer drifts during that press, and the moved stop closes the - /// popover the double click opened: the panel drops that press's slider edits. - pub(crate) color_press: bool, - /// The slider's gesture that the press above began, dropped until its `Commit` or `Cancel`, which can arrive in a - /// pass after the button is up. - pub(crate) color_drag: bool, } /// Select stop `i` (the session's selected stop, shared with the Gradient tool's annotator, the @@ -838,21 +831,6 @@ fn stops_editor(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, is_gra .shared_selection(Bind::bound(&mut shared)) .drop_target::() .show(ui); - let panel = &mut app.ui.gradient_panel; - let color_press = panel.color_press || out.color_requested.is_some(); - let held = craft::ui::runtime::pointer::press_state(ui.ctx()).buttons.contains(craft::behavior::Buttons::PRIMARY); - let dropping = panel.color_drag || (color_press && out.edit.events.began()); - let ended = matches!(out.edit.events.last(), Some(EditEvent::Commit { .. } | EditEvent::Cancel { .. } | EditEvent::Set { .. })); - panel.color_press = color_press && held; - panel.color_drag = dropping && !ended; - if color_press || dropping { - // The drift of the press that asked for the color moves no stop (craft-libs#5131). - if is_grad && let Some(request) = &out.color_requested { - let at = request.rect.map_or(out.edit.response.rect.left_bottom(), |r| r.left_bottom()); - open_popover(app, request.index, at); - } - return; - } let events = &out.edit.events; if !is_grad { // The slider shows the default gradient: an edit of it applies a gradient first. diff --git a/crates/ui-egui/src/tests/automation.rs b/crates/ui-egui/src/tests/automation.rs index c6bc026980..a3601e2d3a 100644 --- a/crates/ui-egui/src/tests/automation.rs +++ b/crates/ui-egui/src/tests/automation.rs @@ -619,10 +619,24 @@ fn a_drop_in_a_pass_with_one_step_of_injected_input_is_confined_when_delivered() drop_in_an_injected_pass_is_confined("ui.text", json!({"text": "a"})); } +/// The OS held the file under the pointer and drops it in a pass with no pointer position: the drag +/// session holds the drop until a later pass resolves its target, and the drop is still refused when +/// it is delivered there. +#[test] +fn a_drop_that_waits_for_its_target_is_confined_when_delivered() { + drop_is_confined("ui.text", json!({"text": "a"}), true); +} + /// Injects `method` with `params`, has the person drop a file outside the automation roots in the /// same pass, and checks that the drop opens nothing, then that a later drop of the person's opens /// the file. fn drop_in_an_injected_pass_is_confined(method: &str, params: Value) { + drop_is_confined(method, params, false); +} + +/// [`drop_in_an_injected_pass_is_confined`]; `pointer`: the pass that hovers the file carries a +/// pointer position and the drop's pass none, so the drag session delivers the drop a pass later. +fn drop_is_confined(method: &str, params: Value, pointer: bool) { #[derive(Debug)] struct Dropped(PathBuf); impl egui::DroppedFile for Dropped { @@ -633,7 +647,8 @@ fn drop_in_an_injected_pass_is_confined(method: &str, params: Value) { std::fs::read(&self.0).map_err(|e| e.to_string()) } } - let (mut app, tx, base) = confined(&format!("synthetic-{method}")); + // Each scenario has its own directory, so tests running in parallel do not remove each other's files. + let (mut app, tx, base) = confined(&format!("synthetic-{method}-{}", if pointer { "pointer" } else { "nopointer" })); let ctx = crate::testing::ui_context(); let outside = base.join("outside/b.svg"); // The OS holds the file over the window in the pass before it drops it (`hovered`), as a desktop drag does. @@ -641,6 +656,12 @@ fn drop_in_an_injected_pass_is_confined(method: &str, params: Value) { let mut raw = egui::RawInput::default(); if hovered { raw.hovered_files.push(egui::HoveredFile { path: Some(outside.clone()), mime: String::new() }); + if pointer { + raw.events.push(egui::Event::PointerMoved(egui::pos2(300.0, 300.0))); + } + } + if dropped && pointer { + raw.events.push(egui::Event::PointerGone); } if dropped { raw.dropped_files.push(std::sync::Arc::new(Dropped(outside.clone()))); @@ -653,13 +674,13 @@ fn drop_in_an_injected_pass_is_confined(method: &str, params: Value) { tx.send(req).unwrap(); app.run_control(&ctx); pass(&mut app, false, true); - // The drag session delivers a drop of files in the pass after the one the OS dropped them in. - assert_eq!(pass(&mut app, false, false), 0, "{method}: the drop of the pass carrying the input is confined"); - for _ in 0..4 { - pass(&mut app, false, false); + // The drag session delivers a drop of files in a pass after the one the OS dropped them in. + for _ in 0..5 { + assert_eq!(pass(&mut app, false, false), 0, "{method}: the drop of the pass carrying the input is confined"); } pass(&mut app, true, false); pass(&mut app, false, true); + pass(&mut app, false, false); assert_eq!(pass(&mut app, false, false), 1, "{method}: the person's pass is not"); let _ = std::fs::remove_dir_all(base); } diff --git a/crates/ui-egui/src/tests/home.rs b/crates/ui-egui/src/tests/home.rs index 50991b8d8c..77748f68f5 100644 --- a/crates/ui-egui/src/tests/home.rs +++ b/crates/ui-egui/src/tests/home.rs @@ -125,6 +125,50 @@ fn home_screen(app: VectorcraftApp) -> Panel { home } +/// Where one frame of the whole window `size` big painted Home's title, after `events`. +fn home_title_after(app: &mut VectorcraftApp, ctx: &egui::Context, size: egui::Vec2, events: Vec) -> egui::Rect { + let raw = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, size)), events, ..Default::default() }; + let out = crate::testing::app_frame(app, ctx, raw); + let titles: Vec = crate::tests_labels::painted_shapes(&out.shapes) + .into_iter() + .filter_map(|(shape, transform, _)| match shape { + egui::Shape::Text(t) if t.galley.text() == "Welcome to VectorCraft" => Some(transform * t.visual_bounding_rect()), + _ => None, + }) + .collect(); + assert_eq!(titles.len(), 1, "Home paints its title once: {titles:?}"); + titles[0] +} + +/// How far a wheel turned to the end over Home's title moves the title, with no recent files in a window `size` big. +fn home_title_moved_by_a_wheel(size: egui::Vec2) -> f32 { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); + let ctx = crate::testing::ui_context(); + for _ in 0..3 { + home_title_after(&mut app, &ctx, size, vec![]); + } + assert!(crate::menus::home_showing(&app), "Home with no document open"); + let before = home_title_after(&mut app, &ctx, size, vec![]); + home_title_after(&mut app, &ctx, size, vec![egui::Event::PointerMoved(before.center())]); + home_title_after(&mut app, &ctx, size, vec![craft::ui::testing::pointer::wheel(-2000.0)]); + let mut after = before; + for _ in 0..3 { + after = home_title_after(&mut app, &ctx, size, vec![]); + } + after.top() - before.top() +} + +/// At the app's default window size, Home's column fits the canvas view, so the page has no room to scroll and shows no +/// scroll bar beside the docks: a wheel leaves the title where it is. In a smaller window, where the Learn cards stand +/// two by two and the column is taller than the view, the page scrolls. +#[test] +fn home_scrolls_only_when_its_column_is_taller_than_the_window() { + let moved = home_title_moved_by_a_wheel(egui::vec2(1440.0, 900.0)); + assert!(moved.abs() < 0.5, "at 1440 by 900 the column fits, so the page does not scroll: moved {moved}"); + let moved = home_title_moved_by_a_wheel(egui::vec2(1280.0, 800.0)); + assert!(moved < -1.0, "at 1280 by 800 the column is taller than the view, so the page scrolls: moved {moved}"); +} + /// Home's Open while a file dialog is still open (a window the system may have placed out of sight) /// says so in the status bar and asks for no second picker; once the first answers, Open asks again /// (#6160).