diff --git a/src/modules/espresso/Espresso.Shell/Core/TrayHelper.cs b/src/modules/espresso/Espresso.Shell/Core/TrayHelper.cs new file mode 100644 index 0000000000..53e6a863b5 --- /dev/null +++ b/src/modules/espresso/Espresso.Shell/Core/TrayHelper.cs @@ -0,0 +1,44 @@ +using Espresso.Shell.Models; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Text; +using System.Windows.Forms; + +namespace Espresso.Shell.Core +{ + internal static class TrayHelper + { + static NotifyIcon trayIcon; + static TrayHelper() + { + trayIcon = new NotifyIcon(); + } + + private static void InitializeTrayIcon(string text, Icon icon, ContextMenuStrip contextMenu) + { + trayIcon.Text = text; + trayIcon.Icon = icon; + trayIcon.ContextMenuStrip = contextMenu; + trayIcon.Visible = true; + } + + internal static void InitializeEspressoTray(EspressoMode mode, bool keepDisplayOn, Action indefiniteSelectionCallback, Action timedSelectionCallback) + { + var contextMenuStrip = new ContextMenuStrip(); + + // Main toolstrip. + var operationContextMenu = new ToolStrip(); + operationContextMenu.Text = "Mode"; + + // Indefinite keep-awake menu item. + var indefiniteMenuItem = new ToolStripMenuItem(); + indefiniteMenuItem.Text = "Indefinite"; + if (mode == EspressoMode.INDEFINITE) + { + indefiniteMenuItem.Checked = true; + } + operationContextMenu.Items.Add(indefiniteMenuItem); + } + } +} diff --git a/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj b/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj index ea61957bbd..ff765c4f09 100644 --- a/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj +++ b/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj @@ -7,7 +7,8 @@ enable AnyCPU;x64 false - false + false + true diff --git a/src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs b/src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs index 8925fac979..db9263dac5 100644 --- a/src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs +++ b/src/modules/espresso/Espresso.Shell/Models/EspressoSettingsModel.cs @@ -6,6 +6,12 @@ using Newtonsoft.Json; namespace Espresso.Shell.Models { + public enum EspressoMode + { + INDEFINITE = 0, + TIMED = 1, + } + public class EspressoSettingsModel { [JsonProperty("properties")] @@ -21,7 +27,7 @@ namespace Espresso.Shell.Models [JsonProperty("espresso_keep_display_on")] public KeepDisplayOn? KeepDisplayOn { get; set; } [JsonProperty("espresso_mode")] - public int? Mode { get; set; } + public EspressoMode Mode { get; set; } [JsonProperty("espresso_hours")] public Hours? Hours { get; set; } [JsonProperty("espresso_minutes")] diff --git a/src/modules/espresso/Espresso.Shell/Program.cs b/src/modules/espresso/Espresso.Shell/Program.cs index 4e16cd1135..2dbbdce75d 100644 --- a/src/modules/espresso/Espresso.Shell/Program.cs +++ b/src/modules/espresso/Espresso.Shell/Program.cs @@ -90,10 +90,10 @@ namespace Espresso.Shell timeOption.Required = false; - var powerToysPidOption = new Option( - aliases: new[] { "--ptpid", "-p" }, + var pidOption = new Option( + aliases: new[] { "--pid", "-p" }, getDefaultValue: () => 0, - description: "Reference to PowerToys PID, when executed under the application context.") + description: "Bind the execution of Espresso to another process.") { Argument = new Argument(() => 0) { @@ -101,14 +101,14 @@ namespace Espresso.Shell }, }; - powerToysPidOption.Required = false; + pidOption.Required = false; var rootCommand = new RootCommand { configOption, displayOption, timeOption, - powerToysPidOption + pidOption }; rootCommand.Description = appName; @@ -126,12 +126,12 @@ namespace Espresso.Shell Environment.Exit(exitCode); } - private static void HandleCommandLineArguments(string config, bool displayOn, long timeLimit, int ptpid) + private static void HandleCommandLineArguments(string config, bool displayOn, long timeLimit, int pid) { log.Info($"The value for --config is: {config}"); log.Info($"The value for --display-on is: {displayOn}"); log.Info($"The value for --time-limit is: {timeLimit}"); - log.Info($"The value for --ptpid is: {ptpid}"); + log.Info($"The value for --pid is: {pid}"); if (!string.IsNullOrWhiteSpace(config)) { @@ -166,14 +166,6 @@ namespace Espresso.Shell log.Info(errorString); log.Debug(errorString); } - - if (ptpid != 0) - { - RunnerHelper.WaitForPowerToysRunner(ptpid, () => - { - Environment.Exit(0); - }); - } } else { @@ -199,6 +191,14 @@ namespace Espresso.Shell } } + if (pid != 0) + { + RunnerHelper.WaitForPowerToysRunner(pid, () => + { + Environment.Exit(0); + }); + } + new ManualResetEvent(false).WaitOne(); } @@ -233,7 +233,7 @@ namespace Espresso.Shell // TIMED = 1 switch (settings.Properties.Mode) { - case 0: + case EspressoMode.INDEFINITE: { // Indefinite keep awake. bool success = APIHelper.SetIndefiniteKeepAwake(settings.Properties.KeepDisplayOn.Value); @@ -249,7 +249,7 @@ namespace Espresso.Shell } break; } - case 1: + case EspressoMode.TIMED: { // Timed keep-awake. long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60); diff --git a/src/modules/espresso/Espresso/dllmain.cpp b/src/modules/espresso/Espresso/dllmain.cpp index 1b877910a3..de9457ea85 100644 --- a/src/modules/espresso/Espresso/dllmain.cpp +++ b/src/modules/espresso/Espresso/dllmain.cpp @@ -81,7 +81,7 @@ private: // Get the configuration file that will be passed to the process. std::wstring espresso_settings_location = PTSettingsHelper::get_module_save_file_location(MODULE_NAME); - std::wstring executable_args = L"--config " + espresso_settings_location + L" --ptpid " + std::to_wstring(powertoys_pid); + std::wstring executable_args = L"--config " + espresso_settings_location + L" --pid " + std::to_wstring(powertoys_pid); Logger::trace(L"Espresso launching with parameters: " + executable_args); SHELLEXECUTEINFOW sei{ sizeof(sei) };