FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#199: make Ctrl+C / Ctrl+X / Ctrl+V work on Windows and Linux
DWAA1660f4221cd1b5c8
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 7829291b9..2130be2e0 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1017,6 +1017,8 @@ impl FilmcraftApp {let focused = self.ui.focused.title();let mut fire = Vec::new();ctx.input_mut(|i| {+ let modifiers = i.modifiers;+ clipboard_events_as_keys(&mut i.events, modifiers);let panel = self.bindings.iter().filter(|b| b.3.as_deref() == Some(focused));let app_wide = self.bindings.iter().filter(|b| b.3.is_none());for (m, k, id, _) in panel.chain(app_wide) {@@ -1564,6 +1566,79 @@ pub fn playback_mix(session: &Session, seq_id: filmcraft_project::ItemId, sr: u3}}+/// The windowing layer (egui-winit, and egui's web backend) reports the clipboard shortcuts as+/// `Event::Copy` / `Event::Cut` / `Event::Paste` instead of key presses: Ctrl+C/X/V on Windows and+/// Linux, plus Ctrl+Insert, Shift+Insert and Shift+Delete on Windows. The shortcut bindings only+/// match key presses, so Copy, Cut, Paste, Paste Insert, Paste Attributes and (on Windows) Ripple+/// Delete never fired from the keyboard (#199). macOS was unaffected because its native menu bar+/// takes the key equivalents first.+///+/// Add the key press each event stands for, with the modifiers held, so the bindings see it. The+/// original event stays for anything else that reads it. Only called when no text field has+/// keyboard focus; text fields keep handling the clipboard themselves.+fn clipboard_events_as_keys(events: &mut Vec<egui::Event>, modifiers: egui::Modifiers) {+ use egui::{Event, Key};+ let keys: Vec<Key> = events+ .iter()+ .filter_map(|e| match e {+ Event::Copy if modifiers.command => Some(Key::C),+ Event::Copy => Some(Key::Copy),+ Event::Cut if modifiers.command => Some(Key::X),+ Event::Cut if modifiers.shift => Some(Key::Delete),+ Event::Cut => Some(Key::Cut),+ Event::Paste(_) if modifiers.command => Some(Key::V),+ Event::Paste(_) if modifiers.shift => Some(Key::Insert),+ Event::Paste(_) => Some(Key::Paste),+ _ => None,+ })+ .collect();+ events.extend(keys.into_iter().map(|key| Event::Key { key, physical_key: Some(key), pressed: true, repeat: false, modifiers }));+}++#[cfg(test)]+mod clipboard_key_tests {+ use egui::{Event, Key, Modifiers};++ fn keys(events: Vec<Event>, m: Modifiers) -> Vec<(Key, Modifiers)> {+ let mut events = events;+ super::clipboard_events_as_keys(&mut events, m);+ events.into_iter().filter_map(|e| if let Event::Key { key, modifiers, pressed: true, .. } = e { Some((key, modifiers)) } else { None }).collect()+ }++ #[test]+ fn ctrl_c_x_v_become_key_presses() {+ let c = Modifiers::COMMAND;+ assert_eq!(keys(vec![Event::Copy], c), vec![(Key::C, c)]);+ assert_eq!(keys(vec![Event::Cut], c), vec![(Key::X, c)]);+ assert_eq!(keys(vec![Event::Paste("x".into())], c), vec![(Key::V, c)]);+ }++ #[test]+ fn extra_modifiers_are_kept() {+ // Paste Insert (Ctrl+Shift+V) and Paste Attributes (Ctrl+Alt+V) also arrive as Paste+ let cs = Modifiers::COMMAND | Modifiers::SHIFT;+ assert_eq!(keys(vec![Event::Paste("x".into())], cs), vec![(Key::V, cs)]);+ let ca = Modifiers::COMMAND | Modifiers::ALT;+ assert_eq!(keys(vec![Event::Paste("x".into())], ca), vec![(Key::V, ca)]);+ }++ #[test]+ fn windows_insert_and_delete_variants() {+ // Shift+Delete (Ripple Delete) arrives as Cut on Windows; Shift+Insert as Paste+ assert_eq!(keys(vec![Event::Cut], Modifiers::SHIFT), vec![(Key::Delete, Modifiers::SHIFT)]);+ assert_eq!(keys(vec![Event::Paste("x".into())], Modifiers::SHIFT), vec![(Key::Insert, Modifiers::SHIFT)]);+ }++ #[test]+ fn other_events_are_left_alone() {+ let mut events = vec![Event::Text("a".into()), Event::Copy];+ super::clipboard_events_as_keys(&mut events, Modifiers::COMMAND);+ assert_eq!(events.len(), 3);+ assert!(matches!(events[0], Event::Text(_)));+ assert!(matches!(events[1], Event::Copy));+ }+}+#[cfg(test)]mod gpu_fallback_tests {use filmcraft_render::plan::{FramePlan, PlanLayer};diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex b020f12c7..a02ecbf9b 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -395,9 +395,25 @@ pub fn invoke(app: &mut FilmcraftApp, ctx: &egui::Context, id: &str, params: Valif let Err(e) = &r {app.ui.status = e.clone();}+ if r.is_ok() && matches!(id, "edit.copy" | "edit.cut") {+ put_clip_names_on_system_clipboard(app, ctx);+ }r}+/// Copied clips live in the session, but on Windows and Linux Ctrl+V only reaches the app when the+/// system clipboard holds text (an empty clipboard sends no paste at all, #199). Put the copied+/// clip names there, like other editors put their own data on the clipboard, so Ctrl+V pastes the+/// clips right after a copy.+fn put_clip_names_on_system_clipboard(app: &FilmcraftApp, ctx: &egui::Context) {+ let names: Vec<&str> = app.session.state.clipboard.iter().map(|(_, _, c)| c.name.as_str()).collect();+ if names.is_empty() {+ return;+ }+ let text = names.join("\n");+ ctx.copy_text(if text.trim().is_empty() { format!("{} clips", names.len()) } else { text });+}+/// A menu tree entry for display / `ui.menu.list`.#[derive(Clone, Debug, serde::Serialize)]pub struct MenuItem {diff --git a/crates/ui-egui/tests/sequence_ui.rs b/crates/ui-egui/tests/sequence_ui.rsindex ad1b300e0..807aaad56 100644--- a/crates/ui-egui/tests/sequence_ui.rs+++ b/crates/ui-egui/tests/sequence_ui.rs@@ -372,3 +372,50 @@ fn later_clips_keep_the_zoom_once_the_sequence_has_content() {let (x_in, x_out) = clip_span(&mut d, clip);assert!(((x_out - x_in) - (secs * 300.0 - 4.0)).abs() < 2.0, "zoom changed: {} px for {secs} s at 300 px/s", x_out - x_in);}++impl Driver {+ /// A clipboard shortcut as Windows and Linux deliver it: egui-winit sends Ctrl+C/X/V as+ /// `Event::Copy`/`Cut`/`Paste`, not as a key press (#199).+ fn clipboard_shortcut(&mut self, ev: egui::Event, modifiers: egui::Modifiers) {+ self.harness.input_mut().events.extend([egui::Event::ModifiersChanged(modifiers), ev]);+ self.frames(1);+ self.harness.input_mut().events.push(egui::Event::ModifiersChanged(egui::Modifiers::NONE));+ self.frames(2);+ }++ /// Names of every clip in the active sequence's video tracks.+ fn video_clip_names(&mut self) -> Vec<String> {+ let q = self.app().session.active_sequence().unwrap();+ q.video_tracks.iter().flat_map(|t| t.items.iter().map(|i| i.name.clone())).collect()+ }+}++#[test]+fn ctrl_c_and_ctrl_v_copy_and_paste_clips() {+ let mut d = Driver::demo();+ d.ok("ui.set", json!({"focused": "Timeline"}));+ let q = d.app().session.active_sequence().unwrap().clone();+ let first = q.video_tracks[0].items[0].clone();+ let end = q.video_tracks.iter().chain(&q.audio_tracks).flat_map(|t| t.items.iter().map(|i| i.end())).max().unwrap();+ let copies = |d: &mut Driver| d.video_clip_names().iter().filter(|n| **n == first.name).count();+ let before = copies(&mut d);+ d.exec("timeline.select", json!({"clips": [first.id.0]}));++ d.clipboard_shortcut(egui::Event::Copy, egui::Modifiers::COMMAND);+ assert!(!d.app().session.state.clipboard.is_empty(), "Ctrl+C copies the selected clip");++ d.exec("playhead.set", json!({"time": end.0}));+ d.clipboard_shortcut(egui::Event::Paste(first.name.clone()), egui::Modifiers::COMMAND);+ assert_eq!(copies(&mut d), before + 1, "Ctrl+V pastes it at the playhead");+}++#[test]+fn ctrl_x_cuts_clips() {+ let mut d = Driver::demo();+ d.ok("ui.set", json!({"focused": "Timeline"}));+ let first = d.app().session.active_sequence().unwrap().video_tracks[0].items[0].clone();+ d.exec("timeline.select", json!({"clips": [first.id.0]}));+ d.clipboard_shortcut(egui::Event::Cut, egui::Modifiers::COMMAND);+ assert!(!d.app().session.state.clipboard.is_empty());+ assert!(d.app().session.active_sequence().unwrap().find_item(first.id).is_none(), "the cut clip leaves the timeline");+}