diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs index 25f83afb12..dee81f7ffe 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/App.xaml.cs @@ -3,15 +3,14 @@ // See the LICENSE file in the project root for more information. using System; -using System.Threading.Tasks; using AdvancedPaste.Helpers; +using AdvancedPaste.Settings; using AdvancedPaste.ViewModels; using ManagedCommon; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; -using Windows.ApplicationModel.DataTransfer; using Windows.Graphics; using WinUIEx; using static AdvancedPaste.Helpers.NativeMethods; @@ -47,6 +46,7 @@ namespace AdvancedPaste Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder().UseContentRoot(AppContext.BaseDirectory).ConfigureServices((context, services) => { services.AddSingleton(); + services.AddSingleton(); }).Build(); viewModel = GetService(); diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Controls/PromptBox.xaml.cs b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Controls/PromptBox.xaml.cs index 691c7257d5..334d23ebd3 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Controls/PromptBox.xaml.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Controls/PromptBox.xaml.cs @@ -2,7 +2,6 @@ // 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; using System.Net; using System.Threading.Tasks; using AdvancedPaste.Helpers; @@ -20,14 +19,13 @@ namespace AdvancedPaste.Controls public sealed partial class PromptBox : Microsoft.UI.Xaml.Controls.UserControl { private readonly DispatcherQueue _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - - private UserSettings _userSettings; + private readonly IUserSettings _userSettings; public static readonly DependencyProperty PromptProperty = DependencyProperty.Register( - nameof(Prompt), - typeof(string), - typeof(PromptBox), - new PropertyMetadata(defaultValue: string.Empty)); + nameof(Prompt), + typeof(string), + typeof(PromptBox), + new PropertyMetadata(defaultValue: string.Empty)); public OptionsViewModel ViewModel { get; private set; } @@ -38,10 +36,10 @@ namespace AdvancedPaste.Controls } public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register( - nameof(PlaceholderText), - typeof(string), - typeof(PromptBox), - new PropertyMetadata(defaultValue: string.Empty)); + nameof(PlaceholderText), + typeof(string), + typeof(PromptBox), + new PropertyMetadata(defaultValue: string.Empty)); public string PlaceholderText { @@ -50,10 +48,10 @@ namespace AdvancedPaste.Controls } public static readonly DependencyProperty FooterProperty = DependencyProperty.Register( - nameof(Footer), - typeof(object), - typeof(PromptBox), - new PropertyMetadata(defaultValue: null)); + nameof(Footer), + typeof(object), + typeof(PromptBox), + new PropertyMetadata(defaultValue: null)); public object Footer { @@ -65,7 +63,7 @@ namespace AdvancedPaste.Controls { this.InitializeComponent(); - _userSettings = new UserSettings(); + _userSettings = App.GetService(); ViewModel = App.GetService(); } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/MainWindow.xaml.cs b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/MainWindow.xaml.cs index 4975453453..401fac840a 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/MainWindow.xaml.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/MainWindow.xaml.cs @@ -3,21 +3,20 @@ // See the LICENSE file in the project root for more information. using System; -using System.Runtime.CompilerServices; using AdvancedPaste.Helpers; +using AdvancedPaste.Settings; using ManagedCommon; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; -using Windows.Graphics; using WinUIEx; using WinUIEx.Messaging; -using static AdvancedPaste.Helpers.NativeMethods; namespace AdvancedPaste { public sealed partial class MainWindow : WindowEx, IDisposable { - private WindowMessageMonitor _msgMonitor; + private readonly WindowMessageMonitor _msgMonitor; + private readonly IUserSettings _userSettings; private bool _disposedValue; @@ -25,6 +24,8 @@ namespace AdvancedPaste { this.InitializeComponent(); + _userSettings = App.GetService(); + AppWindow.SetIcon("Assets/AdvancedPaste/AdvancedPaste.ico"); this.ExtendsContentIntoTitleBar = true; this.SetTitleBar(titleBar); @@ -32,6 +33,8 @@ namespace AdvancedPaste var loader = ResourceLoaderInstance.ResourceLoader; Title = loader.GetString("WindowTitle"); + Activated += OnActivated; + _msgMonitor = new WindowMessageMonitor(this); _msgMonitor.WindowMessageReceived += (_, e) => { @@ -47,6 +50,14 @@ namespace AdvancedPaste WindowHelpers.BringToForeground(this.GetWindowHandle()); } + private void OnActivated(object sender, WindowActivatedEventArgs args) + { + if (_userSettings.CloseAfterLosingFocus && args.WindowActivationState == WindowActivationState.Deactivated) + { + Hide(); + } + } + private void Dispose(bool disposing) { if (!_disposedValue) @@ -66,11 +77,15 @@ namespace AdvancedPaste private void WindowEx_Closed(object sender, Microsoft.UI.Xaml.WindowEventArgs args) { - Windows.Win32.PInvoke.ShowWindow((Windows.Win32.Foundation.HWND)this.GetWindowHandle(), Windows.Win32.UI.WindowsAndMessaging.SHOW_WINDOW_CMD.SW_HIDE); - + Hide(); args.Handled = true; } + private void Hide() + { + Windows.Win32.PInvoke.ShowWindow(new Windows.Win32.Foundation.HWND(this.GetWindowHandle()), Windows.Win32.UI.WindowsAndMessaging.SHOW_WINDOW_CMD.SW_HIDE); + } + public void SetFocus() { MainPage.CustomFormatTextBox.InputTxtBox.Focus(FocusState.Programmatic); diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/IUserSettings.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/IUserSettings.cs index e8394c002e..e7cca14477 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/IUserSettings.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/IUserSettings.cs @@ -9,5 +9,7 @@ namespace AdvancedPaste.Settings public bool ShowCustomPreview { get; } public bool SendPasteKeyCombination { get; } + + public bool CloseAfterLosingFocus { get; } } } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs index 0fe6ef59b3..25051a3970 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/UserSettings.cs @@ -24,12 +24,15 @@ namespace AdvancedPaste.Settings public bool SendPasteKeyCombination { get; private set; } + public bool CloseAfterLosingFocus { get; private set; } + public UserSettings() { _settingsUtils = new SettingsUtils(); ShowCustomPreview = true; SendPasteKeyCombination = true; + CloseAfterLosingFocus = false; LoadSettingsFromJson(); @@ -61,6 +64,7 @@ namespace AdvancedPaste.Settings { ShowCustomPreview = settings.Properties.ShowCustomPreview; SendPasteKeyCombination = settings.Properties.SendPasteKeyCombination; + CloseAfterLosingFocus = settings.Properties.CloseAfterLosingFocus; } retry = false; diff --git a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs index 1ba2625e79..894b8e5536 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs @@ -25,13 +25,12 @@ namespace AdvancedPaste.ViewModels public partial class OptionsViewModel : ObservableObject { private readonly DispatcherQueue _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + private readonly IUserSettings _userSettings; private App app = App.Current as App; private AICompletionsHelper aiHelper; - private UserSettings _userSettings; - public DataPackageView ClipboardData { get; set; } [ObservableProperty] @@ -50,10 +49,10 @@ namespace AdvancedPaste.ViewModels [NotifyPropertyChangedFor(nameof(InputTxtBoxErrorText))] private int _apiRequestStatus; - public OptionsViewModel() + public OptionsViewModel(IUserSettings userSettings) { aiHelper = new AICompletionsHelper(); - _userSettings = new UserSettings(); + _userSettings = userSettings; IsCustomAIEnabled = IsClipboardDataText && aiHelper.IsAIEnabled; diff --git a/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs b/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs index e8a7f72a2d..e6e57431a0 100644 --- a/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs +++ b/src/settings-ui/Settings.UI.Library/AdvancedPasteProperties.cs @@ -22,6 +22,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library PasteAsJsonShortcut = new(); ShowCustomPreview = true; SendPasteKeyCombination = true; + CloseAfterLosingFocus = false; } [JsonConverter(typeof(BoolPropertyJsonConverter))] @@ -31,6 +32,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library [CmdConfigureIgnore] public bool SendPasteKeyCombination { get; set; } + [JsonConverter(typeof(BoolPropertyJsonConverter))] + public bool CloseAfterLosingFocus { get; set; } + [JsonPropertyName("advanced-paste-ui-hotkey")] public HotkeySettings AdvancedPasteUIShortcut { get; set; } diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml index 08fabdff9c..638bd8588e 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml @@ -81,7 +81,7 @@ Severity="Informational" /> - + + + + diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml index e7ec1c3610..4e6913f0fb 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml @@ -35,7 +35,7 @@ - + diff --git a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw index 5099a48b41..7dc0ae28ad 100644 --- a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw +++ b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw @@ -3763,8 +3763,8 @@ Activate by holding the key for the character you want to add an accent to, then Paste with AI - - Clipboard history + + Behavior Custom format preview @@ -4192,4 +4192,8 @@ Activate by holding the key for the character you want to add an accent to, then If you do not have credits you will see an 'API key quota exceeded' error + + Automatically close the AdvancedPaste window after it loses focus + AdvancedPaste is a product name, do not loc + \ No newline at end of file diff --git a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs index a4762c5e98..884061f01c 100644 --- a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs @@ -304,6 +304,19 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } + public bool CloseAfterLosingFocus + { + get => _advancedPasteSettings.Properties.CloseAfterLosingFocus; + set + { + if (value != _advancedPasteSettings.Properties.CloseAfterLosingFocus) + { + _advancedPasteSettings.Properties.CloseAfterLosingFocus = value; + NotifySettingsChanged(); + } + } + } + public bool IsConflictingCopyShortcut { get @@ -328,11 +341,11 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { // Using InvariantCulture as this is an IPC message SendConfigMSG( - string.Format( - CultureInfo.InvariantCulture, - "{{ \"powertoys\": {{ \"{0}\": {1} }} }}", - AdvancedPasteSettings.ModuleName, - JsonSerializer.Serialize(_advancedPasteSettings))); + string.Format( + CultureInfo.InvariantCulture, + "{{ \"powertoys\": {{ \"{0}\": {1} }} }}", + AdvancedPasteSettings.ModuleName, + JsonSerializer.Serialize(_advancedPasteSettings))); } public void RefreshEnabledState()