diff --git a/crates/ui-egui/src/doc_windows.rs b/crates/ui-egui/src/doc_windows.rs index 3ca925ed6a..ca91583b93 100644 --- a/crates/ui-egui/src/doc_windows.rs +++ b/crates/ui-egui/src/doc_windows.rs @@ -449,6 +449,10 @@ fn show_windows(app: &mut VectorcraftApp, ui: &mut egui::Ui) { let window = DocumentWindow::new(title).id_salt((w.uid, w.number, w.generation)).first_placed(w.place.translate(origin)); let window = if raise == Some((w.uid, w.view)) { window.raise() } else { window }; let out = window.show(ui, |ui, _| { + // In an OS window of its own, a Copy or Cut that the window's labels copy nothing for runs on the art. + if ui.ctx().viewport_id() != egui::ViewportId::ROOT { + crate::shortcuts::leave_to_labels(app, ui.ctx()); + } // A press anywhere on the window, its title bar and tab included, makes its document current. pressed = pressed_on_layer(ui); crate::chrome::window_tab(app, ui, win); diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs index 678f797822..091c5d1ce8 100644 --- a/crates/ui-egui/src/panel_docking.rs +++ b/crates/ui-egui/src/panel_docking.rs @@ -1261,6 +1261,10 @@ impl DockPanels for Panels<'_> { // A rail shares its column with nothing, so its group is the rail alone. Its view draws // the Edit Toolbar drawer while the drawer targets it. let owner = panels.iter().find_map(|p| rail_owner(&self.app.ui, p))?; + // A floating toolbar's OS window runs a pass of its own, as a floating panel's does. + if matches!(at, GroupAt::Show(_)) && ui.ctx().viewport_id() != egui::ViewportId::ROOT { + crate::shortcuts::leave_to_labels(self.app, ui.ctx()); + } Some(crate::toolbar::rail_view(self.app, ui, owner, at, cx)) } diff --git a/crates/ui-egui/src/panel_docking/native_tests.rs b/crates/ui-egui/src/panel_docking/native_tests.rs index 3c87ced79c..eebf3d6a77 100644 --- a/crates/ui-egui/src/panel_docking/native_tests.rs +++ b/crates/ui-egui/src/panel_docking/native_tests.rs @@ -32,6 +32,11 @@ struct Window { nodes: Vec<(String, Rect)>, } +/// The texts a frame copied. +fn copied_texts(full: &egui::FullOutput) -> impl Iterator + '_ { + full.platform_output.commands.iter().filter_map(|c| if let egui::OutputCommand::CopyText(t) = c { Some(t.clone()) } else { None }) +} + struct Host { windows: HashMap, /// The window with the keyboard. @@ -153,6 +158,11 @@ impl Harness { } fn frame(&mut self, events: Vec) { + self.main_frame(events); + } + + /// One frame with `events` in the main window: the main window's output. + fn main_frame(&mut self, events: Vec) -> egui::FullOutput { let (viewports, focused, time) = { let mut host = self.host.borrow_mut(); host.time += 0.05; @@ -161,6 +171,7 @@ impl Harness { let input = egui::RawInput { time: Some(time), events, screen_rect: Some(SCREEN), viewports, focused, ..Default::default() }; let full = crate::testing::app_frame(&mut self.app, &self.ctx, input); self.host.borrow_mut().apply(&full); + full } fn frames(&mut self, n: usize) { @@ -474,3 +485,95 @@ fn delete_after_a_row_click_in_a_native_appearance_window_removes_the_row() { assert_eq!(h.objects(), before, "the object stays"); assert_eq!(items(&h), Some(1), "the fill row goes"); } + +/// The text of the selected label. +const LABEL: &str = "234 pt"; + +/// One frame with `events` sent to the native window `viewport`: the main window's output. +fn frame_in(h: &mut Harness, viewport: ViewportId, events: Vec) -> egui::FullOutput { + if let Some(window) = h.host.borrow_mut().windows.get_mut(&viewport) { + window.events.extend(events); + } + h.main_frame(vec![]) +} + +/// Drag across a label `text` in a window of its own, selecting its text there: the label stands for one in any +/// window other than the one a test then presses its keys in. +fn select_label_text_in_another_window(h: &mut Harness, text: &str) { + let window = ViewportId::from_hash_of("window with a selected label"); + let rect = std::cell::Cell::new(Rect::NOTHING); + let pass = |h: &mut Harness, events: Vec| { + let time = { + let mut host = h.host.borrow_mut(); + host.time += 0.05; + host.time + }; + let screen = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 200.0)); + let mut input = egui::RawInput { viewport_id: window, events, time: Some(time), screen_rect: Some(screen), ..Default::default() }; + input.viewports.insert(window, egui::ViewportInfo { parent: Some(ViewportId::ROOT), ..Default::default() }); + h.ctx.run_ui(input, |ui| rect.set(ui.label(text).rect)).drop_without_applying_deltas(); + }; + pass(h, vec![]); + let (from, to) = (rect.get().left_center() + vec2(1.0, 0.0), rect.get().right_center() - vec2(1.0, 0.0)); + pass(h, vec![Event::PointerMoved(from)]); + pass(h, vec![button(from, true)]); + for k in 1..=4 { + pass(h, vec![Event::PointerMoved(from + (to - from) * k as f32 / 4.0)]); + } + pass(h, vec![button(to, false)]); + pass(h, vec![]); + h.frames(2); + assert!( + h.ctx.with_plugin(|s: &mut egui::text_selection::LabelSelectionState| s.has_selection()).unwrap_or(false), + "the text of {text:?} is selected" + ); +} + +/// Cmd+C or Cmd+X (`event`) pressed in the window `viewport`, then let go, and the frames after it: the texts they +/// copied, the native windows' included. +fn clipboard_chord_in(h: &mut Harness, viewport: ViewportId, event: Event) -> Vec { + let frames = + [vec![Event::ModifiersChanged(egui::Modifiers::COMMAND), event], vec![Event::ModifiersChanged(egui::Modifiers::NONE)], vec![], vec![]]; + let mut copied = Vec::new(); + for events in frames { + copied.extend(copied_texts(&frame_in(h, viewport, events))); + } + copied +} + +/// With a label's text selected in another window, Copy and Cut pressed in the OS window of a floating panel, a +/// floating toolbar or a floating document, which shows no selected text, copy and cut the selected art. +#[test] +fn copy_and_cut_in_a_native_window_act_on_the_art_while_another_windows_label_has_text_selected() { + for what in ["panel", "toolbar", "document"] { + let mut h = Harness::new(); + h.app.run("shape.rectangle", json!({"x": 200, "y": 150, "width": 20, "height": 20})).unwrap(); + h.app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 37, "height": 40})).unwrap(); + let viewport = match what { + "panel" => h.float("swatches"), + "toolbar" => { + h.app.run("window.toolbar.new", json!({"name": "Mine"})).unwrap(); + h.app.run("window.toolbar.float", json!({"name": "Mine", "x": 240, "y": 160})).unwrap(); + h.frames(3); + h.viewport(&crate::custom_toolbars::panel_id(crate::custom_toolbars::find(&h.app.ui, "Mine").unwrap().id)) + } + _ => { + let before: Vec = h.host.borrow().windows.keys().copied().collect(); + h.app.run("window.arrange.floatInWindow", json!({})).unwrap(); + h.frames(3); + let opened = h.host.borrow().windows.keys().copied().filter(|id| !before.contains(id)).collect::>(); + assert_eq!(opened.len(), 1, "the document opened an OS window"); + opened[0] + } + }; + h.host.borrow_mut().focus = viewport; + select_label_text_in_another_window(&mut h, LABEL); + assert!(h.app.session.clipboard.is_empty(), "{what}: nothing copied yet"); + let copied = clipboard_chord_in(&mut h, viewport, Event::Copy); + assert!(!h.app.session.clipboard.is_empty(), "{what}: Copy in the floating window copied the art"); + let before = h.objects(); + let cut = clipboard_chord_in(&mut h, viewport, Event::Cut); + assert_eq!(h.objects(), before - 1, "{what}: Cut in the floating window cut the selected object"); + assert!(![copied, cut].concat().iter().any(|t| t == LABEL), "{what}: the floating window copied no label text"); + } +} diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index decf0abcfa..56489b2726 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -190,9 +190,11 @@ pub(crate) fn escape(app: &mut VectorcraftApp, ctx: &egui::Context) { } /// In a floating panel's OS window, the keys none of its controls took, which production's palette windows passed -/// on to the main window: Escape ends one thing as there ([`escape`]), and Enter is the canvas's tool's (in -/// Presentation Mode it opens nothing). +/// on to the main window: Escape ends one thing as there ([`escape`]), Enter is the canvas's tool's (in +/// Presentation Mode it opens nothing), and a Copy or Cut that the window's labels copy nothing for runs on the art +/// ([`leave_to_labels`]). pub(crate) fn palette_window_keys(app: &mut VectorcraftApp, ctx: &egui::Context) { + leave_to_labels(app, ctx); escape(app, ctx); if keys::with_window_intents(ctx, false, |window| window.take(Intent::Submit)) && app.ui.screen_mode != 3 { let view = app.view_info(); diff --git a/crates/ui-egui/src/shortcuts/labels.rs b/crates/ui-egui/src/shortcuts/labels.rs index dd24086d04..cdece818a9 100644 --- a/crates/ui-egui/src/shortcuts/labels.rs +++ b/crates/ui-egui/src/shortcuts/labels.rs @@ -53,7 +53,9 @@ pub(crate) fn run_left_by_labels(app: &mut VectorcraftApp, ctx: &egui::Context) /// After the pass's input, once the text it copied has gone out: a Copy or Cut event still in the input while text /// is selected in a label, and no text field has the keys, is the labels'. The texts copied so far are noted, so a /// copy at the end of the pass beyond them is the labels' ([`CopyAfterLabels`]). The labels' text, or the art copied -/// in the next pass when they copy nothing, replaces what an earlier Copy or Cut has not written yet. +/// in the next pass when they copy nothing, replaces what an earlier Copy or Cut has not written yet. Every window +/// calls it in its own pass, the OS windows of floating panels and documents included: the key stage of each window +/// leaves its Copy or Cut to the labels while text is selected in a label of any window. pub(crate) fn leave_to_labels(app: &mut VectorcraftApp, ctx: &egui::Context) { let viewport = ctx.viewport_id(); let n = copies(ctx);