Update how tray is handled

This commit is contained in:
Den Delimarsky
2021-05-11 19:40:02 -07:00
parent 3d8ed3905e
commit 95bac54c0e
5 changed files with 45 additions and 23 deletions

View File

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

View File

@@ -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<int, int> timedKeepAwakeCallback, Action keepDisplayOnCallback)
internal static void SetTray(string text, bool keepDisplayOn, EspressoMode mode, Action indefiniteKeepAwakeCallback, Action<int, int> 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;

View File

@@ -13,6 +13,7 @@
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<AssemblyName>PowerToys.Espresso</AssemblyName>
<Version>$(Version).0</Version>
<ApplicationIcon>Images\Espresso.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -37,6 +38,10 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<None Remove="Images\Espresso.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="NLog" Version="4.7.9" />
@@ -76,4 +81,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Resource Include="Images\Espresso.ico" />
</ItemGroup>
</Project>

View File

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 66 KiB

View File

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