2024-05-09 10:32:03 -04:00
|
|
|
|
// 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;
|
2024-08-22 16:17:12 +02:00
|
|
|
|
using System.Linq;
|
2024-05-09 10:32:03 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Windows.Input;
|
2024-10-17 05:14:57 -04:00
|
|
|
|
|
2024-05-09 10:32:03 -04:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Helpers;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.ViewModels;
|
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Views
|
|
|
|
|
|
{
|
2025-08-27 05:29:14 +02:00
|
|
|
|
public sealed partial class AdvancedPastePage : NavigablePage, IRefreshablePage
|
2024-05-09 10:32:03 -04:00
|
|
|
|
{
|
|
|
|
|
|
private AdvancedPasteViewModel ViewModel { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public ICommand SaveOpenAIKeyCommand => new RelayCommand(SaveOpenAIKey);
|
|
|
|
|
|
|
|
|
|
|
|
public AdvancedPastePage()
|
|
|
|
|
|
{
|
|
|
|
|
|
var settingsUtils = new SettingsUtils();
|
|
|
|
|
|
ViewModel = new AdvancedPasteViewModel(
|
|
|
|
|
|
settingsUtils,
|
|
|
|
|
|
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
|
|
|
|
|
|
SettingsRepository<AdvancedPasteSettings>.GetInstance(settingsUtils),
|
|
|
|
|
|
ShellPage.SendDefaultIPCMessage);
|
|
|
|
|
|
DataContext = ViewModel;
|
|
|
|
|
|
InitializeComponent();
|
2025-08-20 09:31:52 +08:00
|
|
|
|
|
|
|
|
|
|
Loaded += (s, e) => ViewModel.OnPageLoaded();
|
2024-05-09 10:32:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void RefreshEnabledState()
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewModel.RefreshEnabledState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SaveOpenAIKey()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrEmpty(AdvancedPaste_EnableAIDialogOpenAIApiKey.Text))
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewModel.EnableAI(AdvancedPaste_EnableAIDialogOpenAIApiKey.Text);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void AdvancedPaste_EnableAIButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resourceLoader = Helpers.ResourceLoaderInstance.ResourceLoader;
|
|
|
|
|
|
EnableAIDialog.PrimaryButtonText = resourceLoader.GetString("EnableAIDialog_SaveBtnText");
|
|
|
|
|
|
EnableAIDialog.SecondaryButtonText = resourceLoader.GetString("EnableAIDialog_CancelBtnText");
|
|
|
|
|
|
EnableAIDialog.PrimaryButtonCommand = SaveOpenAIKeyCommand;
|
|
|
|
|
|
|
|
|
|
|
|
AdvancedPaste_EnableAIDialogOpenAIApiKey.Text = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
await ShowEnableDialogAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task ShowEnableDialogAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnableAIDialog.ShowAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AdvancedPaste_DisableAIButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewModel.DisableAI();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AdvancedPaste_EnableAIDialogOpenAIApiKey_TextChanged(object sender, TextChangedEventArgs e)
|
|
|
|
|
|
{
|
2024-08-22 16:17:12 +02:00
|
|
|
|
EnableAIDialog.IsPrimaryButtonEnabled = AdvancedPaste_EnableAIDialogOpenAIApiKey.Text.Length > 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async void DeleteCustomActionButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var customAction = GetBoundCustomAction(sender);
|
|
|
|
|
|
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
|
|
|
|
|
|
|
|
|
|
|
ContentDialog dialog = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
XamlRoot = RootPage.XamlRoot,
|
|
|
|
|
|
Title = customAction.Name,
|
|
|
|
|
|
PrimaryButtonText = resourceLoader.GetString("Yes"),
|
|
|
|
|
|
CloseButtonText = resourceLoader.GetString("No"),
|
|
|
|
|
|
DefaultButton = ContentDialogButton.Primary,
|
|
|
|
|
|
Content = new TextBlock() { Text = resourceLoader.GetString("Delete_Dialog_Description") },
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
dialog.PrimaryButtonClick += (_, _) => ViewModel.DeleteCustomAction(customAction);
|
|
|
|
|
|
|
|
|
|
|
|
await dialog.ShowAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void AddCustomActionButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
|
|
|
|
|
|
|
|
|
|
|
CustomActionDialog.Title = resourceLoader.GetString("AddCustomAction");
|
|
|
|
|
|
CustomActionDialog.DataContext = ViewModel.GetNewCustomAction(resourceLoader.GetString("AdvancedPasteUI_NewCustomActionPrefix"));
|
|
|
|
|
|
CustomActionDialog.PrimaryButtonText = resourceLoader.GetString("CustomActionSave");
|
|
|
|
|
|
await CustomActionDialog.ShowAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void EditCustomActionButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
|
|
|
|
|
|
|
|
|
|
|
CustomActionDialog.Title = resourceLoader.GetString("EditCustomAction");
|
|
|
|
|
|
CustomActionDialog.DataContext = GetBoundCustomAction(sender).Clone();
|
|
|
|
|
|
CustomActionDialog.PrimaryButtonText = resourceLoader.GetString("CustomActionUpdate");
|
|
|
|
|
|
await CustomActionDialog.ShowAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ReorderButtonDown_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var index = ViewModel.CustomActions.IndexOf(GetBoundCustomAction(sender));
|
|
|
|
|
|
ViewModel.CustomActions.Move(index, index + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ReorderButtonUp_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var index = ViewModel.CustomActions.IndexOf(GetBoundCustomAction(sender));
|
|
|
|
|
|
ViewModel.CustomActions.Move(index, index - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CustomActionDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Result != ContentDialogResult.Primary)
|
2024-05-09 10:32:03 -04:00
|
|
|
|
{
|
2024-08-22 16:17:12 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var dialogCustomAction = GetBoundCustomAction(sender);
|
|
|
|
|
|
var existingCustomAction = ViewModel.CustomActions.FirstOrDefault(candidate => candidate.Id == dialogCustomAction.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (existingCustomAction == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewModel.AddCustomAction(dialogCustomAction);
|
|
|
|
|
|
|
|
|
|
|
|
var element = (ContentPresenter)CustomActions.ContainerFromIndex(CustomActions.Items.Count - 1);
|
|
|
|
|
|
element.StartBringIntoView(new BringIntoViewOptions { VerticalOffset = -60, AnimationDesired = true });
|
|
|
|
|
|
element.Focus(FocusState.Programmatic);
|
2024-05-09 10:32:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-08-22 16:17:12 +02:00
|
|
|
|
existingCustomAction.Update(dialogCustomAction);
|
2024-05-09 10:32:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-22 16:17:12 +02:00
|
|
|
|
|
|
|
|
|
|
private static AdvancedPasteCustomAction GetBoundCustomAction(object sender) => (AdvancedPasteCustomAction)((FrameworkElement)sender).DataContext;
|
2024-05-09 10:32:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|