Moved from custom event implementation to EventWaitHanlde

This commit is contained in:
Noraa Junker
2025-12-14 22:48:34 +01:00
parent 5398c16456
commit 99523fe317
23 changed files with 61 additions and 174 deletions

View File

@@ -3,10 +3,6 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
namespace RunnerV2.Extensions

View File

@@ -3,11 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace RunnerV2.Helpers
{

View File

@@ -6,10 +6,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using Windows.System;
using static RunnerV2.NativeMethods;
namespace RunnerV2.Helpers

View File

@@ -6,12 +6,8 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices.Marshalling;
using System.Text.RegularExpressions;
using ManagedCommon;
using ManagedCsWin32;
using RunnerV2.Extensions;
using Windows.ApplicationModel;
using Windows.Foundation;

View File

@@ -2,11 +2,7 @@
// 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.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

View File

@@ -10,6 +10,7 @@ using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Text.Json;
using System.Threading;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.Interop;
@@ -214,9 +215,8 @@ namespace RunnerV2.Helpers
public static void CloseSettingsWindow()
{
InteropEvent closeEventWrapper = new(InteropEvent.SettingsTerminate);
closeEventWrapper.Fire();
closeEventWrapper.Dispose();
using var closeEventWrapper = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.PowerToysRunnerTerminateSettingsEvent());
closeEventWrapper.Set();
}
}
}

View File

@@ -4,16 +4,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using PowerToys.GPOWrapper;
using PowerToys.Interop;
using RunnerV2.Helpers;
namespace RunnerV2.ModuleInterfaces
{
@@ -29,18 +26,18 @@ namespace RunnerV2.ModuleInterfaces
{
if (_ipc != null)
{
_ipc.Send("TerminateApp");
_ipc.Send(Constants.AdvancedPasteTerminateAppMessage());
_ipc.End();
_ipc = null;
}
}
private const string IpcName = @"\\.\pipe\PowerToys.AdvancedPaste";
private TwoWayPipeMessageIPCManaged? _ipc;
private string _ipcName = @"\\.\pipe\PowerToys.AdvancedPaste";
public void Enable()
{
_ipc = new TwoWayPipeMessageIPCManaged(string.Empty, _ipcName, (_) => { });
_ipc = new TwoWayPipeMessageIPCManaged(string.Empty, IpcName, (_) => { });
_ipc.Start();
PopulateShortcuts();
@@ -59,24 +56,24 @@ namespace RunnerV2.ModuleInterfaces
AdvancedPasteSettings settings = new SettingsUtils().GetSettingsOrDefault<AdvancedPasteSettings>(Name);
Shortcuts.Add((settings.Properties.AdvancedPasteUIShortcut, () =>
_ipc.Send("ShowUI")
_ipc.Send(Constants.AdvancedPasteShowUIMessage())
));
Shortcuts.Add((settings.Properties.PasteAsPlainTextShortcut, TryToPasteAsPlainText));
Shortcuts.Add((settings.Properties.PasteAsMarkdownShortcut, () => _ipc.Send("PasteMarkdown")));
Shortcuts.Add((settings.Properties.PasteAsJsonShortcut, () => _ipc.Send("PasteJson")));
Shortcuts.Add((settings.Properties.PasteAsMarkdownShortcut, () => _ipc.Send(Constants.AdvancedPasteMarkdownMessage())));
Shortcuts.Add((settings.Properties.PasteAsJsonShortcut, () => _ipc.Send(Constants.AdvancedPasteJsonMessage())));
HotkeyAccessor[] hotkeyAccessors = settings.GetAllHotkeyAccessors();
int additionalActionsCount = settings.Properties.AdditionalActions.GetAllActions().Count() - 2;
for (int i = 0; i < additionalActionsCount; i++)
{
int scopedI = i;
Shortcuts.Add((hotkeyAccessors[4 + i].Value, () => _ipc.Send("AdditionalAction " + (3 + scopedI))));
Shortcuts.Add((hotkeyAccessors[4 + i].Value, () => _ipc.Send(Constants.AdvancedPasteAdditionalActionMessage() + " " + (3 + scopedI))));
}
for (int i = 4 + additionalActionsCount; i < hotkeyAccessors.Length; i++)
{
int scopedI = i;
Shortcuts.Add((hotkeyAccessors[i].Value, () => _ipc.Send("CustomAction " + (scopedI - 5 - additionalActionsCount))));
Shortcuts.Add((hotkeyAccessors[i].Value, () => _ipc.Send(Constants.AdvancedPasteCustomActionMessage() + " " + (scopedI - 5 - additionalActionsCount))));
}
}
@@ -102,7 +99,7 @@ namespace RunnerV2.ModuleInterfaces
public override string ProcessName => "PowerToys.AdvancedPaste";
public override string ProcessArguments => _ipcName;
public override string ProcessArguments => IpcName;
public override ProcessLaunchOptions LaunchOptions => ProcessLaunchOptions.SingletonProcess | ProcessLaunchOptions.RunnerProcessIdAsFirstArgument;
}

