Skip to content

Commit 928fc2b

Browse files
sergiiopmclaude
andcommitted
feat: add an option to show the lens in recordings
The default lens copies the screen beneath it, so it has to exclude itself from capture, which also hides it from screenshots, OBS, Loom, Snipping Tool and screen sharing. The new "Show lens in recordings" option switches to a lens that hosts the Magnification API's magnifier control instead. The control renders the desktop while skipping the lens's own windows, so the lens can stay visible to capture tools. Corners come from a window region (not anti-aliased) and the shadow lives in a separate layered window. To share code between both variants: - ILens: common interface used by the app - LensChrome: rounded mask, shadow and edge layers - LensAnimation: appear / disappear animation - DibSurface: 32-bit surface that presents scaled, faded frames - Magnification: process-wide MagInitialize / MagUninitialize Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bc375b7 commit 928fc2b

11 files changed

Lines changed: 751 additions & 345 deletions

‎src/DibSurface.cs‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Drawing.Drawing2D;
4+
using System.Drawing.Imaging;
5+
using System.Runtime.InteropServices;
6+
7+
namespace Loupe
8+
{
9+
/// <summary>A top-down 32-bit premultiplied-alpha bitmap selected into a memory DC.</summary>
10+
sealed class DibSurface : IDisposable
11+
{
12+
public readonly int Width;
13+
public readonly int Height;
14+
public IntPtr DC { get; private set; }
15+
public IntPtr Bits { get; private set; }
16+
17+
IntPtr bitmap, previousBitmap;
18+
DibSurface scratch; // receives scaled frames during the appear / disappear animation
19+
20+
public DibSurface(int width, int height)
21+
{
22+
Width = width;
23+
Height = height;
24+
25+
var info = new Native.BITMAPINFOHEADER
26+
{
27+
biSize = Marshal.SizeOf(typeof(Native.BITMAPINFOHEADER)),
28+
biWidth = width,
29+
biHeight = -height, // top-down
30+
biPlanes = 1,
31+
biBitCount = 32,
32+
};
33+
34+
IntPtr screenDC = Native.GetDC(IntPtr.Zero);
35+
DC = Native.CreateCompatibleDC(screenDC);
36+
IntPtr bits;
37+
bitmap = Native.CreateDIBSection(screenDC, ref info, 0, out bits, IntPtr.Zero, 0);
38+
Bits = bits;
39+
previousBitmap = Native.SelectObject(DC, bitmap);
40+
Native.ReleaseDC(IntPtr.Zero, screenDC);
41+
}
42+
43+
/// <summary>
44+
/// Shows this surface in a per-pixel-alpha layered window at <paramref name="position"/>,
45+
/// scaled around its centre and faded according to the animation's appearance.
46+
/// </summary>
47+
public void Present(IntPtr hwnd, Point position, float appearance)
48+
{
49+
IntPtr source = DC;
50+
if (appearance < 0.999f)
51+
{
52+
if (scratch == null) scratch = new DibSurface(Width, Height);
53+
DrawScaled(scratch, LensAnimation.ScaleFor(appearance));
54+
source = scratch.DC;
55+
}
56+
57+
var dst = new Native.POINT { X = position.X, Y = position.Y };
58+
var size = new Native.SIZE { cx = Width, cy = Height };
59+
var origin = new Native.POINT();
60+
var blend = new Native.BLENDFUNCTION
61+
{
62+
BlendOp = Native.AC_SRC_OVER,
63+
SourceConstantAlpha = (byte)Math.Round(255 * Math.Max(0f, Math.Min(1f, appearance))),
64+
AlphaFormat = Native.AC_SRC_ALPHA,
65+
};
66+
Native.UpdateLayeredWindow(hwnd, IntPtr.Zero, ref dst, ref size, source, ref origin, 0,
67+
ref blend, Native.ULW_ALPHA);
68+
}
69+
70+
void DrawScaled(DibSurface target, float scale)
71+
{
72+
using (var src = Wrap())
73+
using (var dst = target.Wrap())
74+
using (var g = Graphics.FromImage(dst))
75+
{
76+
g.Clear(Color.Transparent);
77+
g.InterpolationMode = InterpolationMode.Bilinear;
78+
g.PixelOffsetMode = PixelOffsetMode.Half;
79+
80+
float w = Width * scale, h = Height * scale;
81+
g.DrawImage(src, new RectangleF((Width - w) / 2, (Height - h) / 2, w, h));
82+
}
83+
}
84+
85+
Bitmap Wrap()
86+
{
87+
return new Bitmap(Width, Height, Width * 4, PixelFormat.Format32bppPArgb, Bits);
88+
}
89+
90+
public void Dispose()
91+
{
92+
if (scratch != null) scratch.Dispose();
93+
scratch = null;
94+
if (DC == IntPtr.Zero) return;
95+
Native.SelectObject(DC, previousBitmap);
96+
Native.DeleteObject(bitmap);
97+
Native.DeleteDC(DC);
98+
DC = Bits = bitmap = previousBitmap = IntPtr.Zero;
99+
}
100+
}
101+
}

