From fb3a6a6362f5fc70fde185acb0c8ab398cca1ea3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 23 Nov 2026 07:43:54 -0500 Subject: [PATCH] fw sync: opt-in platform paste for a fired Paste (CommandInput::paste_reads_clipboard) App commands a menu row fires reach the app in the next frame's FrameOutput, so an app that answers Edit > Paste by asking the platform for the clipboard asked a frame late. With paste_reads_clipboard (off by default) the board asks the platform in the pass that fires the library Paste at the app (not from a key) and delivers the command with the clipboard's content, as a point paste does. Trial: menu::openers::a_menu_paste_reads_the_clipboard_when_the_app_asks. --- .../ui-contract/src/runtime/commands/board.rs | 18 +++++++++++ .../ui-contract/src/runtime/commands/mod.rs | 11 +++++++ crates/ui-widgets/tests/it/menu/openers.rs | 30 +++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 72c30b325..5945040d0 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -151,6 +151,8 @@ pub(crate) struct Board { out: Vec, /// The latest point-bearing clipboard request in each viewport. pending_pastes: HashMap, + /// The app's `CommandInput::paste_reads_clipboard`. + paste_reads_clipboard: bool, /// App-scope commands a key pressed while only their own state disabled them, for the next `FrameOutput`. declined: Vec, /// The commands instances took since the last `FrameOutput`, in order. @@ -1082,6 +1084,7 @@ pub(crate) fn set_input(ctx: &Context, input: &CommandInput) { } board.gate = input.gate.clone(); board.app = input.catalog.clone(); + board.paste_reads_clipboard = input.paste_reads_clipboard; let stale = board.keymap.as_ref().is_none_or(|cache| { cache.platform != platform || cache.generation != generation @@ -1637,6 +1640,21 @@ fn queue(ctx: &Context, fired: Fired) { { return; } + // An app that reads the clipboard through the board (`CommandInput::paste_reads_clipboard`) gets a Paste fired at + // it from a menu, a button or the palette with the clipboard's content: the platform is asked in this pass, and + // the paste it delivers fires the command (`deliver_requested_paste`). + let reads_clipboard = fired.phase != FirePhase::Release + && fired.target.is_none() + && fired.pasted.is_none() + && fired.source != FireSource::Key + && with(ctx, |board| board.paste_reads_clipboard && board.registry.edit_of(&fired.command.id) == Some(EditCommand::Paste)); + if reads_clipboard { + with(ctx, |board| { + board.pending_pastes.insert(pass::current_viewport(ctx), fired); + }); + crate::runtime::egui::clipboard::request_paste(ctx); + return; + } with(ctx, |board| if fired.target.is_some() { board.queue.push(fired) } else { board.out.push(fired) }); // A scoped entry is taken next pass, so the root viewport runs one. repaint::request_of(ctx, ViewportId::ROOT); diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index e2d93f46b..040047bc6 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -457,6 +457,7 @@ pub struct CommandInput { pub(crate) overrides: Option>, pub(crate) gate: Option, pub(crate) catalog: Option, + pub(crate) paste_reads_clipboard: bool, } impl CommandInput { @@ -494,6 +495,16 @@ impl CommandInput { self.gate = Some(gate); self } + + /// Whether the library's Paste ([`EditCommands::Paste`]), fired at the app by a menu row, a toolbar button or the + /// palette, first asks the platform for the clipboard in the pass that fires it and reaches the app with the + /// clipboard's content, as ⌘V does. Off (the default), it reaches the app at once with no content and the app + /// reads its clipboard itself. + #[must_use] + pub fn paste_reads_clipboard(mut self, on: bool) -> Self { + self.paste_reads_clipboard = on; + self + } } /// The chain of container instances that receive scoped commands, innermost last. A private inherited field of diff --git a/crates/ui-widgets/tests/it/menu/openers.rs b/crates/ui-widgets/tests/it/menu/openers.rs index e24e6ab77..cc08bc0aa 100644 --- a/crates/ui-widgets/tests/it/menu/openers.rs +++ b/crates/ui-widgets/tests/it/menu/openers.rs @@ -271,3 +271,33 @@ fn a_context_menu_near_the_top_stays_inside_the_window() { let window = Rect::from_min_size(pos2(0.0, 0.0), egui::vec2(400.0, 300.0)); assert!(menu.min.y >= window.min.y - 0.5 && menu.max.y <= window.max.y + 0.5, "the menu lies inside the window: {menu:?}"); } + +/// Edit ▸ Paste fired at the app reaches it at once with no content by default; an app that reads the clipboard +/// through the board (`CommandInput::paste_reads_clipboard`) gets it with the clipboard's text once the platform +/// answers, as ⌘V's paste does. +#[trial] +fn a_menu_paste_reads_the_clipboard_when_the_app_asks() { + for reads in [false, true] { + let mut fixture = Fixture::new(|ui| { + Button::new("Target").show(ui); + }); + fixture.commands(commands::CommandInput::new().paste_reads_clipboard(reads)).settle(); + let _ = take_output(fixture.ctx()); + commands::fire(fixture.ctx(), EditCommands::Paste, FireSource::Menu); + fixture.settle(); + let now = take_output(fixture.ctx()).fired; + if !reads { + assert!(now.iter().any(|fired| fired.command.id == EditCommands::Paste.id() && fired.pasted.is_none()), "at once: {now:?}"); + continue; + } + assert!(now.is_empty(), "the paste waits for the platform's clipboard: {now:?}"); + fixture.frame(vec![egui::Event::Paste("clipboard text".to_owned())]).settle(); + let later = take_output(fixture.ctx()).fired; + assert!( + later.iter().any(|fired| fired.command.id == EditCommands::Paste.id() + && fired.source == FireSource::Menu + && fired.pasted.as_ref().and_then(|content| content.text.as_deref()) == Some("clipboard text")), + "the paste arrives with the clipboard's text: {later:?}" + ); + } +}