Runtime: copy from an egui label selection stays egui's

The focus end hook records whether egui holds a label text selection; while
it does, the key stage leaves the copy and cut events in egui's input, so
egui copies the selection. A focused framework editor still takes them.
This commit is contained in:
aiongg
2026-11-12 14:32:48 +00:00
parent a1362ea605
commit 263ad24c08
7 changed files with 45 additions and 3 deletions
@@ -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));
@@ -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::<egui::text_selection::LabelSelectionState, _>(|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 =
+8 -1
View File
@@ -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.
@@ -585,6 +585,7 @@ pub(super) fn end(state: &mut State, focused: Option<Id>, frame: u64) -> Vec<Act
keys,
menu_bar: std::mem::take(&mut state.menu_bar),
foreign: false,
label_selection: false,
};
acts
}
@@ -33,6 +33,8 @@ struct Strip<'a> {
/// 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;
@@ -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");
}
+1 -1
View File
@@ -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. |