Skip to content

Commit 02ba278

Browse files
chore: remove verbose timing logs, bump to 1.2.8-rc4
- Remove Stopwatch instrumentation and Verbose log calls from ContextMenu.cs, TrayIcon.cs, and AppContext.cs - Simplify PowerScheme.SystemChanged log to omit IconUpdateElapsedMs - Bump VersionSuffix from rc3 to rc4
1 parent 2e1c131 commit 02ba278

6 files changed

Lines changed: 279 additions & 52 deletions

File tree

PowerManagement/PowerManager.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace PowerManagement;
22

33
using System.Diagnostics;
4+
using Serilog;
45
using Vanara.Extensions;
56
using Vanara.InteropServices;
67
using Vanara.PInvoke;
@@ -90,6 +91,8 @@ public static bool IsExecutionStateBlockingIdle()
9091

9192
private bool disposedValue;
9293
private readonly SafeHPOWERNOTIFY powerSettingsChangedCallbackHandler;
94+
private readonly object schemeDispatchLock = new();
95+
private Guid? lastDispatchedSchemeGuid;
9396

9497
// This variable must not be a local but instead have a lifetime
9598
// exceeding the registration of the callback initialized with
@@ -166,11 +169,41 @@ private Win32Error HandlePowerSettingsChanged(
166169
return Win32Error.NO_ERROR;
167170
}
168171

172+
Guid activeSchemeGuid;
169173
try
170174
{
171-
OnActivePowerSchemeChanged(new Guid(setting.Data));
175+
activeSchemeGuid = new Guid(setting.Data);
172176
}
173-
catch (ArgumentException) { }
177+
catch (ArgumentException)
178+
{
179+
return Win32Error.NO_ERROR;
180+
}
181+
182+
lock (schemeDispatchLock)
183+
{
184+
if (lastDispatchedSchemeGuid == activeSchemeGuid)
185+
{
186+
return Win32Error.NO_ERROR;
187+
}
188+
189+
lastDispatchedSchemeGuid = activeSchemeGuid;
190+
}
191+
192+
_ = Task.Run(() =>
193+
{
194+
try
195+
{
196+
OnActivePowerSchemeChanged(activeSchemeGuid);
197+
}
198+
catch (Exception ex)
199+
{
200+
Log.Error(
201+
ex,
202+
"Unhandled exception in active power scheme change dispatch for scheme {PowerSchemeGuid}",
203+
activeSchemeGuid);
204+
}
205+
});
206+
174207
return Win32Error.NO_ERROR;
175208
}
176209

PowerPlanSwitcher/AppContext.cs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace PowerPlanSwitcher;
22

