Skip to content

Commit b971a37

Browse files
emmaussmaxkatz6
authored andcommitted
add api to allow text input method to trigger common context menu actions (#15666)
1 parent 7b33bdc commit b971a37

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Avalonia.Base/Input/TextInput/TextInputMethodClient.cs

+14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public abstract class TextInputMethodClient
6464
/// </summary>
6565
public virtual void SetPreeditText(string? preeditText) { }
6666

67+
/// <summary>
68+
/// Execute specific context menu actions
69+
/// </summary>
70+
/// <param name="action">The <see cref="ContextMenuAction"/> to perform</param>
71+
public virtual void ExecuteContextMenuAction(ContextMenuAction action) { }
72+
6773
/// <summary>
6874
/// Sets the non-committed input string and cursor offset in that string
6975
/// </summary>
@@ -101,4 +107,12 @@ protected virtual void RequestReset()
101107
}
102108

103109
public record struct TextSelection(int Start, int End);
110+
111+
public enum ContextMenuAction
112+
{
113+
Copy,
114+
Cut,
115+
Paste,
116+
SelectAll
117+
}
104118
}

src/Avalonia.Controls/TextBoxTextInputMethodClient.cs

+21
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ private static string GetTextLineText(TextLine textLine)
201201
return lineText;
202202
}
203203

204+
public override void ExecuteContextMenuAction(ContextMenuAction action)
205+
{
206+
base.ExecuteContextMenuAction(action);
207+
208+
switch (action)
209+
{
210+
case ContextMenuAction.Copy:
211+
_parent?.Copy();
212+
break;
213+
case ContextMenuAction.Cut:
214+
_parent?.Cut();
215+
break;
216+
case ContextMenuAction.Paste:
217+
_parent?.Paste();
218+
break;
219+
case ContextMenuAction.SelectAll:
220+
_parent?.SelectAll();
221+
break;
222+
}
223+
}
224+
204225
private void OnParentPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
205226
{
206227
if (e.Property == TextBox.TextProperty)

0 commit comments

Comments
 (0)