Compare commits

...

2 Commits

Author SHA1 Message Date
vanzue
95ef0c2bd3 fix build 2026-02-04 16:58:23 +08:00
vanzue
f0d7c1d61e Cmdpal Powertoys Extension: Support mouse without borders easy mouse and reconnecting command 2026-02-04 14:50:14 +08:00
18 changed files with 277 additions and 5 deletions

View File

@@ -287,4 +287,12 @@ namespace winrt::PowerToys::Interop::implementation
{
return CommonSharedConstants::POWER_DISPLAY_TERMINATE_APP_MESSAGE;
}
hstring Constants::MWBToggleEasyMouseEvent()
{
return CommonSharedConstants::MWB_TOGGLE_EASY_MOUSE_EVENT;
}
hstring Constants::MWBReconnectEvent()
{
return CommonSharedConstants::MWB_RECONNECT_EVENT;
}
}

View File

@@ -75,6 +75,8 @@ namespace winrt::PowerToys::Interop::implementation
static hstring PowerDisplayToggleMessage();
static hstring PowerDisplayApplyProfileMessage();
static hstring PowerDisplayTerminateAppMessage();
static hstring MWBToggleEasyMouseEvent();
static hstring MWBReconnectEvent();
};
}

View File

@@ -72,6 +72,8 @@ namespace PowerToys
static String PowerDisplayToggleMessage();
static String PowerDisplayApplyProfileMessage();
static String PowerDisplayTerminateAppMessage();
static String MWBToggleEasyMouseEvent();
static String MWBReconnectEvent();
}
}
}

View File

@@ -174,6 +174,10 @@ namespace CommonSharedConstants
const wchar_t CMDPAL_SHOW_EVENT[] = L"Local\\PowerToysCmdPal-ShowEvent-62336fcd-8611-4023-9b30-091a6af4cc5a";
const wchar_t CMDPAL_EXIT_EVENT[] = L"Local\\PowerToysCmdPal-ExitEvent-eb73f6be-3f22-4b36-aee3-62924ba40bfd";
// Path to the events used by MouseWithoutBorders
const wchar_t MWB_TOGGLE_EASY_MOUSE_EVENT[] = L"Local\\PowerToysMWB-ToggleEasyMouseEvent-a9c8d7b6-e5f4-3c2a-1b0d-9e8f7a6b5c4d";
const wchar_t MWB_RECONNECT_EVENT[] = L"Local\\PowerToysMWB-ReconnectEvent-b8d7c6a5-f4e3-2b1c-0a9d-8e7f6a5b4c3d";
// Max DWORD for key code to disable keys.
const DWORD VK_DISABLED = 0x100;
}

View File

@@ -229,6 +229,7 @@ namespace MouseWithoutBorders.Class
if (!Common.RunOnLogonDesktop)
{
StartSettingSyncThread();
CommandEventHandler.StartListening();
}
Application.EnableVisualStyles();

View File

