diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index 5019fb308..2566c85c9 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -347,6 +347,11 @@ pub(crate) fn host_focus(ctx: &Context, id: egui::Id) { ctx.memory_mut(|memory| memory.request_focus(id)); } +/// A host's own selectable egui label of `text`; returns its rect. +pub(crate) fn host_label(ui: &mut Ui, text: &str) -> Rect { + ui.add(egui::Label::new(text).selectable(true)).rect +} + /// A host's own egui popup `id` at `at`, shown while egui's memory holds it open. pub(crate) fn host_popup(ui: &Ui, id: egui::Id, at: Pos2) { egui::Popup::new(id, ui.ctx().clone(), at, ui.layer_id()).open_memory(None).show(|ui| ui.allocate_space(ui.spacing().interact_size)); diff --git a/crates/ui-contract/src/runtime/egui/focus.rs b/crates/ui-contract/src/runtime/egui/focus.rs index 6a80f5e6d..ee880710a 100644 --- a/crates/ui-contract/src/runtime/egui/focus.rs +++ b/crates/ui-contract/src/runtime/egui/focus.rs @@ -37,6 +37,11 @@ pub(crate) fn popup_open(ctx: &Context) -> bool { egui::Popup::is_any_open(ctx) } +/// Whether egui holds a label text selection, which egui copies on a copy or cut event. +pub(crate) fn label_selection(ctx: &Context) -> bool { + ctx.with_plugin::(|state| state.has_selection()).unwrap_or(false) +} + /// Gives `id` key focus with `filter`, applied from this pass: a filter set afterwards acts a pass late. pub(crate) fn focus_with_filter(ctx: &Context, id: Id, filter: KeyFilter) { let filter = diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 50c9c31d2..383a30753 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -76,6 +76,9 @@ pub(crate) struct FocusedRecord { /// drag is captured: an app's own focused widget or popup keeps the keys a focused component keeps (Tab, Escape, /// the clipboard events). Written by the end hook after its focus moves. pub(crate) foreign: bool, + /// egui holds a label text selection: a copy or cut event is egui's, which copies the selection. Written by the + /// end hook. + pub(crate) label_selection: bool, } /// A change to egui's focus, applied after the state lock is released. @@ -649,7 +652,11 @@ pub(crate) fn end(ctx: &Context) { let unmade = with(ctx, |state| state.focus_now.filter(|_| state.stop_of_focus().is_none())); let foreign = !captured && (egui_focus::popup_open(ctx) || unmade.is_some_and(|id| !crate::runtime::pointer::made_by_component(ctx, viewport, id))); - with(ctx, |state| state.record.foreign = foreign); + let label_selection = egui_focus::label_selection(ctx); + with(ctx, |state| { + state.record.foreign = foreign; + state.record.label_selection = label_selection; + }); } /// The scopes `rec` sits in, innermost first. diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index 43b6fc2c1..bfa6b3e7a 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -585,6 +585,7 @@ pub(super) fn end(state: &mut State, focused: Option, frame: u64) -> Vec { /// An app's own egui widget or popup has the focus (`FocusedRecord::foreign`): Tab, Escape and the clipboard /// events are its own. foreign: bool, + /// egui holds a label text selection, which a copy or cut event copies. + label_selection: bool, } impl Strip<'_> { @@ -91,6 +93,7 @@ impl Strip<'_> { false } KeyEvent::Clip { .. } if self.foreign => true, + KeyEvent::Clip { command: EditCommand::Copy | EditCommand::Cut, .. } if self.label_selection => true, KeyEvent::Clip { command, .. } => { // The platform sends the clipboard event in place of the chord: an unlisted one is the chord. The // web sends the chord's press as well. @@ -279,6 +282,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> bool { menu_bar, canvas_space: hole.as_ref().is_some_and(|hole| hole.space || hole.space_pressed), foreign: record.foreign, + label_selection: record.label_selection, }; let mut batch = Batch::default(); let mut visible = None; diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 3c07c7afb..993b95391 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -6,7 +6,7 @@ use crate::runtime::focus::KeyUse; use crate::runtime::keys::with_window_intents; use crate::testing::focus::{Stop, egui_focused, named_id, register_stop, widget}; use crate::testing::input::{key_press, key_reached}; -use crate::testing::pointer::{moved, press, release}; +use crate::testing::pointer::{self, moved, press, release}; use crate::testing::{Frames, trial}; use craft_behavior::{Input, Intent}; use egui::{Event, Id, ImeEvent, Key, Modifiers, Pos2, Rect, Ui}; @@ -153,3 +153,23 @@ fn escape_cancels_a_framework_drag_while_an_egui_field_has_focus() { assert_eq!(seen(&host), (Vec::new(), 0), "the Escape went to the drag alone"); frames.frame(vec![release(inside)], &mut show); } + +#[trial] +fn copy_with_a_label_selection_copies_the_label() { + const TEXT: &str = "host label text"; + let label = Rc::new(std::cell::Cell::new(Rect::NOTHING)); + let at = Rc::clone(&label); + let (_, mut base) = host_window(); + let mut show = move |ui: &mut Ui| { + base(ui); + at.set(fixture::host_label(ui, TEXT)); + }; + let mut frames = Frames::new(); + frames.frame(Vec::new(), &mut show); + frames.frame(vec![Event::Copy], &mut show); + assert!(frames.copied().is_empty(), "with no selection, the copy is the app's chord"); + let rect = label.get(); + pointer::drag(&mut frames, rect.left_center() + egui::vec2(1.0, 0.0), egui::vec2(rect.width() - 2.0, 0.0), &mut show); + frames.frame(vec![Event::Copy], &mut show); + assert!(frames.copied().iter().any(|copied| !copied.is_empty() && TEXT.contains(copied.as_str())), "egui copied the selection"); +} diff --git a/docs/craft-framework.md b/docs/craft-framework.md index 7da201a7f..63f98cfc1 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -152,7 +152,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. ` | F6 | F6 and Shift+F6 move between panes and never reach egui. | | Escape | Plain Escape (Alt and Shift may be held) goes to the focused framework stop and its scopes; with no framework stop focused, it goes to the window as `Intent::Cancel`. It reaches egui only while an app's own widget has the focus (below). | | F10, Alt+letter | Taken while a framework menu bar is registered: F10 focuses the bar, Alt+letter opens a menu (not on macOS). | -| Edit chords | Taken while a framework editor is focused. Otherwise egui's copy, cut and paste events become the ⌘C/X/V chord for the app's commands, unless an app's own widget has the focus (below). | +| Edit chords | Taken while a framework editor is focused. Otherwise egui's copy, cut and paste events become the ⌘C/X/V chord for the app's commands, unless an app's own widget has the focus (below). While egui holds a label text selection, copy and cut stay egui's, and egui copies the selection. | | IME | Taken while a framework editor owns the IME: its events and `PlatformOutput.ime` are the runtime's. Otherwise they stay egui's, for the app's own `TextEdit` or IME code. | | An app's own focused widget | While egui focus is on a widget no framework component made, or an egui popup is open, and no framework press or drag is captured, Tab, Shift+Tab, Escape and egui's clipboard events stay in egui's input for it, and the runtime routes nothing for them. | | Releases | The release of every key the runtime took is taken too. |