View File

@@ -4,17 +4,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text.Json;
using ManagedCommon;
using System.Threading;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapper;
using RunnerV2.Helpers;
using PowerToys.Interop;
namespace RunnerV2.ModuleInterfaces
{
internal sealed partial class AlwaysOnTopModuleInterface : ProcessModuleAbstractClass, IPowerToysModule, IDisposable
internal sealed class AlwaysOnTopModuleInterface : ProcessModuleAbstractClass, IPowerToysModule
{
public bool Enabled => new SettingsUtils().GetSettings<GeneralSettings>().Enabled.AlwaysOnTop;
@@ -22,21 +20,15 @@ namespace RunnerV2.ModuleInterfaces
public GpoRuleConfigured GpoRuleConfigured => GPOWrapper.GetConfiguredAlwaysOnTopEnabledValue();
private InteropEvent? _pinEventWrapper;
public void Disable()
{
InteropEvent terminateEventWrapper = new(InteropEvent.AlwaysOnTopTerminate);
terminateEventWrapper.Fire();
terminateEventWrapper.Dispose();
_pinEventWrapper?.Dispose();
_pinEventWrapper = null;
using var terminateEventWrapper = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.AlwaysOnTopTerminateEvent());
terminateEventWrapper.Set();
}
public void Enable()
{
InitializeHotkey();
_pinEventWrapper = new InteropEvent(InteropEvent.AlwaysOnTopPin);
}
private void InitializeHotkey()
@@ -44,7 +36,8 @@ namespace RunnerV2.ModuleInterfaces
Shortcuts.Clear();
Shortcuts.Add((new SettingsUtils().GetSettings<AlwaysOnTopSettings>(Name).Properties.Hotkey.Value, () =>
{
_pinEventWrapper?.Fire();
using var pinEventWrapper = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.AlwaysOnTopPinEvent());
pinEventWrapper.Set();
}
));
}
@@ -61,11 +54,5 @@ namespace RunnerV2.ModuleInterfaces
public override string ProcessName => "PowerToys.AlwaysOnTop";
public override ProcessLaunchOptions LaunchOptions => ProcessLaunchOptions.SingletonProcess | ProcessLaunchOptions.RunnerProcessIdAsFirstArgument | ProcessLaunchOptions.ElevateIfApplicable;
public void Dispose()
{
_pinEventWrapper?.Dispose();
GC.SuppressFinalize(this);
}
}
}

View File

@@ -3,13 +3,11 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Globalization;
using ManagedCommon;
using System.Threading;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapper;
using RunnerV2.Helpers;
using Windows.Media.Capture;
using PowerToys.Interop;
namespace RunnerV2.ModuleInterfaces
{
@@ -31,9 +29,8 @@ namespace RunnerV2.ModuleInterfaces
public void Disable()
{
InteropEvent terminateEventWrapper = new(InteropEvent.AwakeTerminate);
terminateEventWrapper.Fire();
terminateEventWrapper.Dispose();
using var terminateEventWrapper = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.AwakeExitEvent());
terminateEventWrapper.Set();
}
public void Enable()

View File

@@ -6,7 +6,6 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ManagedCommon;
using PowerToys.GPOWrapper;

View File

@@ -4,20 +4,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapper;
using RunnerV2.Helpers;
using PowerToys.Interop;
namespace RunnerV2.ModuleInterfaces
{
internal sealed partial class ColorPickerModuleInterface : ProcessModuleAbstractClass, IPowerToysModule, IDisposable
internal sealed class ColorPickerModuleInterface : ProcessModuleAbstractClass, IPowerToysModule
{
public string Name => "ColorPicker";
@@ -25,22 +20,15 @@ namespace RunnerV2.ModuleInterfaces
public GpoRuleConfigured GpoRuleConfigured => GPOWrapper.GetConfiguredColorPickerEnabledValue();
private InteropEvent? _showUiEventWrapper;
public void Disable()
{
InteropEvent terminateEventWrapper = new(InteropEvent.ColorPickerTerminate);
terminateEventWrapper.Fire();
terminateEventWrapper.Dispose();
_showUiEventWrapper?.Dispose();
_showUiEventWrapper = null;
using var terminateEventWrapper = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.TerminateColorPickerSharedEvent());
terminateEventWrapper.Set();
}
public void Enable()
{
InitializeShortcuts();
_showUiEventWrapper = new InteropEvent(InteropEvent.ColorPickerShow);
}
private void InitializeShortcuts()
@@ -48,7 +36,8 @@ namespace RunnerV2.ModuleInterfaces
Shortcuts.Clear();
Shortcuts.Add((new SettingsUtils().GetSettings<ColorPickerSettings>(Name).Properties.ActivationShortcut, () =>
{
_showUiEventWrapper?.Fire();
using var showUiEventWrapper = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.ShowColorPickerSharedEvent());
showUiEventWrapper.Set();
}
));
}
@@ -65,11 +54,5 @@ namespace RunnerV2.ModuleInterfaces
public override string ProcessName => "PowerToys.ColorPickerUI";
public override ProcessLaunchOptions LaunchOptions => ProcessLaunchOptions.SingletonProcess | ProcessLaunchOptions.RunnerProcessIdAsFirstArgument;
public void Dispose()
{
_showUiEventWrapper?.Dispose();
GC.SuppressFinalize(this);
}
}
}

