-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskbarOverlay.cs
More file actions
278 lines (237 loc) · 10.1 KB
/
Copy pathTaskbarOverlay.cs
File metadata and controls
278 lines (237 loc) · 10.1 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace TaskbarMarker;
internal readonly record struct Mark(Rectangle ButtonBounds, CompiledRule Rule);
/// <summary>
/// Owns one overlay window (one per taskbar) and paints the colored bars / label chips
/// for every taskbar button that matched a rule.
/// </summary>
internal sealed class TaskbarOverlay : IDisposable
{
/// <summary>Windows 11 taskbar height at 100% scaling; used to derive a DPI factor.</summary>
private const float BaselineTaskbarHeight = 48f;
private readonly OverlayWindow _window = new();
private readonly Bitmap _measureBitmap = new(1, 1);
private readonly Graphics _measureGraphics;
private Bitmap? _surface;
private Font? _font;
private float _fontSize = -1f;
private int _lastSignature;
public TaskbarOverlay() => _measureGraphics = Graphics.FromImage(_measureBitmap);
public void Render(Rectangle taskbarRect, Rectangle screenBounds, IReadOnlyList<Mark> marks, Config config)
{
if (marks.Count == 0 || taskbarRect.Width <= 0 || taskbarRect.Height <= 0)
{
Hide();
return;
}
float scale = Math.Clamp(taskbarRect.Height / BaselineTaskbarHeight, 0.75f, 3f);
bool taskbarAtTop = taskbarRect.Top <= screenBounds.Top + 2;
int barHeight = Math.Max(2, (int)Math.Round(config.BarHeight * scale));
int barInset = (int)Math.Round(config.BarInset * scale);
int edgeMargin = (int)Math.Round(3 * scale);
int gap = (int)Math.Round(2 * scale);
Font font = GetFont(config.LabelFontSize * scale);
bool wantsLabels = config.ShowLabel && HasAnyLabel(marks);
int chipPadX = (int)Math.Round(6 * scale);
int chipPadY = (int)Math.Round(2 * scale);
int chipHeight = wantsLabels ? (int)Math.Ceiling(font.GetHeight(96f)) + chipPadY * 2 : 0;
var ordered = new List<Mark>(marks);
ordered.Sort(static (a, b) => a.ButtonBounds.X.CompareTo(b.ButtonBounds.X));
// Everything below is laid out in screen coordinates first, so the overlay window can be
// shrunk to just the painted area instead of spanning the whole taskbar.
var bars = new List<Shape>(ordered.Count);
foreach (Mark mark in ordered)
{
Rectangle button = mark.ButtonBounds;
int barWidth = Math.Max(4, button.Width - barInset * 2);
bars.Add(new Shape(
new Rectangle(
button.X + (button.Width - barWidth) / 2,
taskbarAtTop ? button.Top + edgeMargin : button.Bottom - edgeMargin - barHeight,
barWidth,
barHeight),
mark.Rule.Color,
null));
}
List<Shape> chips = wantsLabels
? LayoutChips(ordered, taskbarRect, taskbarAtTop, font, chipHeight, chipPadX, gap)
: new List<Shape>();
Rectangle overlayRect = Rectangle.Empty;
foreach (Shape shape in bars)
overlayRect = overlayRect.IsEmpty ? shape.Rect : Rectangle.Union(overlayRect, shape.Rect);
foreach (Shape shape in chips)
overlayRect = Rectangle.Union(overlayRect, shape.Rect);
overlayRect.Inflate(1, 1);
if (overlayRect.Width <= 0 || overlayRect.Height <= 0)
{
Hide();
return;
}
int signature = ComputeSignature(overlayRect, bars, chips);
if (signature == _lastSignature && _window.Visible)
{
_window.EnsureTopMost();
return;
}
_lastSignature = signature;
Bitmap surface = GetSurface(overlayRect.Size);
using (var g = Graphics.FromImage(surface))
{
g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.AntiAlias;
// GDI+ text (not TextRenderer/GDI) is required here: GDI ignores the alpha channel
// and would render the label as an invisible or black block on a layered window.
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.TranslateTransform(-overlayRect.X, -overlayRect.Y);
foreach (Shape bar in bars)
{
using var brush = new SolidBrush(bar.Color);
using GraphicsPath path = RoundedRect(bar.Rect, bar.Rect.Height / 2f);
g.FillPath(brush, path);
}
if (chips.Count > 0)
{
using var format = new StringFormat(StringFormatFlags.NoWrap)
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
Trimming = StringTrimming.EllipsisCharacter,
};
foreach (Shape chip in chips)
{
using (var chipBrush = new SolidBrush(chip.Color))
using (GraphicsPath chipPath = RoundedRect(chip.Rect, chip.Rect.Height / 2f))
{
g.FillPath(chipBrush, chipPath);
}
using var textBrush = new SolidBrush(ContrastingText(chip.Color));
g.DrawString(chip.Label, font, textBrush, chip.Rect, format);
}
}
}
_window.UpdateSurface(overlayRect, surface);
}
/// <summary>
/// Chips are usually wider than a 44-66px taskbar button, so they are laid out left to right and
/// each one is pushed past the previous chip instead of being allowed to overwrite it.
/// </summary>
private List<Shape> LayoutChips(List<Mark> ordered, Rectangle taskbarRect, bool taskbarAtTop,
Font font, int chipHeight, int chipPadX, int gap)
{
var chips = new List<Shape>();
int chipY = taskbarAtTop ? taskbarRect.Bottom + gap : taskbarRect.Top - gap - chipHeight;
int nextFreeX = taskbarRect.Left;
foreach (Mark mark in ordered)
{
if (string.IsNullOrWhiteSpace(mark.Rule.Label))
continue;
string label = mark.Rule.Label!;
Rectangle button = mark.ButtonBounds;
int chipWidth = (int)Math.Ceiling(_measureGraphics.MeasureString(label, font).Width) + chipPadX * 2;
chipWidth = Math.Min(chipWidth, taskbarRect.Width);
int chipX = button.X + (button.Width - chipWidth) / 2;
chipX = Math.Max(chipX, nextFreeX);
chipX = Math.Min(chipX, taskbarRect.Right - chipWidth);
if (chipX < nextFreeX)
{
// Ran out of room on the right; shrink instead of overlapping the previous chip.
chipWidth = taskbarRect.Right - nextFreeX;
chipX = nextFreeX;
if (chipWidth < chipPadX * 2)
break;
}
var rect = new Rectangle(chipX, chipY, chipWidth, chipHeight);
chips.Add(new Shape(rect, mark.Rule.Color, label));
nextFreeX = rect.Right + gap * 2;
}
return chips;
}
private static int ComputeSignature(Rectangle overlayRect, List<Shape> bars, List<Shape> chips)
{
var hash = new HashCode();
hash.Add(overlayRect);
foreach (Shape shape in bars)
{
hash.Add(shape.Rect);
hash.Add(shape.Color.ToArgb());
}
foreach (Shape shape in chips)
{
hash.Add(shape.Rect);
hash.Add(shape.Color.ToArgb());
hash.Add(shape.Label);
}
return hash.ToHashCode();
}
private readonly record struct Shape(Rectangle Rect, Color Color, string? Label);
public void Hide()
{
_lastSignature = 0;
_window.HideOverlay();
}
private static bool HasAnyLabel(IReadOnlyList<Mark> marks)
{
foreach (Mark mark in marks)
{
if (!string.IsNullOrWhiteSpace(mark.Rule.Label))
return true;
}
return false;
}
private Bitmap GetSurface(Size size)
{
if (_surface is not null && _surface.Width == size.Width && _surface.Height == size.Height)
return _surface;
_surface?.Dispose();
_surface = new Bitmap(Math.Max(1, size.Width), Math.Max(1, size.Height),
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
return _surface;
}
private Font GetFont(float sizeInPoints)
{
sizeInPoints = Math.Max(6f, sizeInPoints);
if (_font is not null && Math.Abs(_fontSize - sizeInPoints) < 0.01f)
return _font;
_font?.Dispose();
_font = new Font("Segoe UI", sizeInPoints, FontStyle.Bold, GraphicsUnit.Point);
_fontSize = sizeInPoints;
return _font;
}
private static GraphicsPath RoundedRect(Rectangle rect, float radius)
{
var path = new GraphicsPath();
float diameter = Math.Min(radius * 2f, Math.Min(rect.Width, rect.Height));
if (diameter <= 1f)
{
path.AddRectangle(rect);
return path;
}
var arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = rect.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rect.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rect.X;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
private static Color ContrastingText(Color background)
{
double luminance = (0.299 * background.R + 0.587 * background.G + 0.114 * background.B) / 255.0;
return luminance > 0.6 ? Color.FromArgb(255, 20, 20, 20) : Color.White;
}
public void Dispose()
{
_window.Dispose();
_surface?.Dispose();
_font?.Dispose();
_measureGraphics.Dispose();
_measureBitmap.Dispose();
}
}