Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/Avalonia.Controls/MenuItemAccessKeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Avalonia.Controls
/// <summary>
/// Handles access keys within a <see cref="MenuItem"/>
/// </summary>
public class MenuItemAccessKeyHandler : IAccessKeyHandler
public class MenuItemAccessKeyHandler : IAccessKeyHandler, ITextInputHandler
{
/// <summary>
/// The registered access keys.
Expand Down Expand Up @@ -50,7 +50,16 @@ public void SetOwner(IInputRoot owner)

_owner = owner;

_owner.AddHandler(InputElement.TextInputEvent, OnTextInput);
_owner.AddHandler(InputElement.TextInputHandlerSelectionEvent, OnTextInputHandlerSelection, RoutingStrategies.Bubble);
}

private void OnTextInputHandlerSelection(object sender, TextInputHandlerSelectionEventArgs e)
{
if (!e.Handled && e.Handler == null)
{
e.Handler = this;
e.Handled = true;
}
}

/// <summary>
Expand Down Expand Up @@ -84,20 +93,18 @@ public void Unregister(IInputElement element)

/// <summary>
/// Handles a key being pressed in the menu.
/// We are currently using "text" input event, which we shouldn't
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnTextInput(object sender, TextInputEventArgs e)
// TODO: Replace this with KeyDown event
void ITextInputHandler.OnTextEntered (uint timestamp, string text)
{
if (!string.IsNullOrWhiteSpace(e.Text))
if (!string.IsNullOrWhiteSpace(text))
{
var text = e.Text.ToUpper();
text = text.ToUpperInvariant();
var focus = _registered
.FirstOrDefault(x => x.Item1 == text && x.Item2.IsEffectivelyVisible)?.Item2;

focus?.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));

e.Handled = true;
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/Avalonia.Controls/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -308,13 +309,20 @@ protected override void OnLostFocus(RoutedEventArgs e)
_presenter?.HideCaret();
}

protected override void OnTextInput(TextInputEventArgs e)
protected override void OnTextInputHandlerSelection(TextInputHandlerSelectionEventArgs e)
Copy link
Member

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 another ITextInputHandler for a TextBox?

If so, what other handlers do you forsee for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply trying to cast the control

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

{
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);
Copy link
Member

@grokys grokys Nov 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this handler call the old TextInput event by default? Ignore me, I see that's what it's doing.

}

private void HandleTextInput(string input)
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Diagnostics/ViewModels/EventOwnerTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class EventOwnerTreeNode : EventTreeNodeBase
Button.ClickEvent,
InputElement.KeyDownEvent,
InputElement.KeyUpEvent,
InputElement.TextInputEvent,
InputElement.TextInputHandlerSelectionEvent,
InputElement.PointerReleasedEvent,
InputElement.PointerPressedEvent,
};
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.Input/IInputElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public interface IInputElement : IInteractive, IVisual
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>
event EventHandler<TextInputEventArgs> TextInput;
event EventHandler<TextInputHandlerSelectionEventArgs> TextInputHandlerSelection;

/// <summary>
/// Occurs when the pointer enters the control.
Expand Down
7 changes: 7 additions & 0 deletions src/Avalonia.Input/ITextInputHandler.cs
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);
}
}
143 changes: 126 additions & 17 deletions src/Avalonia.Input/InputElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Avalonia.Interactivity;
using Avalonia.VisualTree;

Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 #region.

{
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
}
}
15 changes: 9 additions & 6 deletions src/Avalonia.Input/KeyboardDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,23 @@ public void ProcessRawEvent(RawInputEventArgs e)
}
}


// Compatibility layer with backends that still use RawTextInputEventArgs
var text = e as RawTextInputEventArgs;

if (text != null)
{
var ev = new TextInputEventArgs()
var ev = new TextInputHandlerSelectionEventArgs
{
Device = this,
Text = text.Text,
Source = element,
RoutedEvent = InputElement.TextInputEvent
RoutedEvent = InputElement.TextInputHandlerSelectionEvent
};

element.RaiseEvent(ev);
e.Handled = ev.Handled;
if (ev.Handled && ev.Handler != null)
{
ev.Handler.OnTextEntered(e.Timestamp, text.Text);
e.Handled = ev.Handled;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.Input/Raw/RawTextInputEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;

namespace Avalonia.Input.Raw
{
[Obsolete("Use RawTextInputHandlerSelectionEventArgs")]
public class RawTextInputEventArgs : RawInputEventArgs
{
public string Text { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.Input/TextInputEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;
using Avalonia.Interactivity;

namespace Avalonia.Input
{
[Obsolete("Use TextInputHandlerSelection")]
public class TextInputEventArgs : RoutedEventArgs
{
public IKeyboardDevice Device { get; set; }
Expand Down
9 changes: 9 additions & 0 deletions src/Avalonia.Input/TextInputHandlerSelectionEventArgs.cs
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; }
}
}
16 changes: 6 additions & 10 deletions tests/Avalonia.Controls.UnitTests/TextBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ public void Typing_Beginning_With_0_Should_Not_Modify_Text_When_Bound_To_Int()
Assert.Equal("0", target.Text);

target.CaretIndex = 1;
target.RaiseEvent(new TextInputEventArgs
{
RoutedEvent = InputElement.TextInputEvent,
Text = "2",
});

RaiseTextEvent(target, "2");
Assert.Equal("02", target.Text);
}
}
Expand Down Expand Up @@ -417,11 +412,12 @@ private void RaiseKeyEvent(TextBox textBox, Key key, InputModifiers inputModifie

private void RaiseTextEvent(TextBox textBox, string text)
{
textBox.RaiseEvent(new TextInputEventArgs
var ev = new TextInputHandlerSelectionEventArgs
{
RoutedEvent = InputElement.TextInputEvent,
Text = text
});
RoutedEvent = InputElement.TextInputHandlerSelectionEvent
};
textBox.RaiseEvent(ev);
ev.Handler?.OnTextEntered(0, text);
}

private class Class1 : NotifyingBase
Expand Down