diff --git a/src/modules/espresso/Espresso/Core/APIHelper.cs b/src/modules/espresso/Espresso/Core/APIHelper.cs index b56025286e..54a6bb3b8b 100644 --- a/src/modules/espresso/Espresso/Core/APIHelper.cs +++ b/src/modules/espresso/Espresso/Core/APIHelper.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Drawing; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -37,10 +36,6 @@ namespace Espresso.Shell.Core [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); - // More details about the API used: https://docs.microsoft.com/windows/win32/api/shellapi/nf-shellapi-extracticonexw - [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] - private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons); - static APIHelper() { _log = LogManager.GetCurrentClassLogger(); @@ -225,18 +220,5 @@ namespace Espresso.Shell.Core return string.Empty; } } - - public static Icon? Extract(string file, int number, bool largeIcon) - { - ExtractIconEx(file, number, out IntPtr large, out IntPtr small, 1); - try - { - return Icon.FromHandle(largeIcon ? large : small); - } - catch - { - return null; - } - } } } diff --git a/src/modules/espresso/Espresso/Core/TrayHelper.cs b/src/modules/espresso/Espresso/Core/TrayHelper.cs index fe41509b68..3aea4656e4 100644 --- a/src/modules/espresso/Espresso/Core/TrayHelper.cs +++ b/src/modules/espresso/Espresso/Core/TrayHelper.cs @@ -51,7 +51,16 @@ namespace Espresso.Shell.Core settings.Properties.Mode, IndefiniteKeepAwakeCallback(text), TimedKeepAwakeCallback(text), - KeepDisplayOnCallback(text)); + KeepDisplayOnCallback(text), + ExitCallback()); + } + + private static Action ExitCallback() + { + return () => + { + Environment.Exit(0); + }; } private static Action KeepDisplayOnCallback(string text) @@ -92,7 +101,7 @@ namespace Espresso.Shell.Core }; } - internal static void SetTray(string text, bool keepDisplayOn, EspressoMode mode, Action indefiniteKeepAwakeCallback, Action timedKeepAwakeCallback, Action keepDisplayOnCallback) + internal static void SetTray(string text, bool keepDisplayOn, EspressoMode mode, Action indefiniteKeepAwakeCallback, Action timedKeepAwakeCallback, Action keepDisplayOnCallback, Action exitCallback) { var contextMenuStrip = new ContextMenuStrip(); @@ -105,7 +114,7 @@ namespace Espresso.Shell.Core // Indefinite keep-awake menu item. var indefiniteMenuItem = new ToolStripMenuItem { - Text = "Indefinite", + Text = "Keep awake indefinitely", }; if (mode == EspressoMode.INDEFINITE) @@ -145,8 +154,16 @@ namespace Espresso.Shell.Core // Timed keep-awake menu item var timedMenuItem = new ToolStripMenuItem { - Text = "Timed", + Text = "Keep awake temporarily", }; + if (mode == EspressoMode.TIMED) + { + timedMenuItem.Checked = true; + } + else + { + timedMenuItem.Checked = false; + } var halfHourMenuItem = new ToolStripMenuItem { @@ -178,6 +195,17 @@ namespace Espresso.Shell.Core timedKeepAwakeCallback(2, 0); }; + // Exit menu item. + var exitContextMenu = new ToolStripMenuItem + { + Text = "Exit", + }; + exitContextMenu.Click += (e, s) => + { + // User is setting the keep-awake to 2 hours. + exitCallback(); + }; + timedMenuItem.DropDownItems.Add(halfHourMenuItem); timedMenuItem.DropDownItems.Add(oneHourMenuItem); timedMenuItem.DropDownItems.Add(twoHoursMenuItem); @@ -188,6 +216,8 @@ namespace Espresso.Shell.Core operationContextMenu.DropDownItems.Add(displayOnMenuItem); contextMenuStrip.Items.Add(operationContextMenu); + contextMenuStrip.Items.Add(new ToolStripSeparator()); + contextMenuStrip.Items.Add(exitContextMenu); TrayIcon.Text = text; TrayIcon.ContextMenuStrip = contextMenuStrip; diff --git a/src/modules/espresso/Espresso/Espresso.csproj b/src/modules/espresso/Espresso/Espresso.csproj index 2564272338..8ae7328e0b 100644 --- a/src/modules/espresso/Espresso/Espresso.csproj +++ b/src/modules/espresso/Espresso/Espresso.csproj @@ -13,6 +13,7 @@ true PowerToys.Espresso $(Version).0 + Images\Espresso.ico @@ -37,6 +38,10 @@ true + + + + @@ -76,4 +81,7 @@ all + + + diff --git a/src/modules/espresso/Espresso/Espresso.ico b/src/modules/espresso/Espresso/Images/Espresso.ico similarity index 100% rename from src/modules/espresso/Espresso/Espresso.ico rename to src/modules/espresso/Espresso/Images/Espresso.ico diff --git a/src/modules/espresso/Espresso/Program.cs b/src/modules/espresso/Espresso/Program.cs index 2e3f06cb39..6b826878cb 100644 --- a/src/modules/espresso/Espresso/Program.cs +++ b/src/modules/espresso/Espresso/Program.cs @@ -6,12 +6,14 @@ using System; using System.CommandLine; using System.CommandLine.Invocation; using System.Diagnostics; +using System.Drawing; using System.IO; using System.Linq; using System.Reactive.Concurrency; using System.Reactive.Linq; using System.Reflection; using System.Threading; +using System.Windows; using Espresso.Shell.Core; using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Library; @@ -134,7 +136,7 @@ namespace Espresso.Shell _log.Info($"The value for --pid is: {pid}"); #pragma warning disable CS8604 // Possible null reference argument. - TrayHelper.InitializeTray(AppName, APIHelper.Extract("shell32.dll", 32, true)); + TrayHelper.InitializeTray(AppName, new Icon(Application.GetResourceStream(new Uri("/Images/Espresso.ico", UriKind.Relative)).Stream)); #pragma warning restore CS8604 // Possible null reference argument. if (usePtConfig)