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
13 changes: 8 additions & 5 deletions PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@ Status of each Windows feature on Linux. This file describes the tree it ships w
| Region snip on a frozen screen | working | `linux/Capture.cs`, `linux/OverlayWindow.cs` |
| Click grabs the monitor | working | `linux/OverlayWindow.cs` |
| Click grabs the window under the cursor | partial. X11 only | `linux/OverlayWindow.cs`, `src/CaptureOverlay.cs` |
| `F` selects the monitor | partial. Sets the selection; Enter or the mouse confirms. Windows completes immediately. While recording, `F` completes the monitor. | `linux/OverlayWindow.cs` |
| `A` selects all monitors | partial. Same as `F` for a snip. While recording, `A` completes the one monitor under the cursor. | `linux/OverlayWindow.cs` |
| Enter confirms, arrows nudge, Esc and right-click cancel | working | `linux/OverlayWindow.cs` |
| `F` captures the monitor under the cursor immediately | working | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` |
| `A` captures all monitors immediately | working. Recording selects the monitor under the cursor. | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` |
| Enter confirms the selection or monitor; arrows nudge 1 px, or 10 with Shift | working. X11 warps the pointer; Wayland moves the overlay cursor. | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` |
| Esc and right-click cancel the drag first, then the snip | working | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` |
| Mixed-DPI placement | partial. The overlay uses one scale for the whole desktop. | `linux/OverlayWindow.cs` |
| Magnifier with coordinates | working | `linux/OverlayWindow.cs` |
| Magnifier color | working | `linux/OverlayWindow.cs` |
| X11 capture | working | `linux/Capture.cs` |
| Wayland capture (portal screenshot) | working | `linux/Capture.cs`, `linux/DBus.cs` |
| Copy PNG to the clipboard | working. `xclip` on X11, `wl-copy` on Wayland, then Avalonia. | `linux/ClipboardService.cs` |
| Auto-save PNG or JPG | working | `linux/CapturedFrame.cs`, `linux/SettingsWindow.cs` |
| Monthly folders | working | `src/Settings.cs` |
| Preview thumbnail | working | `linux/ThumbnailWindow.cs` |
| Preview thumbnail | working. It does not take focus and opens on the snipped monitor. | `linux/ThumbnailWindow.cs`, `linux/SnipGeometry.cs` |
| Click the preview to annotate | working | `linux/ThumbnailWindow.cs` |
| Drag the preview into another app | working | `linux/ThumbnailWindow.cs`, `src/SmallWindows.cs` |
| Annotation editor | working | `src/EditorForm.cs` |
| Open the editor after a snip | working | `src/Settings.cs` |
| Pin a snip, including from the clipboard | partial. Pin from clipboard or file; editor pin remains Windows-only | `src/SmallWindows.cs` |
| Delayed snip | working | `linux/CountdownWindow.cs`, `linux/App.cs` |
| Delayed snip | working. The countdown sits at the bottom of the monitor under the pointer, does not take focus, and closes before capture. X11 clicks pass through it; Wayland clicks do not. | `linux/CountdownWindow.cs`, `linux/X11ClickThrough.cs` |
| Tray snip waits for the menu to close | working | `linux/App.cs`, `linux/SnipGesture.cs` |
| Repeat the last region | working | `linux/App.cs` |

