Skip to content

Commit ca13fd6

Browse files
committed
+ Added disk activity monitor
1 parent 1af61c3 commit ca13fd6

File tree

8 files changed

+145
-61
lines changed

8 files changed

+145
-61
lines changed

Codist/Config.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ public enum DisplayOptimizations
723723
HideAccountBox = 1 << 5,
724724
ShowCpu = 1 << 6,
725725
ShowMemory = 1 << 7,
726+
ShowDrive = 1 << 8,
727+
ResourceMonitors = ShowCpu | ShowMemory | ShowDrive
726728
}
727729

728730
[Flags]

Codist/Display/ResourceMonitor.cs

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,47 @@
88
using Microsoft.VisualStudio.PlatformUI;
99
using AppHelpers;
1010
using R = Codist.Properties.Resources;
11+
using Microsoft.VisualStudio.Shell;
12+
using Task = System.Threading.Tasks.Task;
1113

1214
namespace Codist.Display
1315
{
1416
static class ResourceMonitor
1517
{
1618
static Timer _Timer;
17-
static readonly StackPanel _MeterContainer = new StackPanel { Orientation = Orientation.Horizontal };
18-
static Meter _CpuMeter, _RamMeter;
19+
static readonly StackPanel _MeterContainer = new StackPanel {
20+
Orientation = Orientation.Horizontal,
21+
Children = { new ContentPresenter(), new ContentPresenter(), new ContentPresenter(), }
22+
};
23+
static Meter _CpuMeter, _RamMeter, _DriveMeter;
1924
static int _IsInited;
2025

2126
public static void Reload(DisplayOptimizations option) {
22-
if (option.HasAnyFlag(DisplayOptimizations.ShowCpu | DisplayOptimizations.ShowMemory) == false) {
27+
if (option.HasAnyFlag(DisplayOptimizations.ResourceMonitors) == false) {
2328
Stop();
2429
return;
2530
}
26-
if (option.MatchFlags(DisplayOptimizations.ShowCpu)) {
27-
if (_CpuMeter != null) {
28-
_CpuMeter.Start();
29-
}
30-
else {
31-
_CpuMeter = new CpuMeter();
32-
_MeterContainer.Children.Insert(0, _CpuMeter);
33-
}
34-
}
35-
else {
36-
_CpuMeter?.Stop();
31+
ToggleMeter<CpuMeter>(0, option, DisplayOptimizations.ShowCpu, ref _CpuMeter);
32+
ToggleMeter<DriveMeter>(1, option, DisplayOptimizations.ShowDrive, ref _DriveMeter);
33+
ToggleMeter<RamMeter>(2, option, DisplayOptimizations.ShowMemory, ref _RamMeter);
34+
if (_Timer == null) {
35+
_Timer = new Timer(Update, null, 1000, 1000);
3736
}
38-
if (option.MatchFlags(DisplayOptimizations.ShowMemory)) {
39-
if (_RamMeter != null) {
40-
_RamMeter.Start();
37+
}
38+
39+
static void ToggleMeter<TMeter>(int index, DisplayOptimizations option, DisplayOptimizations flag, ref Meter meter) where TMeter : Meter, new() {
40+
if (option.MatchFlags(flag)) {
41+
if (meter != null) {
42+
meter.Start();
4143
}
4244
else {
43-
_RamMeter = new RamMeter();
44-
_MeterContainer.Children.Add(_RamMeter);
45+
meter = new TMeter();
46+
_MeterContainer.Children.RemoveAt(index);
47+
_MeterContainer.Children.Insert(index, meter);
4548
}
4649
}
4750
else {
48-
_RamMeter?.Stop();
49-
}
50-
if (_Timer == null) {
51-
_Timer = new Timer(Update, null, 1000, 1000);
51+
meter?.Stop();
5252
}
5353
}
5454

@@ -58,16 +58,23 @@ static void Stop() {
5858
_Timer = null;
5959
_CpuMeter?.Stop();
6060
_RamMeter?.Stop();
61+
_DriveMeter?.Stop();
6162
}
6263
}
6364

6465
static void Update(object dummy) {
66+
UpdateAsync().FireAndForget();
67+
}
68+
69+
async static Task UpdateAsync() {
70+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
6571
if (_IsInited == 0) {
66-
_MeterContainer.Dispatcher.Invoke(Init);
72+
Init();
6773
return;
6874
}
6975
_CpuMeter?.Update();
7076
_RamMeter?.Update();
77+
_DriveMeter?.Update();
7178
}
7279

7380
static void Init() {
@@ -99,23 +106,25 @@ abstract class Meter : StackPanel
99106
protected Meter(int iconId, string tooltip) {
100107
Orientation = Orientation.Horizontal;
101108
Children.Add(ThemeHelper.GetImage(iconId).WrapMargin(WpfHelper.SmallHorizontalMargin));
102-
Children.Add(_Label = new TextBlock { MinWidth = 40, VerticalAlignment = VerticalAlignment.Center });
103-
_Label.SetResourceReference(Control.ForegroundProperty, EnvironmentColors.StatusBarDefaultTextBrushKey);
109+
Children.Add(_Label = new TextBlock { MinWidth = 40, VerticalAlignment = VerticalAlignment.Center }.ReferenceProperty(Control.ForegroundProperty, EnvironmentColors.StatusBarDefaultTextBrushKey));
104110
_Counter = CreateCounter();
105-
var t = new CommandToolTip(iconId, tooltip);
106-
t.SetResourceReference(ImageThemingUtilities.ImageBackgroundColorProperty, EnvironmentColors.ToolTipColorKey);
107-
ToolTip = t;
108-
ToolTipService.SetPlacement(this, System.Windows.Controls.Primitives.PlacementMode.Top);
111+
ToolTip = new CommandToolTip(iconId, tooltip)
112+
.ReferenceCrispImageBackground(EnvironmentColors.ToolTipColorKey)
113+
.SetTipPlacementTop();
109114
}
110115

111116
protected TextBlock Label => _Label;
112117

113118
public void Update() {
119+
var c = _Counter;
120+
if (c == null) {
121+
return;
122+
}
114123
try {
115-
Dispatcher.Invoke(UpdateMeter);
124+
UpdateDisplay(c.NextValue());
116125
}
117-
catch (System.Threading.Tasks.TaskCanceledException) {
118-
// ignore
126+
catch (Exception ex) {
127+
Debug.WriteLine(ex);
119128
}
120129
}
121130

@@ -135,26 +144,13 @@ public virtual void Stop() {
135144
_Counter = null;
136145
}
137146
}
138-
139-
void UpdateMeter() {
140-
var c = _Counter;
141-
if (c == null) {
142-
return;
143-
}
144-
try {
145-
UpdateDisplay(c.NextValue());
146-
}
147-
catch (Exception ex) {
148-
Debug.WriteLine(ex);
149-
}
150-
}
151147
}
152148

153149
sealed class CpuMeter : Meter
154150
{
155151
const int SampleCount = 10;
156152
readonly float[] _Samples = new float[SampleCount];
157-
float _SampleSum;
153+
float _SampleSum, _LastSample;
158154
int _SampleIndex;
159155

160156
public CpuMeter() : base(IconIds.Cpu, R.T_CpuUsage) {
@@ -173,8 +169,16 @@ protected override void UpdateDisplay(float counterValue) {
173169
if (++_SampleIndex == SampleCount) {
174170
_SampleIndex = 0;
175171
}
176-
counterValue = Math.Min(50, Math.Min(counterValue, _SampleSum / SampleCount)) / 50;
177-
Background = counterValue < 0.2f ? Brushes.Transparent : Brushes.Red.Alpha(counterValue);
172+
counterValue = Math.Min(50, _SampleSum / SampleCount) / 50;
173+
if (counterValue < 0.2f) {
174+
if (_LastSample >= 0.2f) {
175+
ClearValue(BackgroundProperty);
176+
}
177+
}
178+
else {
179+
Background = Brushes.Red.Alpha(counterValue);
180+
}
181+
_LastSample = counterValue;
178182
}
179183
}
180184

@@ -192,5 +196,41 @@ protected override void UpdateDisplay(float counterValue) {
192196
Label.Opacity = (counterValue + 100) / 200;
193197
}
194198
}
199+
200+
sealed class DriveMeter : Meter
201+
{
202+
const int SampleCount = 10;
203+
readonly float[] _Samples = new float[SampleCount];
204+
float _SampleSum, _LastSample;
205+
int _SampleIndex;
206+
207+
public DriveMeter() : base(IconIds.Drive, R.T_DriveUsage) {
208+
}
209+
210+
protected override PerformanceCounter CreateCounter() {
211+
return new PerformanceCounter("LogicalDisk", "% Disk Time", "_Total");
212+
}
213+
214+
protected override void UpdateDisplay(float counterValue) {
215+
Label.Text = counterValue.ToString("0") + "%";
216+
Label.Opacity = (Math.Min(50, counterValue) + 50) / 100;
217+
_SampleSum -= _Samples[_SampleIndex];
218+
_Samples[_SampleIndex] = counterValue;
219+
_SampleSum += counterValue;
220+
if (++_SampleIndex == SampleCount) {
221+
_SampleIndex = 0;
222+
}
223+
counterValue = Math.Min(30, Math.Min(counterValue, _SampleSum / SampleCount)) / 30;
224+
if (counterValue < 0.2f) {
225+
if (_LastSample >= 0.2f) {
226+
ClearValue(BackgroundProperty);
227+
}
228+
}
229+
else {
230+
Background = Brushes.Red.Alpha(counterValue);
231+
}
232+
_LastSample = counterValue;
233+
}
234+
}
195235
}
196236
}

Codist/IconIds.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ static class IconIds
203203
public const int ResetTheme = KnownImageIds.CleanData;
204204
public const int Cpu = KnownImageIds.Processor;
205205
public const int Memory = KnownImageIds.Memory;
206+
public const int Drive = KnownImageIds.HardDrive;
206207
#endregion
207208
#region symbol usage icons
208209
public const int UseToWrite = KnownImageIds.Writeable;

Codist/Options/OptionsPage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ sealed class DisplayPage : OptionsPage
776776
sealed class PageControl : OptionsPageContainer
777777
{
778778
readonly Controls.IntegerBox _TopSpace, _BottomSpace;
779-
readonly OptionBox<DisplayOptimizations> _MainWindow, _CodeWindow, _MenuLayoutOverride, _HideSearchBox, _HideAccountBox, _HideFeedbackButton, _CpuMonitor, _MemoryMonitor;
779+
readonly OptionBox<DisplayOptimizations> _MainWindow, _CodeWindow, _MenuLayoutOverride, _HideSearchBox, _HideAccountBox, _HideFeedbackButton, _CpuMonitor, _MemoryMonitor, _DriveMonitor;
780780
readonly OptionBox<BuildOptions> _BuildTimestamp;
781781

782782
public PageControl(OptionsPage page) : base(page) {
@@ -814,6 +814,7 @@ public PageControl(OptionsPage page) : base(page) {
814814
new WrapPanel {
815815
Children = {
816816
Config.Instance.DisplayOptimizations.CreateOptionBox(DisplayOptimizations.ShowCpu, UpdateResourceManagerOption, R.OT_CpuUsage).Set(ref _CpuMonitor),
817+
Config.Instance.DisplayOptimizations.CreateOptionBox(DisplayOptimizations.ShowDrive, UpdateResourceManagerOption, R.OT_DriveUsage).Set(ref _DriveMonitor),
817818
Config.Instance.DisplayOptimizations.CreateOptionBox(DisplayOptimizations.ShowMemory, UpdateResourceManagerOption, R.OT_MemoryUsage).Set(ref _MemoryMonitor)
818819
}
819820
},

Codist/Properties/Resources.Designer.cs

Lines changed: 22 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Codist/Properties/Resources.en-US.resx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,16 +2088,16 @@ Right click: Threads window</value>
20882088
<value>[Deprecated]</value>
20892089
</data>
20902090
<data name="OT_CpuUsage" xml:space="preserve">
2091-
<value>Monitor CPU usage</value>
2091+
<value>Monitor CPU</value>
20922092
</data>
20932093
<data name="OT_MemoryUsage" xml:space="preserve">
2094-
<value>Monitor memory usage</value>
2094+
<value>Monitor memory</value>
20952095
</data>
20962096
<data name="OT_ResourceMonitor" xml:space="preserve">
20972097
<value>Resource Monitor</value>
20982098
</data>
20992099
<data name="OT_ResourceMonitorNote" xml:space="preserve">
2100-
<value>Adds controls to the status bar, displaying CPU or momory usage</value>
2100+
<value>Adds controls to the status bar, displaying computer resource usage</value>
21012101
</data>
21022102
<data name="T_CpuUsage" xml:space="preserve">
21032103
<value>CPU Usage
@@ -2107,4 +2107,11 @@ Percent of processor time</value>
21072107
<value>Memory Usage
21082108
Percent of memory in use</value>
21092109
</data>
2110+
<data name="OT_DriveUsage" xml:space="preserve">
2111+
<value>Monitor disk</value>
2112+
</data>
2113+
<data name="T_DriveUsage" xml:space="preserve">
2114+
<value>Disk Time Usage
2115+
Percent of logical disk time</value>
2116+
</data>
21102117
</root>

Codist/Properties/Resources.resx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,16 +2086,16 @@ Right click: Threads window</value>
20862086
<value>[Deprecated]</value>
20872087
</data>
20882088
<data name="OT_CpuUsage" xml:space="preserve">
2089-
<value>Monitor CPU usage</value>
2089+
<value>Monitor CPU</value>
20902090
</data>
20912091
<data name="OT_MemoryUsage" xml:space="preserve">
2092-
<value>Monitor memory usage</value>
2092+
<value>Monitor memory</value>
20932093
</data>
20942094
<data name="OT_ResourceMonitor" xml:space="preserve">
20952095
<value>Resource Monitor</value>
20962096
</data>
20972097
<data name="OT_ResourceMonitorNote" xml:space="preserve">
2098-
<value>Adds controls to the status bar, displaying CPU or momory usage</value>
2098+
<value>Adds controls to the status bar, displaying computer resource usage</value>
20992099
</data>
21002100
<data name="T_CpuUsage" xml:space="preserve">
21012101
<value>CPU Usage
@@ -2105,4 +2105,11 @@ Percent of processor time</value>
21052105
<value>Memory Usage
21062106
Percent of memory in use</value>
21072107
</data>
2108+
<data name="OT_DriveUsage" xml:space="preserve">
2109+
<value>Monitor disk</value>
2110+
</data>
2111+
<data name="T_DriveUsage" xml:space="preserve">
2112+
<value>Disk Time Usage
2113+
Percent of logical disk time</value>
2114+
</data>
21082115
</root>

0 commit comments

Comments
 (0)