Skip to content

Commit

Permalink
UI improvements, disabled standard paste actions when no clipboard te…
Browse files Browse the repository at this point in the history
…xt, update clipboard text and gpo state every second
  • Loading branch information
drawbyperpetual committed Aug 8, 2024
1 parent cdb745d commit 8f14757
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -184,7 +184,7 @@ private void OnAdvancedPasteCustomActionHotkey(string[] messageParts)
}
else
{
viewModel.GetClipboardData();
viewModel.ReadClipboard();

if (!int.TryParse(messageParts[1], CultureInfo.InvariantCulture, out int id))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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<AdvancedPasteCustomAction> _customActions;
public ObservableCollection<AdvancedPasteCustomAction> CustomActions => _userSettings.CustomActions;

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

public event EventHandler<TextEventArgs> CustomActionActivated;

Expand All @@ -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<string>();
GeneratedResponses.CollectionChanged += (s, e) =>
{
Expand All @@ -74,25 +72,30 @@ 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);
}

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();
Expand All @@ -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();
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,16 @@
<x:Double x:Key="SettingsCardActionButtonWidth">0</x:Double>
</tkcontrols:SettingsCard.Resources>
<StackPanel Orientation="Horizontal" Spacing="4">
<controls:ShortcutControl
MinWidth="{StaticResource SettingActionControlMinWidth}"
AllowDisable="True"
HotkeySettings="{x:Bind Path=Shortcut, Mode=TwoWay}" />
<ToggleSwitch
x:Uid="Enable_CustomAction"
AutomationProperties.HelpText="{x:Bind Name}"
IsOn="{x:Bind IsShown, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
OffContent=""
OnContent="" />
<controls:ShortcutControl
MinWidth="{StaticResource SettingActionControlMinWidth}"
AllowDisable="True"
HotkeySettings="{x:Bind Path=Shortcut, Mode=TwoWay}" />
<Button
x:Uid="More_Options_Button"
Grid.Column="1"
Expand Down

0 comments on commit 8f14757

Please sign in to comment.