Repository navigation
Expand file tree
/
Copy pathOverlayForm.cs
More file actions
350 lines (306 loc) · 13.5 KB
/
Copy pathOverlayForm.cs
File metadata and controls
350 lines (306 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
namespace WinTextCapture;
internal sealed class OverlayForm : Form
{
private readonly Settings _settings;
private readonly Bitmap _screenshot;
private readonly Bitmap _dimmedScreenshot;
private Point _startPoint;
private Rectangle _selection;
private bool _dragging;
private bool _processing;
private bool _cancelled;
private readonly Func<Bitmap, Task<CaptureOutcome>> _extract;
private readonly Theme _theme;
private readonly Font _hintFont;
private readonly Font _badgeFont;
/// <summary>Client-area rectangle of the monitor under the cursor; the hint pill sits at its top.</summary>
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
internal Rectangle HintArea { get; set; }
public CaptureOutcome Result { get; private set; } = CaptureOutcome.Cancelled;
public OverlayForm(Settings settings) : this(settings, null, null) { }
internal OverlayForm(Settings settings, Bitmap? screenshot, Func<Bitmap, Task<CaptureOutcome>>? extract)
{
_settings = settings;
_extract = extract ?? ExtractTextAsync;
_theme = Theming.Resolve(settings.Theme);
FormBorderStyle = FormBorderStyle.None;
AutoScaleMode = AutoScaleMode.None; // screenshot and mouse coordinates are physical pixels
Bounds = VirtualScreenBounds();
TopMost = true;
Cursor = Cursors.Cross;
DoubleBuffered = true;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
KeyPreview = true;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
if (screenshot is null)
{
_screenshot = CaptureScreenshot(Bounds);
}
else
{
_screenshot = (Bitmap)screenshot.Clone();
Size = screenshot.Size;
}
try { _dimmedScreenshot = CreateDimmedScreenshot(_screenshot); }
catch { _screenshot.Dispose(); throw; }
// Overlay coordinates are physical pixels, so text sizes follow the window's DPI by hand.
float scale = DeviceDpi / 96f;
_hintFont = new Font("Segoe UI", 14f * scale, GraphicsUnit.Pixel);
_badgeFont = new Font("Segoe UI Semibold", 12f * scale, GraphicsUnit.Pixel);
HintArea = MonitorUnderCursor();
MouseDown += OnMouseDown;
MouseMove += OnMouseMove;
MouseUp += OnMouseUpAsync;
KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Escape)
{
_cancelled = true;
Result = CaptureOutcome.Cancelled;
Close();
}
};
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
Native.SetWindowPos(Handle, Native.HWND_TOPMOST, Bounds.Left, Bounds.Top, Bounds.Width, Bounds.Height, Native.SWP_SHOWWINDOW);
Native.SetForegroundWindow(Handle);
}
protected override void OnDpiChanged(DpiChangedEventArgs e)
{
// A screen-spanning screenshot cannot follow one monitor's suggested logical-size rectangle.
e.Cancel = true;
base.OnDpiChanged(e);
}
private static Rectangle VirtualScreenBounds()
{
int x = Native.GetSystemMetrics(Native.SM_XVIRTUALSCREEN);
int y = Native.GetSystemMetrics(Native.SM_YVIRTUALSCREEN);
int w = Native.GetSystemMetrics(Native.SM_CXVIRTUALSCREEN);
int h = Native.GetSystemMetrics(Native.SM_CYVIRTUALSCREEN);
return new Rectangle(x, y, w, h);
}
private Rectangle MonitorUnderCursor()
{
var client = new Rectangle(Point.Empty, _screenshot.Size);
var monitor = Screen.FromPoint(Cursor.Position).Bounds;
monitor.Offset(-Bounds.Left, -Bounds.Top);
var area = Rectangle.Intersect(client, monitor);
return area.Width > 0 && area.Height > 0 ? area : client;
}
private void OnMouseDown(object? sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left || _processing) return;
_startPoint = e.Location;
_selection = Rectangle.Empty;
_dragging = true;
}
private void OnMouseMove(object? sender, MouseEventArgs e)
{
if (!_dragging) return;
var previous = _selection;
var next = Rectangle.Intersect(new Rectangle(Point.Empty, _screenshot.Size), new Rectangle(
Math.Min(_startPoint.X, e.X),
Math.Min(_startPoint.Y, e.Y),
Math.Abs(_startPoint.X - e.X),
Math.Abs(_startPoint.Y - e.Y)));
if (next == previous) return;
_selection = next;
Invalidate(DamageRect(previous, _selection));
if (!previous.IsEmpty) Invalidate(Inflated(BadgeBounds(previous), 2));
if (!_selection.IsEmpty) Invalidate(Inflated(BadgeBounds(_selection), 2));
}
private async void OnMouseUpAsync(object? sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left || !_dragging || _processing) return;
OnMouseMove(sender, e); // the release can be newer than the last coalesced WM_MOUSEMOVE
_dragging = false;
_processing = true;
// Hiding a modal form ends ShowDialog immediately and disposes this form while OCR is pending.
// A transparent visible form keeps ownership until the asynchronous operation completes.
Opacity = 0;
if (_selection.Width <= 10 || _selection.Height <= 10)
{
Result = CaptureOutcome.Cancelled;
Close();
return;
}
try
{
using var cropped = new Bitmap(_selection.Width, _selection.Height);
using (var g = Graphics.FromImage(cropped))
g.DrawImage(_screenshot, new Rectangle(0, 0, cropped.Width, cropped.Height), _selection, GraphicsUnit.Pixel);
var outcome = await _extract(cropped);
if (_cancelled || IsDisposed) return;
Result = outcome;
}
catch (Exception ex)
{
CrashLogger.Write(ex, fatal: false);
if (_cancelled || IsDisposed) return;
Result = CaptureOutcome.Error;
}
DialogResult = DialogResult.OK;
Close();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
PaintOverlay(e.Graphics, e.ClipRectangle);
}
/// <summary>
/// Draws the overlay surface only: the dimmed screenshot, the clear selection with its accent outline
/// and size badge, and the hint pill. The screenshot bitmaps are read, never drawn on.
/// </summary>
internal void PaintOverlay(Graphics g, Rectangle clip)
{
g.DrawImage(_dimmedScreenshot, clip, clip, GraphicsUnit.Pixel);
if (_selection.Width > 0 && _selection.Height > 0)
{
g.DrawImage(_screenshot, _selection, _selection, GraphicsUnit.Pixel);
using var pen = new Pen(_theme.Accent, 2f);
g.DrawRectangle(pen, _selection);
DrawBadge(g, _selection);
}
DrawHint(g);
}
/// <summary>Test and documentation hook: the selection a drag would have made.</summary>
internal void PreviewSelection(Rectangle selection) =>
_selection = Rectangle.Intersect(new Rectangle(Point.Empty, _screenshot.Size), selection);
internal static string SizeText(Rectangle selection) => $"{selection.Width} × {selection.Height}";
internal Rectangle HintBounds()
{
var text = TextRenderer.MeasureText(Strings.OverlayHint, _hintFont, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
int padX = (int)Math.Round(16 * DeviceDpi / 96f), padY = (int)Math.Round(8 * DeviceDpi / 96f);
int top = (int)Math.Round(24 * DeviceDpi / 96f);
var size = new Size(text.Width + padX * 2, text.Height + padY * 2);
return new Rectangle(HintArea.Left + (HintArea.Width - size.Width) / 2, HintArea.Top + top, size.Width, size.Height);
}
internal Rectangle BadgeBounds(Rectangle selection)
{
if (selection.Width <= 0 || selection.Height <= 0) return Rectangle.Empty;
var text = TextRenderer.MeasureText(SizeText(selection), _badgeFont, Size.Empty, TextFormatFlags.NoPadding);
int pad = (int)Math.Round(6 * DeviceDpi / 96f), gap = (int)Math.Round(6 * DeviceDpi / 96f);
var size = new Size(text.Width + pad * 2, text.Height + pad);
var client = new Rectangle(Point.Empty, _screenshot.Size);
int y = selection.Bottom + gap;
if (y + size.Height > client.Bottom) y = selection.Top - gap - size.Height; // no room below: above
if (y < client.Top) y = selection.Bottom - gap - size.Height; // no room above: inside
int x = Math.Clamp(selection.Left, client.Left, Math.Max(client.Left, client.Right - size.Width));
return new Rectangle(x, y, size.Width, size.Height);
}
private void DrawBadge(Graphics g, Rectangle selection)
{
var bounds = BadgeBounds(selection);
var smoothing = g.SmoothingMode;
g.SmoothingMode = SmoothingMode.AntiAlias;
using (var path = Theming.Rounded(bounds, Math.Max(3, bounds.Height / 4)))
using (var fill = new SolidBrush(_theme.Accent))
g.FillPath(fill, path);
g.SmoothingMode = smoothing;
TextRenderer.DrawText(g, SizeText(selection), _badgeFont, bounds, _theme.AccentText,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding);
}
private void DrawHint(Graphics g)
{
var bounds = HintBounds();
var smoothing = g.SmoothingMode;
g.SmoothingMode = SmoothingMode.AntiAlias;
using (var path = Theming.Rounded(bounds, bounds.Height / 2))
{
using (var fill = new SolidBrush(Color.FromArgb(240, _theme.Surface))) g.FillPath(fill, path);
using (var pen = new Pen(_theme.BorderStrong)) g.DrawPath(pen, path);
}
g.SmoothingMode = smoothing;
TextRenderer.DrawText(g, Strings.OverlayHint, _hintFont, bounds, _theme.Text,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
}
private static Rectangle Inflated(Rectangle r, int by) { r.Inflate(by, by); return r; }
private static Bitmap CreateDimmedScreenshot(Bitmap source)
{
var dimmed = new Bitmap(source.Width, source.Height);
using var g = Graphics.FromImage(dimmed);
g.DrawImageUnscaled(source, Point.Empty);
using var dim = new SolidBrush(Color.FromArgb(125, 0, 0, 0));
g.FillRectangle(dim, new Rectangle(Point.Empty, source.Size));
return dimmed;
}
private static Rectangle DamageRect(Rectangle a, Rectangle b)
{
var damage = a.IsEmpty ? b : Rectangle.Union(a, b);
damage.Inflate(8, 8);
return damage;
}
private async Task<CaptureOutcome> ExtractTextAsync(Bitmap bitmap)
{
string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".png");
try
{
using var ocrBitmap = OcrImagePreprocessor.Prepare(bitmap, checked((int)OcrEngine.MaxImageDimension));
ocrBitmap.Save(tempFile, ImageFormat.Png);
var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(tempFile);
using var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var decoder = await BitmapDecoder.CreateAsync(stream);
using var softwareBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
var engine = CreateOcrEngine();
if (engine == null) return CaptureOutcome.Error;
var result = await engine.RecognizeAsync(softwareBitmap);
if (_cancelled || IsDisposed) return CaptureOutcome.Cancelled;
if (string.IsNullOrWhiteSpace(result.Text))
return CaptureOutcome.NoText;
string text = result.Text.Trim();
return ClipboardWriter.SetTextWithRetry(text)
? CaptureOutcome.Copied(text)
: CaptureOutcome.ClipboardUnchanged;
}
finally
{
try
{
if (File.Exists(tempFile))
File.Delete(tempFile);
}
catch
{
// Temporary file cleanup is best-effort.
}
}
}
private OcrEngine? CreateOcrEngine()
{
if (_settings.OcrLanguage == "auto")
return OcrEngine.TryCreateFromUserProfileLanguages()
?? OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("tr-TR"));
return OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language(_settings.OcrLanguage))
?? OcrEngine.TryCreateFromUserProfileLanguages();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_cancelled = true;
_screenshot.Dispose();
_dimmedScreenshot.Dispose();
_hintFont.Dispose();
_badgeFont.Dispose();
}
base.Dispose(disposing);
}
private static Bitmap CaptureScreenshot(Rectangle bounds)
{
var bitmap = new Bitmap(bounds.Width, bounds.Height);
try
{
using var graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
return bitmap;
}
catch { bitmap.Dispose(); throw; }
}
}