@@ -0,0 +1,114 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
using MouseWithoutBorders.Class;
using PowerToys.Interop;
namespace MouseWithoutBorders.Core
{
/// <summary>
/// Handles command events from external sources (e.g., Command Palette).
/// Uses named events for inter-process communication, following the same pattern as other PowerToys modules.
/// </summary>
internal static class CommandEventHandler
{
private static CancellationTokenSource _cancellationTokenSource;
/// <summary>
/// Starts listening for command events on background threads.
/// </summary>
public static void StartListening()
{
_cancellationTokenSource = new CancellationTokenSource();
CancellationToken exitToken = _cancellationTokenSource.Token;
// Start listener for Toggle Easy Mouse event
StartEventListener(Constants.MWBToggleEasyMouseEvent(), ToggleEasyMouse, exitToken);
// Start listener for Reconnect event
StartEventListener(Constants.MWBReconnectEvent(), Reconnect, exitToken);
}
/// <summary>
/// Stops listening for command events.
/// </summary>
public static void StopListening()
{
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
}
private static void StartEventListener(string eventName, Action callback, CancellationToken cancel)
{
new System.Threading.Thread(() =>
{
try
{
using var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
WaitHandle[] waitHandles = new WaitHandle[] { cancel.WaitHandle, eventHandle };
while (!cancel.IsCancellationRequested)
{
int result = WaitHandle.WaitAny(waitHandles);
if (result == 1)
{
// Execute callback on UI thread using Common.DoSomethingInUIThread
Common.DoSomethingInUIThread(callback);
}
else
{
// Cancellation requested
return;
}
}
}
catch (Exception ex)
{
Logger.Log($"Error in event listener for {eventName}: {ex.Message}");
}
})
{ IsBackground = true, Name = $"MWB-{eventName}-Listener" }.Start();
}
/// <summary>
/// Toggles Easy Mouse between Enabled and Disabled states.
/// This is the same logic used by the hotkey handler.
/// </summary>
public static void ToggleEasyMouse()
{
if (Common.RunOnLogonDesktop || Common.RunOnScrSaverDesktop)
{
return;
}
EasyMouseOption easyMouseOption = (EasyMouseOption)Setting.Values.EasyMouse;
if (easyMouseOption is EasyMouseOption.Disable or EasyMouseOption.Enable)
{
Setting.Values.EasyMouse = (int)(easyMouseOption == EasyMouseOption.Disable ? EasyMouseOption.Enable : EasyMouseOption.Disable);
Common.ShowToolTip($"Easy Mouse has been toggled to [{(EasyMouseOption)Setting.Values.EasyMouse}].", 3000);
Logger.Log($"Easy Mouse toggled to {(EasyMouseOption)Setting.Values.EasyMouse} via command event.");
}
}
/// <summary>
/// Initiates a reconnection attempt to all machines.
/// This is the same logic used by the hotkey handler.
/// </summary>
public static void Reconnect()
{
Common.ShowToolTip("Reconnecting...", 2000);
Common.LastReconnectByHotKeyTime = Common.GetTick();
InitAndCleanup.PleaseReopenSocket = InitAndCleanup.REOPEN_WHEN_HOTKEY;
Logger.Log("Reconnect initiated via command event.");
}
}
}

View File

@@ -218,6 +218,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\common\GPOWrapper\GPOWrapper.vcxproj" />
<ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
using Microsoft.CommandPalette.Extensions.Toolkit;
using PowerToys.Interop;
namespace PowerToysExtension.Commands;
/// <summary>
/// Triggers a reconnection attempt in Mouse Without Borders via the shared event.
/// </summary>
internal sealed partial class MWBReconnectCommand : InvokableCommand
{
public MWBReconnectCommand()
{
Name = "Mouse Without Borders: Reconnect";
}
public override CommandResult Invoke()
{
try
{
using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.MWBReconnectEvent());
evt.Set();
return CommandResult.Dismiss();
}
catch (Exception ex)
{
return CommandResult.ShowToast($"Failed to reconnect Mouse Without Borders: {ex.Message}");
}
}
}

View File

@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
using Microsoft.CommandPalette.Extensions.Toolkit;
using PowerToys.Interop;
namespace PowerToysExtension.Commands;
/// <summary>
/// Toggles Easy Mouse feature in Mouse Without Borders via the shared event.
/// </summary>
internal sealed partial class ToggleMWBEasyMouseCommand : InvokableCommand
{
public ToggleMWBEasyMouseCommand()
{
Name = "Mouse Without Borders: Toggle Easy Mouse";
}
public override CommandResult Invoke()
{
try
{
using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.MWBToggleEasyMouseEvent());
evt.Set();
return CommandResult.Dismiss();
}
catch (Exception ex)
{
return CommandResult.ShowToast($"Failed to toggle Easy Mouse: {ex.Message}");
}
}
}

View File

@@ -13,7 +13,11 @@ internal static class PowerToysResourcesHelper
internal static IconInfo IconFromSettingsIcon(string fileName) => IconHelpers.FromRelativePath($"{SettingsIconRoot}{fileName}");
#if DEBUG
public static IconInfo ProviderIcon() => IconFromSettingsIcon("PowerToys.dark.png");
#else
public static IconInfo ProviderIcon() => IconFromSettingsIcon("PowerToys.png");
#endif
public static IconInfo ModuleIcon(this SettingsWindow module)
{

View File

@@ -30,6 +30,10 @@
<Content Include="..\..\..\..\settings-ui\Settings.UI\Assets\Settings\Icons\*.png" Link="WinUI3Apps\Assets\Settings\Icons\%(Filename)%(Extension)">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<!-- Monochrome icons from PowerToys Run Plugin for debug mode differentiation -->
<Content Include="..\..\..\launcher\Plugins\Microsoft.PowerToys.Run.Plugin.PowerToys\Images\PowerToys.dark.png" Link="WinUI3Apps\Assets\Settings\Icons\PowerToys.dark.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>

View File

@@ -24,5 +24,19 @@ internal sealed class MouseWithoutBordersModuleCommandProvider : ModuleCommandPr
Subtitle = Resources.MouseWithoutBorders_Settings_Subtitle,
Icon = icon,
};
yield return new ListItem(new ToggleMWBEasyMouseCommand())
{
Title = Resources.MouseWithoutBorders_ToggleEasyMouse_Title,
Subtitle = Resources.MouseWithoutBorders_ToggleEasyMouse_Subtitle,
Icon = icon,
};
yield return new ListItem(new MWBReconnectCommand())
{
Title = Resources.MouseWithoutBorders_Reconnect_Title,
Subtitle = Resources.MouseWithoutBorders_Reconnect_Subtitle,
Icon = icon,
};
}
}

