mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Working on putting together tray capabilities.
This commit is contained in:
44
src/modules/espresso/Espresso.Shell/Core/TrayHelper.cs
Normal file
44
src/modules/espresso/Espresso.Shell/Core/TrayHelper.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
<Platforms>AnyCPU;x64</Platforms>
|
<Platforms>AnyCPU;x64</Platforms>
|
||||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||||
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
|||||||
@@ -6,6 +6,12 @@ using Newtonsoft.Json;
|
|||||||
|
|
||||||
namespace Espresso.Shell.Models
|
namespace Espresso.Shell.Models
|
||||||
{
|
{
|
||||||
|
public enum EspressoMode
|
||||||
|
{
|
||||||
|
INDEFINITE = 0,
|
||||||
|
TIMED = 1,
|
||||||
|
}
|
||||||
|
|
||||||
public class EspressoSettingsModel
|
public class EspressoSettingsModel
|
||||||
{
|
{
|
||||||
[JsonProperty("properties")]
|
[JsonProperty("properties")]
|
||||||
@@ -21,7 +27,7 @@ namespace Espresso.Shell.Models
|
|||||||
[JsonProperty("espresso_keep_display_on")]
|
[JsonProperty("espresso_keep_display_on")]
|
||||||
public KeepDisplayOn? KeepDisplayOn { get; set; }
|
public KeepDisplayOn? KeepDisplayOn { get; set; }
|
||||||
[JsonProperty("espresso_mode")]
|
[JsonProperty("espresso_mode")]
|
||||||
public int? Mode { get; set; }
|
public EspressoMode Mode { get; set; }
|
||||||
[JsonProperty("espresso_hours")]
|
[JsonProperty("espresso_hours")]
|
||||||
public Hours? Hours { get; set; }
|
public Hours? Hours { get; set; }
|
||||||
[JsonProperty("espresso_minutes")]
|
[JsonProperty("espresso_minutes")]
|
||||||
|
|||||||
@@ -90,10 +90,10 @@ namespace Espresso.Shell
|
|||||||
|
|
||||||
timeOption.Required = false;
|
timeOption.Required = false;
|
||||||
|
|
||||||
var powerToysPidOption = new Option<int>(
|
var pidOption = new Option<int>(
|
||||||
aliases: new[] { "--ptpid", "-p" },
|
aliases: new[] { "--pid", "-p" },
|
||||||
getDefaultValue: () => 0,
|
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)
|
Argument = new Argument<int>(() => 0)
|
||||||
{
|
{
|
||||||
@@ -101,14 +101,14 @@ namespace Espresso.Shell
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
powerToysPidOption.Required = false;
|
pidOption.Required = false;
|
||||||
|
|
||||||
var rootCommand = new RootCommand
|
var rootCommand = new RootCommand
|
||||||
{
|
{
|
||||||
configOption,
|
configOption,
|
||||||
displayOption,
|
displayOption,
|
||||||
timeOption,
|
timeOption,
|
||||||
powerToysPidOption
|
pidOption
|
||||||
};
|
};
|
||||||
|
|
||||||
rootCommand.Description = appName;
|
rootCommand.Description = appName;
|
||||||
@@ -126,12 +126,12 @@ namespace Espresso.Shell
|
|||||||
Environment.Exit(exitCode);
|
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 --config is: {config}");
|
||||||
log.Info($"The value for --display-on is: {displayOn}");
|
log.Info($"The value for --display-on is: {displayOn}");
|
||||||
log.Info($"The value for --time-limit is: {timeLimit}");
|
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))
|
if (!string.IsNullOrWhiteSpace(config))
|
||||||
{
|
{
|
||||||
@@ -166,14 +166,6 @@ namespace Espresso.Shell
|
|||||||
log.Info(errorString);
|
log.Info(errorString);
|
||||||
log.Debug(errorString);
|
log.Debug(errorString);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ptpid != 0)
|
|
||||||
{
|
|
||||||
RunnerHelper.WaitForPowerToysRunner(ptpid, () =>
|
|
||||||
{
|
|
||||||
Environment.Exit(0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -199,6 +191,14 @@ namespace Espresso.Shell
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pid != 0)
|
||||||
|
{
|
||||||
|
RunnerHelper.WaitForPowerToysRunner(pid, () =>
|
||||||
|
{
|
||||||
|
Environment.Exit(0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
new ManualResetEvent(false).WaitOne();
|
new ManualResetEvent(false).WaitOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,7 +233,7 @@ namespace Espresso.Shell
|
|||||||
// TIMED = 1
|
// TIMED = 1
|
||||||
switch (settings.Properties.Mode)
|
switch (settings.Properties.Mode)
|
||||||
{
|
{
|
||||||
case 0:
|
case EspressoMode.INDEFINITE:
|
||||||
{
|
{
|
||||||
// Indefinite keep awake.
|
// Indefinite keep awake.
|
||||||
bool success = APIHelper.SetIndefiniteKeepAwake(settings.Properties.KeepDisplayOn.Value);
|
bool success = APIHelper.SetIndefiniteKeepAwake(settings.Properties.KeepDisplayOn.Value);
|
||||||
@@ -249,7 +249,7 @@ namespace Espresso.Shell
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 1:
|
case EspressoMode.TIMED:
|
||||||
{
|
{
|
||||||
// Timed keep-awake.
|
// Timed keep-awake.
|
||||||
long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60);
|
long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60);
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ private:
|
|||||||
// Get the configuration file that will be passed to the process.
|
// 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 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);
|
Logger::trace(L"Espresso launching with parameters: " + executable_args);
|
||||||
|
|
||||||
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
||||||
|
|||||||
Reference in New Issue
Block a user