diff --git a/crates/ui-egui/src/framework.rs b/crates/ui-egui/src/framework.rs index 9a8a3e7de..a2194aa97 100644 --- a/crates/ui-egui/src/framework.rs +++ b/crates/ui-egui/src/framework.rs @@ -96,11 +96,13 @@ impl EffectcraftApp { } /// The root values of this frame. EffectCraft registers no commands with the runtime (its own shortcut table runs - /// every key, [`command_overrides`]) and lets it toggle no chrome, so the output carries nothing it acts on. The + /// every key, [`command_overrides`]) and lets it toggle no chrome. The one output it acts on is the clipboard + /// chords of a window with no canvas ([`clipboard_events`]). The /// interface scale is Settings > Appearance > UI Scale; the runtime writes it to the context. While one of /// EffectCraft's own dialogs or a command's file dialog is up, the runtime blocks the panels floated into windows /// of their own. - pub(crate) fn frame_input(&mut self, ctx: &egui::Context, _output: FrameOutput) -> FrameInput { + pub(crate) fn frame_input(&mut self, ctx: &egui::Context, output: FrameOutput) -> FrameInput { + self.forwarded.clips.extend(clipboard_events(&output)); let mode = if self.tokens.kind.is_light() { craft_theme::Mode::Light } else { craft_theme::Mode::Dark }; let scale = self.session.prefs.appearance.ui_scale as f32 / 100.0; let root = RootEnv::new().mode(mode).scale(Scale::new(scale)); @@ -115,6 +117,31 @@ impl EffectcraftApp { } } +/// The Copy, Cut and Paste the runtime took in a window with no canvas, as the clipboard events that window's chord +/// would have been. Only a command a key press fired, and a Paste only with the platform's content, which a key press +/// never carries (so a pasted key press cannot run twice). A Copy or Cut from a synthetic key press that egui kept +/// as well runs twice. Native only: the web sends the key press with the clipboard event. +#[cfg(not(target_arch = "wasm32"))] +fn clipboard_events(output: &FrameOutput) -> Vec { + use craft_ui::runtime::commands::{EditCommands, FirePhase, FireSource}; + output + .fired + .iter() + .filter(|fired| fired.source == FireSource::Key && fired.phase == FirePhase::Press && fired.target.is_none()) + .filter_map(|fired| match fired.command.id.as_str() { + id if id == EditCommands::Copy.id() => Some(egui::Event::Copy), + id if id == EditCommands::Cut.id() => Some(egui::Event::Cut), + id if id == EditCommands::Paste.id() => fired.pasted.as_ref()?.text.as_ref().map(|text| egui::Event::Paste(text.to_string())), + _ => None, + }) + .collect() +} + +#[cfg(target_arch = "wasm32")] +fn clipboard_events(_: &FrameOutput) -> Vec { + Vec::new() +} + /// The Composition viewer's widget, its window's document canvas. pub(crate) fn viewer_id() -> egui::Id { egui::Id::unique("viewer-interact") @@ -167,3 +194,31 @@ fn take_window_intent(ctx: &egui::Context, intent: Intent) -> bool { pub(crate) fn decide(close: bool) -> CloseDecision { if close { CloseDecision::Close } else { CloseDecision::Hold } } + +#[cfg(test)] +mod tests { + use super::*; + use craft_ui::runtime::commands::{ClipContent, CommandRef, FirePhase, FireSource, Fired}; + + fn fired(id: &str, pasted: Option<&str>) -> Fired { + let mut fired = Fired::new(CommandRef::new(id), FireSource::Key, FirePhase::Press, 0.0); + fired.pasted = pasted.map(|text| { + let mut content = ClipContent::default(); + content.text = Some(text.into()); + content + }); + fired + } + + /// Copy pressed in a window with no canvas reaches the shortcuts once; a Paste with no content of its own is a key + /// press the app already reads, and so is any other edit command. + #[test] + fn clipboard_chords_of_a_window_with_no_canvas_queue_once() { + let mut output = FrameOutput::default(); + output.fired = vec![fired("edit.copy", None), fired("edit.paste", None), fired("edit.undo", None), fired("edit.paste", Some("x"))]; + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let ctx = egui::Context::default(); + app.frame_input(&ctx, output); + assert_eq!(app.forwarded.clips, [egui::Event::Copy, egui::Event::Paste("x".into())]); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 158b6d515..423408c28 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -582,6 +582,9 @@ impl FileDialogsUp { #[derive(Default)] pub(crate) struct ForwardedInput { pub(crate) events: Vec, + /// Copy, Cut and Paste the runtime took from a window with no canvas ([`crate::framework`]), for the + /// next pass of the shortcuts to read after that pass's own events. + pub(crate) clips: Vec, pub(crate) files: Vec, /// Files handed over by a floating window whose panel under the pointer was the Timeline. The /// paths lose the window they came from, so this keeps the one thing the import still needs diff --git a/crates/ui-egui/src/menus/shortcuts.rs b/crates/ui-egui/src/menus/shortcuts.rs index 655d5c0b9..703a9cd7c 100644 --- a/crates/ui-egui/src/menus/shortcuts.rs +++ b/crates/ui-egui/src/menus/shortcuts.rs @@ -278,7 +278,7 @@ fn cancel_dialog(app: &mut EffectcraftApp, ctx: &egui::Context) { type KeyEvent = (egui::Key, egui::Modifiers, bool, Option); /// The frame's key presses, with clipboard events mapped back to Ctrl+C / X / V. -fn collect_key_events(ctx: &egui::Context, start: Option, clipboard: bool, project: bool) -> Vec { +fn collect_key_events(ctx: &egui::Context, start: Option, clipboard: bool, project: bool, queued: &[egui::Event]) -> Vec { ctx.input(|i| { // The windowing layer turns Ctrl+C / Ctrl+X / Ctrl+V into clipboard events instead of // key presses: map them back to the keys (with the modifiers held) so Edit ▸ Copy, Cut, @@ -290,7 +290,7 @@ fn collect_key_events(ctx: &egui::Context, start: Option, clipb let mut at = if changed { start } else { Some(i.modifiers) }; let held = |at: Option| at.filter(|m| m.command).unwrap_or(egui::Modifiers::COMMAND); let mut out = Vec::new(); - for e in &i.events { + for e in i.events.iter().chain(queued) { match e { egui::Event::ModifiersChanged(m) => at = Some(*m), // egui forgets the modifiers held when the window loses focus. @@ -349,6 +349,8 @@ fn panel_owns_enter(kind: PanelKind) -> bool { } fn handle_shortcuts_impl(app: &mut EffectcraftApp, ctx: &egui::Context, tab_only: bool) { + // The clipboard chords of a window with no canvas wait for this frame's pass only, whatever stops it. + let queued = if tab_only { Vec::new() } else { std::mem::take(&mut app.forwarded.clips) }; let start = modifiers_at_start(ctx); if !tab_only && app.dialog.is_some() && ctx.input(|i| i.key_pressed(egui::Key::Escape)) { escape_dialog(app, ctx); @@ -370,7 +372,7 @@ fn handle_shortcuts_impl(app: &mut EffectcraftApp, ctx: &egui::Context, tab_only // The paste event's text: Edit ▸ Paste pastes SVG markup as a shape layer. let pasted = if clipboard { ctx.input(|i| i.events.iter().rev().find_map(|e| if let egui::Event::Paste(t) = e { Some(t.clone()) } else { None })) } else { None }; - let mut events = collect_key_events(ctx, start, clipboard, project); + let mut events = collect_key_events(ctx, start, clipboard, project, &queued); events.retain(|(key, _, _, _)| (*key == egui::Key::Tab) == tab_only); if !tab_only && clipboard && app.dialog.is_none() { numpad_star_marker(app, ctx);