Working on putting together tray capabilities.

This commit is contained in:
Den Delimarsky 2021-05-04 08:21:58 -07:00
parent 33a81416fc
commit ff1ca4dac1
No known key found for this signature in database
GPG Key ID: E1BE1355085F0BCF
5 changed files with 71 additions and 20 deletions

View File

@ -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);
}
}
}

View File

@ -7,7 +7,8 @@
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">

View File

@ -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")]

View File

@ -90,10 +90,10 @@ namespace Espresso.Shell
timeOption.Required = false;
var powerToysPidOption = new Option<int>(
aliases: new[] { "--ptpid", "-p" },
var pidOption = new Option<int>(
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<int>(() => 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);

View File

@ -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) };