## Screen recording
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ On Wayland, bind `opensnip --snip`, `opensnip --snip-delay`, or `opensnip --repe
| `A` | All monitors |
| `Enter` | Grab what's selected |
| Arrow keys | Nudge the cursor 1 px (`Shift` for 10 px) |
| `Esc` / right-click | Cancel |
| `Esc` / right-click | Cancel the current drag, or cancel |

On X11 the outline follows the window under the pointer. On Wayland a click grabs the monitor.

Expand Down
43 changes: 27 additions & 16 deletions linux/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sealed class App : Application
readonly List<PinWindow> _pins = new();
readonly List<EditorWindow> _editors = new();
PixelRect? _lastRegion;
PixelRect? _lastScreen;
bool _trayAvailable;
bool _busy;
bool _exiting;
Expand Down Expand Up @@ -130,12 +131,12 @@ async Task StartAsyncCore()
void CreateTray()
{
var menu = new NativeMenu();
menu.Add(Item("Snip", () => StartSnip()));
menu.Add(Item("Snip", () => StartSnip(0, SnipTiming.MenuSettleMs)));
_delayItem = Item($"Snip after delay ({Settings.DelaySeconds}s)", () => StartSnip(Settings.DelaySeconds, SnipTiming.MenuSettleMs));
_recordItem = Item("Record video", () => _ = DelayedRecord());
menu.Add(_recordItem);
_delayItem = Item($"Snip after delay ({Settings.DelaySeconds}s)", () => StartSnip(Settings.DelaySeconds));
menu.Add(_delayItem);
menu.Add(Item("Repeat last region", RepeatLast));
menu.Add(Item("Repeat last region", () => RepeatLast(SnipTiming.MenuSettleMs)));
menu.Add(new NativeMenuItemSeparator());
menu.Add(Item("Pin image from clipboard", () => _ = PinAsync(null)));
menu.Add(Item("Open image in editor…", () => _ = OpenImageAsync()));
Expand All @@ -155,7 +156,7 @@ void CreateTray()
_tray.Clicked += (_, _) =>
{
if (_recording != null) ToggleRecording();
else StartSnip();
else StartSnip(0, SnipTiming.MenuSettleMs);
};
TrayIcon.SetIcons(this, new TrayIcons { _tray });
UpdateTrayText();
Expand Down Expand Up @@ -480,15 +481,15 @@ async Task OnRecordingSavedAsync(RecordingSession session, string path, Captured
ShowThumbnail(preview, path, Math.Max(Settings.PreviewSeconds, 8), play: true);
}

public async void StartSnip(int delaySeconds = 0)
public async void StartSnip(int delaySeconds = 0, int settleMs = 0)
{
if (_recording != null || _busy) return;
_busy = true;
try
{
bool hadThumbnail = _thumbnail != null;
CloseThumbnail();
if (hadThumbnail) await Task.Delay(100);
await SettleAsync(settleMs, hadThumbnail);
if (delaySeconds > 0) await CountdownWindow.RunAsync(delaySeconds);

var frame = await CaptureService.CaptureAsync();
Expand All @@ -497,8 +498,9 @@ public async void StartSnip(int delaySeconds = 0)
if (region is not { } selected) return;

_lastRegion = selected;
_lastScreen = SnipGeometry.ToScreen(selected, overlay.VirtualBounds, frame.Width, frame.Height);
var cropped = frame.Crop(selected.X, selected.Y, selected.Width, selected.Height);
await DeliverAsync(cropped);
await DeliverAsync(cropped, _lastScreen.Value);
}
catch (CaptureException ex)
{
Expand All @@ -516,12 +518,12 @@ public async void StartSnip(int delaySeconds = 0)
}
}

public async void RepeatLast()
public async void RepeatLast(int settleMs = 0)
{
if (_recording != null || _busy) return;
if (_lastRegion is not { } region)
{
StartSnip();
StartSnip(0, settleMs);
return;
}

Expand All @@ -531,15 +533,16 @@ public async void RepeatLast()
{
bool hadThumbnail = _thumbnail != null;
CloseThumbnail();
if (hadThumbnail) await Task.Delay(100);
await SettleAsync(settleMs, hadThumbnail);
var frame = await CaptureService.CaptureAsync();
if (region.X < 0 || region.Y < 0 || region.Right > frame.Width || region.Bottom > frame.Height)
{
fallback = true;
return;
}
var cropped = frame.Crop(region.X, region.Y, region.Width, region.Height);
await DeliverAsync(cropped);
var screen = _lastScreen ?? SnipGeometry.ToScreen(region, new PixelRect(0, 0, frame.Width, frame.Height), frame.Width, frame.Height);
await DeliverAsync(cropped, screen);
}
catch (CaptureException ex)
{
Expand All @@ -558,6 +561,14 @@ public async void RepeatLast()
}
}

static async Task SettleAsync(int settleMs, bool hadThumbnail)
{
// The tray menu and our own preview are still on screen for a moment.
int wait = settleMs;
if (hadThumbnail) wait = Math.Max(wait, SnipTiming.ThumbnailClearMs);
if (wait > 0) await Task.Delay(wait);
}

static PixelRect[] WindowsFor(CapturedFrame frame)
{
// Rectangles are root-window pixels, so they only match a full-root capture.
Expand All @@ -566,7 +577,7 @@ static PixelRect[] WindowsFor(CapturedFrame frame)
return snap.Windows;
}

async Task DeliverAsync(CapturedFrame frame)
async Task DeliverAsync(CapturedFrame frame, PixelRect screenRect)
{
bool copied = false;
if (Settings.CopyToClipboard)
Expand Down Expand Up @@ -604,11 +615,11 @@ async Task DeliverAsync(CapturedFrame frame)
catch (Exception ex)
{
Log.Error(ex);
ShowThumbnail(frame, saved);
ShowThumbnail(frame, saved, anchor: screenRect);
}
}
else if (Settings.ShowThumbnail || (!copied && saved == null))
ShowThumbnail(frame, saved);
ShowThumbnail(frame, saved, anchor: screenRect);
}

public void OpenEditor(CapturedFrame frame)
Expand Down Expand Up @@ -708,10 +719,10 @@ public async Task OpenImageAsync()
}
}

void ShowThumbnail(CapturedFrame frame, string path, int? seconds = null, bool play = false)
void ShowThumbnail(CapturedFrame frame, string path, int? seconds = null, bool play = false, PixelRect? anchor = null)
{
CloseThumbnail();
var thumbnail = new ThumbnailWindow(frame, path, seconds ?? Settings.PreviewSeconds, play);
var thumbnail = new ThumbnailWindow(frame, path, seconds ?? Settings.PreviewSeconds, play, anchor);
thumbnail.Closed += (_, _) =>
{
if (ReferenceEquals(_thumbnail, thumbnail)) _thumbnail = null;
Expand Down
56 changes: 45 additions & 11 deletions linux/CountdownWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Threading;

namespace OpenSnip;

Expand All @@ -24,6 +25,8 @@ public CountdownWindow(int seconds)
CanResize = false;
Topmost = true;
ShowInTaskbar = false;
ShowActivated = false;
Focusable = false;
Width = 128;
Height = 128;
WindowStartupLocation = WindowStartupLocation.Manual;
Expand All @@ -36,18 +39,47 @@ public CountdownWindow(int seconds)
Child = _number,
};
_number.Text = seconds.ToString();
Place();
}

protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
Place();
PassClicksThrough();
// Avalonia may map the window after OnOpened. Apply the shape again once that settles.
Dispatcher.UIThread.Post(PassClicksThrough, DispatcherPriority.Background);
}

var screen = Screens.Primary
?? Screens.All.FirstOrDefault(candidate => candidate.IsPrimary)
?? Screens.All.FirstOrDefault();
if (screen != null)
void Place()
{
var screen = ScreenUnderPointer();
if (screen == null) return;
double scale = screen.Scaling <= 0 ? 1 : screen.Scaling;
int width = (int)Math.Round(Width * scale);
int height = (int)Math.Round(Height * scale);
int margin = (int)Math.Round(40 * scale);
Position = SnipGeometry.BottomCenter(screen.WorkingArea, width, height, margin);
}

Avalonia.Platform.Screen ScreenUnderPointer()
{
var screens = Screens?.All;
if (screens == null || screens.Count == 0) return null;
if (X11Cursor.TryGet() is { } pointer)
{
var area = screen.WorkingArea;
double scale = screen.Scaling <= 0 ? 1 : screen.Scaling;
Position = new PixelPoint(
(int)(area.X + area.Width / 2.0 - Width * scale / 2),
(int)(area.Y + area.Height / 2.0 - Height * scale / 2));
var hit = Screens.ScreenFromPoint(new PixelPoint(pointer.X, pointer.Y));
if (hit != null) return hit;
}
return Screens.Primary
?? screens.FirstOrDefault(candidate => candidate.IsPrimary)
?? screens[0];
}

void PassClicksThrough()
{
if (TryGetPlatformHandle() is { Handle: var xid } && xid != IntPtr.Zero)
X11ClickThrough.TryEnable(xid);
}

public static async Task RunAsync(int seconds)
Expand All @@ -65,8 +97,10 @@ public static async Task RunAsync(int seconds)
}
finally
{
window.Close();
// Hide first so the compositor can drop the window before the screen is captured.
try { window.Hide(); } catch { }
await Task.Delay(SnipTiming.CountdownClearMs);
try { window.Close(); } catch { }
}
await Task.Delay(80);
}
}
Loading
Loading