AdvancedPasteSettings.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; 8 using System.Text.Json.Serialization; 9 using ManagedCommon; 10 using Microsoft.PowerToys.Settings.UI.Library.Helpers; 11 using Microsoft.PowerToys.Settings.UI.Library.Interfaces; 12 13 namespace Microsoft.PowerToys.Settings.UI.Library 14 { 15 public class AdvancedPasteSettings : BasePTModuleSettings, ISettingsConfig, IHotkeyConfig 16 { 17 public const string ModuleName = "AdvancedPaste"; 18 19 private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions 20 { 21 WriteIndented = true, 22 }; 23 24 [JsonPropertyName("properties")] 25 public AdvancedPasteProperties Properties { get; set; } 26 27 public AdvancedPasteSettings() 28 { 29 Properties = new AdvancedPasteProperties(); 30 Version = "1"; 31 Name = ModuleName; 32 } 33 34 public virtual void Save(SettingsUtils settingsUtils) 35 { 36 // Save settings to file 37 var options = _serializerOptions; 38 39 ArgumentNullException.ThrowIfNull(settingsUtils); 40 41 settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName); 42 } 43 44 public ModuleType GetModuleType() => ModuleType.AdvancedPaste; 45 46 public HotkeyAccessor[] GetAllHotkeyAccessors() 47 { 48 var hotkeyAccessors = new List<HotkeyAccessor> 49 { 50 new HotkeyAccessor( 51 () => Properties.PasteAsPlainTextShortcut, 52 value => Properties.PasteAsPlainTextShortcut = value ?? AdvancedPasteProperties.DefaultPasteAsPlainTextShortcut, 53 "PasteAsPlainText_Shortcut"), 54 new HotkeyAccessor( 55 () => Properties.AdvancedPasteUIShortcut, 56 value => Properties.AdvancedPasteUIShortcut = value ?? AdvancedPasteProperties.DefaultAdvancedPasteUIShortcut, 57 "AdvancedPasteUI_Shortcut"), 58 new HotkeyAccessor( 59 () => Properties.PasteAsMarkdownShortcut, 60 value => Properties.PasteAsMarkdownShortcut = value ?? new HotkeySettings(), 61 "PasteAsMarkdown_Shortcut"), 62 new HotkeyAccessor( 63 () => Properties.PasteAsJsonShortcut, 64 value => Properties.PasteAsJsonShortcut = value ?? new HotkeySettings(), 65 "PasteAsJson_Shortcut"), 66 }; 67 68 string[] additionalActionHeaderKeys = 69 [ 70 "ImageToText", 71 "PasteAsTxtFile", 72 "PasteAsPngFile", 73 "PasteAsHtmlFile", 74 "TranscodeToMp3", 75 "TranscodeToMp4", 76 ]; 77 int index = 0; 78 foreach (var action in Properties.AdditionalActions.GetAllActions()) 79 { 80 if (action is AdvancedPasteAdditionalAction additionalAction) 81 { 82 hotkeyAccessors.Add(new HotkeyAccessor( 83 () => additionalAction.Shortcut, 84 value => additionalAction.Shortcut = value ?? new HotkeySettings(), 85 additionalActionHeaderKeys[index])); 86 index++; 87 } 88 } 89 90 // Custom actions do not have localization header, just use the action name. 91 foreach (var customAction in Properties.CustomActions.Value) 92 { 93 hotkeyAccessors.Add(new HotkeyAccessor( 94 () => customAction.Shortcut, 95 value => customAction.Shortcut = value ?? new HotkeySettings(), 96 customAction.Name)); 97 } 98 99 return hotkeyAccessors.ToArray(); 100 } 101 102 public string GetModuleName() 103 => Name; 104 105 // This can be utilized in the future if the settings.json file is to be modified/deleted. 106 public bool UpgradeSettingsConfiguration() 107 => false; 108 } 109 }