View File

@@ -3,16 +3,13 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapper;
using RunnerV2.Helpers;
using Windows.ApplicationModel;
namespace RunnerV2.ModuleInterfaces
{

View File

@@ -4,14 +4,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapper;
using RunnerV2.Helpers;
using PowerToys.Interop;
namespace RunnerV2.ModuleInterfaces
{
@@ -23,23 +21,17 @@ namespace RunnerV2.ModuleInterfaces
public GpoRuleConfigured GpoRuleConfigured => GPOWrapper.GetConfiguredCropAndLockEnabledValue();
private InteropEvent _reparentEvent = new(InteropEvent.CropAndLockReparent);
private InteropEvent _thumbnailEvent = new(InteropEvent.CropAndLockThumbnail);
private InteropEvent _terminateEvent = new(InteropEvent.CropAndLockTerminate);
private EventWaitHandle _reparentEvent = new(false, EventResetMode.AutoReset, Constants.CropAndLockReparentEvent());
private EventWaitHandle _thumbnailEvent = new(false, EventResetMode.AutoReset, Constants.CropAndLockThumbnailEvent());
private EventWaitHandle _terminateEvent = new(false, EventResetMode.AutoReset, Constants.CropAndLockExitEvent());
public void Disable()
{
_terminateEvent.Fire();
_reparentEvent.Dispose();
_thumbnailEvent.Dispose();
_terminateEvent.Dispose();
_terminateEvent.Set();
}
public void Enable()
{
_reparentEvent = new(InteropEvent.CropAndLockReparent);
_thumbnailEvent = new(InteropEvent.CropAndLockThumbnail);
_terminateEvent = new(InteropEvent.CropAndLockTerminate);
PopulateShortcuts();
}
@@ -47,8 +39,8 @@ namespace RunnerV2.ModuleInterfaces
{
Shortcuts.Clear();
var settings = new SettingsUtils().GetSettings<CropAndLockSettings>(Name);
Shortcuts.Add((settings.Properties.ThumbnailHotkey.Value, _thumbnailEvent.Fire));
Shortcuts.Add((settings.Properties.ReparentHotkey.Value, _reparentEvent.Fire));
Shortcuts.Add((settings.Properties.ThumbnailHotkey.Value, () => _thumbnailEvent.Set()));
Shortcuts.Add((settings.Properties.ReparentHotkey.Value, () => _reparentEvent.Set()));
}
public void OnSettingsChanged(string settingsKind, JsonElement jsonProperties)

View File

@@ -2,10 +2,8 @@
// 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.Diagnostics;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapper;
using RunnerV2.Helpers;
namespace RunnerV2.ModuleInterfaces
{

View File

@@ -2,9 +2,6 @@
// 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.Diagnostics;
using System.Globalization;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapper;

View File

@@ -3,14 +3,8 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerToys.GPOWrapper;
using RunnerV2.Helpers;
namespace RunnerV2.ModuleInterfaces
@@ -56,6 +50,11 @@ namespace RunnerV2.ModuleInterfaces
/// </summary>
/// <remarks>Use this value when using a helper process to launch an application like explorer.exe.</remarks>
NeverExit = 32,
/// <summary>
/// Suppresses UI when process is launched.
/// </summary>
HideUI = 64,
}
/// <summary>

View File

@@ -5,7 +5,6 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using ManagedCommon;
using Windows.ApplicationModel;

View File

@@ -6,7 +6,6 @@ using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using PowerToys.GPOWrapperProjection;

View File

@@ -8,19 +8,15 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ManagedCommon;
using RunnerV2.Helpers;
using RunnerV2.ModuleInterfaces;
using Update;
using Windows.ApplicationModel;
using static RunnerV2.NativeMethods;
namespace RunnerV2
@@ -53,6 +49,7 @@ namespace RunnerV2
new CmdNotFoundModuleInterface(),
new CommandPaletteModuleInterface(),
new CropAndLockModuleInterface(),
new EnvironmentVariablesModuleInterface(),
];
/// <summary>

View File

@@ -1,53 +0,0 @@
// 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 static ManagedCommon.NativeMethods;
namespace ManagedCommon
{
public partial class InteropEvent : IDisposable
{
public const string AlwaysOnTopPin = "Local\\AlwaysOnTopPinEvent-892e0aa2-cfa8-4cc4-b196-ddeb32314ce8";
public const string AlwaysOnTopTerminate = "Local\\AlwaysOnTopTerminateEvent-cfdf1eae-791f-4953-8021-2f18f3837eae";
public const string AwakeTerminate = "Local\\PowerToysAwakeExitEvent-c0d5e305-35fc-4fb5-83ec-f6070cfaf7fe";
public const string ColorPickerShow = "Local\\ShowColorPickerEvent-8c46be2a-3e05-4186-b56b-4ae986ef2525";
public const string ColorPickerTerminate = "Local\\TerminateColorPickerEvent-3d676258-c4d5-424e-a87a-4be22020e813";
public const string SettingsTerminate = "Local\\PowerToysRunnerTerminateSettingsEvent-c34cb661-2e69-4613-a1f8-4e39c25d7ef6";
public const string CropAndLockReparent = "Local\\PowerToysCropAndLockReparentEvent-6060860a-76a1-44e8-8d0e-6355785e9c36";
public const string CropAndLockThumbnail = "Local\\PowerToysCropAndLockThumbnailEvent-1637be50-da72-46b2-9220-b32b206b2434";
public const string CropAndLockTerminate = "Local\\PowerToysCropAndLockExitEvent-d995d409-7b70-482b-bad6-e7c8666f375a";
private IntPtr _eventHandle;
public InteropEvent(string eventName)
{
_eventHandle = CreateEventW(IntPtr.Zero, false, false, eventName);
}
public void Fire()
{
if (_eventHandle != IntPtr.Zero)
{
SetEvent(_eventHandle);
}
}
~InteropEvent()
{
Dispose();
}
public void Dispose()
{
if (_eventHandle != IntPtr.Zero)
{
CloseHandle(_eventHandle);
_eventHandle = IntPtr.Zero;
}
GC.SuppressFinalize(this);
}
}
}

View File

@@ -195,4 +195,16 @@ namespace winrt::PowerToys::Interop::implementation
{
return CommonSharedConstants::CMDPAL_SHOW_EVENT;
}
hstring Constants::AlwaysOnTopPinEvent()
{
return CommonSharedConstants::ALWAYS_ON_TOP_PIN_EVENT;
}
hstring Constants::AlwaysOnTopTerminateEvent()
{
return CommonSharedConstants::ALWAYS_ON_TOP_TERMINATE_EVENT;
}
hstring Constants::CropAndLockExitEvent()
{
return CommonSharedConstants::CROP_AND_LOCK_EXIT_EVENT;
}
}

