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

This commit is contained in:
bflatastic committed 2026-11-23 05:59:18 +07:00
1 parent 51cc84a7c1
commit f671a2f623
3 files changed
+13 -6

No files matched your search

+6 -3
View File
@@ -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 {
@@ -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());
@@ -18,7 +18,8 @@ fn stop(fixture: &Fixture) -> Option<Id> {
}
/// 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.