Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

#697: Paste cut clips from the keyboard even when old OS files linger

Vikram Bhaskaran199223f3b994
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 5b6208ed6..c1d3cb816 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -322,6 +322,9 @@ pub struct FilmcraftApp {
pub command_inbox: Option<Receiver<String>>,
/// A V key press (or a Ctrl+V paste) reached the app and its release hasn't yet.
v_down: bool,
+ /// OS files/image that were already on the clipboard when the last Copy/Cut ran. Paste ignores
+ /// that same media so it pastes the copied clips instead (#697).
+ pub(crate) stale_clipboard_media: Option<String>,
/// GPU compositor (when running on wgpu): device state + compositor + the egui texture it feeds.
pub gpu: Option<GpuState>,
/// Preview render job being watched (job id, where to start playing when it completes).
@@ -535,6 +538,7 @@ impl FilmcraftApp {
tl: Default::default(),
command_inbox: None,
v_down: false,
+ stale_clipboard_media: None,
gpu: None,
watched_render: None,
applied_prefs: None,
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 1cca0b691..a6b8fea04 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -510,8 +510,9 @@ pub fn invoke(app: &mut FilmcraftApp, ctx: &egui::Context, id: &str, mut params:
return Ok(json!({"dialog": "revert"}));
}
// Files or an image on the system clipboard paste into the Timeline, or import into the
- // Project panel (#611). Copying clips puts their names there as text, so media on the
- // clipboard was copied after them; without media, Paste pastes the copied clips.
+ // Project panel (#611). Copying clips puts their names there as text, so media copied
+ // *after* them still wins; media that was already there when Copy/Cut ran is ignored so
+ // Paste of cut/copied clips still works (#697).
"edit.paste" | "edit.pasteInsert"
if params.as_object().is_none_or(|m| m.is_empty()) && matches!(app.ui.focused, PanelKind::Timeline | PanelKind::Project) =>
{
@@ -556,11 +557,32 @@ pub fn invoke(app: &mut FilmcraftApp, ctx: &egui::Context, id: &str, mut params:
app.ui.status = e.clone();
}
if r.is_ok() && matches!(id, "edit.copy" | "edit.cut") {
+ remember_stale_clipboard_media(app);
put_clip_names_on_system_clipboard(app, ctx);
}
r
}
+/// Snapshot OS files/image present at Copy/Cut. Many platforms keep a previous file list after
+/// `copy_text`, so Paste would otherwise import those files instead of the clips (#697).
+fn remember_stale_clipboard_media(app: &mut FilmcraftApp) {
+ app.stale_clipboard_media = app.hooks.clipboard_media.as_mut().and_then(|f| f()).as_ref().map(clipboard_media_key);
+}
+
+fn clipboard_media_key(media: &crate::ClipboardMedia) -> String {
+ match media {
+ crate::ClipboardMedia::Files(paths) => format!("files:{}", paths.join("\0")),
+ crate::ClipboardMedia::Image { width, height, rgba } => {
+ let mut h: u64 = 0xcbf2_9ce4_8422_2325;
+ for b in rgba {
+ h ^= u64::from(*b);
+ h = h.wrapping_mul(0x0100_0000_01b3);
+ }
+ format!("img:{width}x{height}:{h}")
+ }
+ }
+}
+
/// 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
@@ -578,6 +600,9 @@ fn put_clip_names_on_system_clipboard(app: &FilmcraftApp, ctx: &egui::Context) {
/// imported into the shown bin in the Project panel. None when the clipboard holds neither.
fn paste_clipboard_media(app: &mut FilmcraftApp, insert: bool) -> Option<Result<Value, String>> {
let media = (app.hooks.clipboard_media.as_mut()?)()?;
+ if app.stale_clipboard_media.as_ref() == Some(&clipboard_media_key(&media)) {
+ return None;
+ }
let paths = match media {
crate::ClipboardMedia::Files(paths) => paths,
crate::ClipboardMedia::Image { width, height, rgba } => match encode_png(width, height, rgba)
diff --git a/crates/ui-egui/tests/clipboard_paste_ui.rs b/crates/ui-egui/tests/clipboard_paste_ui.rs
index 86beeb73c..c01d2e165 100644
--- a/crates/ui-egui/tests/clipboard_paste_ui.rs
+++ b/crates/ui-egui/tests/clipboard_paste_ui.rs
@@ -185,12 +185,14 @@ fn without_media_ctrl_v_pastes_copied_clips() {
let asked = d.clipboard(None);
let first = d.clips().into_iter().find(|c| c.0 == 'V' && c.1 == 0).unwrap();
d.ok("engine.execute", json!({"command": "timeline.select", "params": {"clips": [first.2]}}));
- d.ok("engine.execute", json!({"command": "edit.copy", "params": {}}));
+ d.ok("ui.menu.invoke", json!({"id": "edit.copy"}));
let copies = |d: &mut Driver| d.clips().iter().filter(|c| c.0 == 'V' && c.3 == first.3).count();
let n = copies(&mut d);
+ let asked_after_copy = asked.get();
+ assert!(asked_after_copy >= 1, "Copy looks at lingering OS media");
d.ctrl_v_without_text(egui::Modifiers::COMMAND);
assert_eq!(copies(&mut d), n + 1, "the copied clip is pasted");
- assert_eq!(asked.get(), 1);
+ assert_eq!(asked.get(), asked_after_copy + 1);
for pressed in [true, false] {
d.harness.input_mut().events.push(egui::Event::Key {
key: egui::Key::V,
@@ -202,5 +204,48 @@ fn without_media_ctrl_v_pastes_copied_clips() {
d.frames(1);
}
d.frames(2);
- assert_eq!((copies(&mut d), asked.get()), (n + 1, 1), "V alone pastes nothing");
+ assert_eq!((copies(&mut d), asked.get()), (n + 1, asked_after_copy + 1), "V alone pastes nothing");
+}
+
+/// Files left on the OS clipboard from before Copy/Cut must not steal Paste: Ctrl+V pastes the
+/// cut clip, as the Edit menu does (#697).
+#[test]
+fn ctrl_v_pastes_a_cut_clip_even_when_old_files_linger_on_the_clipboard() {
+ let mut d = Driver::demo("cut-linger");
+ d.ok("ui.set", json!({"focused": "Timeline"}));
+ let png = d.dir.join("stale.png");
+ image::RgbaImage::from_pixel(8, 8, image::Rgba([0, 0, 200, 255])).save(&png).unwrap();
+ let path = png.to_string_lossy().into_owned();
+ d.app().hooks.clipboard_media = Some(Box::new(move || Some(ClipboardMedia::Files(vec![path.clone()]))));
+ let first = d.clips().into_iter().find(|c| c.0 == 'V' && c.1 == 0).unwrap();
+ d.ok("engine.execute", json!({"command": "timeline.select", "params": {"clips": [first.2]}}));
+ d.ok("ui.menu.invoke", json!({"id": "edit.cut"}));
+ assert!(d.clips().iter().all(|c| c.2 != first.2), "the clip left the timeline");
+ let named = |d: &mut Driver| d.clips().iter().filter(|c| c.3 == first.3).count();
+ assert_eq!(named(&mut d), 0);
+ d.ctrl_v_with_text();
+ assert!(named(&mut d) >= 1, "the cut clip is pasted");
+ assert!(!d.clips().iter().any(|c| c.3.contains("stale")), "lingering OS files are not pasted");
+}
+
+/// Copy from the keyboard (Ctrl+C as `Event::Copy`) also snapshots lingering OS media, so Ctrl+V
+/// pastes the clips rather than those files.
+#[test]
+fn ctrl_c_then_ctrl_v_pastes_clips_not_lingering_files() {
+ let mut d = Driver::demo("copy-linger");
+ d.ok("ui.set", json!({"focused": "Timeline"}));
+ let png = d.dir.join("stale.png");
+ image::RgbaImage::from_pixel(8, 8, image::Rgba([0, 200, 0, 255])).save(&png).unwrap();
+ let path = png.to_string_lossy().into_owned();
+ d.app().hooks.clipboard_media = Some(Box::new(move || Some(ClipboardMedia::Files(vec![path.clone()]))));
+ let first = d.clips().into_iter().find(|c| c.0 == 'V' && c.1 == 0).unwrap();
+ d.ok("engine.execute", json!({"command": "timeline.select", "params": {"clips": [first.2]}}));
+ d.harness.input_mut().events.extend([egui::Event::ModifiersChanged(egui::Modifiers::COMMAND), egui::Event::Copy]);
+ d.frames(1);
+ d.harness.input_mut().events.push(egui::Event::ModifiersChanged(egui::Modifiers::NONE));
+ d.frames(2);
+ assert!(!d.app().session.state.clipboard.is_empty());
+ let n = d.clips().iter().filter(|c| c.0 == 'V' && c.3 == first.3).count();
+ d.ctrl_v_with_text();
+ assert_eq!(d.clips().iter().filter(|c| c.0 == 'V' && c.3 == first.3).count(), n + 1);
}