-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Internal text input rework #2077
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
|
||
namespace Avalonia.Controls | ||
{ | ||
public class TextBox : TemplatedControl, UndoRedoHelper<TextBox.UndoRedoState>.IUndoRedoHost | ||
public class TextBox : TemplatedControl, UndoRedoHelper<TextBox.UndoRedoState>.IUndoRedoHost, | ||
ITextInputHandler | ||
{ | ||
public static readonly StyledProperty<bool> AcceptsReturnProperty = | ||
AvaloniaProperty.Register<TextBox, bool>(nameof(AcceptsReturn)); | ||
|
@@ -308,13 +309,20 @@ protected override void OnLostFocus(RoutedEventArgs e) | |
_presenter?.HideCaret(); | ||
} | ||
|
||
protected override void OnTextInput(TextInputEventArgs e) | ||
protected override void OnTextInputHandlerSelection(TextInputHandlerSelectionEventArgs e) | ||
{ | ||
if (!e.Handled) | ||
{ | ||
HandleTextInput(e.Text); | ||
e.Handler = this; | ||
e.Handled = true; | ||
} | ||
base.OnTextInputHandlerSelection(e); | ||
} | ||
|
||
void ITextInputHandler.OnTextEntered (uint timestamp, string text) | ||
{ | ||
if (!RaiseCompatibleTextInput(text)) | ||
HandleTextInput(text); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
private void HandleTextInput(string input) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Avalonia.Input | ||
{ | ||
public interface ITextInputHandler | ||
{ | ||
void OnTextEntered(uint timestamp, string text); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Avalonia.Interactivity; | ||
using Avalonia.VisualTree; | ||
|
||
|
@@ -85,11 +86,11 @@ public class InputElement : Interactive, IInputElement | |
RoutingStrategies.Tunnel | RoutingStrategies.Bubble); | ||
|
||
/// <summary> | ||
/// Defines the <see cref="TextInput"/> event. | ||
/// Defines the <see cref=" TextInputHandlerSelection"/> event. | ||
/// </summary> | ||
public static readonly RoutedEvent<TextInputEventArgs> TextInputEvent = | ||
RoutedEvent.Register<InputElement, TextInputEventArgs>( | ||
"TextInput", | ||
public static readonly RoutedEvent<TextInputHandlerSelectionEventArgs> TextInputHandlerSelectionEvent = | ||
RoutedEvent.Register<InputElement, TextInputHandlerSelectionEventArgs>( | ||
"TextInputHandlerSelection", | ||
RoutingStrategies.Tunnel | RoutingStrategies.Bubble); | ||
|
||
/// <summary> | ||
|
@@ -160,7 +161,7 @@ static InputElement() | |
LostFocusEvent.AddClassHandler<InputElement>(x => x.OnLostFocus); | ||
KeyDownEvent.AddClassHandler<InputElement>(x => x.OnKeyDown); | ||
KeyUpEvent.AddClassHandler<InputElement>(x => x.OnKeyUp); | ||
TextInputEvent.AddClassHandler<InputElement>(x => x.OnTextInput); | ||
TextInputHandlerSelectionEvent.AddClassHandler<InputElement>(x => x.OnTextInputHandlerSelection); | ||
PointerEnterEvent.AddClassHandler<InputElement>(x => x.OnPointerEnterCore); | ||
PointerLeaveEvent.AddClassHandler<InputElement>(x => x.OnPointerLeaveCore); | ||
PointerMovedEvent.AddClassHandler<InputElement>(x => x.OnPointerMoved); | ||
|
@@ -210,12 +211,12 @@ public event EventHandler<KeyEventArgs> KeyUp | |
} | ||
|
||
/// <summary> | ||
/// Occurs when a user typed some text while the control has focus. | ||
/// Occurs when text input system asks for a handler to be selected | ||
/// </summary> | ||
public event EventHandler<TextInputEventArgs> TextInput | ||
public event EventHandler<TextInputHandlerSelectionEventArgs> TextInputHandlerSelection | ||
{ | ||
add { AddHandler(TextInputEvent, value); } | ||
remove { RemoveHandler(TextInputEvent, value); } | ||
add { AddHandler(TextInputHandlerSelectionEvent, value); } | ||
remove { RemoveHandler(TextInputHandlerSelectionEvent, value); } | ||
} | ||
|
||
/// <summary> | ||
|
@@ -430,14 +431,6 @@ protected virtual void OnKeyUp(KeyEventArgs e) | |
{ | ||
} | ||
|
||
/// <summary> | ||
/// Called before the <see cref="TextInput"/> event occurs. | ||
/// </summary> | ||
/// <param name="e">The event args.</param> | ||
protected virtual void OnTextInput(TextInputEventArgs e) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Called before the <see cref="PointerEnter"/> event occurs. | ||
/// </summary> | ||
|
@@ -540,5 +533,121 @@ private void UpdateIsEnabledCore(InputElement parent) | |
child.UpdateIsEnabledCore(this); | ||
} | ||
} | ||
|
||
#region OnTextInput compatibility layer | ||
|
||
private static Dictionary<Type, bool> s_compatibleTextInputRegistry = new Dictionary<Type, bool>(); | ||
private CompatibleTextInputHandler _compatibleTextInputHandler; | ||
class CompatibleTextInputHandler : ITextInputHandler | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is any more than WIP code it should be moved to a separate class rather than putting it in a |
||
{ | ||
private readonly InputElement _parent; | ||
|
||
public CompatibleTextInputHandler(InputElement parent) | ||
{ | ||
_parent = parent; | ||
} | ||
|
||
public EventHandler<TextInputEventArgs> Event; | ||
public void OnTextEntered(uint timestamp, string text) | ||
{ | ||
_parent.RaiseCompatibleTextInput(text); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Called before the <see cref="TextInputHandlerSelection"/> event occurs. | ||
/// </summary> | ||
/// <param name="e">The event args.</param> | ||
protected virtual void OnTextInputHandlerSelection(TextInputHandlerSelectionEventArgs e) | ||
{ | ||
if (_compatibleTextInputHandler != null && !e.Handled) | ||
{ | ||
e.Handler = _compatibleTextInputHandler; | ||
e.Handled = true; | ||
} | ||
} | ||
|
||
[Obsolete("Use TextInputHandlerSelection")] | ||
public event EventHandler<TextInputEventArgs> TextInput | ||
{ | ||
add | ||
{ | ||
if (_compatibleTextInputHandler == null) | ||
{ | ||
_compatibleTextInputHandler = new CompatibleTextInputHandler(this); | ||
_compatibleTextInputHandler.Event += value; | ||
} | ||
} | ||
remove | ||
{ | ||
if (_compatibleTextInputHandler != null) | ||
{ | ||
_compatibleTextInputHandler.Event -= value; | ||
if (_compatibleTextInputHandler.Event == null) | ||
_compatibleTextInputHandler = null; | ||
} | ||
} | ||
} | ||
|
||
protected internal bool RaiseCompatibleTextInput(string text) | ||
{ | ||
if (_compatibleTextInputHandler != null) | ||
{ | ||
var ev = new TextInputEventArgs | ||
{ | ||
Text = text, | ||
Source = this | ||
}; | ||
_compatibleTextInputHandler.Event?.Invoke(this, ev); | ||
if(!ev.Handled) | ||
OnTextInput(ev); | ||
return ev.Handled; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
[Obsolete("Use TextInputHandlerSelection")] | ||
protected virtual void OnTextInput(TextInputEventArgs e) | ||
{ | ||
|
||
} | ||
|
||
static bool CheckIfOverridden(MethodInfo method, Type target) | ||
{ | ||
while (true) | ||
{ | ||
var baseMethod = method.GetBaseDefinition(); | ||
if(baseMethod == method || baseMethod == null) | ||
break; | ||
method = baseMethod; | ||
} | ||
|
||
var fromType = method.DeclaringType; | ||
while (fromType != target && target != null) | ||
{ | ||
var found = target.GetMethod(method.Name, | ||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, | ||
null, method.GetParameters().Select(p => p.ParameterType).ToArray(), null); | ||
if (found != null && found.DeclaringType != fromType) | ||
return true; | ||
|
||
target = target.BaseType; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public InputElement() | ||
{ | ||
var type = GetType(); | ||
if (!s_compatibleTextInputRegistry.TryGetValue(type, out var needsCompat)) | ||
s_compatibleTextInputRegistry[type] = needsCompat = | ||
CheckIfOverridden(new Action<TextInputEventArgs>(OnTextInput).Method, type); | ||
if (needsCompat) | ||
TextInput += (o, e) => OnTextInput(e); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Avalonia.Interactivity; | ||
|
||
namespace Avalonia.Input | ||
{ | ||
public class TextInputHandlerSelectionEventArgs : RoutedEventArgs | ||
{ | ||
public ITextInputHandler Handler { get; set; } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What scenario does this cover that simply trying to cast the control to
ITextInputHandler
doesn't cover? Is it to allow a user-level override of the text input handler, i.e. someone writing an application could choose anotherITextInputHandler
for aTextBox
?If so, what other handlers do you forsee for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What control exactly? We could try to check for the currently focused control, but I'd like to have a way for more complex scenario. E. g. some kind of inline completion list taking focus for handling arrow keys while delegating text input to the parent control