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.Collections.Generic ;
using System.Collections.ObjectModel ;
using System.Collections.Specialized ;
using System.ComponentModel ;
2024-05-09 10:32:03 -04:00
using System.Globalization ;
2024-08-22 16:17:12 +02:00
using System.Linq ;
using System.Reflection ;
2024-05-09 10:32:03 -04:00
using System.Text.Json ;
2024-08-22 16:17:12 +02:00
using System.Text.Json.Serialization ;
2024-05-09 10:32:03 -04:00
using System.Timers ;
using global : : PowerToys . GPOWrapper ;
2025-08-20 09:31:52 +08:00
using Microsoft.PowerToys.Settings.UI.Helpers ;
2024-05-09 10:32:03 -04:00
using Microsoft.PowerToys.Settings.UI.Library ;
using Microsoft.PowerToys.Settings.UI.Library.Helpers ;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces ;
2025-02-25 02:48:54 +08:00
using Microsoft.PowerToys.Settings.UI.SerializationContext ;
2024-05-09 10:32:03 -04:00
using Microsoft.Win32 ;
using Windows.Security.Credentials ;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
2025-08-20 09:31:52 +08:00
public partial class AdvancedPasteViewModel : PageViewModelBase
2024-05-09 10:32:03 -04:00
{
2024-08-22 16:17:12 +02:00
private static readonly HashSet < string > WarnHotkeys = [ "Ctrl + V" , "Ctrl + Shift + V" ] ;
2025-08-20 09:31:52 +08:00
private bool _disposed ;
2024-05-09 10:32:03 -04:00
Updates for check-spelling v0.0.25 (#40386)
## Summary of the Pull Request
- #39572 updated check-spelling but ignored:
> 🐣 Breaking Changes
[Code Scanning action requires a Code Scanning
Ruleset](https://github.com/check-spelling/check-spelling/wiki/Breaking-Change:-Code-Scanning-action-requires-a-Code-Scanning-Ruleset)
If you use SARIF reporting, then instead of the workflow yielding an ❌
when it fails, it will rely on [github-advanced-security
🤖](https://github.com/apps/github-advanced-security) to report the
failure. You will need to adjust your checks for PRs.
This means that check-spelling hasn't been properly doing its job 😦.
I'm sorry, I should have pushed a thing to this repo earlier,...
Anyway, as with most refreshes, this comes with a number of fixes, some
are fixes for typos that snuck in before the 0.0.25 upgrade, some are
for things that snuck in after, some are based on new rules in
spell-check-this, and some are hand written patterns based on running
through this repository a few times.
About the 🐣 **breaking change**: someone needs to create a ruleset for
this repository (see [Code Scanning action requires a Code Scanning
Ruleset: Sample ruleset
](https://github.com/check-spelling/check-spelling/wiki/Breaking-Change:-Code-Scanning-action-requires-a-Code-Scanning-Ruleset#sample-ruleset)).
The alternative to adding a ruleset is to change the condition to not
use sarif for this repository. In general, I think the github
integration from sarif is prettier/more helpful, so I think that it's
the better choice.
You can see an example of it working in:
- https://github.com/check-spelling-sandbox/PowerToys/pull/23
---------
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: Mike Griese <migrie@microsoft.com>
Co-authored-by: Dustin L. Howett <dustin@howett.net>
2025-07-08 18:16:52 -04:00
// Delay saving of settings in order to avoid calling save multiple times and hitting file in use exception. If there is no other request to save settings in given interval, we proceed to save it; otherwise, we schedule saving it after this interval
2024-05-09 10:32:03 -04:00
private const int SaveSettingsDelayInMs = 500 ;
2025-08-20 09:31:52 +08:00
protected override string ModuleName = > AdvancedPasteSettings . ModuleName ;
2024-05-09 10:32:03 -04:00
private GeneralSettings GeneralSettingsConfig { get ; set ; }
private readonly ISettingsUtils _settingsUtils ;
2024-11-13 12:36:45 -05:00
private readonly System . Threading . Lock _delayedActionLock = new System . Threading . Lock ( ) ;
2024-05-09 10:32:03 -04:00
private readonly AdvancedPasteSettings _advancedPasteSettings ;
2024-10-18 15:34:09 +02:00
private readonly AdvancedPasteAdditionalActions _additionalActions ;
2024-08-22 16:17:12 +02:00
private readonly ObservableCollection < AdvancedPasteCustomAction > _customActions ;
2024-05-09 10:32:03 -04:00
private Timer _delayedTimer ;
private GpoRuleConfigured _enabledGpoRuleConfiguration ;
private bool _enabledStateIsGPOConfigured ;
2024-05-26 13:22:50 +02:00
private GpoRuleConfigured _onlineAIModelsGpoRuleConfiguration ;
private bool _onlineAIModelsDisallowedByGPO ;
2024-05-09 10:32:03 -04:00
private bool _isEnabled ;
private Func < string , int > SendConfigMSG { get ; }
public AdvancedPasteViewModel (
ISettingsUtils settingsUtils ,
ISettingsRepository < GeneralSettings > settingsRepository ,
ISettingsRepository < AdvancedPasteSettings > advancedPasteSettingsRepository ,
Func < string , int > ipcMSGCallBackFunc )
{
// To obtain the general settings configurations of PowerToys Settings.
ArgumentNullException . ThrowIfNull ( settingsRepository ) ;
GeneralSettingsConfig = settingsRepository . SettingsConfig ;
// To obtain the settings configurations of Fancy zones.
ArgumentNullException . ThrowIfNull ( settingsRepository ) ;
_settingsUtils = settingsUtils ? ? throw new ArgumentNullException ( nameof ( settingsUtils ) ) ;
ArgumentNullException . ThrowIfNull ( advancedPasteSettingsRepository ) ;
_advancedPasteSettings = advancedPasteSettingsRepository . SettingsConfig ;
2024-10-18 15:34:09 +02:00
_additionalActions = _advancedPasteSettings . Properties . AdditionalActions ;
2024-08-22 16:17:12 +02:00
_customActions = _advancedPasteSettings . Properties . CustomActions . Value ;
2024-05-09 10:32:03 -04:00
InitializeEnabledValue ( ) ;
// set the callback functions value to handle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc ;
_delayedTimer = new Timer ( ) ;
_delayedTimer . Interval = SaveSettingsDelayInMs ;
_delayedTimer . Elapsed + = DelayedTimer_Tick ;
_delayedTimer . AutoReset = false ;
2024-08-22 16:17:12 +02:00
2025-02-25 22:33:39 +01:00
foreach ( var action in _additionalActions . GetAllActions ( ) )
2024-10-18 15:34:09 +02:00
{
action . PropertyChanged + = OnAdditionalActionPropertyChanged ;
}
2024-08-22 16:17:12 +02:00
foreach ( var customAction in _customActions )
{
customAction . PropertyChanged + = OnCustomActionPropertyChanged ;
}
_customActions . CollectionChanged + = OnCustomActionsCollectionChanged ;
UpdateCustomActionsCanMoveUpDown ( ) ;
2024-05-09 10:32:03 -04:00
}
2025-08-20 09:31:52 +08:00
public override Dictionary < string , HotkeySettings [ ] > GetAllHotkeySettings ( )
{
var hotkeySettings = new List < HotkeySettings >
{
PasteAsPlainTextShortcut ,
AdvancedPasteUIShortcut ,
PasteAsMarkdownShortcut ,
PasteAsJsonShortcut ,
} ;
foreach ( var action in _additionalActions . GetAllActions ( ) )
{
if ( action is AdvancedPasteAdditionalAction additionalAction )
{
hotkeySettings . Add ( additionalAction . Shortcut ) ;
}
}
// Custom actions do not have localization header, just use the action name.
foreach ( var customAction in _customActions )
{
hotkeySettings . Add ( customAction . Shortcut ) ;
}
return new Dictionary < string , HotkeySettings [ ] >
{
[ModuleName] = hotkeySettings . ToArray ( ) ,
} ;
}
2024-05-09 10:32:03 -04:00
private void InitializeEnabledValue ( )
{
_enabledGpoRuleConfiguration = GPOWrapper . GetConfiguredAdvancedPasteEnabledValue ( ) ;
if ( _enabledGpoRuleConfiguration = = GpoRuleConfigured . Disabled | | _enabledGpoRuleConfiguration = = GpoRuleConfigured . Enabled )
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true ;
_isEnabled = _enabledGpoRuleConfiguration = = GpoRuleConfigured . Enabled ;
}
else
{
_isEnabled = GeneralSettingsConfig . Enabled . AdvancedPaste ;
}
2024-05-26 13:22:50 +02:00
_onlineAIModelsGpoRuleConfiguration = GPOWrapper . GetAllowedAdvancedPasteOnlineAIModelsValue ( ) ;
if ( _onlineAIModelsGpoRuleConfiguration = = GpoRuleConfigured . Disabled )
{
_onlineAIModelsDisallowedByGPO = true ;
// disable AI if it was enabled
DisableAI ( ) ;
}
2024-05-09 10:32:03 -04:00
}
public bool IsEnabled
{
get = > _isEnabled ;
set
{
if ( _enabledStateIsGPOConfigured )
{
// If it's GPO configured, shouldn't be able to change this state.
return ;
}
if ( _isEnabled ! = value )
{
_isEnabled = value ;
OnPropertyChanged ( nameof ( IsEnabled ) ) ;
2024-06-08 22:56:01 +02:00
OnPropertyChanged ( nameof ( ShowOnlineAIModelsGpoConfiguredInfoBar ) ) ;
OnPropertyChanged ( nameof ( ShowClipboardHistoryIsGpoConfiguredInfoBar ) ) ;
2024-05-09 10:32:03 -04:00
// Set the status of AdvancedPaste in the general settings
GeneralSettingsConfig . Enabled . AdvancedPaste = value ;
var outgoing = new OutGoingGeneralSettings ( GeneralSettingsConfig ) ;
SendConfigMSG ( outgoing . ToString ( ) ) ;
}
}
}
2024-08-22 16:17:12 +02:00
public ObservableCollection < AdvancedPasteCustomAction > CustomActions = > _customActions ;
2024-10-18 15:34:09 +02:00
public AdvancedPasteAdditionalActions AdditionalActions = > _additionalActions ;
2024-05-09 10:32:03 -04:00
private bool OpenAIKeyExists ( )
{
PasswordVault vault = new PasswordVault ( ) ;
PasswordCredential cred = null ;
try
{
cred = vault . Retrieve ( "https://platform.openai.com/api-keys" , "PowerToys_AdvancedPaste_OpenAIKey" ) ;
}
catch ( Exception )
{
return false ;
}
return cred is not null ;
}
2024-05-26 13:22:50 +02:00
public bool IsOpenAIEnabled = > OpenAIKeyExists ( ) & & ! IsOnlineAIModelsDisallowedByGPO ;
2024-05-09 10:32:03 -04:00
public bool IsEnabledGpoConfigured
{
get = > _enabledStateIsGPOConfigured ;
}
2024-05-26 13:22:50 +02:00
public bool IsOnlineAIModelsDisallowedByGPO
{
get = > _onlineAIModelsDisallowedByGPO | | _enabledGpoRuleConfiguration = = GpoRuleConfigured . Disabled ;
}
public bool ShowOnlineAIModelsGpoConfiguredInfoBar
{
2024-06-08 22:56:01 +02:00
get = > _onlineAIModelsDisallowedByGPO & & _isEnabled ;
2024-05-26 13:22:50 +02:00
}
2024-05-09 10:32:03 -04:00
private bool IsClipboardHistoryEnabled ( )
{
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Clipboard\" ;
try
{
int enableClipboardHistory = ( int ) Registry . GetValue ( registryKey , "EnableClipboardHistory" , false ) ;
return enableClipboardHistory ! = 0 ;
}
catch ( Exception )
{
return false ;
}
}
2024-05-24 00:19:01 +01:00
private bool IsClipboardHistoryDisabledByGPO ( )
{
string registryKey = @"HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\System\" ;
try
{
object allowClipboardHistory = Registry . GetValue ( registryKey , "AllowClipboardHistory" , null ) ;
if ( allowClipboardHistory ! = null )
{
return ( int ) allowClipboardHistory = = 0 ;
}
else
{
return false ;
}
}
catch ( Exception )
{
return false ;
}
}
2024-05-09 10:32:03 -04:00
private void SetClipboardHistoryEnabled ( bool value )
{
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Clipboard\" ;
try
{
Registry . SetValue ( registryKey , "EnableClipboardHistory" , value ? 1 : 0 ) ;
}
catch ( Exception )
{
}
}
public bool ClipboardHistoryEnabled
{
get = > IsClipboardHistoryEnabled ( ) ;
set
{
if ( IsClipboardHistoryEnabled ( ) ! = value )
{
SetClipboardHistoryEnabled ( value ) ;
}
}
}
2024-05-24 00:19:01 +01:00
public bool ClipboardHistoryDisabledByGPO
{
get = > IsClipboardHistoryDisabledByGPO ( ) ;
}
2024-06-08 22:56:01 +02:00
public bool ShowClipboardHistoryIsGpoConfiguredInfoBar
{
get = > IsClipboardHistoryDisabledByGPO ( ) & & _isEnabled ;
}
2024-05-09 10:32:03 -04:00
public HotkeySettings AdvancedPasteUIShortcut
{
get = > _advancedPasteSettings . Properties . AdvancedPasteUIShortcut ;
set
{
if ( _advancedPasteSettings . Properties . AdvancedPasteUIShortcut ! = value )
{
_advancedPasteSettings . Properties . AdvancedPasteUIShortcut = value ? ? AdvancedPasteProperties . DefaultAdvancedPasteUIShortcut ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2025-08-20 09:31:52 +08:00
OnPropertyChanged ( nameof ( AdvancedPasteUIShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
public HotkeySettings PasteAsPlainTextShortcut
{
get = > _advancedPasteSettings . Properties . PasteAsPlainTextShortcut ;
set
{
if ( _advancedPasteSettings . Properties . PasteAsPlainTextShortcut ! = value )
{
_advancedPasteSettings . Properties . PasteAsPlainTextShortcut = value ? ? AdvancedPasteProperties . DefaultPasteAsPlainTextShortcut ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2025-08-20 09:31:52 +08:00
OnPropertyChanged ( nameof ( PasteAsPlainTextShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
public HotkeySettings PasteAsMarkdownShortcut
{
get = > _advancedPasteSettings . Properties . PasteAsMarkdownShortcut ;
set
{
if ( _advancedPasteSettings . Properties . PasteAsMarkdownShortcut ! = value )
{
_advancedPasteSettings . Properties . PasteAsMarkdownShortcut = value ? ? new HotkeySettings ( ) ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2025-08-20 09:31:52 +08:00
OnPropertyChanged ( nameof ( PasteAsMarkdownShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
public HotkeySettings PasteAsJsonShortcut
{
get = > _advancedPasteSettings . Properties . PasteAsJsonShortcut ;
set
{
if ( _advancedPasteSettings . Properties . PasteAsJsonShortcut ! = value )
{
_advancedPasteSettings . Properties . PasteAsJsonShortcut = value ? ? new HotkeySettings ( ) ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2025-08-20 09:31:52 +08:00
OnPropertyChanged ( nameof ( PasteAsJsonShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
2024-12-11 10:28:44 +01:00
public bool IsAdvancedAIEnabled
{
get = > _advancedPasteSettings . Properties . IsAdvancedAIEnabled ;
set
{
if ( value ! = _advancedPasteSettings . Properties . IsAdvancedAIEnabled )
{
_advancedPasteSettings . Properties . IsAdvancedAIEnabled = value ;
OnPropertyChanged ( nameof ( IsAdvancedAIEnabled ) ) ;
NotifySettingsChanged ( ) ;
}
}
}
2024-05-09 10:32:03 -04:00
public bool ShowCustomPreview
{
get = > _advancedPasteSettings . Properties . ShowCustomPreview ;
set
{
if ( value ! = _advancedPasteSettings . Properties . ShowCustomPreview )
{
_advancedPasteSettings . Properties . ShowCustomPreview = value ;
NotifySettingsChanged ( ) ;
}
}
}
2024-06-24 16:03:46 +02:00
public bool CloseAfterLosingFocus
{
get = > _advancedPasteSettings . Properties . CloseAfterLosingFocus ;
set
{
if ( value ! = _advancedPasteSettings . Properties . CloseAfterLosingFocus )
{
_advancedPasteSettings . Properties . CloseAfterLosingFocus = value ;
NotifySettingsChanged ( ) ;
}
}
}
2024-10-18 15:34:09 +02:00
public bool IsConflictingCopyShortcut = >
_customActions . Select ( customAction = > customAction . Shortcut )
. Concat ( [ PasteAsPlainTextShortcut , AdvancedPasteUIShortcut , PasteAsMarkdownShortcut , PasteAsJsonShortcut ] )
. Any ( hotkey = > WarnHotkeys . Contains ( hotkey . ToString ( ) ) ) ;
public bool IsAdditionalActionConflictingCopyShortcut = >
2025-02-25 22:33:39 +01:00
_additionalActions . GetAllActions ( )
2024-10-18 15:34:09 +02:00
. OfType < AdvancedPasteAdditionalAction > ( )
. Select ( additionalAction = > additionalAction . Shortcut )
. Any ( hotkey = > WarnHotkeys . Contains ( hotkey . ToString ( ) ) ) ;
2024-05-09 10:32:03 -04:00
private void DelayedTimer_Tick ( object sender , EventArgs e )
{
lock ( _delayedActionLock )
{
_delayedTimer . Stop ( ) ;
NotifySettingsChanged ( ) ;
}
}
private void NotifySettingsChanged ( )
{
// Using InvariantCulture as this is an IPC message
SendConfigMSG (
2024-06-24 16:03:46 +02:00
string . Format (
CultureInfo . InvariantCulture ,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}" ,
AdvancedPasteSettings . ModuleName ,
2025-02-25 02:48:54 +08:00
JsonSerializer . Serialize ( _advancedPasteSettings , SourceGenerationContextContext . Default . AdvancedPasteSettings ) ) ) ;
2024-05-09 10:32:03 -04:00
}
public void RefreshEnabledState ( )
{
InitializeEnabledValue ( ) ;
OnPropertyChanged ( nameof ( IsEnabled ) ) ;
2024-06-08 22:56:01 +02:00
OnPropertyChanged ( nameof ( ShowOnlineAIModelsGpoConfiguredInfoBar ) ) ;
OnPropertyChanged ( nameof ( ShowClipboardHistoryIsGpoConfiguredInfoBar ) ) ;
2024-05-09 10:32:03 -04:00
}
2025-08-20 09:31:52 +08:00
protected override void Dispose ( bool disposing )
2024-05-09 10:32:03 -04:00
{
2025-08-20 09:31:52 +08:00
if ( ! _disposed )
2024-05-09 10:32:03 -04:00
{
if ( disposing )
{
2025-08-20 09:31:52 +08:00
_delayedTimer ? . Dispose ( ) ;
foreach ( var action in _additionalActions . GetAllActions ( ) )
{
action . PropertyChanged - = OnAdditionalActionPropertyChanged ;
}
foreach ( var customAction in _customActions )
{
customAction . PropertyChanged - = OnCustomActionPropertyChanged ;
}
_customActions . CollectionChanged - = OnCustomActionsCollectionChanged ;
2024-05-09 10:32:03 -04:00
}
2025-08-20 09:31:52 +08:00
_disposed = true ;
2024-05-09 10:32:03 -04:00
}
2025-08-20 09:31:52 +08:00
base . Dispose ( disposing ) ;
2024-05-09 10:32:03 -04:00
}
internal void DisableAI ( )
{
2024-05-26 13:22:50 +02:00
try
{
PasswordVault vault = new PasswordVault ( ) ;
PasswordCredential cred = vault . Retrieve ( "https://platform.openai.com/api-keys" , "PowerToys_AdvancedPaste_OpenAIKey" ) ;
vault . Remove ( cred ) ;
OnPropertyChanged ( nameof ( IsOpenAIEnabled ) ) ;
2024-09-24 19:16:20 +02:00
NotifySettingsChanged ( ) ;
2024-05-26 13:22:50 +02:00
}
catch ( Exception )
{
}
2024-05-09 10:32:03 -04:00
}
internal void EnableAI ( string password )
{
2024-05-26 13:22:50 +02:00
try
{
2024-12-11 10:28:44 +01:00
PasswordVault vault = new ( ) ;
PasswordCredential cred = new ( "https://platform.openai.com/api-keys" , "PowerToys_AdvancedPaste_OpenAIKey" , password ) ;
2024-05-26 13:22:50 +02:00
vault . Add ( cred ) ;
OnPropertyChanged ( nameof ( IsOpenAIEnabled ) ) ;
2024-12-11 10:28:44 +01:00
IsAdvancedAIEnabled = true ; // new users should get Semantic Kernel benefits immediately
2024-09-24 19:16:20 +02:00
NotifySettingsChanged ( ) ;
2024-05-26 13:22:50 +02:00
}
catch ( Exception )
{
}
2024-05-09 10:32:03 -04:00
}
2024-08-22 16:17:12 +02:00
internal AdvancedPasteCustomAction GetNewCustomAction ( string namePrefix )
{
ArgumentException . ThrowIfNullOrEmpty ( namePrefix ) ;
var maxUsedPrefix = _customActions . Select ( customAction = > customAction . Name )
. Where ( name = > name . StartsWith ( namePrefix , StringComparison . InvariantCulture ) )
. Select ( name = > int . TryParse ( name . AsSpan ( namePrefix . Length ) , out int number ) ? number : 0 )
. DefaultIfEmpty ( 0 )
. Max ( ) ;
var maxUsedId = _customActions . Select ( customAction = > customAction . Id )
. DefaultIfEmpty ( - 1 )
. Max ( ) ;
return new ( )
{
Id = maxUsedId + 1 ,
Name = $"{namePrefix} {maxUsedPrefix + 1}" ,
IsShown = true ,
} ;
}
internal void AddCustomAction ( AdvancedPasteCustomAction customAction )
{
if ( _customActions . Any ( existingCustomAction = > existingCustomAction . Id = = customAction . Id ) )
{
throw new ArgumentException ( "Duplicate custom action" , nameof ( customAction ) ) ;
}
_customActions . Add ( customAction ) ;
}
internal void DeleteCustomAction ( AdvancedPasteCustomAction customAction ) = > _customActions . Remove ( customAction ) ;
private void SaveCustomActions ( ) = > SaveAndNotifySettings ( ) ;
private void SaveAndNotifySettings ( )
{
_settingsUtils . SaveSettings ( _advancedPasteSettings . ToJsonString ( ) , AdvancedPasteSettings . ModuleName ) ;
NotifySettingsChanged ( ) ;
}
2024-10-18 15:34:09 +02:00
private void OnAdditionalActionPropertyChanged ( object sender , PropertyChangedEventArgs e )
{
SaveAndNotifySettings ( ) ;
if ( e . PropertyName = = nameof ( AdvancedPasteAdditionalAction . Shortcut ) )
{
OnPropertyChanged ( nameof ( IsAdditionalActionConflictingCopyShortcut ) ) ;
}
}
2024-08-22 16:17:12 +02:00
private void OnCustomActionPropertyChanged ( object sender , PropertyChangedEventArgs e )
{
if ( typeof ( AdvancedPasteCustomAction ) . GetProperty ( e . PropertyName ) . GetCustomAttribute < JsonIgnoreAttribute > ( ) = = null )
{
SaveCustomActions ( ) ;
}
if ( e . PropertyName = = nameof ( AdvancedPasteCustomAction . Shortcut ) )
{
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
}
}
private void OnCustomActionsCollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
{
void AddRange ( System . Collections . IList items )
{
foreach ( AdvancedPasteCustomAction item in items )
{
item . PropertyChanged + = OnCustomActionPropertyChanged ;
}
}
void RemoveRange ( System . Collections . IList items )
{
foreach ( AdvancedPasteCustomAction item in items )
{
item . PropertyChanged - = OnCustomActionPropertyChanged ;
}
}
switch ( e . Action )
{
case NotifyCollectionChangedAction . Add :
AddRange ( e . NewItems ) ;
break ;
case NotifyCollectionChangedAction . Remove :
RemoveRange ( e . OldItems ) ;
break ;
case NotifyCollectionChangedAction . Replace :
AddRange ( e . NewItems ) ;
RemoveRange ( e . OldItems ) ;
break ;
case NotifyCollectionChangedAction . Move :
break ;
default :
throw new ArgumentException ( $"Unsupported {nameof(e.Action)} {e.Action}" , nameof ( e ) ) ;
}
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
UpdateCustomActionsCanMoveUpDown ( ) ;
SaveCustomActions ( ) ;
}
private void UpdateCustomActionsCanMoveUpDown ( )
{
for ( int index = 0 ; index < _customActions . Count ; index + + )
{
var customAction = _customActions [ index ] ;
customAction . CanMoveUp = index ! = 0 ;
customAction . CanMoveDown = index ! = _customActions . Count - 1 ;
}
}
2024-05-09 10:32:03 -04:00
}
}