Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
17 changes: 17 additions & 0 deletions linux/FileUri.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
using System.Text;
using Avalonia.Input;
using Avalonia.Platform.Storage;

namespace OpenSnip;

static class FileUri
{
public static readonly DataFormat<byte[]> 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);
Expand Down
31 changes: 18 additions & 13 deletions linux/ThumbnailWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sealed class ThumbnailWindow : Window
{
readonly PixelRect? _anchor;
readonly CapturedFrame _frame;
readonly bool _play;
readonly DispatcherTimer _timer;
string _path;
Point _press;
Expand All @@ -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";
Expand Down Expand Up @@ -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 += (_, _) =>
{
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions tests/OpenSnip.Tests/FileUriTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}
Loading