33
using System.Configuration;
4+
using System.Threading;
45
using PowerManagement;
56
using PowerPlanSwitcher.Properties;
67
using ProcessManagement;
@@ -13,7 +14,9 @@ internal class AppContext : ApplicationContext
1314
{
1415
private IPowerManager PowerManager { get; init; }
1516
private TrayIcon TrayIcon { get; init; }
17+
private SynchronizationContext UiSynchronizationContext { get; }
1618
private Guid BaselineSchemeGuid { get; set; }
19+
private Guid LastHandledSystemSchemeGuid { get; set; }
1720

1821
public AppContext(
1922
TrayIcon trayIcon,
@@ -25,35 +28,19 @@ public AppContext(
2528
{
2629
TrayIcon = trayIcon;
2730
PowerManager = powerManager;
31+
UiSynchronizationContext =
32+
SynchronizationContext.Current
33+
?? new WindowsFormsSynchronizationContext();
2834

2935
BaselineSchemeGuid = powerManager.GetActivePowerSchemeGuid();
36+
LastHandledSystemSchemeGuid = BaselineSchemeGuid;
3037

3138
ToastDlg.Initialize();
3239

33-
powerManager.ActivePowerSchemeChanged +=
34-
(s, e) => trayIcon.UpdateIcon(e.ActiveSchemeGuid);
35-
36-
powerManager.ActivePowerSchemeChanged += (s, e) =>
37-
Log.ForContext("EventType", "PowerScheme.SystemChanged")
38-
.Information(
39-
"System activated power scheme: {PowerSchemeName} {PowerSchemeGuid}",
40-
powerManager.GetPowerSchemeName(e.ActiveSchemeGuid) ?? "<No Name>",
41-
e.ActiveSchemeGuid);
42-
4340
powerManager.ActivePowerSchemeChanged += (s, e) =>
44-
{
45-
if (ruleManager.AppliedRule is not null)
46-
{
47-
return;
48-
}
49-
50-
if (e.ActiveSchemeGuid == Guid.Empty)
51-
{
52-
return;
53-
}
54-
55-
BaselineSchemeGuid = e.ActiveSchemeGuid;
56-
};
41+
UiSynchronizationContext.Post(
42+
_ => HandleSystemPowerSchemeChanged(ruleManager, e.ActiveSchemeGuid),
43+
null);
5744

5845
ruleManager.RuleApplicationChanged +=
5946
RuleManager_RuleApplicationChanged;
@@ -70,6 +57,44 @@ public AppContext(
7057
ruleManager.StartMonitoring();
7158
}
7259

60+
private void HandleSystemPowerSchemeChanged(
61+
RuleManager ruleManager,
62+
Guid activeSchemeGuid)
63+
{
64+
if (activeSchemeGuid == LastHandledSystemSchemeGuid)
65+
{
66+
return;
67+
}
68+
69+
try
70+
{
71+
TrayIcon.UpdateIcon(activeSchemeGuid);
72+
}
73+
catch (ObjectDisposedException)
74+
{
75+
return;
76+
}
77+
78+
LastHandledSystemSchemeGuid = activeSchemeGuid;
79+
80+
Log.ForContext("EventType", "PowerScheme.SystemChanged")
81+
.Information(
82+
"System activated power scheme: {PowerSchemeGuid}",
83+
activeSchemeGuid);
84+
85+
if (ruleManager.AppliedRule is not null)
86+
{
87+
return;
88+
}
89+
90+
if (activeSchemeGuid == Guid.Empty)
91+
{
92+
return;
93+
}
94+
95+
BaselineSchemeGuid = activeSchemeGuid;
96+
}
97+
7398
private void Default_SettingChanging(
7499
object sender,
75100
SettingChangingEventArgs e)

PowerPlanSwitcher/ContextMenu.cs

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,137 @@ namespace PowerPlanSwitcher;
22

33
using Hotkeys;
44
using PowerManagement;
5+
using PowerPlanSwitcher.Properties;
56
using Serilog;
67

78
internal class ContextMenu : ContextMenuStrip
89
{
10+
private sealed record PowerSchemeMenuEntry(
11+
Guid Guid,
12+
string? Name,
13+
Image? Icon,
14+
bool Visible);
15+
916
private HotkeyManager HotkeyManager { get; init; }
1017
private Func<SettingsDlg> SettingsDlgFactory { get; init; }
18+
private IPowerManager PowerManager { get; init; }
19+
private Guid ActiveSchemeGuid { get; set; }
20+
private bool IsMenuDirty { get; set; }
21+
private readonly List<(Guid guid, ToolStripMenuItem menuItem, string label)> schemeButtons = [];
1122

1223
public ContextMenu(
1324
HotkeyManager hotkeyManager,
25+
IPowerManager powerManager,
1426
Func<SettingsDlg> settingsDlgFactory)
1527
{
1628
HotkeyManager = hotkeyManager;
29+
PowerManager = powerManager;
1730
SettingsDlgFactory = settingsDlgFactory;
31+
ActiveSchemeGuid = powerManager.GetActivePowerSchemeGuid();
32+
IsMenuDirty = false;
33+
34+
powerManager.ActivePowerSchemeChanged += (_, e) =>
35+
ActiveSchemeGuid = e.ActiveSchemeGuid;
36+
Settings.Default.PropertyChanged += (_, e) =>
37+
{
38+
if (string.Equals(
39+
e.PropertyName,
40+
nameof(Settings.Default.PowerSchemeSettings),
41+
StringComparison.Ordinal))
42+
{
43+
IsMenuDirty = true;
44+
}
45+
};
1846

1947
BuildContextMenu();
2048

21-
Opening += (s, e) => BuildContextMenu();
49+
Opening += (s, e) =>
50+
{
51+
if (IsMenuDirty)
52+
{
53+
BuildContextMenu();
54+
}
55+
else
56+
{
57+
RefreshPowerSchemeButtons();
58+
}
59+
};
2260
}
2361

2462
private void BuildContextMenu()
2563
{
64+
var activeSchemeGuid = ActiveSchemeGuid;
65+
2666
Items.Clear();
27-
AddPowerSchemes();
67+
schemeButtons.Clear();
68+
69+
AddPowerSchemes(activeSchemeGuid);
2870
_ = Items.Add(new ToolStripSeparator());
2971
AddSettingsButton();
3072
AddAboutButton();
3173
_ = Items.Add(new ToolStripSeparator());
3274
AddCloseButton();
75+
76+
IsMenuDirty = false;
3377
}
3478

35-
private void AddPowerSchemes()
79+
private void RefreshPowerSchemeButtons()
3680
{
37-
var activeSchemeGuid = PowerManager.Static.GetActivePowerSchemeGuid();
38-
foreach (var (guid, name) in PowerManager.Static.GetPowerSchemes())
81+
var activeSchemeGuid = ActiveSchemeGuid;
82+
foreach (var (guid, menuItem, label) in schemeButtons)
3983
{
40-
var setting = PowerSchemeSettings.GetSetting(guid);
41-
if (setting is not null && !setting.Visible)
84+
menuItem.Text = (guid == activeSchemeGuid ? "(Active) " : string.Empty) + label;
85+
}
86+
}
87+
88+
private void AddPowerSchemes(Guid activeSchemeGuid)
89+
{
90+
foreach (var scheme in GetPowerSchemeEntries())
91+
{
92+
if (!scheme.Visible)
4293
{
4394
continue;
4495
}
4596

4697
var button = new ToolStripMenuItem
4798
{
48-
Image = setting?.Icon,
49-
Text = (activeSchemeGuid == guid ? "(Active) " : string.Empty)
50-
+ (name ?? guid.ToString()),
99+
Image = scheme.Icon,
100+
Text = (activeSchemeGuid == scheme.Guid ? "(Active) " : string.Empty)
101+
+ (scheme.Name ?? scheme.Guid.ToString()),
51102
};
52103

104+
var label = scheme.Name ?? scheme.Guid.ToString();
105+
schemeButtons.Add((scheme.Guid, button, label));
106+
53107
button.Click += (_, _) =>
54108
{
55109
Log.ForContext("EventType", "PowerScheme.ActivationRequested")
56110
.Information(
57111
"Activating power scheme: {PowerSchemeName} " +
58112
"{PowerSchemeGuid} Reason: User selection",
59-
name,
60-
guid);
61-
_ = PowerManager.Static.SetActivePowerSchemeAsync(guid);
113+
scheme.Name,
114+
scheme.Guid);
115+
_ = PowerManager.SetActivePowerSchemeAsync(scheme.Guid);
62116
};
63117

64118
_ = Items.Add(button);
65119
}
66120
}
67121

122+
private IEnumerable<PowerSchemeMenuEntry> GetPowerSchemeEntries()
123+
{
124+
foreach (var (guid, name) in PowerManager.GetPowerSchemes())
125+
{
126+
var setting = PowerSchemeSettings.GetSetting(guid);
127+
128+
yield return new PowerSchemeMenuEntry(
129+
guid,
130+
name,
131+
setting?.Icon,
132+
setting?.Visible ?? true);
133+
}
134+
}
135+
68136
private void AddSettingsButton()
69137
{
70138
var button = new ToolStripMenuItem

PowerPlanSwitcher/PowerPlanSwitcher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<VersionPrefix>1.2.8</VersionPrefix>
10-
<VersionSuffix>rc3</VersionSuffix>
10+
<VersionSuffix>rc4</VersionSuffix>
1111
<ApplicationIcon>Resources\power_surge.ico</ApplicationIcon>
1212
<Description>Tray-Icon tool to show the currently selected windows power plan, switch it manually and automatically.
1313

0 commit comments

Comments
 (0)