From 8f14757da7ed0421dbba70c3824a41ac0972592b Mon Sep 17 00:00:00 2001 From: Ani <115020168+drawbyperpetual@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:44:41 +0200 Subject: [PATCH] UI improvements, disabled standard paste actions when no clipboard text, update clipboard text and gpo state every second --- .../AdvancedPasteXAML/App.xaml.cs | 6 +- .../AdvancedPasteXAML/Pages/MainPage.xaml | 1 + .../ViewModels/OptionsViewModel.cs | 71 +++++++++++-------- .../SettingsXAML/Views/AdvancedPaste.xaml | 8 +-- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs index e936157213c..9bbb681130e 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs @@ -142,13 +142,13 @@ private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledEx private void OnAdvancedPasteJsonHotkey() { - viewModel.GetClipboardData(); + viewModel.ReadClipboard(); viewModel.ToJsonFunction(true); } private void OnAdvancedPasteMarkdownHotkey() { - viewModel.GetClipboardData(); + viewModel.ReadClipboard(); viewModel.ToMarkdownFunction(true); } @@ -184,7 +184,7 @@ private void OnAdvancedPasteCustomActionHotkey(string[] messageParts) } else { - viewModel.GetClipboardData(); + viewModel.ReadClipboard(); if (!int.TryParse(messageParts[1], CultureInfo.InvariantCulture, out int id)) { diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml index 2e20d568324..06d4ab9329a 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml @@ -120,6 +120,7 @@ x:Name="PasteOptionsListView" Grid.Row="0" VerticalAlignment="Bottom" + IsEnabled="{x:Bind ViewModel.IsClipboardDataText, Mode=OneWay}" IsItemClickEnabled="True" ItemClick="PasteOptionsListView_ItemClick" ItemContainerTransitions="{x:Null}" diff --git a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs index 6bc136b25ea..b21a575d3f8 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs @@ -17,20 +17,20 @@ using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.UI.Dispatching; +using Microsoft.UI.Xaml; using Microsoft.Win32; using Windows.ApplicationModel.DataTransfer; using WinUIEx; namespace AdvancedPaste.ViewModels { - public partial class OptionsViewModel : ObservableObject + public partial class OptionsViewModel : ObservableObject, IDisposable { private readonly DispatcherQueue _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + private readonly DispatcherTimer _clipboardTimer; private readonly IUserSettings _userSettings; - - private App app = App.Current as App; - - private AICompletionsHelper aiHelper; + private readonly AICompletionsHelper aiHelper; + private readonly App app = App.Current as App; public DataPackageView ClipboardData { get; set; } @@ -40,18 +40,20 @@ public partial class OptionsViewModel : ObservableObject private bool _isClipboardDataText; [ObservableProperty] - [NotifyPropertyChangedFor(nameof(InputTxtBoxPlaceholderText))] - private bool _isCustomAIEnabled; + private bool _clipboardHistoryEnabled; [ObservableProperty] - private bool _clipboardHistoryEnabled; + [NotifyPropertyChangedFor(nameof(InputTxtBoxPlaceholderText))] + [NotifyPropertyChangedFor(nameof(IsCustomAIEnabled))] + private bool _isAllowedByGPO; [ObservableProperty] [NotifyPropertyChangedFor(nameof(InputTxtBoxErrorText))] private int _apiRequestStatus; - [ObservableProperty] - private ObservableCollection _customActions; + public ObservableCollection CustomActions => _userSettings.CustomActions; + + public bool IsCustomAIEnabled => IsAllowedByGPO && IsClipboardDataText && aiHelper.IsAIEnabled; public event EventHandler CustomActionActivated; @@ -60,12 +62,8 @@ public OptionsViewModel(IUserSettings userSettings) aiHelper = new AICompletionsHelper(); _userSettings = userSettings; - IsCustomAIEnabled = IsClipboardDataText && aiHelper.IsAIEnabled; - ApiRequestStatus = (int)HttpStatusCode.OK; - CustomActions = _userSettings.CustomActions; - GeneratedResponses = new ObservableCollection(); GeneratedResponses.CollectionChanged += (s, e) => { @@ -74,10 +72,19 @@ public OptionsViewModel(IUserSettings userSettings) }; ClipboardHistoryEnabled = IsClipboardHistoryEnabled(); - GetClipboardData(); + ReadClipboard(); + _clipboardTimer = new() { Interval = TimeSpan.FromSeconds(1) }; + _clipboardTimer.Tick += OnClipboardTimerTick; + _clipboardTimer.Start(); + } + + public void Dispose() + { + _clipboardTimer.Stop(); + GC.SuppressFinalize(this); } - public void GetClipboardData() + public void ReadClipboard() { ClipboardData = Clipboard.GetContent(); IsClipboardDataText = ClipboardData.Contains(StandardDataFormats.Text); @@ -85,14 +92,10 @@ public void GetClipboardData() public void OnShow() { - GetClipboardData(); + ReadClipboard(); + UpdateAllowedByGPO(); - if (PowerToys.GPOWrapper.GPOWrapper.GetAllowedAdvancedPasteOnlineAIModelsValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled) - { - IsCustomAIEnabled = false; - OnPropertyChanged(nameof(InputTxtBoxPlaceholderText)); - } - else + if (IsAllowedByGPO) { var openAIKey = AICompletionsHelper.LoadOpenAIKey(); var currentKey = aiHelper.GetKey(); @@ -112,15 +115,11 @@ public void OnShow() { app.GetMainWindow().FinishLoading(aiHelper.IsAIEnabled); OnPropertyChanged(nameof(InputTxtBoxPlaceholderText)); - IsCustomAIEnabled = IsClipboardDataText && aiHelper.IsAIEnabled; + OnPropertyChanged(nameof(IsCustomAIEnabled)); }); }, TaskScheduler.Default); } - else - { - IsCustomAIEnabled = IsClipboardDataText && aiHelper.IsAIEnabled; - } } ClipboardHistoryEnabled = IsClipboardHistoryEnabled(); @@ -160,7 +159,7 @@ public string InputTxtBoxPlaceholderText { app.GetMainWindow().ClearInputText(); - if (PowerToys.GPOWrapper.GPOWrapper.GetAllowedAdvancedPasteOnlineAIModelsValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled) + if (!IsAllowedByGPO) { return ResourceLoaderInstance.ResourceLoader.GetString("OpenAIGpoDisabled"); } @@ -466,5 +465,19 @@ private bool IsClipboardHistoryEnabled() return false; } } + + private void UpdateAllowedByGPO() + { + IsAllowedByGPO = PowerToys.GPOWrapper.GPOWrapper.GetAllowedAdvancedPasteOnlineAIModelsValue() != PowerToys.GPOWrapper.GpoRuleConfigured.Disabled; + } + + private void OnClipboardTimerTick(object sender, object e) + { + if (app.GetMainWindow()?.Visible is true) + { + ReadClipboard(); + UpdateAllowedByGPO(); + } + } } } diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml index 1ed597d90c3..ed7a634388a 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml @@ -145,16 +145,16 @@ 0 + -