/ src / settings-ui / Settings.UI.Library / AdvancedPasteCustomAction.cs
AdvancedPasteCustomAction.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System;
  6  using System.Collections.Generic;
  7  using System.Text.Json.Serialization;
  8  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
  9  using Microsoft.PowerToys.Settings.UI.Library.HotkeyConflicts;
 10  
 11  namespace Microsoft.PowerToys.Settings.UI.Library;
 12  
 13  public sealed class AdvancedPasteCustomAction : Observable, IAdvancedPasteAction, ICloneable
 14  {
 15      private int _id;
 16      private string _name = string.Empty;
 17      private string _description = string.Empty;
 18      private string _prompt = string.Empty;
 19      private HotkeySettings _shortcut = new();
 20      private bool _isShown;
 21      private bool _canMoveUp;
 22      private bool _canMoveDown;
 23      private bool _isValid;
 24      private bool _hasConflict;
 25      private string _tooltip;
 26  
 27      [JsonPropertyName("id")]
 28      public int Id
 29      {
 30          get => _id;
 31          set => Set(ref _id, value);
 32      }
 33  
 34      [JsonPropertyName("name")]
 35      public string Name
 36      {
 37          get => _name;
 38          set
 39          {
 40              if (Set(ref _name, value))
 41              {
 42                  UpdateIsValid();
 43              }
 44          }
 45      }
 46  
 47      [JsonPropertyName("description")]
 48      public string Description
 49      {
 50          get => _description;
 51          set => Set(ref _description, value ?? string.Empty);
 52      }
 53  
 54      [JsonPropertyName("prompt")]
 55      public string Prompt
 56      {
 57          get => _prompt;
 58          set
 59          {
 60              if (Set(ref _prompt, value))
 61              {
 62                  UpdateIsValid();
 63              }
 64          }
 65      }
 66  
 67      [JsonPropertyName("shortcut")]
 68      public HotkeySettings Shortcut
 69      {
 70          get => _shortcut;
 71          set
 72          {
 73              if (_shortcut != value)
 74              {
 75                  // We null-coalesce here rather than outside this branch as we want to raise PropertyChanged when the setter is called
 76                  // with null; the ShortcutControl depends on this.
 77                  _shortcut = value ?? new();
 78                  OnPropertyChanged();
 79              }
 80          }
 81      }
 82  
 83      [JsonPropertyName("isShown")]
 84      public bool IsShown
 85      {
 86          get => _isShown;
 87          set => Set(ref _isShown, value);
 88      }
 89  
 90      [JsonIgnore]
 91      public bool CanMoveUp
 92      {
 93          get => _canMoveUp;
 94          set => Set(ref _canMoveUp, value);
 95      }
 96  
 97      [JsonIgnore]
 98      public bool CanMoveDown
 99      {
100          get => _canMoveDown;
101          set => Set(ref _canMoveDown, value);
102      }
103  
104      [JsonIgnore]
105      public bool IsValid
106      {
107          get => _isValid;
108          private set => Set(ref _isValid, value);
109      }
110  
111      [JsonIgnore]
112      public bool HasConflict
113      {
114          get => _hasConflict;
115          set => Set(ref _hasConflict, value);
116      }
117  
118      [JsonIgnore]
119      public string Tooltip
120      {
121          get => _tooltip;
122          set => Set(ref _tooltip, value);
123      }
124  
125      [JsonIgnore]
126      public IEnumerable<IAdvancedPasteAction> SubActions => [];
127  
128      public object Clone()
129      {
130          AdvancedPasteCustomAction clone = new();
131          clone.Update(this);
132          return clone;
133      }
134  
135      public void Update(AdvancedPasteCustomAction other)
136      {
137          Id = other.Id;
138          Name = other.Name;
139          Description = other.Description;
140          Prompt = other.Prompt;
141          Shortcut = other.GetShortcutClone();
142          IsShown = other.IsShown;
143          CanMoveUp = other.CanMoveUp;
144          CanMoveDown = other.CanMoveDown;
145          HasConflict = other.HasConflict;
146          Tooltip = other.Tooltip;
147      }
148  
149      private HotkeySettings GetShortcutClone()
150      {
151          object shortcut = null;
152          if (Shortcut.TryToCmdRepresentable(out string shortcutString))
153          {
154              _ = HotkeySettings.TryParseFromCmd(shortcutString, out shortcut);
155          }
156  
157          return (shortcut as HotkeySettings) ?? new HotkeySettings();
158      }
159  
160      private void UpdateIsValid()
161      {
162          IsValid = !string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(Prompt);
163      }
164  }