From c2f1cbf86762cea7a572b1e36af68e25a954f07f Mon Sep 17 00:00:00 2001 From: Den Delimarsky <1389609+dend@users.noreply.github.com> Date: Mon, 10 May 2021 20:58:34 -0700 Subject: [PATCH] Code cleanup --- .../espresso/Espresso/Core/APIHelper.cs | 8 ++- .../espresso/Espresso/Core/TrayHelper.cs | 53 +++++++++++++------ src/modules/espresso/Espresso/Program.cs | 3 -- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/modules/espresso/Espresso/Core/APIHelper.cs b/src/modules/espresso/Espresso/Core/APIHelper.cs index befa736a73..fae3d4733f 100644 --- a/src/modules/espresso/Espresso/Core/APIHelper.cs +++ b/src/modules/espresso/Espresso/Core/APIHelper.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; namespace Espresso.Shell.Core { - [FlagsAttribute] + [Flags] public enum EXECUTION_STATE : uint { ES_AWAYMODE_REQUIRED = 0x00000040, @@ -32,7 +32,7 @@ namespace Espresso.Shell.Core private static CancellationTokenSource TokenSource = new CancellationTokenSource(); private static CancellationToken ThreadToken; - private static Logger log; + private static readonly Logger log; // More details about the API used: https://docs.microsoft.com/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] @@ -191,9 +191,7 @@ namespace Espresso.Shell.Core public static Icon? Extract(string file, int number, bool largeIcon) { - IntPtr large; - IntPtr small; - ExtractIconEx(file, number, out large, out small, 1); + ExtractIconEx(file, number, out IntPtr large, out IntPtr small, 1); try { return Icon.FromHandle(largeIcon ? large : small); diff --git a/src/modules/espresso/Espresso/Core/TrayHelper.cs b/src/modules/espresso/Espresso/Core/TrayHelper.cs index ead4705514..0afb5ac8e2 100644 --- a/src/modules/espresso/Espresso/Core/TrayHelper.cs +++ b/src/modules/espresso/Espresso/Core/TrayHelper.cs @@ -63,7 +63,7 @@ namespace Espresso.Shell.Core { // Just changing the display mode. var currentSettings = ModuleSettings.GetSettings(text); - currentSettings.Properties.KeepDisplayOn = settings.Properties.KeepDisplayOn; + currentSettings.Properties.KeepDisplayOn.Value = !currentSettings.Properties.KeepDisplayOn.Value; ModuleSettings.SaveSettings(JsonSerializer.Serialize(currentSettings), text); }); @@ -74,29 +74,43 @@ namespace Espresso.Shell.Core var contextMenuStrip = new ContextMenuStrip(); // Main toolstrip. - var operationContextMenu = new ToolStripMenuItem(); - operationContextMenu.Text = "Mode"; + var operationContextMenu = new ToolStripMenuItem + { + Text = "Mode" + }; // Indefinite keep-awake menu item. - var indefiniteMenuItem = new ToolStripMenuItem(); - indefiniteMenuItem.Text = "Indefinite"; + var indefiniteMenuItem = new ToolStripMenuItem + { + Text = "Indefinite" + }; if (mode == EspressoMode.INDEFINITE) { indefiniteMenuItem.Checked = true; } + else + { + indefiniteMenuItem.Checked = false; + } indefiniteMenuItem.Click += (e, s) => { // User opted to set the mode to indefinite, so we need to write new settings. indefiniteKeepAwakeCallback(); }; - var displayOnMenuItem = new ToolStripMenuItem(); - displayOnMenuItem.Text = "Keep display on"; + var displayOnMenuItem = new ToolStripMenuItem + { + Text = "Keep display on" + }; if (keepDisplayOn) { displayOnMenuItem.Checked = true; } + else + { + displayOnMenuItem.Checked = false; + } displayOnMenuItem.Click += (e, s) => { // User opted to set the display mode directly. @@ -104,27 +118,35 @@ namespace Espresso.Shell.Core }; // Timed keep-awake menu item - var timedMenuItem = new ToolStripMenuItem(); - timedMenuItem.Text = "Timed"; + var timedMenuItem = new ToolStripMenuItem + { + Text = "Timed" + }; - var halfHourMenuItem = new ToolStripMenuItem(); - halfHourMenuItem.Text = "30 minutes"; + var halfHourMenuItem = new ToolStripMenuItem + { + Text = "30 minutes" + }; halfHourMenuItem.Click += (e, s) => { // User is setting the keep-awake to 30 minutes. timedKeepAwakeCallback(0, 30); }; - var oneHourMenuItem = new ToolStripMenuItem(); - oneHourMenuItem.Text = "1 hour"; + var oneHourMenuItem = new ToolStripMenuItem + { + Text = "1 hour" + }; oneHourMenuItem.Click += (e, s) => { // User is setting the keep-awake to 1 hour. timedKeepAwakeCallback(1, 0); }; - var twoHoursMenuItem = new ToolStripMenuItem(); - twoHoursMenuItem.Text = "2 hours"; + var twoHoursMenuItem = new ToolStripMenuItem + { + Text = "2 hours" + }; twoHoursMenuItem.Click += (e, s) => { // User is setting the keep-awake to 2 hours. @@ -142,6 +164,7 @@ namespace Espresso.Shell.Core contextMenuStrip.Items.Add(operationContextMenu); + TrayIcon.Text = text; TrayIcon.ContextMenuStrip = contextMenuStrip; } } diff --git a/src/modules/espresso/Espresso/Program.cs b/src/modules/espresso/Espresso/Program.cs index e9a3469e60..29f259edaa 100644 --- a/src/modules/espresso/Espresso/Program.cs +++ b/src/modules/espresso/Espresso/Program.cs @@ -230,9 +230,6 @@ namespace Espresso.Shell if (settings != null) { - // If the settings were successfully processed, we need to set the right mode of operation. - // INDEFINITE = 0 - // TIMED = 1 switch (settings.Properties.Mode) { case EspressoMode.INDEFINITE: