Set up IHostEnvironment
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public static class FileConfigurationExtensions
|
||||
{
|
||||
public static IConfigurationBuilder SetBasePath(this IConfigurationBuilder builder,
|
||||
string basePath, bool createDirectory)
|
||||
{
|
||||
if (!Directory.Exists(basePath) && createDirectory)
|
||||
{
|
||||
Directory.CreateDirectory(basePath);
|
||||
}
|
||||
|
||||
return builder.SetFileProvider(new PhysicalFileProvider(basePath));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public static class HostingHostBuilderExtensions
|
||||
{
|
||||
public static IHostBuilder UseContentRoot(this IHostBuilder hostBuilder, string contentRoot, bool createDirectory)
|
||||
{
|
||||
if (!Directory.Exists(contentRoot) && createDirectory)
|
||||
{
|
||||
Directory.CreateDirectory(contentRoot);
|
||||
}
|
||||
|
||||
return hostBuilder.UseContentRoot(contentRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,23 @@
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public static class PointerLocationExtensions
|
||||
{
|
||||
public static bool IsWithinBounds(this PointerLocation args, Rect bounds)
|
||||
{
|
||||
if (args.X >= bounds.X
|
||||
&& args.X <= bounds.X + bounds.Width
|
||||
&& args.Y >= bounds.Y
|
||||
&& args.Y <= bounds.Y + bounds.Height)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddAsyncHandler<TAsyncMessageHandle>(this IServiceCollection serviceCollection)
|
||||
@@ -55,11 +72,12 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
.AddSingleton<IMessageInvoker, MessageInvoker>()
|
||||
.AddSingleton<IMessenger, Messenger>()
|
||||
.AddSingleton<IMediator, Mediator>()
|
||||
.AddSingleton<IInitializable, WndProcMonitor>()
|
||||
.AddSingleton<IWndProcMonitor, WndProcMonitor>()
|
||||
.AddSingleton<ITaskbar, Taskbar>()
|
||||
.AddSingleton<IInitializable, TaskbarMonitor>()
|
||||
.AddSingleton<IInitializable, PointerMonitor>()
|
||||
.AddSingleton<IInitializable, TaskbarButtonMonitor>();
|
||||
.AddSingleton<ITaskbarList, TaskbarList>()
|
||||
.AddSingleton<IPointerMonitor, PointerMonitor>()
|
||||
.AddSingleton<ITaskbarButtonMonitor, TaskbarButtonMonitor>()
|
||||
.AddSingleton<ITaskbarButtonShortcutMonitor, TaskbarButtonShortcutMonitor>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public interface ITaskbar
|
||||
public interface ITaskbar : IInitializable, IDisposable
|
||||
{
|
||||
TaskbarState GetCurrentState();
|
||||
|
||||
IntPtr GetHandle();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
public interface ITaskbarButton : IDisposable
|
||||
{
|
||||
TaskbarButtonBounds Bounds { get; }
|
||||
Rect Rect { get; }
|
||||
|
||||
string Name { get; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public interface ITaskbarButtonShortcutMonitor : IInitializable
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public interface ITaskbarList
|
||||
{
|
||||
IntPtr GetHandle();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public interface ITaskbarMonitor : IInitializable
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Win32.Foundation;
|
||||
|
||||
namespace Windows.Win32
|
||||
{
|
||||
public static partial class PInvoke
|
||||
{
|
||||
public static readonly int SPI_SETWORKAREA = 0x002F;
|
||||
public static readonly uint WM_TASKBARCREATED = PInvoke.RegisterWindowMessage("TaskbarCreated");
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct AppBarData
|
||||
{
|
||||
public uint cbSize;
|
||||
public IntPtr hWnd;
|
||||
public uint uCallbackMessage;
|
||||
public AppBarEdge uEdge;
|
||||
public RECT rect;
|
||||
public int lParam;
|
||||
}
|
||||
|
||||
public enum AppBarMessage : uint
|
||||
{
|
||||
New = 0x00000000,
|
||||
Remove = 0x00000001,
|
||||
QueryPos = 0x00000002,
|
||||
SetPos = 0x00000003,
|
||||
GetState = 0x00000004,
|
||||
GetTaskbarPos = 0x00000005,
|
||||
Activate = 0x00000006,
|
||||
GetAutoHideBar = 0x00000007,
|
||||
SetAutoHideBar = 0x00000008,
|
||||
WindowPosChanged = 0x00000009,
|
||||
SetState = 0x0000000A,
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
public static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("shell32.dll", SetLastError = true)]
|
||||
public static extern IntPtr SHAppBarMessage(AppBarMessage dwMessage, ref AppBarData pData);
|
||||
|
||||
public static AppBarData GetAppBarData(IntPtr handle)
|
||||
{
|
||||
return new AppBarData
|
||||
{
|
||||
cbSize = (uint)Marshal.SizeOf(typeof(AppBarData)),
|
||||
hWnd = handle
|
||||
};
|
||||
}
|
||||
|
||||
public enum AppBarEdge : uint
|
||||
{
|
||||
Left = 0,
|
||||
Top = 1,
|
||||
Right = 2,
|
||||
Bottom = 3
|
||||
}
|
||||
|
||||
public static void GetAppBarPosition(ref AppBarData appBarData) => SHAppBarMessage(AppBarMessage.GetTaskbarPos, ref appBarData);
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,10 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
internal static class RECTExtensions
|
||||
{
|
||||
internal static Rect ToRect(this RECT rect)
|
||||
internal static Windows.Foundation.Rect ToRect(this RECT rect)
|
||||
{
|
||||
if (rect.right - rect.left < 0 || rect.bottom - rect.top < 0) return new Rect(rect.left, rect.top, 0, 0);
|
||||
return new Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||
if (rect.right - rect.left < 0 || rect.bottom - rect.top < 0) return new Windows.Foundation.Rect(rect.left, rect.top, 0, 0);
|
||||
return new Windows.Foundation.Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,13 +1,13 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskbarButtonBounds
|
||||
public record Rect
|
||||
{
|
||||
public TaskbarButtonBounds()
|
||||
public Rect()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public TaskbarButtonBounds(int x, int y, int width, int height)
|
||||
public Rect(int x, int y, int width, int height)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
@@ -32,7 +32,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
var monitorData = GetMonitorData(monitorHandle);
|
||||
|
||||
Bounds = new Rect(monitorData.MonitorRect.left, monitorData.MonitorRect.top, monitorData.MonitorRect.right - monitorData.MonitorRect.left, monitorData.MonitorRect.bottom - monitorData.MonitorRect.top);
|
||||
Bounds = new Windows.Foundation.Rect(monitorData.MonitorRect.left, monitorData.MonitorRect.top, monitorData.MonitorRect.right - monitorData.MonitorRect.left, monitorData.MonitorRect.bottom - monitorData.MonitorRect.top);
|
||||
Primary = (monitorData.Flags & (int)MonitorFlag.MONITOR_DEFAULTTOPRIMARY) != 0;
|
||||
DeviceName = monitorData.DeviceName;
|
||||
}
|
||||
@@ -47,13 +47,13 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
MONITOR_DEFAULTTONEAREST = 2
|
||||
}
|
||||
|
||||
public Rect Bounds { get; }
|
||||
public Windows.Foundation.Rect Bounds { get; }
|
||||
|
||||
public string DeviceName { get; }
|
||||
|
||||
public bool Primary { get; }
|
||||
|
||||
public Rect WorkingArea => GetWorkingArea();
|
||||
public Windows.Foundation.Rect WorkingArea => GetWorkingArea();
|
||||
|
||||
public static Screen FromHandle(IntPtr handle)
|
||||
{
|
||||
@@ -83,7 +83,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
return monitorData;
|
||||
}
|
||||
|
||||
private Rect GetWorkingArea()
|
||||
private Windows.Foundation.Rect GetWorkingArea()
|
||||
{
|
||||
if (!_multiMonitorSupport || _monitorHandle == (IntPtr)PRIMARY_MONITOR)
|
||||
{
|
||||
@@ -91,7 +91,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
}
|
||||
|
||||
var monitorData = GetMonitorData(_monitorHandle);
|
||||
return new Rect(monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.top, monitorData.WorkAreaRect.right - monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.bottom - monitorData.WorkAreaRect.top);
|
||||
return new Windows.Foundation.Rect(monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.top, monitorData.WorkAreaRect.right - monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.bottom - monitorData.WorkAreaRect.top);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
|
||||
@@ -10,21 +10,21 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
private const int SPI_GETWORKAREA = 48;
|
||||
|
||||
public static Rect VirtualScreen => GetVirtualScreen();
|
||||
public static Rect WorkingArea => GetWorkingArea();
|
||||
public static Windows.Foundation.Rect VirtualScreen => GetVirtualScreen();
|
||||
public static Windows.Foundation.Rect WorkingArea => GetWorkingArea();
|
||||
|
||||
private static Rect GetVirtualScreen()
|
||||
private static Windows.Foundation.Rect GetVirtualScreen()
|
||||
{
|
||||
var size = new Size(PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSCREEN), PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CYSCREEN));
|
||||
return new Rect(0, 0, size.Width, size.Height);
|
||||
return new Windows.Foundation.Rect(0, 0, size.Width, size.Height);
|
||||
}
|
||||
|
||||
private static Rect GetWorkingArea()
|
||||
private static Windows.Foundation.Rect GetWorkingArea()
|
||||
{
|
||||
var rect = new RECT();
|
||||
|
||||
SystemParametersInfo(SPI_GETWORKAREA, 0, ref rect, 0);
|
||||
return new Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||
return new Windows.Foundation.Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskButtonShortcutCreated;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskButtonShortcutRemoved;
|
||||
}
|
||||
@@ -1,45 +1,37 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class Taskbar : ITaskbar
|
||||
public partial class Taskbar : ITaskbar
|
||||
{
|
||||
private const string ShellTrayHandleName = "Shell_TrayWnd";
|
||||
private readonly IDisposer disposer;
|
||||
private readonly IMessenger messenger;
|
||||
private bool isDrag;
|
||||
private bool isWithinBounds;
|
||||
|
||||
private enum AppBarEdge : uint
|
||||
public Taskbar(IMessenger messenger,
|
||||
IDisposer disposer)
|
||||
{
|
||||
Left = 0,
|
||||
Top = 1,
|
||||
Right = 2,
|
||||
Bottom = 3
|
||||
this.messenger = messenger;
|
||||
this.disposer = disposer;
|
||||
}
|
||||
|
||||
private enum AppBarMessage : uint
|
||||
public void Dispose()
|
||||
{
|
||||
New = 0x00000000,
|
||||
Remove = 0x00000001,
|
||||
QueryPos = 0x00000002,
|
||||
SetPos = 0x00000003,
|
||||
GetState = 0x00000004,
|
||||
GetTaskbarPos = 0x00000005,
|
||||
Activate = 0x00000006,
|
||||
GetAutoHideBar = 0x00000007,
|
||||
SetAutoHideBar = 0x00000008,
|
||||
WindowPosChanged = 0x00000009,
|
||||
SetState = 0x0000000A,
|
||||
disposer.Dispose(this);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public TaskbarState GetCurrentState()
|
||||
{
|
||||
var handle = GetSystemTrayHandle();
|
||||
var handle = GetHandle();
|
||||
var state = new TaskbarState
|
||||
{
|
||||
Screen = Screen.FromHandle(handle)
|
||||
};
|
||||
|
||||
var appBarData = GetAppBarData(handle);
|
||||
GetAppBarPosition(ref appBarData);
|
||||
var appBarData = PInvoke.GetAppBarData(handle);
|
||||
PInvoke.GetAppBarPosition(ref appBarData);
|
||||
|
||||
state.Rect = appBarData.rect.ToRect();
|
||||
state.Placement = (TaskbarPlacement)appBarData.uEdge;
|
||||
@@ -47,34 +39,77 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
return state;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern IntPtr DefWindowProcW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
private static IntPtr GetSystemTrayHandle() => WindowHelper.Find(ShellTrayHandleName);
|
||||
|
||||
[DllImport("shell32.dll", SetLastError = true)]
|
||||
private static extern IntPtr SHAppBarMessage(AppBarMessage dwMessage, ref AppBarData pData);
|
||||
|
||||
private AppBarData GetAppBarData(IntPtr handle)
|
||||
public IntPtr GetHandle()
|
||||
{
|
||||
return new AppBarData
|
||||
{
|
||||
cbSize = (uint)Marshal.SizeOf(typeof(AppBarData)),
|
||||
hWnd = handle
|
||||
};
|
||||
return WindowHelper.Find("Shell_TrayWnd");
|
||||
}
|
||||
|
||||
private void GetAppBarPosition(ref AppBarData appBarData) => SHAppBarMessage(AppBarMessage.GetTaskbarPos, ref appBarData);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct AppBarData
|
||||
public void Initialize()
|
||||
{
|
||||
public uint cbSize;
|
||||
public IntPtr hWnd;
|
||||
public uint uCallbackMessage;
|
||||
public AppBarEdge uEdge;
|
||||
public RECT rect;
|
||||
public int lParam;
|
||||
disposer.Add(this, messenger.Subscribe<WndProc>(OnWndProc));
|
||||
disposer.Add(this, messenger.Subscribe<PointerReleased>(OnPointerReleased));
|
||||
disposer.Add(this, messenger.Subscribe<PointerMoved>(OnPointerMoved));
|
||||
disposer.Add(this, messenger.Subscribe<PointerDrag>(OnPointerDrag));
|
||||
}
|
||||
|
||||
private void OnPointerDrag(PointerDrag args)
|
||||
{
|
||||
if (isWithinBounds)
|
||||
{
|
||||
if (isDrag)
|
||||
{
|
||||
messenger.Send<TaskbarDragOver>();
|
||||
}
|
||||
else
|
||||
{
|
||||
messenger.Send<TaskbarDragEnter>();
|
||||
}
|
||||
|
||||
isDrag = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
isDrag = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPointerMoved(PointerMoved args)
|
||||
{
|
||||
var taskbarHandle = GetHandle();
|
||||
if (WindowHelper.TryGetBounds(taskbarHandle, out var rect))
|
||||
{
|
||||
if (args.Location.IsWithinBounds(rect))
|
||||
{
|
||||
if (isWithinBounds)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
isWithinBounds = true;
|
||||
messenger.Send<TaskbarEnter>();
|
||||
}
|
||||
else
|
||||
{
|
||||
isDrag = false;
|
||||
isWithinBounds = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPointerReleased(PointerReleased args)
|
||||
{
|
||||
if (isDrag)
|
||||
{
|
||||
isDrag = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnWndProc(WndProc args)
|
||||
{
|
||||
if (args.Message == PInvoke.WM_TASKBARCREATED || args.Message == (int)WndProcMessages.WM_SETTINGCHANGE && (int)args.WParam == PInvoke.SPI_SETWORKAREA)
|
||||
{
|
||||
messenger.Send<TaskbarChanged>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,22 +7,22 @@
|
||||
private bool isWithinBounds;
|
||||
private bool isDrag;
|
||||
|
||||
public TaskbarButton(IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
string name,
|
||||
TaskbarButtonBounds bounds)
|
||||
public TaskbarButton(string name,
|
||||
Rect rect,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer)
|
||||
{
|
||||
this.messenger = messenger;
|
||||
this.disposer = disposer;
|
||||
Name = name;
|
||||
Bounds = bounds;
|
||||
Rect = rect;
|
||||
|
||||
disposer.Add(this, messenger.Subscribe<PointerReleased>(OnPointerReleased));
|
||||
disposer.Add(this, messenger.Subscribe<PointerMoved>(OnPointerMoved));
|
||||
disposer.Add(this, messenger.Subscribe<PointerDrag>(OnPointerDrag));
|
||||
}
|
||||
|
||||
public TaskbarButtonBounds Bounds { get; internal set; }
|
||||
public Rect Rect { get; internal set; }
|
||||
|
||||
public string Name { get; internal set; }
|
||||
|
||||
@@ -32,21 +32,6 @@
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private bool IsWithinBounds(PointerLocation args)
|
||||
{
|
||||
if (args.X >= Bounds.X
|
||||
&& args.X <= Bounds.X + Bounds.Width
|
||||
&& args.Y >= Bounds.Y
|
||||
&& args.Y <= Bounds.Y + Bounds.Height)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPointerDrag(PointerDrag args)
|
||||
{
|
||||
if (isWithinBounds)
|
||||
@@ -70,7 +55,7 @@
|
||||
|
||||
private void OnPointerMoved(PointerMoved args)
|
||||
{
|
||||
if (IsWithinBounds(args.Location))
|
||||
if (args.Location.IsWithinBounds(Rect))
|
||||
{
|
||||
if (isWithinBounds)
|
||||
{
|
||||
@@ -78,7 +63,7 @@
|
||||
}
|
||||
|
||||
isWithinBounds = true;
|
||||
messenger.Send(new TaskbarButtonEntered(this));
|
||||
messenger.Send(new TaskbarButtonEnter(this));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class TaskbarButtonConfiguration
|
||||
{
|
||||
public string? PinnedShortcutDirectory { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskbarButtonEnter(TaskbarButton Button);
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskbarButtonEntered(TaskbarButton Button);
|
||||
}
|
||||
@@ -4,27 +4,34 @@ using System.Diagnostics;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
|
||||
public class TaskbarButtonMonitor : ITaskbarButtonMonitor
|
||||
{
|
||||
private readonly IDispatcherTimer dispatcherTimer;
|
||||
private readonly IDispatcherTimerFactory dispatcherTimerFactory;
|
||||
private readonly IServiceFactory serviceFactory;
|
||||
private readonly ITaskbarList taskbarList;
|
||||
private readonly IMessenger messenger;
|
||||
private readonly IServiceFactory serviceFactory;
|
||||
private readonly IDisposer disposer;
|
||||
private readonly Dictionary<string, TaskbarButton> taskbarButtons = new();
|
||||
private RECT taskbarBoundsCache;
|
||||
private Rect? taskbarRectCache;
|
||||
private IUIAutomationCondition? taskListCondition;
|
||||
private IUIAutomationElement? taskListElement;
|
||||
private HWND taskListHandle;
|
||||
private IntPtr taskListHandle;
|
||||
|
||||
public TaskbarButtonMonitor(IMessenger messenger,
|
||||
public TaskbarButtonMonitor(ITaskbarList taskbarList,
|
||||
IMessenger messenger,
|
||||
IDispatcherTimerFactory dispatcherTimerFactory,
|
||||
IServiceFactory serviceFactory)
|
||||
IServiceFactory serviceFactory,
|
||||
IDisposer disposer)
|
||||
{
|
||||
this.taskbarList = taskbarList;
|
||||
this.messenger = messenger;
|
||||
this.dispatcherTimerFactory = dispatcherTimerFactory;
|
||||
this.serviceFactory = serviceFactory;
|
||||
this.disposer = disposer;
|
||||
|
||||
dispatcherTimer = dispatcherTimerFactory.Create(OnDispatcher, TimeSpan.FromMilliseconds(500));
|
||||
disposer.Add(this, dispatcherTimer = dispatcherTimerFactory.Create(OnDispatcher, TimeSpan.FromMilliseconds(500)));
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
@@ -32,17 +39,12 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
var clientUIAutomation = new CUIAutomation();
|
||||
taskListCondition = clientUIAutomation.CreateTrueCondition();
|
||||
|
||||
var trayHandle = WindowHelper.Find("Shell_TrayWnd");
|
||||
|
||||
var rebarHandle = WindowHelper.Find("ReBarWindow32", trayHandle);
|
||||
var taskHandle = WindowHelper.Find("MSTaskSwWClass", rebarHandle);
|
||||
taskListHandle = WindowHelper.Find("MSTaskListWClass", taskHandle);
|
||||
|
||||
taskListHandle = taskbarList.GetHandle();
|
||||
taskListElement = clientUIAutomation.ElementFromHandle(taskListHandle);
|
||||
|
||||
if (WindowHelper.TryGetBounds(taskListHandle, out var bounds))
|
||||
if (WindowHelper.TryGetBounds(taskListHandle, out var rect))
|
||||
{
|
||||
taskbarBoundsCache = bounds;
|
||||
taskbarRectCache = rect;
|
||||
}
|
||||
|
||||
dispatcherTimer.Start();
|
||||
@@ -51,17 +53,11 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
|
||||
private bool CheckDirtyTaskbarRegion()
|
||||
{
|
||||
if (WindowHelper.TryGetBounds(taskListHandle, out var bounds))
|
||||
if (WindowHelper.TryGetBounds(taskListHandle, out var rect))
|
||||
{
|
||||
var width = taskbarBoundsCache.right - taskbarBoundsCache.left;
|
||||
var height = taskbarBoundsCache.bottom - taskbarBoundsCache.top;
|
||||
|
||||
var deltaWidth = bounds.right - bounds.left;
|
||||
var deltaHeight = bounds.bottom - bounds.top;
|
||||
|
||||
if (width != deltaWidth || height != deltaHeight)
|
||||
if (taskbarRectCache?.Width != rect.Width || taskbarRectCache?.Height != rect.Height)
|
||||
{
|
||||
taskbarBoundsCache = bounds;
|
||||
taskbarRectCache = rect;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -76,7 +72,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
var buttons = new Dictionary<string, tagRECT>();
|
||||
if (taskElements is not null)
|
||||
{
|
||||
for (int index = 0; index <= taskElements.Length - 1; index++)
|
||||
for (var index = 0; index <= taskElements.Length - 1; index++)
|
||||
{
|
||||
var taskUIElement = taskElements.GetElement(index);
|
||||
var name = taskUIElement.CurrentName;
|
||||
@@ -128,7 +124,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
var name = button.Key;
|
||||
var bounds = button.Value;
|
||||
|
||||
var buttonBounds = new TaskbarButtonBounds(bounds.left,
|
||||
var rect = new Rect(bounds.left,
|
||||
bounds.top,
|
||||
bounds.right - bounds.left,
|
||||
bounds.bottom - bounds.top);
|
||||
@@ -137,14 +133,14 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
Debug.WriteLine($"{name} button updated");
|
||||
|
||||
taskbarButtons[name].Bounds = buttonBounds;
|
||||
taskbarButtons[name].Rect = rect;
|
||||
messenger.Send(new TaskbarButtonUpdated(taskbarButtons[name]));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine($"{name} button added");
|
||||
|
||||
taskbarButtons.Add(name, serviceFactory.Create<TaskbarButton>(name, buttonBounds));
|
||||
taskbarButtons.Add(name, serviceFactory.Create<TaskbarButton>(name, rect));
|
||||
messenger.Send(new TaskbarButtonCreated(taskbarButtons[name]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class TaskbarButtonShortcutMonitor : ITaskbarButtonShortcutMonitor
|
||||
{
|
||||
private readonly IMessenger messenger;
|
||||
private FileSystemWatcher? _watcher;
|
||||
private readonly TaskbarButtonConfiguration configuration;
|
||||
|
||||
public TaskbarButtonShortcutMonitor(
|
||||
IMessenger messenger)
|
||||
{
|
||||
this.messenger = messenger;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
//_watcher = new FileSystemWatcher(configuration.PinnedShortcutDirectory)
|
||||
//{
|
||||
// NotifyFilter = NotifyFilters.FileName,
|
||||
// Filter = "*.ink",
|
||||
// IncludeSubdirectories = true,
|
||||
// EnableRaisingEvents = true
|
||||
//};
|
||||
|
||||
//_watcher.Changed += OnChanged;
|
||||
}
|
||||
|
||||
private void OnChanged(object sender, FileSystemEventArgs args)
|
||||
{
|
||||
if (args.ChangeType is WatcherChangeTypes.Created)
|
||||
{
|
||||
messenger.Send<TaskButtonShortcutRemoved>();
|
||||
}
|
||||
|
||||
if (args.ChangeType is WatcherChangeTypes.Deleted)
|
||||
{
|
||||
messenger.Send<TaskButtonShortcutRemoved>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskbarDragEnter();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskbarDragOver();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public record TaskbarEnter();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class TaskbarList : ITaskbarList
|
||||
{
|
||||
private readonly ITaskbar taskbar;
|
||||
|
||||
public TaskbarList(ITaskbar taskbar)
|
||||
{
|
||||
this.taskbar = taskbar;
|
||||
}
|
||||
|
||||
public IntPtr GetHandle()
|
||||
{
|
||||
var trayHandle = taskbar.GetHandle();
|
||||
|
||||
var rebarHandle = WindowHelper.Find("ReBarWindow32", trayHandle);
|
||||
var taskHandle = WindowHelper.Find("MSTaskSwWClass", rebarHandle);
|
||||
return WindowHelper.Find("MSTaskListWClass", taskHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using Windows.Win32;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class TaskbarMonitor : ITaskbarMonitor
|
||||
{
|
||||
private const int SPI_SETWORKAREA = 0x002F;
|
||||
|
||||
private readonly uint WM_TASKBARCREATED = PInvoke.RegisterWindowMessage("TaskbarCreated");
|
||||
private readonly IMessenger messenger;
|
||||
|
||||
public TaskbarMonitor(IMessenger messenger)
|
||||
{
|
||||
this.messenger = messenger;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
messenger.Subscribe<WndProc>(OnWndProc);
|
||||
}
|
||||
|
||||
private void OnWndProc(WndProc args)
|
||||
{
|
||||
if (args.Message == WM_TASKBARCREATED || args.Message == (int)WndProcMessages.WM_SETTINGCHANGE && (int)args.WParam == SPI_SETWORKAREA)
|
||||
{
|
||||
messenger.Send<TaskbarChanged>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
public struct TaskbarState
|
||||
{
|
||||
public TaskbarPlacement Placement;
|
||||
public Rect Rect;
|
||||
public Windows.Foundation.Rect Rect;
|
||||
public Screen Screen;
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,10 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0-preview2" />
|
||||
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.635-beta">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Windows.Win32;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.UI.WindowsAndMessaging;
|
||||
|
||||
@@ -6,27 +7,42 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class WindowHelper
|
||||
{
|
||||
public static void MoveAndResize(HWND handle, int x, int y, int width, int height)
|
||||
{
|
||||
PInvoke.SetWindowPos(handle, new HWND(), x, y, width, height, 0);
|
||||
}
|
||||
|
||||
public static void BringToForeground(HWND handle)
|
||||
{
|
||||
if (TryGetBounds(handle, out var bounds))
|
||||
if (TryGetBoundsUnsafe(handle, out var bounds))
|
||||
{
|
||||
PInvoke.SetWindowPos(handle, new HWND(), bounds.left, bounds.top, bounds.right - bounds.left, bounds.bottom - bounds.top, SET_WINDOW_POS_FLAGS.SWP_SHOWWINDOW | SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE);
|
||||
}
|
||||
}
|
||||
|
||||
public static HWND Find(string windowName) => PInvoke.FindWindow(windowName, null);
|
||||
|
||||
public static HWND Find(string windowName, HWND parentHandle)
|
||||
public static IntPtr Find(string windowName)
|
||||
{
|
||||
return PInvoke.FindWindowEx(parentHandle, new HWND(), windowName, null);
|
||||
return PInvoke.FindWindow(windowName, null);
|
||||
}
|
||||
|
||||
public static unsafe bool TryGetBounds(IntPtr handle, out RECT rect)
|
||||
public static IntPtr Find(string windowName, IntPtr parentHandle)
|
||||
{
|
||||
return PInvoke.FindWindowEx(new HWND(parentHandle), new HWND(), windowName, null);
|
||||
}
|
||||
|
||||
public static void MoveAndResize(HWND handle, int x, int y, int width, int height)
|
||||
{
|
||||
PInvoke.SetWindowPos(handle, new HWND(), x, y, width, height, 0);
|
||||
}
|
||||
public static bool TryGetBounds(IntPtr handle, [MaybeNullWhen(false)]out Rect rect)
|
||||
{
|
||||
if (TryGetBoundsUnsafe(handle, out var unsafeRect))
|
||||
{
|
||||
rect = new Rect(unsafeRect.left, unsafeRect.top, unsafeRect.right - unsafeRect.left, unsafeRect.bottom - unsafeRect.top);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
rect = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static unsafe bool TryGetBoundsUnsafe(IntPtr handle, out RECT rect)
|
||||
{
|
||||
fixed (RECT* lpRectLocal = &rect)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user