Repository navigation
Expand file tree
/
Copy pathTaskbarPerformanceLayout.cs
More file actions
232 lines (206 loc) · 9.23 KB
/
Copy pathTaskbarPerformanceLayout.cs
File metadata and controls
232 lines (206 loc) · 9.23 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
using System.Globalization;
using System.Windows;
using System.Windows.Media;
namespace TaskbarInfo;
/// <summary>
/// Pure layout helpers for the independent taskbar performance component.
/// Keeping the width stable prevents changing numbers from moving the lyric window.
/// </summary>
public static class TaskbarPerformanceLayout
{
private const int HorizontalPadding = 14;
private const int ItemGap = 7;
private const int ComponentGap = 6;
private const string DefaultFontFamily = "Microsoft YaHei UI, Segoe UI";
public static int GetWidth(IEnumerable<string>? metricIds, bool doubleLine = false)
{
var metrics = TaskbarPerformanceMetricCatalog.Normalize(metricIds);
if (metrics.Count == 0) return 0;
static int GetMetricWidth(string metric) => metric switch
{
TaskbarPerformanceMetricCatalog.Cpu => 58,
TaskbarPerformanceMetricCatalog.Memory => 66,
TaskbarPerformanceMetricCatalog.Gpu => 58,
TaskbarPerformanceMetricCatalog.CpuFrequency => 90,
TaskbarPerformanceMetricCatalog.GpuDedicatedMemory => 124,
TaskbarPerformanceMetricCatalog.MemoryUsed => 138,
TaskbarPerformanceMetricCatalog.CpuTemperature => 67,
TaskbarPerformanceMetricCatalog.GpuTemperature => 67,
TaskbarPerformanceMetricCatalog.DiskTemperature => 72,
TaskbarPerformanceMetricCatalog.DiskRead or TaskbarPerformanceMetricCatalog.DiskWrite => 124,
TaskbarPerformanceMetricCatalog.Download or TaskbarPerformanceMetricCatalog.Upload => 82,
_ => 0
};
if (doubleLine && metrics.Count > 1)
{
int firstLineCount = (metrics.Count + 1) / 2;
int firstLineWidth = metrics.Take(firstLineCount).Sum(GetMetricWidth);
int secondLineWidth = metrics.Skip(firstLineCount).Sum(GetMetricWidth);
int firstGaps = Math.Max(0, firstLineCount - 1) * ItemGap;
int secondGaps = Math.Max(0, metrics.Count - firstLineCount - 1) * ItemGap;
return Math.Max(firstLineWidth + firstGaps, secondLineWidth + secondGaps) + HorizontalPadding * 2;
}
int width = metrics.Sum(GetMetricWidth) + Math.Max(0, metrics.Count - 1) * ItemGap;
return width + HorizontalPadding * 2;
}
/// <summary>
/// Calculates the physical width needed by the rendered labels. The old overload is
/// intentionally retained for persisted-layout compatibility and unit tests; runtime
/// callers should use this overload so custom fonts, sizes, and monitor DPI are honored.
/// </summary>
public static int GetWidth(
IEnumerable<string>? metricIds,
bool doubleLine,
string? fontFamily,
double fontSize,
string? fontWeight = null,
double pixelsPerDip = 1)
{
var metrics = TaskbarPerformanceMetricCatalog.Normalize(metricIds);
if (metrics.Count == 0) return 0;
pixelsPerDip = double.IsFinite(pixelsPerDip) && pixelsPerDip > 0 ? pixelsPerDip : 1;
fontSize = double.IsFinite(fontSize) && fontSize > 0 ? fontSize : 10;
var typeface = new Typeface(
new System.Windows.Media.FontFamily(string.IsNullOrWhiteSpace(fontFamily) ? DefaultFontFamily : fontFamily),
FontStyles.Normal,
GetFontWeight(fontWeight),
FontStretches.Normal);
static string Sample(string metric) => metric switch
{
TaskbarPerformanceMetricCatalog.Cpu => "CPU 100%",
TaskbarPerformanceMetricCatalog.Memory => "内存 100%",
TaskbarPerformanceMetricCatalog.Gpu => "GPU 100%",
TaskbarPerformanceMetricCatalog.CpuFrequency => "CPU 9.99 GHz",
TaskbarPerformanceMetricCatalog.GpuDedicatedMemory => "GPU 显存 999.99 GB",
TaskbarPerformanceMetricCatalog.MemoryUsed => "内存 999.9 / 999.9 GB",
TaskbarPerformanceMetricCatalog.CpuTemperature => "CPU 100°C",
TaskbarPerformanceMetricCatalog.GpuTemperature => "GPU 100°C",
TaskbarPerformanceMetricCatalog.DiskTemperature => "磁盘 100°C",
TaskbarPerformanceMetricCatalog.DiskRead => "磁盘读 999.99 GB/s",
TaskbarPerformanceMetricCatalog.DiskWrite => "磁盘写 999.99 GB/s",
TaskbarPerformanceMetricCatalog.Download or TaskbarPerformanceMetricCatalog.Upload => "↓ 999.99 GB/s",
_ => string.Empty
};
int Measure(string metric)
{
string sample = Sample(metric);
if (sample.Length == 0) return 0;
var text = new FormattedText(
sample,
CultureInfo.CurrentUICulture,
System.Windows.FlowDirection.LeftToRight,
typeface,
fontSize,
System.Windows.Media.Brushes.White,
pixelsPerDip);
return (int)Math.Ceiling(text.Width);
}
int GetLineWidth(IEnumerable<string> lineMetrics)
{
var widths = lineMetrics.Select(Measure).ToList();
return widths.Sum() + Math.Max(0, widths.Count - 1) * ItemGap;
}
int contentWidth;
if (doubleLine && metrics.Count > 1)
{
int firstLineCount = (metrics.Count + 1) / 2;
contentWidth = Math.Max(
GetLineWidth(metrics.Take(firstLineCount)),
GetLineWidth(metrics.Skip(firstLineCount)));
}
else
{
contentWidth = GetLineWidth(metrics);
}
// Border padding plus the drag handle/text margin. These values are WPF DIPs,
// so scale the complete logical width before passing it to MoveWindow (pixels).
// Keep a small safety pixel so glyph overhang and ClearType rounding cannot clip
// the final character.
return (int)Math.Ceiling((contentWidth + HorizontalPadding * 2) * pixelsPerDip) + 2;
}
private static FontWeight GetFontWeight(string? value) => value switch
{
"Light" => FontWeights.Light,
"SemiBold" => FontWeights.SemiBold,
"Bold" => FontWeights.Bold,
_ => FontWeights.Normal
};
public static int GetLeftBesideLyrics(
int taskbarWidth,
int lyricLeft,
IEnumerable<string>? metricIds,
bool doubleLine = false)
{
int performanceWidth = GetWidth(metricIds, doubleLine);
if (performanceWidth == 0 || taskbarWidth <= 0) return 0;
int maxLeft = Math.Max(0, taskbarWidth - performanceWidth);
int candidate = lyricLeft - performanceWidth - ComponentGap;
return Math.Clamp(candidate, 0, maxLeft);
}
public static int GetLeftBesideLyrics(
int taskbarWidth,
int lyricLeft,
IEnumerable<string>? metricIds,
bool doubleLine,
string? fontFamily,
double fontSize,
string? fontWeight,
double pixelsPerDip)
{
int performanceWidth = GetWidth(metricIds, doubleLine, fontFamily, fontSize, fontWeight, pixelsPerDip);
if (performanceWidth == 0 || taskbarWidth <= 0) return 0;
int maxLeft = Math.Max(0, taskbarWidth - performanceWidth);
int candidate = lyricLeft - performanceWidth - ComponentGap;
return Math.Clamp(candidate, 0, maxLeft);
}
public static int GetLeftFromTray(
int taskbarWidth,
int trayLeft,
IEnumerable<string>? metricIds,
int offsetX,
bool doubleLine = false)
{
int performanceWidth = GetWidth(metricIds, doubleLine);
if (performanceWidth == 0 || taskbarWidth <= 0) return 0;
int maxLeft = Math.Max(0, taskbarWidth - performanceWidth);
int candidate = trayLeft - performanceWidth - Math.Max(0, offsetX);
return Math.Clamp(candidate, 0, maxLeft);
}
public static int GetOffsetForLeft(int trayLeft, int performanceWidth, int left)
{
return Math.Max(0, trayLeft - performanceWidth - left);
}
public static (int Left, int Top, int Width, int Height) GetPosition(
int taskbarWidth,
int taskbarHeight,
int trayLeft,
int offsetX,
IEnumerable<string>? metricIds,
bool doubleLine = false)
{
int width = GetWidth(metricIds, doubleLine);
return (GetLeftFromTray(taskbarWidth, trayLeft, metricIds, offsetX, doubleLine), 0, width, Math.Max(1, taskbarHeight));
}
public static (int Left, int Top, int Width, int Height) GetPosition(
int taskbarWidth,
int taskbarHeight,
int trayLeft,
int offsetX,
IEnumerable<string>? metricIds,
bool doubleLine,
string? fontFamily,
double fontSize,
string? fontWeight,
double pixelsPerDip)
{
int width = GetWidth(metricIds, doubleLine, fontFamily, fontSize, fontWeight, pixelsPerDip);
return (GetLeftFromTray(taskbarWidth, trayLeft, width, offsetX), 0, width, Math.Max(1, taskbarHeight));
}
private static int GetLeftFromTray(int taskbarWidth, int trayLeft, int performanceWidth, int offsetX)
{
if (performanceWidth == 0 || taskbarWidth <= 0) return 0;
int maxLeft = Math.Max(0, taskbarWidth - performanceWidth);
int candidate = trayLeft - performanceWidth - Math.Max(0, offsetX);
return Math.Clamp(candidate, 0, maxLeft);
}
}