Stuff for tunesync
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.System.Threading;
|
||||
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
public class EfficiencyMode :
|
||||
IEfficiencyMode
|
||||
{
|
||||
public unsafe void SetProcessQualityOfServiceLevel(QualityOfService level)
|
||||
{
|
||||
PROCESS_POWER_THROTTLING_STATE powerThrottling = new PROCESS_POWER_THROTTLING_STATE
|
||||
{
|
||||
Version = PInvoke.PROCESS_POWER_THROTTLING_CURRENT_VERSION
|
||||
};
|
||||
|
||||
switch (level)
|
||||
{
|
||||
case QualityOfService.Default:
|
||||
powerThrottling.ControlMask = 0;
|
||||
powerThrottling.StateMask = 0;
|
||||
break;
|
||||
|
||||
case QualityOfService.Eco when Environment.OSVersion.Version >= new Version(11, 0):
|
||||
case QualityOfService.Low:
|
||||
powerThrottling.ControlMask = PInvoke.PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
|
||||
powerThrottling.StateMask = PInvoke.PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
|
||||
break;
|
||||
|
||||
case QualityOfService.High:
|
||||
powerThrottling.ControlMask = PInvoke.PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
|
||||
powerThrottling.StateMask = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
_ = PInvoke.SetProcessInformation(
|
||||
hProcess: PInvoke.GetCurrentProcess(),
|
||||
ProcessInformationClass: PROCESS_INFORMATION_CLASS.ProcessPowerThrottling,
|
||||
ProcessInformation: &powerThrottling,
|
||||
ProcessInformationSize: (uint)sizeof(PROCESS_POWER_THROTTLING_STATE));
|
||||
}
|
||||
|
||||
public unsafe void SetProcessPriorityClass(ProcessPriority priorityClass)
|
||||
{
|
||||
PROCESS_CREATION_FLAGS flags = priorityClass switch
|
||||
{
|
||||
ProcessPriority.Default => PROCESS_CREATION_FLAGS.NORMAL_PRIORITY_CLASS,
|
||||
ProcessPriority.Idle => PROCESS_CREATION_FLAGS.IDLE_PRIORITY_CLASS,
|
||||
ProcessPriority.BelowNormal => PROCESS_CREATION_FLAGS.BELOW_NORMAL_PRIORITY_CLASS,
|
||||
ProcessPriority.Normal => PROCESS_CREATION_FLAGS.NORMAL_PRIORITY_CLASS,
|
||||
ProcessPriority.AboveNormal => PROCESS_CREATION_FLAGS.ABOVE_NORMAL_PRIORITY_CLASS,
|
||||
ProcessPriority.High => PROCESS_CREATION_FLAGS.HIGH_PRIORITY_CLASS,
|
||||
ProcessPriority.Realtime => PROCESS_CREATION_FLAGS.REALTIME_PRIORITY_CLASS,
|
||||
_ => throw new NotImplementedException(),
|
||||
};
|
||||
|
||||
_ = PInvoke.SetPriorityClass(
|
||||
hProcess: PInvoke.GetCurrentProcess(),
|
||||
dwPriorityClass: flags);
|
||||
}
|
||||
|
||||
public void SetEfficiencyMode(bool value)
|
||||
{
|
||||
QualityOfService ecoLevel = Environment.OSVersion.Version >= new Version(11, 0)
|
||||
? QualityOfService.Eco
|
||||
: QualityOfService.Low;
|
||||
|
||||
SetProcessQualityOfServiceLevel(value ? ecoLevel : QualityOfService.Default);
|
||||
SetProcessPriorityClass(value ? ProcessPriority.Idle : ProcessPriority.Default);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Toolkit.Windows
|
||||
{
|
||||
public interface IEfficiencyMode
|
||||
{
|
||||
void SetEfficiencyMode(bool value);
|
||||
void SetProcessPriorityClass(ProcessPriority priorityClass);
|
||||
void SetProcessQualityOfServiceLevel(QualityOfService level);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
public interface INotifyIcon :
|
||||
IInitialization,
|
||||
IDisposable
|
||||
{
|
||||
void SetIcon(IntPtr iconHandle);
|
||||
}
|
||||
@@ -4,4 +4,4 @@ namespace Toolkit.Windows;
|
||||
|
||||
public interface IPointerMonitor :
|
||||
IInitialization,
|
||||
IDisposable;
|
||||
IDisposable;
|
||||
@@ -9,4 +9,4 @@ public interface ITaskbar :
|
||||
TaskbarState GetCurrentState();
|
||||
|
||||
IntPtr GetHandle();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
public interface IWndProcMonitor :
|
||||
public interface IWndProc :
|
||||
IInitialization,
|
||||
IDisposable;
|
||||
IDisposable
|
||||
{
|
||||
|
||||
IntPtr Handle { get; }
|
||||
}
|
||||
@@ -14,4 +14,28 @@ MonitorFromWindow
|
||||
RegisterWindowMessage
|
||||
GetDpiForWindow
|
||||
SetWindowPos
|
||||
SHCreateShellItemArrayFromDataObject
|
||||
SHCreateShellItemArrayFromDataObject
|
||||
Shell_NotifyIcon
|
||||
GetSystemMetricsForDpi
|
||||
GetSystemMetrics
|
||||
GetCurrentProcess
|
||||
SetProcessInformation
|
||||
PROCESS_POWER_THROTTLING_STATE
|
||||
PROCESS_POWER_THROTTLING_CURRENT_VERSION
|
||||
PROCESS_POWER_THROTTLING_EXECUTION_SPEED
|
||||
SetPriorityClass
|
||||
WINDOW_STYLE
|
||||
DefSubclassProc
|
||||
SetLayeredWindowAttributes
|
||||
WM_ERASEBKGND
|
||||
SetWindowSubclass
|
||||
GetClientRect
|
||||
CreateSolidBrush
|
||||
DwmSetWindowAttribute
|
||||
DWM_WINDOW_CORNER_PREFERENCE
|
||||
FillRect
|
||||
SetLayeredWindowAttributes
|
||||
DeleteObject
|
||||
RemoveWindowSubclass
|
||||
GetWindowLong
|
||||
SetWindowLong
|
||||
@@ -0,0 +1,183 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using System.Runtime.InteropServices;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
|
||||
public partial class NotifyIcon(IWndProc wndProc,
|
||||
IMessenger messenger) :
|
||||
INotifyIcon,
|
||||
IRecipient<WndProcEventArgs>
|
||||
{
|
||||
private const int CallbackMessage = 0x400;
|
||||
private const uint IconVersion = 0x4;
|
||||
|
||||
private readonly Lock notifyLock = new();
|
||||
private bool isDisposed;
|
||||
private NotifyIconData notifyIconData;
|
||||
|
||||
~NotifyIcon()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
private enum NotifyIconBalloonType
|
||||
{
|
||||
None = 0x00,
|
||||
Info = 0x01,
|
||||
Warning = 0x02,
|
||||
Error = 0x03,
|
||||
User = 0x04,
|
||||
NoSound = 0x10,
|
||||
LargeIcon = 0x20,
|
||||
RespectQuietTime = 0x80
|
||||
}
|
||||
|
||||
private enum NotifyIconCommand : uint
|
||||
{
|
||||
Add = 0x0,
|
||||
Delete = 0x2,
|
||||
Modify = 0x1,
|
||||
SetVersion = 0x4
|
||||
}
|
||||
|
||||
[Flags]
|
||||
private enum NotifyIconDataMember : uint
|
||||
{
|
||||
Message = 0x01,
|
||||
Icon = 0x02,
|
||||
Tip = 0x04,
|
||||
State = 0x08,
|
||||
Info = 0x10,
|
||||
Realtime = 0x40,
|
||||
UseLegacyToolTips = 0x80
|
||||
}
|
||||
|
||||
private enum NotifyIconState : uint
|
||||
{
|
||||
Visible = 0x00,
|
||||
Hidden = 0x01
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
messenger.RegisterAll(this);
|
||||
CreateNotificationIcon();
|
||||
}
|
||||
|
||||
public void Receive(WndProcEventArgs message)
|
||||
{
|
||||
if (message.Message == CallbackMessage)
|
||||
{
|
||||
switch (message.LParam)
|
||||
{
|
||||
case (uint)WndProcMessages.WM_LBUTTONUP:
|
||||
messenger.Send(new NotifyIconInvokedEventArgs(PointerButton.Left));
|
||||
break;
|
||||
|
||||
case (uint)WndProcMessages.WM_MBUTTONUP:
|
||||
messenger.Send(new NotifyIconInvokedEventArgs(PointerButton.Middle));
|
||||
break;
|
||||
|
||||
case (uint)WndProcMessages.WM_RBUTTONUP:
|
||||
messenger.Send(new NotifyIconInvokedEventArgs(PointerButton.Right));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetIcon(IntPtr iconHandle)
|
||||
{
|
||||
lock (notifyLock)
|
||||
{
|
||||
notifyIconData.IconHandle = iconHandle;
|
||||
WriteNotifyIconData(NotifyIconCommand.Modify, NotifyIconDataMember.Icon);
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("shell32.dll", SetLastError = true)]
|
||||
private static extern int Shell_NotifyIcon(NotifyIconCommand notifyCommand, ref NotifyIconData notifyIconData);
|
||||
|
||||
private void CreateNotificationIcon()
|
||||
{
|
||||
lock (notifyLock)
|
||||
{
|
||||
notifyIconData = new NotifyIconData();
|
||||
|
||||
notifyIconData.cbSize = (uint)Marshal.SizeOf(notifyIconData);
|
||||
notifyIconData.WindowHandle = wndProc.Handle;
|
||||
notifyIconData.TaskbarIconId = 0x0;
|
||||
notifyIconData.CallbackMessageId = CallbackMessage;
|
||||
notifyIconData.VersionOrTimeout = IconVersion;
|
||||
|
||||
notifyIconData.IconHandle = IntPtr.Zero;
|
||||
|
||||
notifyIconData.IconState = NotifyIconState.Hidden;
|
||||
notifyIconData.StateMask = NotifyIconState.Hidden;
|
||||
|
||||
WriteNotifyIconData(NotifyIconCommand.Add, NotifyIconDataMember.Message | NotifyIconDataMember.Icon | NotifyIconDataMember.Tip);
|
||||
}
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (isDisposed || !disposing) return;
|
||||
lock (notifyLock)
|
||||
{
|
||||
isDisposed = true;
|
||||
|
||||
messenger.UnregisterAll(this);
|
||||
RemoveNotificationIcon();
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveNotificationIcon() => WriteNotifyIconData(NotifyIconCommand.Delete, NotifyIconDataMember.Message);
|
||||
|
||||
private void WriteNotifyIconData(NotifyIconCommand command, NotifyIconDataMember flags)
|
||||
{
|
||||
notifyIconData.ValidMembers = flags;
|
||||
lock (notifyLock)
|
||||
{
|
||||
Shell_NotifyIcon(command, ref notifyIconData);
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private struct NotifyIconData
|
||||
{
|
||||
public uint cbSize;
|
||||
public IntPtr WindowHandle;
|
||||
public uint TaskbarIconId;
|
||||
public NotifyIconDataMember ValidMembers;
|
||||
public uint CallbackMessageId;
|
||||
public IntPtr IconHandle;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
|
||||
public string ToolTipText;
|
||||
|
||||
public NotifyIconState IconState;
|
||||
public NotifyIconState StateMask;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
public string BalloonText;
|
||||
|
||||
public uint VersionOrTimeout;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string BalloonTitle;
|
||||
|
||||
public NotifyIconBalloonType BalloonFlags;
|
||||
public Guid TaskbarIconGuid;
|
||||
public IntPtr CustomBalloonIconHandle;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
public record NotifyIconInvokedEventArgs(PointerButton PointerButton);
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
public enum ProcessPriority
|
||||
{
|
||||
Default,
|
||||
Idle,
|
||||
BelowNormal,
|
||||
Normal,
|
||||
AboveNormal,
|
||||
High,
|
||||
Realtime
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Runtime.Versioning;
|
||||
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
[SupportedOSPlatform("windows8.0")]
|
||||
public enum QualityOfService
|
||||
{
|
||||
Default,
|
||||
[SupportedOSPlatform("windows10.0.16299.0")]
|
||||
High,
|
||||
[SupportedOSPlatform("windows10.0.16299.0")]
|
||||
Medium,
|
||||
[SupportedOSPlatform("windows10.0.16299.0")]
|
||||
Low,
|
||||
[SupportedOSPlatform("windows11.0.22621.0")]
|
||||
Utility,
|
||||
[SupportedOSPlatform("windows11.0")]
|
||||
Eco,
|
||||
[SupportedOSPlatform("windows10.0.19041.0")]
|
||||
Media,
|
||||
[SupportedOSPlatform("windows10.0.19041.0")]
|
||||
Deadline
|
||||
}
|
||||
@@ -7,6 +7,10 @@ namespace Toolkit.Windows;
|
||||
|
||||
public class WindowHelper
|
||||
{
|
||||
public static IntPtr GetHandle(string windowName) => PInvoke.FindWindow(windowName, null);
|
||||
|
||||
public static uint GetDpi(IntPtr handle) => PInvoke.GetDpiForWindow((HWND)handle);
|
||||
|
||||
public static void BringToForeground(HWND handle)
|
||||
{
|
||||
if (TryGetBoundsUnsafe(handle, out RECT bounds))
|
||||
|
||||
@@ -6,8 +6,8 @@ using Windows.Win32.UI.WindowsAndMessaging;
|
||||
|
||||
namespace Toolkit.Windows;
|
||||
|
||||
public class WndProcMonitor(IMessenger messenger) :
|
||||
IWndProcMonitor
|
||||
public class WndProc(IMessenger messenger) :
|
||||
IWndProc
|
||||
{
|
||||
private WNDPROC? handler;
|
||||
|
||||
Reference in New Issue
Block a user