Skip to content

Commit

Permalink
[AdvancedPaste] Custom Actions follow-up fixes #1 (#34404)
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbyperpetual authored Aug 23, 2024
1 parent 5796199 commit a5757fd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed partial class MainPage : Page
{
private readonly ObservableCollection<ClipboardItem> 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; }

Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,11 @@ public partial class OptionsViewModel : ObservableObject, IDisposable

private bool _pasteFormatsDirty;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsCustomAIEnabled))]
private bool _isCustomAIEnabledOverride = false;

public ObservableCollection<PasteFormat> StandardPasteFormats { get; } = [];

public ObservableCollection<PasteFormat> CustomActionPasteFormats { get; } = [];

public bool IsCustomAIEnabled => IsCustomAIEnabledOverride || IsCustomAIEnabledCore;

private bool IsCustomAIEnabledCore => IsAllowedByGPO && IsClipboardDataText && aiHelper.IsAIEnabled;
public bool IsCustomAIEnabled => IsAllowedByGPO && IsClipboardDataText && aiHelper.IsAIEnabled;

public event EventHandler<CustomActionActivatedEventArgs> CustomActionActivated;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<int>(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<int>(object.GetNamedNumber(JSON_KEY_ID)));
}
}
}
}
Expand Down

0 comments on commit a5757fd

Please sign in to comment.