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
4 changes: 2 additions & 2 deletions PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Status of each Windows feature on Linux. This file describes the tree it ships w
| Esc and right-click cancel the snip | partial | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` |
| Mixed-DPI placement | working on X11, with one window per monitor. Wayland cross-monitor drags are exact when scales match. | `linux/OverlayWindow.cs`, `linux/MonitorMap.cs`, `linux/X11Pointer.cs` |
| X11 outputs left of or above the origin | working. Capture falls back to the root at (0, 0) if the server rejects the bounding box. | `linux/X11Randr.cs`, `linux/Capture.cs` |
| Magnifier with coordinates | working | `linux/OverlayWindow.cs` |
| Magnifier color | working | `linux/OverlayWindow.cs` |
| Pixel-grid magnifier, coordinate, and color | working. 15×15 nearest-neighbor cells stay centered on the cursor pixel at frame edges; the strip shows a swatch and `#RRGGBB`. | `linux/Magnifier.cs`, `linux/OverlayWindow.cs` |
| Magnifier can be turned off | working | `linux/SettingsWindow.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` |
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Run `OpenSnip.exe --diagnostics` to write a report (displays, audio devices, and

The Linux app is a separate native Avalonia build. It covers screenshots, annotation, pinned images, global shortcuts and screen recording on X11.

**What works:** X11 capture through libX11, with `maim`, ImageMagick `import` and `scrot` as fallbacks; Wayland capture through the `xdg-desktop-portal` Screenshot API, with `grim`, `gnome-screenshot` and `spectacle` as fallbacks; region, window, monitor and all-monitor selection with a magnifier and one overlay window per screen at its scale; clipboard as PNG; auto-save as PNG or JPG; preview; annotation; pinned images (under the pointer on X11, centered on Wayland); tray icon; global shortcuts and MP4 recording on X11; and settings. X11 capture includes outputs left of or above the origin.
**What works:** X11 capture through libX11, with `maim`, ImageMagick `import` and `scrot` as fallbacks; Wayland capture through the `xdg-desktop-portal` Screenshot API, with `grim`, `gnome-screenshot` and `spectacle` as fallbacks; region, window, monitor and all-monitor selection with a pixel-grid magnifier showing coordinates and color, and one overlay window per screen at its scale; clipboard as PNG; auto-save as PNG or JPG; preview; annotation; pinned images (under the pointer on X11, centered on Wayland); tray icon; global shortcuts and MP4 recording on X11; and settings. X11 capture includes outputs left of or above the origin.

**Install:** download `OpenSnip-linux-x64.tar.gz` from [Releases](https://github.com/DevanMetz/OpenSnip/releases), then:

Expand Down Expand Up @@ -228,7 +228,7 @@ Issues and pull requests are welcome. The code is plain WinForms + GDI+; recordi
| `linux/Program.cs` / `linux/App.cs` | Linux app startup, single instance, tray and snip flow |
| `linux/Capture.cs` / `linux/DBus.cs` | X11, Wayland portal and external capture backends |
| `linux/X11Windows.cs` | Top-level X11 windows for click-to-capture |
| `linux/OverlayWindow.cs` | Per-monitor Linux selection overlays and magnifier |
| `linux/OverlayWindow.cs` / `linux/Magnifier.cs` | Per-monitor Linux selection overlays and pixel magnifier |
| `linux/MonitorMap.cs` | Desktop-pixel mapping for mixed-DPI snips |
| `linux/RecordingSession.cs` | X11 recording, red outline, and control bar |
| `linux/PinWindow.cs` | Linux pinned images |
Expand Down
161 changes: 161 additions & 0 deletions linux/Magnifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
namespace OpenSnip;

/// <summary>
/// The snip loupe: a 15×15 grid of the frozen frame, centered on the cursor pixel.
/// Cells are sampled from that pixel so the center stays put when the cursor
/// reaches the edge of the frame. A clamped source rectangle would slide it.
/// </summary>
static class Magnifier
{
public const int Cells = 15;
public const int Center = 7;

public readonly record struct Box(double X, double Y, double Width, double Height)
{
public double Right => X + Width;
public double Bottom => Y + Height;

public bool Overlaps(Box other) =>
X < other.Right && Right > other.X && Y < other.Bottom && Bottom > other.Y;
}

public static int ImageCoord(double local, double span, int pixels)
{
if (pixels <= 1 || span <= 0) return 0;
int value = (int)(local * pixels / span);
if (value < 0) return 0;
if (value >= pixels) return pixels - 1;
return value;
}

public static bool TryPixel(CapturedFrame frame, int x, int y, out byte r, out byte g, out byte b)
{
r = g = b = 0;
if (frame == null || (uint)x >= (uint)frame.Width || (uint)y >= (uint)frame.Height) return false;
int i = (y * frame.Width + x) * 4;
b = frame.Bgra[i];
g = frame.Bgra[i + 1];
r = frame.Bgra[i + 2];
return true;
}

public static string Label(int x, int y, byte r, byte g, byte b) =>
$"{x}, {y} #{r:X2}{g:X2}{b:X2}";

/// <summary>Packs a <see cref="Cells"/> square of BGRA pixels. Outside the frame stays black.</summary>
public static void Fill(CapturedFrame frame, int x, int y, byte[] bgra)
{
if (frame == null) throw new ArgumentNullException(nameof(frame));
if (bgra == null || bgra.Length < Cells * Cells * 4)
throw new ArgumentException("The loupe buffer is too small.", nameof(bgra));

int count = Cells * Cells * 4;
for (int i = 0; i < count; i += 4)
{
bgra[i] = 0;
bgra[i + 1] = 0;
bgra[i + 2] = 0;
bgra[i + 3] = 255;
}

for (int row = 0; row < Cells; row++)
{
int sy = y + row - Center;
if ((uint)sy >= (uint)frame.Height) continue;
for (int col = 0; col < Cells; col++)
{
int sx = x + col - Center;
if ((uint)sx >= (uint)frame.Width) continue;
int src = (sy * frame.Width + sx) * 4;
int dst = (row * Cells + col) * 4;
bgra[dst] = frame.Bgra[src];
bgra[dst + 1] = frame.Bgra[src + 1];
bgra[dst + 2] = frame.Bgra[src + 2];
bgra[dst + 3] = 255;
}
}
}

/// <summary>Nearest-neighbor scale of the cell grid into <paramref name="cell"/> screen pixels per cell.</summary>
public static void Expand(byte[] cells, int cell, byte[] dest)
{
if (cell < 1) throw new ArgumentOutOfRangeException(nameof(cell));
if (cells == null || cells.Length < Cells * Cells * 4)
throw new ArgumentException("The loupe buffer is too small.", nameof(cells));
int size = Cells * cell;
if (dest == null || dest.Length < size * size * 4)
throw new ArgumentException("The scaled buffer is too small.", nameof(dest));

int dstStride = size * 4;
for (int row = 0; row < Cells; row++)
{
for (int col = 0; col < Cells; col++)
{
int src = (row * Cells + col) * 4;
byte b = cells[src];
byte g = cells[src + 1];
byte r = cells[src + 2];
byte a = cells[src + 3];
int row0 = row * cell;
int col0 = col * cell;
for (int dy = 0; dy < cell; dy++)
{
int dst = (row0 + dy) * dstStride + col0 * 4;
for (int dx = 0; dx < cell; dx++)
{
dest[dst++] = b;
dest[dst++] = g;
dest[dst++] = r;
dest[dst++] = a;
}
}
}
}
}

/// <summary>
/// Puts the loupe beside the cursor, flipping to stay on screen and off <paramref name="avoid"/>.
/// </summary>
public static Box Place(double cursorX, double cursorY, double width, double height,
double boundsWidth, double boundsHeight, Box? avoid)
{
const double gap = 22;
const double margin = 4;

Box? TryAt(double x, double y)
{
if (x < margin || y < margin ||
x + width > boundsWidth - margin ||
y + height > boundsHeight - margin)
return null;
var rect = new Box(x, y, width, height);
if (avoid is { } blocked && rect.Overlaps(blocked)) return null;
return rect;
}

double right = cursorX + gap;
double left = cursorX - width - gap;
double down = cursorY + gap;
double up = cursorY - height - gap;

var found = TryAt(right, down) ?? TryAt(left, down) ?? TryAt(right, up) ?? TryAt(left, up);
if (found == null && avoid is { } blocked)
{
found = TryAt(blocked.Right + gap, down)
?? TryAt(blocked.X - width - gap, down)
?? TryAt(right, blocked.Bottom + gap)
?? TryAt(right, blocked.Y - height - gap)
?? TryAt(left, blocked.Bottom + gap)
?? TryAt(left, blocked.Y - height - gap);
}
if (found is { } placed) return placed;

double fx = right;
double fy = down;
if (fx + width > boundsWidth - margin) fx = left;
if (fy + height > boundsHeight - margin) fy = up;
fx = Math.Clamp(fx, margin, Math.Max(margin, boundsWidth - width - margin));
fy = Math.Clamp(fy, margin, Math.Max(margin, boundsHeight - height - margin));
return new Box(fx, fy, width, height);
}
}
140 changes: 76 additions & 64 deletions linux/OverlayWindow.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Globalization;
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using Avalonia.VisualTree;

namespace OpenSnip;

Expand Down Expand Up @@ -289,10 +292,18 @@ sealed class CaptureCanvas : Control
readonly int _index;
readonly Typeface _typeface = new("Inter");

byte[] _loupePixels;
byte[] _loupeScaled;
WriteableBitmap _loupe;
int _loupeCell;

static readonly IBrush DimBrush = new SolidColorBrush(Color.FromArgb(150, 0, 0, 0));
static readonly IBrush ReadoutBrush = new SolidColorBrush(Color.FromArgb(238, 38, 38, 44));
static readonly IBrush GridTint = new SolidColorBrush(Color.FromArgb(60, AppTheme.AccentColor.R, AppTheme.AccentColor.G, AppTheme.AccentColor.B));
static readonly IPen AccentPen = new Pen(AppTheme.Accent, 2);
static readonly IPen CrosshairPen = new Pen(AppTheme.Accent, 1);
static readonly IPen CenterWhite = new Pen(Brushes.White, 1);
static readonly IPen CenterBlack = new Pen(Brushes.Black, 1);

public CaptureCanvas(OverlaySession session, int index)
{
Expand Down Expand Up @@ -431,77 +442,78 @@ Rect DrawReadout(DrawingContext context, Rect box, PixelRect selection)

void DrawMagnifier(DrawingContext context, Point cursor, Rect? readout, Rect view)
{
if (!_session.Map.TryToImage(new PixelRect(_session.Snip.Cursor.X, _session.Snip.Cursor.Y, 1, 1), out var pixel))
return;

const double inner = 104;
const double pad = 3;
const double labelHeight = 20;
double total = inner + pad * 2;
var box = PlaceMagnifier(cursor, total, total + labelHeight, readout, view);
int imageX = Math.Clamp(pixel.X, 0, Math.Max(0, _session.Bitmap.PixelSize.Width - 1));
int imageY = Math.Clamp(pixel.Y, 0, Math.Max(0, _session.Bitmap.PixelSize.Height - 1));
int frameW = _session.Bitmap.PixelSize.Width;
int frameH = _session.Bitmap.PixelSize.Height;
const int sourceSize = 24;
int left = imageX - sourceSize / 2;
int top = imageY - sourceSize / 2;
int srcL = Math.Max(0, left), srcT = Math.Max(0, top);
int srcR = Math.Min(frameW, left + sourceSize), srcB = Math.Min(frameH, top + sourceSize);
var source = new Rect(srcL, srcT, srcR - srcL, srcB - srcT);
var dest = new Rect(box.X + pad, box.Y + pad, inner, inner);

context.DrawRectangle(ReadoutBrush, new Pen(AppTheme.Accent, 2), new Rect(box.X, box.Y, total, total + labelHeight), 7, 7);
using (context.PushClip(dest))
context.DrawImage(_session.Bitmap, source, new Rect(dest.X + (srcL - left) * inner / sourceSize, dest.Y + (srcT - top) * inner / sourceSize, source.Width * inner / sourceSize, source.Height * inner / sourceSize));
var center = new Point(dest.X + (imageX - left + 0.5) * inner / sourceSize, dest.Y + (imageY - top + 0.5) * inner / sourceSize);
context.DrawLine(CrosshairPen, new Point(center.X - 7, center.Y), new Point(center.X + 7, center.Y));
context.DrawLine(CrosshairPen, new Point(center.X, center.Y - 7), new Point(center.X, center.Y + 7));

var label = new FormattedText($"{_session.Snip.Cursor.X}, {_session.Snip.Cursor.Y} {_session.Frame.ColorLabel(imageX, imageY)}", CultureInfo.InvariantCulture,
FlowDirection.LeftToRight, _typeface, 11, AppTheme.Muted);
context.DrawText(label, new Point(box.X + (total - label.Width) / 2, box.Y + total + 2));
const double pad = 7;
const double swatch = 10;
const double infoH = 24;

if (!_session.Map.TryToImage(new PixelRect(_session.Snip.Cursor.X, _session.Snip.Cursor.Y, 1, 1), out var pixel)) return;
int imageX = pixel.X;
int imageY = pixel.Y;
Magnifier.TryPixel(_session.Frame, imageX, imageY, out byte red, out byte green, out byte blue);
var label = new FormattedText(Magnifier.Label(_session.Snip.Cursor.X, _session.Snip.Cursor.Y, red, green, blue), CultureInfo.InvariantCulture,
FlowDirection.LeftToRight, _typeface, 12, AppTheme.Text);

int cell = Math.Max(8, (int)Math.Ceiling((label.Width + swatch + pad * 3) / Magnifier.Cells));
double grid = Magnifier.Cells * cell;
double boxW = Math.Max(grid, label.Width + swatch + pad * 3);
double boxH = grid + infoH;
Magnifier.Box? avoid = readout is { } block
? new Magnifier.Box(block.X, block.Y, block.Width, block.Height)
: null;
var placed = Magnifier.Place(cursor.X, cursor.Y, boxW, boxH, view.Width, view.Height, avoid);
var box = new Rect(placed.X, placed.Y, placed.Width, placed.Height);

context.FillRectangle(Brushes.Black, box);

PaintLoupe(context, imageX, imageY, box.X + Math.Round((boxW - grid) / 2), box.Y, grid, cell);

double infoY = box.Y + grid;
context.FillRectangle(new SolidColorBrush(Color.FromRgb(red, green, blue)),
new Rect(box.X + pad, infoY + (infoH - swatch) / 2, swatch, swatch));
context.DrawText(label, new Point(box.X + pad + swatch + pad, infoY + (infoH - label.Height) / 2));
context.DrawRectangle(null, AccentPen, box);
}

static Rect PlaceMagnifier(Point cursor, double width, double height, Rect? readout, Rect view)
void PaintLoupe(DrawingContext context, int imageX, int imageY, double gridX, double gridY, double grid, int cell)
{
const double gap = 22;
const double margin = 4;

Rect? TryAt(double x, double y)
_loupePixels ??= new byte[Magnifier.Cells * Magnifier.Cells * 4];
Magnifier.Fill(_session.Frame, imageX, imageY, _loupePixels);
int size = Magnifier.Cells * cell;
if (_loupe == null || _loupeCell != cell)
{
if (x < margin || y < margin || x + width > view.Width - margin || y + height > view.Height - margin)
return null;
var rect = new Rect(x, y, width, height);
if (readout is { } box && Overlaps(rect, box)) return null;
return rect;
_loupe?.Dispose();
_loupe = new WriteableBitmap(new PixelSize(size, size), new Vector(96, 96), PixelFormat.Bgra8888, AlphaFormat.Opaque);
_loupeScaled = new byte[size * size * 4];
_loupeCell = cell;
}

double right = cursor.X + gap;
double left = cursor.X - width - gap;
double down = cursor.Y + gap;
double up = cursor.Y - height - gap;
var found = TryAt(right, down) ?? TryAt(left, down) ?? TryAt(right, up) ?? TryAt(left, up);
if (found == null && readout is { } blocked)
Magnifier.Expand(_loupePixels, cell, _loupeScaled);
using (var buffer = _loupe.Lock())
{
found = TryAt(blocked.Right + gap, down)
?? TryAt(blocked.Left - width - gap, down)
?? TryAt(right, blocked.Bottom + gap)
?? TryAt(right, blocked.Top - height - gap)
?? TryAt(left, blocked.Bottom + gap)
?? TryAt(left, blocked.Top - height - gap);
int stride = size * 4;
for (int row = 0; row < size; row++)
Marshal.Copy(_loupeScaled, row * stride, IntPtr.Add(buffer.Address, row * buffer.RowBytes), stride);
}
if (found is { } placed) return placed;

double fx = right;
double fy = down;
if (fx + width > view.Width - margin) fx = left;
if (fy + height > view.Height - margin) fy = up;
fx = Math.Clamp(fx, margin, Math.Max(margin, view.Width - width - margin));
fy = Math.Clamp(fy, margin, Math.Max(margin, view.Height - height - margin));
return new Rect(fx, fy, width, height);

// Already nearest-neighbor. Draw 1:1 so the compositor does not blend the cells.
var dest = new Rect(gridX, gridY, grid, grid);
context.DrawImage(_loupe, dest);

double c0 = Magnifier.Center * cell;
double cellSize = cell;
context.FillRectangle(GridTint, new Rect(dest.X, dest.Y + c0, c0, cellSize));
context.FillRectangle(GridTint, new Rect(dest.X + c0 + cellSize, dest.Y + c0, grid - c0 - cellSize, cellSize));
context.FillRectangle(GridTint, new Rect(dest.X + c0, dest.Y, cellSize, c0));
context.FillRectangle(GridTint, new Rect(dest.X + c0, dest.Y + c0 + cellSize, cellSize, grid - c0 - cellSize));

var center = new Rect(dest.X + c0, dest.Y + c0, cellSize, cellSize);
context.DrawRectangle(null, CenterBlack, new Rect(center.X - 1, center.Y - 1, center.Width + 2, center.Height + 2));
context.DrawRectangle(null, CenterWhite, center);
}

static bool Overlaps(Rect a, Rect b) =>
a.Left < b.Right && a.Right > b.Left && a.Top < b.Bottom && a.Bottom > b.Top;
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
_loupe?.Dispose();
_loupe = null;
}
}
Loading
Loading