Skip to content

Commit c7fe18a

Browse files
committed
fix: adjust arrow navigation in TextBox
1 parent b51bbe5 commit c7fe18a

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/Runtime/Runtime/System.Windows.Controls/TextBox.Navigation.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,46 @@ private bool NavigateInDirection(KeyEventArgs e)
9898
switch (e.Key)
9999
{
100100
case Key.Left:
101-
case Key.Up:
102101
return CaretPosition > 0;
103102

103+
case Key.Up:
104+
{
105+
int caretPosition = CaretPosition;
106+
if (caretPosition == 0)
107+
{
108+
return false;
109+
}
110+
111+
string text = _textViewHost.View.GetText();
112+
if (text.IndexOf('\n', 0, caretPosition) == -1 && text.IndexOf('\r', 0, caretPosition) == -1)
113+
{
114+
e.PreventDefault();
115+
e.Cancellable = false; // make sure that PreventDefault will not be called again
116+
return false;
117+
}
118+
return true;
119+
}
120+
104121
case Key.Right:
105-
case Key.Down:
106122
return CaretPosition < _textViewHost.View.GetText().Length;
123+
124+
case Key.Down:
125+
{
126+
int caretPosition = CaretPosition;
127+
string text = _textViewHost.View.GetText();
128+
if (caretPosition == text.Length)
129+
{
130+
return false;
131+
}
132+
133+
if (text.IndexOf('\n', caretPosition) == -1 && text.IndexOf('\r', caretPosition) == -1)
134+
{
135+
e.PreventDefault();
136+
e.Cancellable = false; // make sure that PreventDefault will not be called again
137+
return false;
138+
}
139+
return true;
140+
}
107141

108142
default:
109143
Debug.Assert(false);

src/Runtime/Runtime/System.Windows.input/InputManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,9 @@ private void ProcessOnKeyDown(UIElement uie, object jsEventArg)
373373

374374
keyboardTarget.RaiseEvent(e);
375375

376-
if (e.PreventDefault)
376+
if (e.Handled && e.Cancellable)
377377
{
378-
OpenSilver.Interop.ExecuteJavaScriptVoid(
379-
$"{CSHTML5.INTERNAL_InteropImplementation.GetVariableStringForJS(jsEventArg)}.preventDefault()");
378+
e.PreventDefault();
380379
}
381380
}
382381

src/Runtime/Runtime/System.Windows.input/KeyRoutedEventArgs.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ public bool Handled
6161

6262
internal bool Cancellable { get; set; } = true;
6363

64-
internal bool PreventDefault => Handled && Cancellable;
64+
internal void PreventDefault()
65+
{
66+
if (UIEventArg != null)
67+
{
68+
OpenSilver.Interop.ExecuteJavaScriptVoid(
69+
$"{INTERNAL_InteropImplementation.GetVariableStringForJS(UIEventArg)}.preventDefault();");
70+
}
71+
}
6572

6673
// Returns:
6774
// A system value that indicates the code for the key referenced by the event.

0 commit comments

Comments
 (0)