View File

@@ -46,12 +46,15 @@ namespace winrt::PowerToys::Interop::implementation
static hstring TerminateHostsSharedEvent();
static hstring CropAndLockThumbnailEvent();
static hstring CropAndLockReparentEvent();
static hstring CropAndLockExitEvent();
static hstring ShowEnvironmentVariablesSharedEvent();
static hstring ShowEnvironmentVariablesAdminSharedEvent();
static hstring WorkspacesLaunchEditorEvent();
static hstring WorkspacesHotkeyEvent();
static hstring PowerToysRunnerTerminateSettingsEvent();
static hstring ShowCmdPalEvent();
static hstring AlwaysOnTopPinEvent();
static hstring AlwaysOnTopTerminateEvent();
};
}

View File

@@ -43,12 +43,15 @@ namespace PowerToys
static String TerminateHostsSharedEvent();
static String CropAndLockThumbnailEvent();
static String CropAndLockReparentEvent();
static String CropAndLockExitEvent();
static String ShowEnvironmentVariablesSharedEvent();
static String ShowEnvironmentVariablesAdminSharedEvent();
static String WorkspacesLaunchEditorEvent();
static String WorkspacesHotkeyEvent();
static String PowerToysRunnerTerminateSettingsEvent();
static String ShowCmdPalEvent();
static String AlwaysOnTopPinEvent();
static String AlwaysOnTopTerminateEvent();
}
}
}