mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Update how tray is handled
This commit is contained in:
@@ -3,7 +3,6 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -37,10 +36,6 @@ namespace Espresso.Shell.Core
|
|||||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||||
private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
|
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()
|
static APIHelper()
|
||||||
{
|
{
|
||||||
_log = LogManager.GetCurrentClassLogger();
|
_log = LogManager.GetCurrentClassLogger();
|
||||||
@@ -225,18 +220,5 @@ namespace Espresso.Shell.Core
|
|||||||
return string.Empty;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,16 @@ namespace Espresso.Shell.Core
|
|||||||
settings.Properties.Mode,
|
settings.Properties.Mode,
|
||||||
IndefiniteKeepAwakeCallback(text),
|
IndefiniteKeepAwakeCallback(text),
|
||||||
TimedKeepAwakeCallback(text),
|
TimedKeepAwakeCallback(text),
|
||||||
KeepDisplayOnCallback(text));
|
KeepDisplayOnCallback(text),
|
||||||
|
ExitCallback());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Action ExitCallback()
|
||||||
|
{
|
||||||
|
return () =>
|
||||||
|
{
|
||||||
|
Environment.Exit(0);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Action KeepDisplayOnCallback(string text)
|
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();
|
var contextMenuStrip = new ContextMenuStrip();
|
||||||
|
|
||||||
@@ -105,7 +114,7 @@ namespace Espresso.Shell.Core
|
|||||||
// Indefinite keep-awake menu item.
|
// Indefinite keep-awake menu item.
|
||||||
var indefiniteMenuItem = new ToolStripMenuItem
|
var indefiniteMenuItem = new ToolStripMenuItem
|
||||||
{
|
{
|
||||||
Text = "Indefinite",
|
Text = "Keep awake indefinitely",
|
||||||
};
|
};
|
||||||
|
|
||||||
if (mode == EspressoMode.INDEFINITE)
|
if (mode == EspressoMode.INDEFINITE)
|
||||||
@@ -145,8 +154,16 @@ namespace Espresso.Shell.Core
|
|||||||
// Timed keep-awake menu item
|
// Timed keep-awake menu item
|
||||||
var timedMenuItem = new ToolStripMenuItem
|
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
|
var halfHourMenuItem = new ToolStripMenuItem
|
||||||
{
|
{
|
||||||
@@ -178,6 +195,17 @@ namespace Espresso.Shell.Core
|
|||||||
timedKeepAwakeCallback(2, 0);
|
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(halfHourMenuItem);
|
||||||
timedMenuItem.DropDownItems.Add(oneHourMenuItem);
|
timedMenuItem.DropDownItems.Add(oneHourMenuItem);
|
||||||
timedMenuItem.DropDownItems.Add(twoHoursMenuItem);
|
timedMenuItem.DropDownItems.Add(twoHoursMenuItem);
|
||||||
@@ -188,6 +216,8 @@ namespace Espresso.Shell.Core
|
|||||||
operationContextMenu.DropDownItems.Add(displayOnMenuItem);
|
operationContextMenu.DropDownItems.Add(displayOnMenuItem);
|
||||||
|
|
||||||
contextMenuStrip.Items.Add(operationContextMenu);
|
contextMenuStrip.Items.Add(operationContextMenu);
|
||||||
|
contextMenuStrip.Items.Add(new ToolStripSeparator());
|
||||||
|
contextMenuStrip.Items.Add(exitContextMenu);
|
||||||
|
|
||||||
TrayIcon.Text = text;
|
TrayIcon.Text = text;
|
||||||
TrayIcon.ContextMenuStrip = contextMenuStrip;
|
TrayIcon.ContextMenuStrip = contextMenuStrip;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
|
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
|
||||||
<AssemblyName>PowerToys.Espresso</AssemblyName>
|
<AssemblyName>PowerToys.Espresso</AssemblyName>
|
||||||
<Version>$(Version).0</Version>
|
<Version>$(Version).0</Version>
|
||||||
|
<ApplicationIcon>Images\Espresso.ico</ApplicationIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
@@ -37,6 +38,10 @@
|
|||||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Images\Espresso.ico" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
|
||||||
<PackageReference Include="NLog" Version="4.7.9" />
|
<PackageReference Include="NLog" Version="4.7.9" />
|
||||||
@@ -76,4 +81,7 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Resource Include="Images\Espresso.ico" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 66 KiB |
@@ -6,12 +6,14 @@ using System;
|
|||||||
using System.CommandLine;
|
using System.CommandLine;
|
||||||
using System.CommandLine.Invocation;
|
using System.CommandLine.Invocation;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Drawing;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reactive.Concurrency;
|
using System.Reactive.Concurrency;
|
||||||
using System.Reactive.Linq;
|
using System.Reactive.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Windows;
|
||||||
using Espresso.Shell.Core;
|
using Espresso.Shell.Core;
|
||||||
using ManagedCommon;
|
using ManagedCommon;
|
||||||
using Microsoft.PowerToys.Settings.UI.Library;
|
using Microsoft.PowerToys.Settings.UI.Library;
|
||||||
@@ -134,7 +136,7 @@ namespace Espresso.Shell
|
|||||||
_log.Info($"The value for --pid is: {pid}");
|
_log.Info($"The value for --pid is: {pid}");
|
||||||
|
|
||||||
#pragma warning disable CS8604 // Possible null reference argument.
|
#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.
|
#pragma warning restore CS8604 // Possible null reference argument.
|
||||||
|
|
||||||
if (usePtConfig)
|
if (usePtConfig)
|
||||||
|
|||||||
Reference in New Issue
Block a user