View File

@@ -14,7 +14,7 @@ internal sealed partial class PowerToysExtensionPage : ListPage
{
public PowerToysExtensionPage()
{
Icon = Helpers.PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png");
Icon = Helpers.PowerToysResourcesHelper.ProviderIcon();
Title = Resources.PowerToys_DisplayName;
Name = Resources.PowerToysExtension_CommandsName;
}

View File

@@ -15,13 +15,13 @@ internal sealed partial class PowerToysListPage : ListPage
public PowerToysListPage()
{
Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png");
Icon = PowerToysResourcesHelper.ProviderIcon();
Name = Title = Resources.PowerToys_DisplayName;
Id = "com.microsoft.cmdpal.powertoys";
SettingsChangeNotifier.SettingsChanged += OnSettingsChanged;
_empty = new CommandItem()
{
Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png"),
Icon = PowerToysResourcesHelper.ProviderIcon(),
Title = Resources.PowerToys_NoMatchingModule,
Subtitle = SearchText,
};

View File

@@ -16,7 +16,7 @@ public sealed partial class PowerToysCommandsProvider : CommandProvider
public PowerToysCommandsProvider()
{
DisplayName = Resources.PowerToys_DisplayName;
Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png");
Icon = PowerToysResourcesHelper.ProviderIcon();
}
public override ICommandItem[] TopLevelCommands() =>

View File

@@ -17,7 +17,7 @@ public partial class PowerToysExtensionCommandsProvider : CommandProvider
public PowerToysExtensionCommandsProvider()
{
DisplayName = Resources.PowerToys_DisplayName;
Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png");
Icon = PowerToysResourcesHelper.ProviderIcon();
_commands = [
new CommandItem(new Pages.PowerToysListPage())
{

View File

@@ -996,6 +996,42 @@ namespace PowerToysExtension.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Toggle Easy Mouse.
/// </summary>
internal static string MouseWithoutBorders_ToggleEasyMouse_Title {
get {
return ResourceManager.GetString("MouseWithoutBorders_ToggleEasyMouse_Title", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Toggle easy mouse switching between machines.
/// </summary>
internal static string MouseWithoutBorders_ToggleEasyMouse_Subtitle {
get {
return ResourceManager.GetString("MouseWithoutBorders_ToggleEasyMouse_Subtitle", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Reconnect.
/// </summary>
internal static string MouseWithoutBorders_Reconnect_Title {
get {
return ResourceManager.GetString("MouseWithoutBorders_Reconnect_Title", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Reconnect to other machines.
/// </summary>
internal static string MouseWithoutBorders_Reconnect_Subtitle {
get {
return ResourceManager.GetString("MouseWithoutBorders_Reconnect_Subtitle", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Open New+ settings.
/// </summary>

View File

@@ -456,6 +456,18 @@
<data name="MouseWithoutBorders_Settings_Subtitle" xml:space="preserve">
<value>Open Mouse Without Borders settings</value>
</data>
<data name="MouseWithoutBorders_ToggleEasyMouse_Title" xml:space="preserve">
<value>Toggle Easy Mouse</value>
</data>
<data name="MouseWithoutBorders_ToggleEasyMouse_Subtitle" xml:space="preserve">
<value>Toggle Easy Mouse feature on/off</value>
</data>
<data name="MouseWithoutBorders_Reconnect_Title" xml:space="preserve">
<value>Reconnect</value>
</data>
<data name="MouseWithoutBorders_Reconnect_Subtitle" xml:space="preserve">
<value>Reconnect to all machines</value>
</data>
<!-- New+ Module -->
<data name="NewPlus_Settings_Subtitle" xml:space="preserve">
<value>Open New+ settings</value>