‎src/FullscreenMagnifier.cs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ namespace Loupe
77
/// <summary>Whole-screen zoom on the primary monitor via the Windows Magnification API.</summary>
88
sealed class FullscreenMagnifier : IDisposable
99
{
10-
bool initialized;
1110
bool active;
1211

1312
public void Apply(Point cursor, double zoom)
1413
{
15-
if (!initialized) initialized = Native.MagInitialize();
16-
if (!initialized) return;
14+
if (!Magnification.EnsureInitialized()) return;
1715

1816
// Keep the pointer at the centre of the magnified view, clamped to the screen edges.
1917
Rectangle screen = Screen.PrimaryScreen.Bounds;
@@ -35,8 +33,6 @@ public void Reset()
3533
public void Dispose()
3634
{
3735
Reset();
38-
if (initialized) Native.MagUninitialize();
39-
initialized = false;
4036
}
4137
}
4238
}

‎src/ILens.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Drawing;
3+
4+
namespace Loupe
5+
{
6+
/// <summary>A floating magnifier that follows the pointer.</summary>
7+
interface ILens : IDisposable
8+
{
9+
/// <summary>True while the lens is on screen, including during its exit animation.</summary>
10+
bool IsShown { get; }
11+
12+
/// <summary>
13+
/// Draws one frame. <paramref name="show"/> is where the lens is heading: it animates in or out
14+
/// towards that state, and hides itself once the exit animation finishes.
15+
/// </summary>
16+
void Update(Point cursor, double zoom, Settings settings, bool show);
17+
18+
/// <summary>Hides the lens immediately, without animating.</summary>
19+
void HideLens();
20+
}
21+
}

‎src/LensAnimation.cs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace Loupe
5+
{
6+
/// <summary>
7+
/// Appear / disappear animation shared by both lenses: the lens grows from <see cref="StartScale"/>
8+
/// while fading in, and plays the same motion in reverse on the way out.
9+
/// </summary>
10+
sealed class LensAnimation
11+
{
12+
public const float StartScale = 0.9f;
13+
const double ShowSeconds = 0.22;
14+
const double HideSeconds = 0.18;
15+
16+
readonly Stopwatch clock = Stopwatch.StartNew();
17+
double lastFrameTime = -1;
18+
double progress; // linear: 0 = hidden, 1 = fully shown
19+
20+
/// <summary>
21+
/// Advances towards shown or hidden and returns the eased appearance (0..1).
22+
/// Returns false once the exit animation has finished.
23+
/// </summary>
24+
public bool Step(bool show, out float appearance)
25+
{
26+
double now = clock.Elapsed.TotalSeconds;
27+
double elapsed = lastFrameTime < 0 ? 0 : Math.Min(0.05, now - lastFrameTime);
28+
lastFrameTime = now;
29+
30+
progress += show ? elapsed / ShowSeconds : -elapsed / HideSeconds;
31+
progress = Math.Max(0, Math.Min(1, progress));
32+
33+
// Ease-out cubic: fast start, gentle settle. Run backwards it accelerates away on exit,
34+
// and reversing mid-animation never jumps because it's a function of progress alone.
35+
appearance = (float)(1 - Math.Pow(1 - progress, 3));
36+
return show || progress > 0;
37+
}
38+
39+
public void Reset()
40+
{
41+
progress = 0;
42+
lastFrameTime = -1;
43+
}
44+
45+
public static float ScaleFor(float appearance)
46+
{
47+
return StartScale + (1 - StartScale) * appearance;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)