-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathInfoControl.xaml.cs
213 lines (186 loc) · 5.29 KB
/
InfoControl.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;
namespace Lkytal.StatusInfo
{
public partial class InfoControl : UserControl
{
public readonly string[] Formats;
public readonly string[] FormatDescriptions;
private readonly Dictionary<string, TextBlockList> textBlockLists;
private readonly long totalRam;
private int fixedWidth = 150;
private bool useFixedWidth;
public int CpuUsage
{
set
{
string CpuValue = $"{value,2}%";
textBlockLists["<CPU>"].Text = CpuValue;
textBlockLists["<#CPU>"].Text = CpuValue;
textBlockLists["<#CPU>"].Foreground = GetCpuColor(value);
}
}
public int FixedWidth
{
get => fixedWidth;
set
{
fixedWidth = value;
UseFixedWidth = UseFixedWidth;
}
}
public string Format
{
set
{
stackPanel.Children.Clear();
textBlockLists.Clear();
InitTextBlockLists();
string str = value;
while (str != "")
{
TextBlockList textBlockList;
TextBlock nextTextBlock = GetNextTextBlock(ref str, out textBlockList);
textBlockList?.Add(nextTextBlock);
stackPanel.Children.Add(nextTextBlock);
}
foreach (TextBlockList textBlockList1 in textBlockLists.Values)
{
textBlockList1.Text = "N/A";
}
}
}
public long FreeRam
{
set
{
long num = totalRam - value;
string readableByteSize = num.ToReadableByteSize("####.00");
textBlockLists["<TOTAL_USE_RAM>"].Text = readableByteSize;
textBlockLists["<#TOTAL_USE_RAM>"].Text = readableByteSize;
textBlockLists["<#TOTAL_USE_RAM>"].Foreground = GetRamColor(num);
int num1 = (int) (num * 100 / totalRam);
readableByteSize = $"{num1:####.00}%";
textBlockLists["<TOTAL_USE_RAM%>"].Text = readableByteSize;
textBlockLists["<#TOTAL_USE_RAM%>"].Text = readableByteSize;
textBlockLists["<#TOTAL_USE_RAM%>"].Foreground = GetRamColor(num);
readableByteSize = value.ToReadableByteSize("####.00");
textBlockLists["<FREE_RAM>"].Text = readableByteSize;
textBlockLists["<#FREE_RAM>"].Text = readableByteSize;
textBlockLists["<#FREE_RAM>"].Foreground = GetRamColor(num);
num1 = (int) (value * 100 / totalRam);
readableByteSize = $"{num1:####.00}%";
textBlockLists["<FREE_RAM%>"].Text = readableByteSize;
textBlockLists["<#FREE_RAM%>"].Text = readableByteSize;
textBlockLists["<#FREE_RAM%>"].Foreground = GetRamColor(num);
}
}
public long RamUsage
{
set
{
string readableByteSize = value.ToReadableByteSize("####.00");
textBlockLists["<RAM>"].Text = readableByteSize;
textBlockLists["<#RAM>"].Text = readableByteSize;
textBlockLists["<#RAM>"].Foreground = GetRamColor(value);
int num = (int) (value * 100 / totalRam);
readableByteSize = $"{num:####.00}%";
textBlockLists["<RAM%>"].Text = readableByteSize;
textBlockLists["<#RAM%>"].Text = readableByteSize;
textBlockLists["<#RAM%>"].Foreground = GetCpuColor(num);
}
}
public int TotalCpuUsage
{
set
{
string TotalCpuValue = $"{value,2}%";
textBlockLists["<TOTAL_CPU>"].Text = TotalCpuValue;
textBlockLists["<#TOTAL_CPU>"].Text = TotalCpuValue;
textBlockLists["<#TOTAL_CPU>"].Foreground = GetCpuColor(value);
}
}
public bool UseFixedWidth
{
get => useFixedWidth;
set
{
useFixedWidth = value;
if (!useFixedWidth)
{
Width = double.NaN;
return;
}
Width = FixedWidth;
}
}
public InfoControl(long pTotalRam)
{
Formats = new []{ "CPU", "TOTAL_CPU", "RAM", "FREE_RAM", "TOTAL_USE_RAM", "RAM%", "FREE_RAM%", "TOTAL_USE_RAM%" };
FormatDescriptions = new [] { "Cpu usage of Visual Studio", "Cpu usage of computer", "Ram usage of Visual Studio", "Free ram of computer", "Ram usage of computer", "Ram usage of Visual Studio in percent", "Free ram of computer in percent", "Ram usage of computer in percent" };
totalRam = pTotalRam;
InitializeComponent();
textBlockLists = new Dictionary<string, TextBlockList>();
Format = "CPU: <#CPU> RAM: <#RAM>";
}
private Brush GetCpuColor(int cpu)
{
Color color;
if (cpu > 50)
{
Color yellow = Colors.Yellow;
color = yellow.FadeTo(Colors.Red, (cpu - 50) / 50f);
}
else
{
Color white = Colors.White;
color = white.FadeTo(Colors.Yellow, cpu / 50f);
}
return new SolidColorBrush(color);
}
private TextBlock GetNextTextBlock(ref string format, out TextBlockList textBlockList)
{
TextBlock textBlock = new TextBlock
{
Foreground = new SolidColorBrush(Colors.White)
};
string str;
int num = format.IndexOfAny(textBlockLists.Keys.ToArray(), out str);
if (num == -1)
{
textBlock.Text = format;
format = "";
textBlockList = null;
}
else if (num != 0)
{
textBlock.Text = format.Substring(0, num);
format = format.Substring(num);
textBlockList = null;
}
else
{
textBlock.Text = "";
format = format.Substring(str.Length);
textBlockList = textBlockLists[str];
}
return textBlock;
}
private Brush GetRamColor(long ram)
{
int num = (int) (ram * 100 / totalRam);
return GetCpuColor(num);
}
private void InitTextBlockLists()
{
foreach (string t in Formats)
{
textBlockLists[$"<{t}>"] = new TextBlockList();
textBlockLists[$"<#{t}>"] = new TextBlockList();
}
}
}
}