AdvancedPasteAdditionalActions.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.Collections.Generic; 6 using System.Linq; 7 using System.Text.Json.Serialization; 8 9 namespace Microsoft.PowerToys.Settings.UI.Library; 10 11 public sealed class AdvancedPasteAdditionalActions 12 { 13 private AdvancedPasteAdditionalAction _imageToText = new(); 14 private AdvancedPastePasteAsFileAction _pasteAsFile = new(); 15 private AdvancedPasteTranscodeAction _transcode = new(); 16 17 public static class PropertyNames 18 { 19 public const string ImageToText = "image-to-text"; 20 public const string PasteAsFile = "paste-as-file"; 21 public const string Transcode = "transcode"; 22 } 23 24 [JsonPropertyName(PropertyNames.ImageToText)] 25 public AdvancedPasteAdditionalAction ImageToText 26 { 27 get => _imageToText; 28 init => _imageToText = value ?? new(); 29 } 30 31 [JsonPropertyName(PropertyNames.PasteAsFile)] 32 public AdvancedPastePasteAsFileAction PasteAsFile 33 { 34 get => _pasteAsFile; 35 init => _pasteAsFile = value ?? new(); 36 } 37 38 [JsonPropertyName(PropertyNames.Transcode)] 39 public AdvancedPasteTranscodeAction Transcode 40 { 41 get => _transcode; 42 init => _transcode = value ?? new(); 43 } 44 45 public IEnumerable<IAdvancedPasteAction> GetAllActions() 46 { 47 return GetAllActionsRecursive([ImageToText, PasteAsFile, Transcode]); 48 } 49 50 /// <summary> 51 /// Changed to depth-first traversal to ensure ordered output 52 /// </summary> 53 /// <param name="actions">The collection of actions to traverse</param> 54 /// <returns>All actions returned in depth-first order</returns> 55 private static IEnumerable<IAdvancedPasteAction> GetAllActionsRecursive(IEnumerable<IAdvancedPasteAction> actions) 56 { 57 foreach (var action in actions) 58 { 59 yield return action; 60 61 foreach (var subAction in GetAllActionsRecursive(action.SubActions)) 62 { 63 yield return subAction; 64 } 65 } 66 } 67 }