From 92ba4bc19c8fb36f574f74ba5067d619df045188 Mon Sep 17 00:00:00 2001 From: nicola36631 Date: Sun, 2 Apr 2023 14:39:56 +0200 Subject: [PATCH] Added PageLeft, PageRight, PageUp, PageDown handling in TextBox Added cases in OnKeyDown method and added FetchChildScrollViewer method to fetch the child ScrollViewer in the VisualChildren. --- src/Avalonia.Controls/TextBox.cs | 92 ++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 9bb68ba419..9c4be3a907 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -19,6 +19,7 @@ using Avalonia.Media.TextFormatting.Unicode; using Avalonia.Automation.Peers; using Avalonia.Threading; using Avalonia.Platform; +using Avalonia.Collections; namespace Avalonia.Controls { @@ -1240,6 +1241,34 @@ namespace Avalonia.Controls selection = true; handled = true; } + else if (Match(keymap.PageLeft)) + { + MovePageLeft(); + movement = true; + selection = false; + handled = true; + } + else if (Match(keymap.PageRight)) + { + MovePageRight(); + movement = true; + selection = false; + handled = true; + } + else if (Match(keymap.PageUp)) + { + MovePageUp(); + movement = true; + selection = false; + handled = true; + } + else if (Match(keymap.PageDown)) + { + MovePageDown(); + movement = true; + selection = false; + handled = true; + } else { bool hasWholeWordModifiers = modifiers.HasAllFlags(keymap.WholeWordTextActionModifiers); @@ -1755,6 +1784,69 @@ namespace Avalonia.Controls } } + private void MovePageRight() + { + ScrollViewer? childScrollviewer = FetchChildScrollViewer(10, 0, this.VisualChildren); + if (childScrollviewer == null) + { + return; + } + childScrollviewer.PageRight(); + } + + private void MovePageLeft() + { + ScrollViewer? childScrollviewer = FetchChildScrollViewer(10, 0, this.VisualChildren); + if (childScrollviewer == null) + { + return; + } + childScrollviewer.PageLeft(); + } + private void MovePageUp() + { + ScrollViewer? childScrollviewer = FetchChildScrollViewer(10, 0, this.VisualChildren); + if (childScrollviewer == null) + { + return; + } + childScrollviewer.PageUp(); + } + + private void MovePageDown() + { + ScrollViewer? childScrollviewer = FetchChildScrollViewer(10, 0, this.VisualChildren); + if (childScrollviewer == null) + { + return; + } + childScrollviewer.PageDown(); + } + + private ScrollViewer? FetchChildScrollViewer(in int maxSearchDepth, int currentSearchDepth, IAvaloniaList visualChildren) + { + foreach(Visual innerChild in visualChildren) + { + if(innerChild is ScrollViewer) + { + return (ScrollViewer?)innerChild; + } + else + { + if (currentSearchDepth < maxSearchDepth) + { + ScrollViewer? innerScrollViewer = FetchChildScrollViewer(maxSearchDepth, currentSearchDepth + 1, innerChild.VisualChildren); + if (innerScrollViewer != null) + { + return innerScrollViewer; + } + } + } + } + + return null; + } + /// /// Select all text in the TextBox ///