From a5757fd525f8c2d768b730fe07bc5f349e2bf52b Mon Sep 17 00:00:00 2001 From: Ani <115020168+drawbyperpetual@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:23:23 +0200 Subject: [PATCH] [AdvancedPaste] Custom Actions follow-up fixes #1 (#34404) --- .../AdvancedPasteXAML/Pages/MainPage.xaml.cs | 10 ++++++++ .../ViewModels/OptionsViewModel.cs | 24 ++----------------- .../AdvancedPasteModuleInterface/dllmain.cpp | 8 +++++-- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs index 17947fcabf07..3783be843044 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs @@ -25,6 +25,7 @@ public sealed partial class MainPage : Page { private readonly ObservableCollection clipboardHistory; private readonly Microsoft.UI.Dispatching.DispatcherQueue _dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread(); + private (VirtualKey Key, DateTime Timestamp) _lastKeyEvent = (VirtualKey.None, DateTime.MinValue); public OptionsViewModel ViewModel { get; private set; } @@ -145,6 +146,15 @@ private void KeyboardAccelerator_Invoked(Microsoft.UI.Xaml.Input.KeyboardAcceler Logger.LogTrace(); + var thisKeyEvent = (sender.Key, Timestamp: DateTime.Now); + if (thisKeyEvent.Key == _lastKeyEvent.Key && (thisKeyEvent.Timestamp - _lastKeyEvent.Timestamp) < TimeSpan.FromMilliseconds(200)) + { + // Sometimes, multiple keyboard accelerator events are raised for a single Ctrl + VirtualKey press. + return; + } + + _lastKeyEvent = thisKeyEvent; + switch (sender.Key) { case VirtualKey.Escape: diff --git a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs index 22b2da1353f4..b0cacd027423 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs @@ -61,17 +61,11 @@ public partial class OptionsViewModel : ObservableObject, IDisposable private bool _pasteFormatsDirty; - [ObservableProperty] - [NotifyPropertyChangedFor(nameof(IsCustomAIEnabled))] - private bool _isCustomAIEnabledOverride = false; - public ObservableCollection StandardPasteFormats { get; } = []; public ObservableCollection CustomActionPasteFormats { get; } = []; - public bool IsCustomAIEnabled => IsCustomAIEnabledOverride || IsCustomAIEnabledCore; - - private bool IsCustomAIEnabledCore => IsAllowedByGPO && IsClipboardDataText && aiHelper.IsAIEnabled; + public bool IsCustomAIEnabled => IsAllowedByGPO && IsClipboardDataText && aiHelper.IsAIEnabled; public event EventHandler CustomActionActivated; @@ -218,20 +212,6 @@ public void OnShow() ClipboardHistoryEnabled = IsClipboardHistoryEnabled(); GeneratedResponses.Clear(); - - _dispatcherQueue.TryEnqueue(async () => - { - // Work-around for ListViews being disabled but sometimes not appearing grayed out. - // It appears that this is sometimes only triggered by a change event. This - // work-around sometimes still doesn't work, but it's better than not having it. - await Task.Delay(5); - IsClipboardDataText = true; - IsCustomAIEnabledOverride = true; - - await Task.Delay(150); - ReadClipboard(); - IsCustomAIEnabledOverride = false; - }); } // List to store generated responses @@ -437,7 +417,7 @@ internal void ExecutePasteFormat(VirtualKey key) internal void ExecutePasteFormat(PasteFormat pasteFormat) { - if (!IsClipboardDataText || (pasteFormat.Format == PasteFormats.Custom && !IsCustomAIEnabledCore)) + if (!IsClipboardDataText || (pasteFormat.Format == PasteFormats.Custom && !IsCustomAIEnabled)) { return; } diff --git a/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp b/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp index 0f0eb6fcbd24..bb6f2f952a68 100644 --- a/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp +++ b/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp @@ -41,6 +41,7 @@ namespace const wchar_t JSON_KEY_PROPERTIES[] = L"properties"; const wchar_t JSON_KEY_CUSTOM_ACTIONS[] = L"custom-actions"; const wchar_t JSON_KEY_SHORTCUT[] = L"shortcut"; + const wchar_t JSON_KEY_IS_SHOWN[] = L"isShown"; const wchar_t JSON_KEY_ID[] = L"id"; const wchar_t JSON_KEY_WIN[] = L"win"; const wchar_t JSON_KEY_ALT[] = L"alt"; @@ -220,8 +221,11 @@ class AdvancedPaste : public PowertoyModuleIface { const auto object = customAction.GetObjectW(); - m_custom_action_hotkeys.push_back(parse_single_hotkey(object.GetNamedObject(JSON_KEY_SHORTCUT))); - m_custom_action_ids.push_back(static_cast(object.GetNamedNumber(JSON_KEY_ID))); + if (object.GetNamedBoolean(JSON_KEY_IS_SHOWN, false)) + { + m_custom_action_hotkeys.push_back(parse_single_hotkey(object.GetNamedObject(JSON_KEY_SHORTCUT))); + m_custom_action_ids.push_back(static_cast(object.GetNamedNumber(JSON_KEY_ID))); + } } } }