From aae9838c51572b9034c04fc7a4def23c1efb830d Mon Sep 17 00:00:00 2001 From: Devan <68340659+DevanMetz@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:13:47 -0500 Subject: [PATCH] Support native MP4 file drags from Linux previews --- PARITY.md | 2 +- linux/FileUri.cs | 17 ++++++++++ linux/ThumbnailWindow.cs | 31 ++++++++++-------- tests/OpenSnip.Tests/FileUriTests.cs | 48 ++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 tests/OpenSnip.Tests/FileUriTests.cs diff --git a/PARITY.md b/PARITY.md index 5781e49..eeb0007 100644 --- a/PARITY.md +++ b/PARITY.md @@ -53,7 +53,7 @@ Status of each Windows feature on Linux. This file describes the tree it ships w | Mouse cursor in the video | working. XFixes cursor image. | `linux/X11FrameSource.cs` | | Copy the MP4 | partial. `text/uri-list` through `xclip` or `wl-copy`. | `linux/ClipboardService.cs`, `linux/FileUri.cs` | | Preview still that plays on click | working. Play badge; click opens the file. The preview stays at least 8 seconds. | `linux/ThumbnailWindow.cs`, `linux/App.cs` | -| Drag the video preview out | missing | `linux/ThumbnailWindow.cs` | +| Drag the video preview out | implemented. MP4 storage item and `text/uri-list` for native drops; desktop interoperability needs manual verification on X11/Wayland. | `linux/ThumbnailWindow.cs`, `linux/FileUri.cs` | | `Win+Shift+R` | missing. The tray item and `opensnip --record` start and stop a recording. A global key grab is not built in. | `linux/App.cs`, `linux/Cli.cs` | | Tray click stops a recording, otherwise snips | working | `linux/App.cs` | | Snip blocked while recording | working | `linux/App.cs` | diff --git a/linux/FileUri.cs b/linux/FileUri.cs index 7a2bdee..5df0a08 100644 --- a/linux/FileUri.cs +++ b/linux/FileUri.cs @@ -1,9 +1,26 @@ using System.Text; +using Avalonia.Input; +using Avalonia.Platform.Storage; namespace OpenSnip; static class FileUri { + public static readonly DataFormat UriListFormat = + DataFormat.CreateBytesPlatformFormat("text/uri-list"); + + public static DataTransfer CreateDragData(string path, IStorageItem file) + { + // Native targets consume URI lists; Avalonia targets can consume the storage item. + // Keep both representations on one item so a drop represents exactly one file. + var item = new DataTransferItem(); + item.Set(UriListFormat, Encoding.UTF8.GetBytes(FromPath(path) + "\r\n")); + if (file != null) item.SetFile(file); + var data = new DataTransfer(); + data.Add(item); + return data; + } + public static string FromPath(string path) { string full = Path.GetFullPath(path); diff --git a/linux/ThumbnailWindow.cs b/linux/ThumbnailWindow.cs index 0417e4b..afb11b6 100644 --- a/linux/ThumbnailWindow.cs +++ b/linux/ThumbnailWindow.cs @@ -15,6 +15,7 @@ sealed class ThumbnailWindow : Window { readonly PixelRect? _anchor; readonly CapturedFrame _frame; + readonly bool _play; readonly DispatcherTimer _timer; string _path; Point _press; @@ -24,6 +25,7 @@ sealed class ThumbnailWindow : Window public ThumbnailWindow(CapturedFrame frame, string savedPath, int seconds, bool play = false, PixelRect? anchor = null) { _frame = frame; + _play = play; _path = savedPath; _anchor = anchor; Title = "OpenSnip"; @@ -70,7 +72,7 @@ public ThumbnailWindow(CapturedFrame frame, string savedPath, int seconds, bool Child = picture, }; - _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(Math.Max(1, seconds)) }; + _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(Math.Max(play ? 8 : 1, seconds)) }; _timer.Tick += (_, _) => Close(); Opened += (_, _) => { @@ -175,37 +177,38 @@ void OnReleased(object sender, PointerReleasedEventArgs e) e.Pointer.Capture(null); if (!_pressed) return; _pressed = false; - try { App.Instance?.OpenEditor(_frame); } + try + { + if (_play) PlatformInfo.Open(_path); + else App.Instance?.OpenEditor(_frame); + } catch (Exception ex) { Log.Error(ex); } Close(); e.Handled = true; } - void StartDrag(PointerEventArgs e) + async void StartDrag(PointerEventArgs e) { try { string path = EnsureDragFile(); - var file = StorageProvider.TryGetFileFromPathAsync(path).GetAwaiter().GetResult(); - if (file == null) - { - AbandonDrag(); - return; - } - var data = new DataTransfer(); - data.Add(DataTransferItem.CreateFile(file)); - _ = FinishDragAsync(e, data); + using var file = await StorageProvider.TryGetFileFromPathAsync(path); + using var data = FileUri.CreateDragData(path, file); + await FinishDragAsync(e, data); } catch (Exception ex) { Log.Error(ex); AbandonDrag(); } + finally + { + e.Pointer.Capture(null); + } } void AbandonDrag() { - _dragged = false; _timer.Start(); } @@ -227,6 +230,8 @@ async Task FinishDragAsync(PointerEventArgs e, DataTransfer data) string EnsureDragFile() { if (!string.IsNullOrEmpty(_path) && File.Exists(_path)) return _path; + // A missing recording must never turn into a PNG drag of its poster frame. + if (_play) throw new FileNotFoundException("Recording is no longer available.", _path); string path = CapturedFrame.UniquePath(Path.Combine(Path.GetTempPath(), "OpenSnip"), "png"); _frame.Save(path, "png"); _path = path; diff --git a/tests/OpenSnip.Tests/FileUriTests.cs b/tests/OpenSnip.Tests/FileUriTests.cs new file mode 100644 index 0000000..2fa32a9 --- /dev/null +++ b/tests/OpenSnip.Tests/FileUriTests.cs @@ -0,0 +1,48 @@ +using System.Text; +using Avalonia.Input; +using Avalonia.Platform.Storage; + +namespace OpenSnip.Tests; + +public class FileUriTests +{ + [Theory] + [InlineData("/tmp/clip.mp4", "file:///tmp/clip.mp4")] + [InlineData("/tmp/a b#?%\".mp4", "file:///tmp/a%20b%23%3F%25%22.mp4")] + [InlineData("/tmp/日本語🎥.mp4", "file:///tmp/%E6%97%A5%E6%9C%AC%E8%AA%9E%F0%9F%8E%A5.mp4")] + [InlineData("/tmp/a\r\nb.mp4", "file:///tmp/a%0D%0Ab.mp4")] + public void Uri_list_encodes_one_file_with_crlf(string path, string uri) + { + using var data = FileUri.CreateDragData(path, null); + var item = Assert.Single(data.Items); + Assert.Equal(uri + "\r\n", Encoding.UTF8.GetString(item.TryGetValue(FileUri.UriListFormat))); + Assert.Equal(uri, FileUri.FromPath(path)); + Assert.Equal("text/uri-list", FileUri.UriListFormat.Identifier); + Assert.Null(item.TryGetFile()); + } + + [Fact] + public void Storage_item_and_native_uri_represent_the_same_mp4() + { + // Avalonia seals storage implementations behind internal constructors. Use its + // real local-file implementation without starting a desktop or file picker. + var type = typeof(IStorageItem).Assembly.GetType("Avalonia.Platform.Storage.FileIO.BclStorageFile", true); + using var file = (IStorageItem)Activator.CreateInstance(type, + System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic, + null, new object[] { new FileInfo("/tmp/recording.mp4") }, null); + using var data = FileUri.CreateDragData(file.Path.LocalPath, file); + var item = Assert.Single(data.Items); + Assert.Same(file, item.TryGetFile()); + Assert.Equal("file:///tmp/recording.mp4\r\n", + Encoding.UTF8.GetString(item.TryGetValue(FileUri.UriListFormat))); + Assert.Equal(2, item.Formats.Count); + } + + [Fact] + public void Relative_paths_are_made_absolute() + { + string uri = FileUri.FromPath("recording.mp4"); + Assert.Equal(Path.GetFullPath("recording.mp4"), new Uri(uri).LocalPath); + } + +}