Преглед изворни кода

add api to allow text input method to trigger common context menu actions (#15666)

Emmanuel Hansen пре 1 година
родитељ
комит
a7f7d80019

+ 14 - 0
src/Avalonia.Base/Input/TextInput/TextInputMethodClient.cs

@@ -64,6 +64,12 @@ namespace Avalonia.Input.TextInput
         /// </summary>
         public virtual void SetPreeditText(string? preeditText) { }
 
+        /// <summary>
+        /// Execute specific context menu actions
+        /// </summary>
+        /// <param name="action">The <see cref="ContextMenuAction"/> to perform</param>
+        public virtual void ExecuteContextMenuAction(ContextMenuAction action) { }
+
         /// <summary>
         /// Sets the non-committed input string and cursor offset in that string
         /// </summary>
@@ -99,4 +105,12 @@ namespace Avalonia.Input.TextInput
     }
 
     public record struct TextSelection(int Start, int End);
+
+    public enum ContextMenuAction
+    {
+        Copy,
+        Cut,
+        Paste,
+        SelectAll
+    }
 }

+ 21 - 0
src/Avalonia.Controls/TextBoxTextInputMethodClient.cs

@@ -184,6 +184,27 @@ namespace Avalonia.Controls
             return lineText;
         }
 
+        public override void ExecuteContextMenuAction(ContextMenuAction action)
+        {
+            base.ExecuteContextMenuAction(action);
+
+            switch (action)
+            {
+                case ContextMenuAction.Copy:
+                    _parent?.Copy();
+                    break;
+                case ContextMenuAction.Cut:
+                    _parent?.Cut();
+                    break;
+                case ContextMenuAction.Paste:
+                    _parent?.Paste();
+                    break;
+                case ContextMenuAction.SelectAll:
+                    _parent?.SelectAll();
+                    break;
+            }
+        }
+
         private void OnParentPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
         {
             if (e.Property == TextBox.TextProperty)