-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverlayWindow.xaml.cs
More file actions
562 lines (487 loc) · 22.8 KB
/
OverlayWindow.xaml.cs
File metadata and controls
562 lines (487 loc) · 22.8 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Shapes;
namespace ScreenGrid
{
public partial class OverlayWindow : Window
{
// ── Zone data ───────────────────────────────────────────────────
private readonly List<GridZone> _zones = new();
private readonly Dictionary<GridZone, Border> _zoneBorders = new();
private GridZone? _highlightedZone;
private Rectangle? _snapPreview;
private int _visiblePairStartRow;
// ── Screen info (physical pixels) ───────────────────────────────
private NativeMethods.RECT _workArea;
private double _dpiScale = 1.0;
// ── Grid configuration (loaded from GridConfig) ─────────────────────────
private GridConfig _gridConfig = GridConfig.CreateDefault();
// ── Colors (computed from appearance settings) ──────────────────
private Color _overlayBg;
private Color _zoneFill;
private Color _zoneBorder;
private Color _highlightFill;
private Color _highlightBorder;
private Color _snapPreviewFill;
private Color _snapPreviewStroke;
private static readonly Color LabelPrimary = Color.FromArgb(230, 255, 255, 255);
private static readonly Color LabelSecondary = Color.FromArgb(130, 255, 255, 255);
private static readonly Color LabelDim = Color.FromArgb(90, 255, 255, 255);
private void UpdateColorsFromAppearance()
{
var a = _gridConfig.Appearance;
_overlayBg = Color.FromArgb((byte)Math.Clamp(a.OverlayAlpha, 0, 255), 10, 10, 15);
_zoneFill = Color.FromArgb((byte)Math.Clamp(a.ZoneFillAlpha, 0, 255), a.ZoneR, a.ZoneG, a.ZoneB);
_zoneBorder = Color.FromArgb((byte)Math.Clamp(a.ZoneBorderAlpha, 0, 255), a.ZoneR, a.ZoneG, a.ZoneB);
_highlightFill = Color.FromArgb((byte)Math.Clamp(a.HighlightFillAlpha, 0, 255), a.HighlightR, a.HighlightG, a.HighlightB);
_highlightBorder = Color.FromArgb((byte)Math.Clamp(a.HighlightBorderAlpha, 0, 255), a.HighlightR, a.HighlightG, a.HighlightB);
_snapPreviewFill = Color.FromArgb((byte)Math.Clamp(a.SnapPreviewFillAlpha, 0, 255), a.SnapPreviewR, a.SnapPreviewG, a.SnapPreviewB);
_snapPreviewStroke = Color.FromArgb((byte)Math.Clamp(a.SnapPreviewBorderAlpha, 0, 255), a.SnapPreviewR, a.SnapPreviewG, a.SnapPreviewB);
}
// Snap preview label
private TextBlock? _snapLabel;
// ─────────────────────────────────────────────────────────────────
public OverlayWindow()
{
InitializeComponent();
UpdateColorsFromAppearance();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
MakeClickThrough();
}
/// <summary>
/// Makes this window transparent to all mouse / keyboard input so it
/// never interferes with the window drag happening underneath.
/// </summary>
private void MakeClickThrough()
{
var hwnd = new WindowInteropHelper(this).Handle;
int exStyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_EXSTYLE);
NativeMethods.SetWindowLong(hwnd, NativeMethods.GWL_EXSTYLE,
exStyle
| NativeMethods.WS_EX_TRANSPARENT
| NativeMethods.WS_EX_TOOLWINDOW
| NativeMethods.WS_EX_NOACTIVATE);
}
// ── Public API ──────────────────────────────────────────────────
/// <summary>
/// Update the grid configuration and rebuild zones on next SetupForScreen.
/// </summary>
public void SetGridConfig(GridConfig config)
{
_gridConfig = config;
_visiblePairStartRow = 0;
UpdateColorsFromAppearance();
}
public void ResetVariantPair()
{
_visiblePairStartRow = 0;
}
public void ScrollVariantPair(int direction)
{
int rowCount = _gridConfig.Rows.Count;
if (rowCount <= 2) return;
int maxStart = Math.Max(0, ((rowCount - 1) / 2) * 2);
int next = _visiblePairStartRow + (direction > 0 ? 2 : -2);
if (next < 0) next = 0;
if (next > maxStart) next = maxStart;
if (next == _visiblePairStartRow) return;
_visiblePairStartRow = next;
_highlightedZone = null;
BuildZones();
RenderZones();
UpdateSnapPreview(null);
}
/// <summary>
/// Configure the overlay for the given monitor work area and rebuild
/// all grid zones. workArea is in physical screen pixels.
/// </summary>
internal void SetupForScreen(NativeMethods.RECT workArea)
{
_workArea = workArea;
_dpiScale = GetDpiScaleForWorkArea(workArea);
// Position & size in WPF DIPs
Left = workArea.Left / _dpiScale;
Top = workArea.Top / _dpiScale;
Width = (workArea.Right - workArea.Left) / _dpiScale;
Height = (workArea.Bottom - workArea.Top) / _dpiScale;
BuildZones();
RenderZones();
}
/// <summary>
/// Returns the zone whose display bounds contain the given point
/// (physical screen coordinates), or null.
/// </summary>
public GridZone? GetZoneAtPoint(int screenX, int screenY)
{
var pt = new Point(screenX, screenY);
foreach (var z in _zones)
if (z.DisplayBounds.Contains(pt))
return z;
return null;
}
/// <summary>
/// Highlight a single zone (and show snap preview). Pass null to clear.
/// </summary>
public void HighlightZone(GridZone? zone)
{
if (zone == _highlightedZone) return;
// Un-highlight previous
if (_highlightedZone != null && _zoneBorders.TryGetValue(_highlightedZone, out var prev))
{
prev.Background = new SolidColorBrush(_zoneFill);
prev.BorderBrush = new SolidColorBrush(_zoneBorder);
prev.BorderThickness = new Thickness(1);
prev.Effect = null;
_highlightedZone.IsHighlighted = false;
}
// Highlight new
if (zone != null && _zoneBorders.TryGetValue(zone, out var cur))
{
cur.Background = new SolidColorBrush(_highlightFill);
cur.BorderBrush = new SolidColorBrush(_highlightBorder);
cur.BorderThickness = new Thickness(2);
cur.Effect = new DropShadowEffect
{
Color = _highlightBorder,
BlurRadius = 24,
ShadowDepth = 0,
Opacity = 0.6
};
zone.IsHighlighted = true;
}
UpdateSnapPreview(zone);
_highlightedZone = zone;
}
public GridZone? GetHighlightedZone() => _highlightedZone;
/// <summary>Fade the overlay in.</summary>
public void ShowOverlay()
{
_highlightedZone = null;
Opacity = 0;
Show();
BeginAnimation(OpacityProperty,
new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(120))
{ EasingFunction = new QuadraticEase() });
}
/// <summary>Fade the overlay out then hide.</summary>
public void HideOverlay(Action? onComplete = null)
{
var anim = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(90));
anim.Completed += (_, _) =>
{
Hide();
onComplete?.Invoke();
};
BeginAnimation(OpacityProperty, anim);
}
// ── Zone building ───────────────────────────────────────────────
private void BuildZones()
{
_zones.Clear();
int screenW = _workArea.Right - _workArea.Left;
int screenH = _workArea.Bottom - _workArea.Top;
int screenLeft = _workArea.Left;
int screenTop = _workArea.Top;
var rows = GetVisibleRows();
int rowCount = rows.Count;
if (rowCount == 0) return;
int rowH = screenH / rowCount;
const int gap = 4;
for (int row = 0; row < rowCount; row++)
{
var rowDef = rows[row];
int dispTop = screenTop + row * rowH;
int totalParts = 0;
foreach (int r in rowDef.Ratios) totalParts += r;
if (totalParts <= 0) continue;
// Height ratios (vertical subdivision)
var heightRatios = rowDef.HasHeightSplit ? rowDef.HeightRatios! : new List<int> { 1 };
int totalHeightParts = 0;
foreach (int hr in heightRatios) totalHeightParts += hr;
int numVRows = heightRatios.Count;
int numCols = rowDef.Ratios.Count;
int xOffset = screenLeft;
for (int col = 0; col < numCols; col++)
{
int w = (int)((double)rowDef.Ratios[col] / totalParts * screenW);
if (col == numCols - 1)
w = screenLeft + screenW - xOffset;
// Vertical sub-rows within this column
int snapYOffset = screenTop;
for (int vRow = 0; vRow < numVRows; vRow++)
{
int snapH = (int)((double)heightRatios[vRow] / totalHeightParts * screenH);
if (vRow == numVRows - 1)
snapH = screenTop + screenH - snapYOffset;
int dispSubH = rowH / numVRows;
int dispSubTop = dispTop + vRow * dispSubH;
if (vRow == numVRows - 1)
dispSubH = dispTop + rowH - dispSubTop;
// Label: combine column label with height label
string colLabel = rowDef.GetColumnLabel(col);
string hLabel = rowDef.GetHeightLabel(vRow);
string label = string.IsNullOrEmpty(hLabel) ? colLabel : $"{colLabel} {hLabel}";
_zones.Add(new GridZone
{
DisplayBounds = new Rect(xOffset + gap, dispSubTop + gap, w - 2 * gap, dispSubH - 2 * gap),
SnapBounds = new Rect(xOffset, snapYOffset, w, snapH),
Label = label,
Row = row,
Column = col,
TotalColumns = numCols
});
snapYOffset += snapH;
}
xOffset += w;
}
}
}
private List<GridRowDef> GetVisibleRows()
{
var rows = _gridConfig.Rows;
if (rows.Count <= 2)
return rows;
if (_visiblePairStartRow >= rows.Count)
_visiblePairStartRow = Math.Max(0, ((rows.Count - 1) / 2) * 2);
int take = Math.Min(2, rows.Count - _visiblePairStartRow);
return rows.GetRange(_visiblePairStartRow, take);
}
// ── Zone rendering ──────────────────────────────────────────────
private void RenderZones()
{
GridCanvas.Children.Clear();
_zoneBorders.Clear();
// Darkened background
var bg = new Rectangle
{
Width = Width,
Height = Height,
Fill = new SolidColorBrush(_overlayBg)
};
GridCanvas.Children.Add(bg);
var visibleRows = GetVisibleRows();
string pairTitle = visibleRows.Count == 2
? $"{visibleRows[0].Name} + {visibleRows[1].Name}"
: visibleRows[0].Name;
int totalPairs = Math.Max(1, (_gridConfig.Rows.Count + 1) / 2);
int currentPair = (_visiblePairStartRow / 2) + 1;
var headerBar = new Border
{
Background = new SolidColorBrush(Color.FromArgb(150, 0, 0, 0)),
BorderBrush = new SolidColorBrush(Color.FromArgb(70, 255, 255, 255)),
BorderThickness = new Thickness(0, 0, 0, 1),
Width = Width,
Height = 68
};
var headerStack = new StackPanel
{
Orientation = Orientation.Vertical,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
Margin = new Thickness(0, 8, 0, 0)
};
headerStack.Children.Add(new TextBlock
{
Text = pairTitle,
Foreground = new SolidColorBrush(Color.FromArgb(235, 255, 255, 255)),
FontSize = 20,
FontWeight = FontWeights.SemiBold,
TextAlignment = TextAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
});
var dots = new StackPanel
{
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Center,
Margin = new Thickness(0, 6, 0, 0)
};
for (int i = 1; i <= totalPairs; i++)
{
bool isActive = i == currentPair;
dots.Children.Add(new Ellipse
{
Width = isActive ? 12 : 8,
Height = isActive ? 12 : 8,
Margin = new Thickness(4, 0, 4, 0),
Fill = new SolidColorBrush(isActive
? Color.FromArgb(230, 0, 170, 255)
: Color.FromArgb(110, 255, 255, 255)),
Stroke = new SolidColorBrush(Color.FromArgb(190, 255, 255, 255)),
StrokeThickness = isActive ? 1.2 : 0.8
});
}
headerStack.Children.Add(dots);
headerBar.Child = headerStack;
Canvas.SetLeft(headerBar, 0);
Canvas.SetTop(headerBar, 0);
Panel.SetZIndex(headerBar, 7);
GridCanvas.Children.Add(headerBar);
var scrollHint = new TextBlock
{
Text = "Scroll mouse wheel for more variants",
Foreground = new SolidColorBrush(Color.FromArgb(205, 255, 255, 255)),
FontSize = 13,
FontWeight = FontWeights.Medium,
Background = new SolidColorBrush(Color.FromArgb(120, 0, 0, 0)),
Padding = new Thickness(10, 5, 10, 5)
};
scrollHint.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
Canvas.SetLeft(scrollHint, Math.Max(10, (Width - scrollHint.DesiredSize.Width) / 2));
Canvas.SetTop(scrollHint, 74);
Panel.SetZIndex(scrollHint, 7);
GridCanvas.Children.Add(scrollHint);
// Snap preview rectangle (hidden until a zone is highlighted)
_snapPreview = new Rectangle
{
Fill = new SolidColorBrush(_snapPreviewFill),
Stroke = new SolidColorBrush(_snapPreviewStroke),
StrokeThickness = 3,
Visibility = Visibility.Collapsed,
RadiusX = 6,
RadiusY = 6
};
GridCanvas.Children.Add(_snapPreview);
Panel.SetZIndex(_snapPreview, 0);
// Snap preview size label
_snapLabel = new TextBlock
{
Foreground = new SolidColorBrush(Colors.White),
FontSize = 20,
FontWeight = FontWeights.Bold,
Background = new SolidColorBrush(Color.FromArgb(180, 0, 100, 200)),
Padding = new Thickness(14, 8, 14, 8),
Visibility = Visibility.Collapsed,
TextAlignment = TextAlignment.Center
};
GridCanvas.Children.Add(_snapLabel);
Panel.SetZIndex(_snapLabel, 5);
// Row separators (subtle horizontal lines between bands)
int rowCount = visibleRows.Count;
for (int i = 1; i < rowCount; i++)
{
double y = (Height / rowCount) * i;
var line = new Line
{
X1 = 0,
Y1 = y,
X2 = Width,
Y2 = y,
Stroke = new SolidColorBrush(Color.FromArgb(40, 255, 255, 255)),
StrokeThickness = 1,
StrokeDashArray = new DoubleCollection { 8, 4 }
};
GridCanvas.Children.Add(line);
}
// Zone borders
foreach (var zone in _zones)
{
double x = (zone.DisplayBounds.X - _workArea.Left) / _dpiScale;
double y = (zone.DisplayBounds.Y - _workArea.Top) / _dpiScale;
double w = zone.DisplayBounds.Width / _dpiScale;
double h = zone.DisplayBounds.Height / _dpiScale;
// Build label stack
var stack = new StackPanel
{
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
};
stack.Children.Add(new TextBlock
{
Text = zone.Label,
Foreground = new SolidColorBrush(LabelPrimary),
FontSize = 34,
FontWeight = FontWeights.SemiBold,
HorizontalAlignment = HorizontalAlignment.Center
});
stack.Children.Add(new TextBlock
{
Text = visibleRows.Count > zone.Row ? visibleRows[zone.Row].Name : "",
Foreground = new SolidColorBrush(LabelDim),
FontSize = 12,
HorizontalAlignment = HorizontalAlignment.Center,
Margin = new Thickness(0, 4, 0, 0)
});
var border = new Border
{
Width = w,
Height = h,
Background = new SolidColorBrush(_zoneFill),
BorderBrush = new SolidColorBrush(_zoneBorder),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(8),
Child = stack
};
Canvas.SetLeft(border, x);
Canvas.SetTop(border, y);
Panel.SetZIndex(border, 1);
GridCanvas.Children.Add(border);
_zoneBorders[zone] = border;
}
}
// ── Snap preview ────────────────────────────────────────────────
private void UpdateSnapPreview(GridZone? zone)
{
if (_snapPreview == null) return;
if (zone == null)
{
_snapPreview.Visibility = Visibility.Collapsed;
if (_snapLabel != null) _snapLabel.Visibility = Visibility.Collapsed;
return;
}
double x = (zone.SnapBounds.X - _workArea.Left) / _dpiScale;
double y = (zone.SnapBounds.Y - _workArea.Top) / _dpiScale;
double w = zone.SnapBounds.Width / _dpiScale;
double h = zone.SnapBounds.Height / _dpiScale;
double previewMargin = 6;
Canvas.SetLeft(_snapPreview, x + previewMargin);
Canvas.SetTop(_snapPreview, y + previewMargin);
_snapPreview.Width = w - 2 * previewMargin;
_snapPreview.Height = h - 2 * previewMargin;
_snapPreview.Visibility = Visibility.Visible;
// Show a clear size label centered in the preview
if (_snapLabel != null)
{
int snapW = (int)zone.SnapBounds.Width;
int snapH = (int)zone.SnapBounds.Height;
_snapLabel.Text = $"{snapW} × {snapH} px";
_snapLabel.Visibility = Visibility.Visible;
_snapLabel.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
double labelW = _snapLabel.DesiredSize.Width;
double labelH = _snapLabel.DesiredSize.Height;
Canvas.SetLeft(_snapLabel, x + (w - labelW) / 2);
Canvas.SetTop(_snapLabel, y + (h - labelH) / 2);
}
}
// ── DPI helper ──────────────────────────────────────────────────
private static double GetDpiScaleForWorkArea(NativeMethods.RECT workArea)
{
try
{
var pt = new NativeMethods.POINT
{
X = workArea.Left + 1,
Y = workArea.Top + 1
};
var hMonitor = NativeMethods.MonitorFromPoint(pt, NativeMethods.MONITOR_DEFAULTTONEAREST);
int hr = NativeMethods.GetDpiForMonitor(hMonitor, 0 /* MDT_EFFECTIVE_DPI */, out uint dpiX, out _);
if (hr == 0 && dpiX > 0)
return dpiX / 96.0;
}
catch
{
// Fallback
}
return 1.0;
}
}
}