From f671a2f623e2b6361fb9d2bda4b061549fa42e45 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 05:59:18 +0700 Subject: [PATCH] ui-contract: Cut in a focused selection part copies its selected text, as a label's selection does, instead of running the app's Cut --- crates/ui-contract/src/contract/text.rs | 9 ++++++--- crates/ui-contract/src/primitives/text_adapter.rs | 5 +++-- crates/ui-widgets/tests/it/text/selectable.rs | 5 ++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index b0cc98966d..d7c81b26d4 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -65,9 +65,12 @@ pub(crate) struct SelectDecl { } impl SelectDecl { - /// The keys a focused selection part keeps: a read-only text's, with Select All and Copy its only edit commands. + /// The keys a focused selection part keeps: a read-only text's, with Select All, Copy and Cut its only edit + /// commands. Cut copies, as in a label's selection: the text removes nothing, and the app's Cut never runs on what + /// lies behind it. pub(crate) fn keys(&self) -> KeyUse { - let keys = KeyUse::of(crate::contract::Input::Text).with_edits(EditSet::NONE.with(EditCommand::SelectAll).with(EditCommand::Copy)); + let edits = EditSet::NONE.with(EditCommand::SelectAll).with(EditCommand::Copy).with(EditCommand::Cut); + let keys = KeyUse::of(crate::contract::Input::Text).with_edits(edits); if self.multiline { keys.with_keep(KeepKeys::BlockArrows) } else { keys } } } @@ -77,7 +80,7 @@ pub(crate) fn selection_id(id: egui::Id, part: PartRef) -> egui::Id { sub(id, ("selection", part.key, part.index)) } -/// Step 8b for selection parts: the focused one takes this pass's Select All and Copy. +/// Step 8b for selection parts: the focused one takes this pass's Select All, Copy and Cut. pub(crate) fn run_selections(core: &mut super::cx::CxCore<'_>, selections: &[SelectDecl]) { let ctx = core.ctx.clone(); let Some(focused) = selections.iter().find(|decl| crate::runtime::egui::focus::has_focus(&ctx, selection_id(core.id, decl.part))) else { diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index 1d16a065f9..29c11e6b99 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -400,11 +400,12 @@ fn apply(input: &mut AdapterIn<'_>, state: &mut TextState, text: &mut String, wi changed } -/// Select All or Copy on the text widget `widget` showing `text`: the edit commands read-only text takes. +/// Select All or Copy on the text widget `widget` showing `text`: the edit commands read-only text takes. A Cut that +/// reaches it copies, as read-only text removes nothing. pub(crate) fn select_or_copy(ctx: &Context, widget: Id, text: &str, command: EditCommand) { match command { EditCommand::SelectAll => text_edit::select(ctx, widget, 0, text.chars().count()), - EditCommand::Copy => { + EditCommand::Copy | EditCommand::Cut => { let Some((a, b)) = text_edit::selection(ctx, widget).filter(|(a, b)| a != b) else { return }; let (lo, hi) = (a.min(b), a.max(b)); clipboard::copy_text(ctx, text[byte_of(text, lo)..byte_of(text, hi)].to_owned()); diff --git a/crates/ui-widgets/tests/it/text/selectable.rs b/crates/ui-widgets/tests/it/text/selectable.rs index 93f68d3149..854390ab07 100644 --- a/crates/ui-widgets/tests/it/text/selectable.rs +++ b/crates/ui-widgets/tests/it/text/selectable.rs @@ -18,7 +18,8 @@ fn stop(fixture: &Fixture) -> Option { } /// Tab goes from the field before the text to the one after it; a press on the text focuses it, and Select All then -/// Copy puts the whole text on the clipboard. +/// Copy puts the whole text on the clipboard. Cut copies it too, as a label's selection does: read-only text removes +/// nothing, and the Cut is the text's, not the app's. #[trial] fn tab_passes_over_read_only_text_and_a_press_selects_it_for_copy() { let ids = Rc::new([Cell::new(Id::NULL), Cell::new(Id::NULL), Cell::new(Id::NULL)]); @@ -40,6 +41,8 @@ fn tab_passes_over_read_only_text_and_a_press_selects_it_for_copy() { fixture.key(Key::A, Modifiers::COMMAND); fixture.frame(vec![Event::Copy]); assert_eq!(fixture.copied(), ["/projects/poster.indd"]); + fixture.frame(vec![Event::Cut]); + assert_eq!(fixture.copied(), ["/projects/poster.indd"], "Cut copies the selected text"); } /// Where `text` was painted.