From e8dd81fa201aa8030b79037c0196af6c1b979b87 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 9 Jun 2016 21:50:43 +0200 Subject: [PATCH] Support horiz keyboard movement. --- .../Presenters/ItemVirtualizerSimple.cs | 12 +++ ...emsPresenterTests_Virtualization_Simple.cs | 77 ++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs index 788ed10379..a6ca5dc18c 100644 --- a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs +++ b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs @@ -165,6 +165,18 @@ namespace Avalonia.Controls.Presenters break; } } + else + { + switch (direction) + { + case FocusNavigationDirection.Left: + newItemIndex = itemIndex - 1; + break; + case FocusNavigationDirection.Right: + newItemIndex = itemIndex + 1; + break; + } + } if (newItemIndex >= 0 && newItemIndex < ItemCount) { diff --git a/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs b/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs index 8c1b0cfa57..f70915ad3c 100644 --- a/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs +++ b/tests/Avalonia.Controls.UnitTests/Presenters/ItemsPresenterTests_Virtualization_Simple.cs @@ -427,7 +427,7 @@ namespace Avalonia.Controls.UnitTests.Presenters } [Fact] - public void GetControlInDirection_Up_Should_Scroll_If_Partially_Visible_Is_Currently_Shown() + public void GetControlInDirection_Up_Should_Scroll_If_Partially_Visible_Item_Is_Currently_Shown() { var target = CreateTarget(); @@ -446,6 +446,81 @@ namespace Avalonia.Controls.UnitTests.Presenters } } + public class Horizontal + { + [Fact] + public void GetControlInDirection_Right_Should_Return_Existing_Container_If_Materialized() + { + var target = CreateTarget(orientation: Orientation.Horizontal); + + target.ApplyTemplate(); + target.Measure(new Size(100, 100)); + target.Arrange(new Rect(0, 0, 100, 100)); + + var from = target.Panel.Children[5]; + var result = ((ILogicalScrollable)target).GetControlInDirection( + FocusNavigationDirection.Right, + from); + + Assert.Same(target.Panel.Children[6], result); + } + + [Fact] + public void GetControlInDirection_Right_Should_Scroll_If_Necessary() + { + var target = CreateTarget(orientation: Orientation.Horizontal); + + target.ApplyTemplate(); + target.Measure(new Size(100, 100)); + target.Arrange(new Rect(0, 0, 100, 100)); + + var from = target.Panel.Children[9]; + var result = ((ILogicalScrollable)target).GetControlInDirection( + FocusNavigationDirection.Right, + from); + + Assert.Equal(new Vector(1, 0), ((ILogicalScrollable)target).Offset); + Assert.Same(target.Panel.Children[9], result); + } + + [Fact] + public void GetControlInDirection_Right_Should_Scroll_If_Partially_Visible() + { + var target = CreateTarget(orientation: Orientation.Horizontal); + + target.ApplyTemplate(); + target.Measure(new Size(95, 100)); + target.Arrange(new Rect(0, 0, 95, 100)); + + var from = target.Panel.Children[8]; + var result = ((ILogicalScrollable)target).GetControlInDirection( + FocusNavigationDirection.Right, + from); + + Assert.Equal(new Vector(1, 0), ((ILogicalScrollable)target).Offset); + Assert.Same(target.Panel.Children[8], result); + } + + [Fact] + public void GetControlInDirection_Left_Should_Scroll_If_Partially_Visible_Item_Is_Currently_Shown() + { + var target = CreateTarget(orientation: Orientation.Horizontal); + + target.ApplyTemplate(); + target.Measure(new Size(95, 100)); + target.Arrange(new Rect(0, 0, 95, 100)); + ((ILogicalScrollable)target).Offset = new Vector(11, 0); + + var from = target.Panel.Children[1]; + var result = ((ILogicalScrollable)target).GetControlInDirection( + FocusNavigationDirection.Left, + from); + + Assert.Equal(new Vector(10, 0), ((ILogicalScrollable)target).Offset); + Assert.Same(target.Panel.Children[0], result); + } + } + public class WithContainers { [Fact]