From 95735d239b83c71e1c854827f1911e4aeccccd9f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 21 Apr 2020 16:17:52 +0200 Subject: [PATCH 001/169] Measure Window to MaxAutoSizeHint. - Renamed `MaxClientSize` to `MaxAutoSizeHint` - On Windows get its value from `WM_GETMINMAXINFO` message - Remove `ILayoutRoot.MaxClientSize` as it's not used any more --- src/Avalonia.Controls/Platform/IWindowBaseImpl.cs | 5 ++--- .../Presenters/ItemVirtualizerSimple.cs | 2 +- src/Avalonia.Controls/TopLevel.cs | 3 --- src/Avalonia.Controls/Window.cs | 10 ++++------ .../Remote/PreviewerWindowImpl.cs | 2 +- src/Avalonia.DesignerSupport/Remote/Stubs.cs | 2 +- src/Avalonia.Layout/ILayoutRoot.cs | 5 ----- src/Avalonia.Native/WindowImplBase.cs | 2 +- src/Avalonia.X11/X11Window.cs | 2 +- src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs | 2 ++ src/Windows/Avalonia.Win32/WindowImpl.cs | 12 ++---------- tests/Avalonia.Controls.UnitTests/WindowTests.cs | 13 ++++++++----- tests/Avalonia.UnitTests/MockWindowingPlatform.cs | 1 + 13 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs b/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs index 8c99dffc28..b190c4f2e7 100644 --- a/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs +++ b/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs @@ -1,5 +1,4 @@ using System; -using Avalonia.Controls; namespace Avalonia.Platform { @@ -46,9 +45,9 @@ namespace Avalonia.Platform IPlatformHandle Handle { get; } /// - /// Gets the maximum size of a window on the system. + /// Gets a maximum client size hint for an auto-sizing window, in device-independent pixels. /// - Size MaxClientSize { get; } + Size MaxAutoSizeHint { get; } /// /// Sets whether this window appears on top of all other windows diff --git a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs index b8d338741a..58fa8124db 100644 --- a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs +++ b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs @@ -113,7 +113,7 @@ namespace Avalonia.Controls.Presenters { var scrollable = (ILogicalScrollable)Owner; var visualRoot = Owner.GetVisualRoot(); - var maxAvailableSize = (visualRoot as WindowBase)?.PlatformImpl?.MaxClientSize + var maxAvailableSize = (visualRoot as WindowBase)?.PlatformImpl?.MaxAutoSizeHint ?? (visualRoot as TopLevel)?.ClientSize; // If infinity is passed as the available size and we're virtualized then we need to diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index d17f3b0423..0be051d65b 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -212,9 +212,6 @@ namespace Avalonia.Controls set { SetValue(AccessText.ShowAccessKeyProperty, value); } } - /// - Size ILayoutRoot.MaxClientSize => Size.Infinity; - /// double ILayoutRoot.LayoutScaling => PlatformImpl?.Scaling ?? 1; diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index ee596432f7..f985a52467 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -184,7 +184,7 @@ namespace Avalonia.Controls { impl.Closing = HandleClosing; impl.WindowStateChanged = HandleWindowStateChanged; - _maxPlatformClientSize = PlatformImpl?.MaxClientSize ?? default(Size); + _maxPlatformClientSize = PlatformImpl?.MaxAutoSizeHint ?? default(Size); this.GetObservable(ClientSizeProperty).Skip(1).Subscribe(x => PlatformImpl?.Resize(x)); PlatformImpl?.ShowTaskbarIcon(ShowInTaskbar); @@ -314,9 +314,6 @@ namespace Avalonia.Controls /// public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e) => PlatformImpl?.BeginResizeDrag(edge, e); - /// - Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize; - /// Type IStyleable.StyleKey => typeof(Window); @@ -572,15 +569,16 @@ namespace Avalonia.Controls var sizeToContent = SizeToContent; var clientSize = ClientSize; var constraint = clientSize; + var maxAutoSize = PlatformImpl?.MaxAutoSizeHint ?? Size.Infinity; if (sizeToContent.HasFlagCustom(SizeToContent.Width)) { - constraint = constraint.WithWidth(double.PositiveInfinity); + constraint = constraint.WithWidth(maxAutoSize.Width); } if (sizeToContent.HasFlagCustom(SizeToContent.Height)) { - constraint = constraint.WithHeight(double.PositiveInfinity); + constraint = constraint.WithHeight(maxAutoSize.Height); } var result = base.MeasureOverride(constraint); diff --git a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs index 7480b3519c..57fcb785e0 100644 --- a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs +++ b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs @@ -44,7 +44,7 @@ namespace Avalonia.DesignerSupport.Remote public IPlatformHandle Handle { get; } public WindowState WindowState { get; set; } public Action WindowStateChanged { get; set; } - public Size MaxClientSize { get; } = new Size(4096, 4096); + public Size MaxAutoSizeHint { get; } = new Size(4096, 4096); public event Action LostFocus { add {} diff --git a/src/Avalonia.DesignerSupport/Remote/Stubs.cs b/src/Avalonia.DesignerSupport/Remote/Stubs.cs index 7bf1d236bd..6d77d0dee5 100644 --- a/src/Avalonia.DesignerSupport/Remote/Stubs.cs +++ b/src/Avalonia.DesignerSupport/Remote/Stubs.cs @@ -19,7 +19,7 @@ namespace Avalonia.DesignerSupport.Remote public Action Deactivated { get; set; } public Action Activated { get; set; } public IPlatformHandle Handle { get; } - public Size MaxClientSize { get; } + public Size MaxAutoSizeHint { get; } public Size ClientSize { get; } public double Scaling { get; } = 1.0; public IEnumerable Surfaces { get; } diff --git a/src/Avalonia.Layout/ILayoutRoot.cs b/src/Avalonia.Layout/ILayoutRoot.cs index 56aca75871..e2f16b338a 100644 --- a/src/Avalonia.Layout/ILayoutRoot.cs +++ b/src/Avalonia.Layout/ILayoutRoot.cs @@ -10,11 +10,6 @@ namespace Avalonia.Layout /// Size ClientSize { get; } - /// - /// The maximum client size available. - /// - Size MaxClientSize { get; } - /// /// The scaling factor to use in layout. /// diff --git a/src/Avalonia.Native/WindowImplBase.cs b/src/Avalonia.Native/WindowImplBase.cs index c4ededd197..c97a3ebccb 100644 --- a/src/Avalonia.Native/WindowImplBase.cs +++ b/src/Avalonia.Native/WindowImplBase.cs @@ -309,7 +309,7 @@ namespace Avalonia.Native _native.BeginMoveDrag(); } - public Size MaxClientSize => Screen.AllScreens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity)) + public Size MaxAutoSizeHint => Screen.AllScreens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity)) .OrderByDescending(x => x.Width + x.Height).FirstOrDefault(); public void SetTopmost(bool value) diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index 3a919e2bc4..340cad7842 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -915,7 +915,7 @@ namespace Avalonia.X11 public IScreenImpl Screen => _platform.Screens; - public Size MaxClientSize => _platform.X11Screens.Screens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity)) + public Size MaxAutoSizeHint => _platform.X11Screens.Screens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity)) .OrderByDescending(x => x.Width + x.Height).FirstOrDefault(); diff --git a/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs index 3ea8c1e48f..6c4ac3da2e 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs @@ -399,6 +399,8 @@ namespace Avalonia.Win32 case WindowsMessage.WM_GETMINMAXINFO: { MINMAXINFO mmi = Marshal.PtrToStructure(lParam); + + _maxTrackSize = mmi.ptMaxTrackSize; if (_minSize.Width > 0) { diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index e193c72ef7..aa61b0d48f 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -62,6 +62,7 @@ namespace Avalonia.Win32 private OleDropTarget _dropTarget; private Size _minSize; private Size _maxSize; + private POINT _maxTrackSize; private WindowImpl _parent; public WindowImpl() @@ -164,16 +165,7 @@ namespace Avalonia.Win32 public IPlatformHandle Handle { get; private set; } - public Size MaxClientSize - { - get - { - return (new Size( - GetSystemMetrics(SystemMetric.SM_CXMAXTRACK), - GetSystemMetrics(SystemMetric.SM_CYMAXTRACK)) - - BorderThickness) / Scaling; - } - } + public Size MaxAutoSizeHint => new Size(_maxTrackSize.X / Scaling, _maxTrackSize.Y / Scaling); public IMouseDevice MouseDevice => _mouseDevice; diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index cf2920998a..d6634778ef 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs @@ -297,12 +297,12 @@ namespace Avalonia.Controls.UnitTests { var parentWindowImpl = MockWindowingPlatform.CreateWindowMock(); parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480)); - parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080)); + parentWindowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080)); parentWindowImpl.Setup(x => x.Scaling).Returns(1); var windowImpl = MockWindowingPlatform.CreateWindowMock(); windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200)); - windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080)); + windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080)); windowImpl.Setup(x => x.Scaling).Returns(1); var parentWindowServices = TestServices.StyledWindow.With( @@ -380,12 +380,15 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Child_Should_Be_Measured_With_Infinity_If_SizeToContent_Is_WidthAndHeight() + public void Child_Should_Be_Measured_With_MaxAutoSizeHint_If_SizeToContent_Is_WidthAndHeight() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { + var windowImpl = MockWindowingPlatform.CreateWindowMock(); + windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1200, 1000)); + var child = new ChildControl(); - var target = new Window + var target = new Window(windowImpl.Object) { Width = 100, Height = 50, @@ -396,7 +399,7 @@ namespace Avalonia.Controls.UnitTests target.Show(); Assert.Equal(1, child.MeasureSizes.Count); - Assert.Equal(Size.Infinity, child.MeasureSizes[0]); + Assert.Equal(new Size(1200, 1000), child.MeasureSizes[0]); } } diff --git a/tests/Avalonia.UnitTests/MockWindowingPlatform.cs b/tests/Avalonia.UnitTests/MockWindowingPlatform.cs index b3e4b4edbc..105a028120 100644 --- a/tests/Avalonia.UnitTests/MockWindowingPlatform.cs +++ b/tests/Avalonia.UnitTests/MockWindowingPlatform.cs @@ -28,6 +28,7 @@ namespace Avalonia.UnitTests windowImpl.SetupAllProperties(); windowImpl.Setup(x => x.ClientSize).Returns(() => clientSize); + windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(s_screenSize); windowImpl.Setup(x => x.Scaling).Returns(1); windowImpl.Setup(x => x.Screen).Returns(CreateScreenMock().Object); windowImpl.Setup(x => x.Position).Returns(() => position); From ba6adb78c19f502d71a50e7ad818f8b343d34c04 Mon Sep 17 00:00:00 2001 From: Deadpikle Date: Wed, 13 May 2020 10:06:59 -0400 Subject: [PATCH 002/169] Add failing test for no items in TabControl --- .../TabControlTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/TabControlTests.cs b/tests/Avalonia.Controls.UnitTests/TabControlTests.cs index 24aacd4000..db9211ac3c 100644 --- a/tests/Avalonia.Controls.UnitTests/TabControlTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TabControlTests.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using Avalonia.Collections; using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; +using Avalonia.Controls.Utils; using Avalonia.LogicalTree; using Avalonia.Styling; using Avalonia.UnitTests; @@ -325,6 +327,28 @@ namespace Avalonia.Controls.UnitTests Assert.NotEqual(dataContext, tabItem.Content); } + [Fact] + public void Can_Have_Empty_Tab_Control() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var loader = new Markup.Xaml.AvaloniaXamlLoader(); + var window = (Window)loader.Load(xaml); + var tabControl = window.FindControl("tabs"); + + tabControl.DataContext = new { Tabs = new List() }; + window.ApplyTemplate(); + + Assert.Equal(0, tabControl.Items.Count()); + } + } + private IControlTemplate TabControlTemplate() { return new FuncControlTemplate((parent, scope) => From c1dc0017d4b794420a3b8049acb09833ea14ff55 Mon Sep 17 00:00:00 2001 From: Deadpikle Date: Wed, 13 May 2020 11:17:00 -0400 Subject: [PATCH 003/169] Add tests from chat with @grokys --- .../SelectionModelTests.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index 246ff723a1..60fe9f3674 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -1752,6 +1752,26 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(0, node.PropertyChangedSubscriptions); } + [Fact] + public void Setting_SelectedIndex_To_Minus_1_Clears_Selection() + { + var data = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel { Source = data }; + target.SelectedIndex = new IndexPath(1); + target.SelectedIndex = new IndexPath(-1); + Assert.Empty(target.SelectedIndices); + } + + [Fact] + public void Assigning_Source_With_Less_Items_Than_Selection_Trims_Selection() + { + var data = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel { RetainSelectionOnReset = true }; + target.SelectedIndex = new IndexPath(4); + target.Source = data; + Assert.Empty(target.SelectedIndices); + } + private int GetSubscriberCount(AvaloniaList list) { return ((INotifyCollectionChangedDebug)list).GetCollectionChangedSubscribers()?.Length ?? 0; From 62e7dbded926a33415b5c2d636dced2203f4b4aa Mon Sep 17 00:00:00 2001 From: Deadpikle Date: Wed, 13 May 2020 11:51:22 -0400 Subject: [PATCH 004/169] Add more tests for selection adjustments --- .../SelectionModelTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index 60fe9f3674..9d795f6247 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -1762,6 +1762,18 @@ namespace Avalonia.Controls.UnitTests Assert.Empty(target.SelectedIndices); } + [Fact] + public void Assigning_Source_With_Less_Items_Than_Previous_Clears_Selection() + { + var data = new[] { "foo", "bar", "baz", "boo", "hoo" }; + var smallerData = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel { RetainSelectionOnReset = true }; + target.Source = data; + target.SelectedIndex = new IndexPath(4); + target.Source = smallerData; + Assert.Empty(target.SelectedIndices); + } + [Fact] public void Assigning_Source_With_Less_Items_Than_Selection_Trims_Selection() { @@ -1772,6 +1784,18 @@ namespace Avalonia.Controls.UnitTests Assert.Empty(target.SelectedIndices); } + [Fact] + public void Assigning_Source_With_Less_Items_Than_Multiple_Selection_Trims_Selection() + { + var data = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel { RetainSelectionOnReset = true }; + target.Select(4); + target.Select(2); + target.Source = data; + Assert.Equal(1, target.SelectedIndices.Count); + Assert.Equal(new IndexPath(2), target.SelectedIndices.First()); + } + private int GetSubscriberCount(AvaloniaList list) { return ((INotifyCollectionChangedDebug)list).GetCollectionChangedSubscribers()?.Length ?? 0; From 8fa6c05b7da7178d627eb8826d134a06f5c63083 Mon Sep 17 00:00:00 2001 From: Deadpikle Date: Wed, 13 May 2020 12:10:06 -0400 Subject: [PATCH 005/169] Another failing test for selection handling --- .../SelectionModelTests.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index 9d795f6247..211fc44caf 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -1796,6 +1796,19 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new IndexPath(2), target.SelectedIndices.First()); } + // test that going from non-null source to non-null source clears selection + [Fact] + public void Changing_Source_With_Valid_IndexPath_Retains_Selection() + { + var data = new[] { "foo", "bar", "baz", "boo", "hoo" }; + var smallerData = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel { RetainSelectionOnReset = true }; + target.Source = data; + target.SelectedIndex = new IndexPath(2); + target.Source = smallerData; + Assert.Equal(1, target.SelectedIndices.Count); + } + private int GetSubscriberCount(AvaloniaList list) { return ((INotifyCollectionChangedDebug)list).GetCollectionChangedSubscribers()?.Length ?? 0; From 89fa3b77f5ae91e6c9dba253fef5ac7482850a3d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 21 May 2020 11:36:03 +0200 Subject: [PATCH 006/169] Added nullable annotations to ContextMenu. --- src/Avalonia.Controls/ContextMenu.cs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index 86499530da..a026c8fa72 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Linq; using Avalonia.Controls.Generators; using Avalonia.Controls.Platform; using Avalonia.Controls.Primitives; @@ -9,9 +8,10 @@ using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Layout; -using Avalonia.LogicalTree; using Avalonia.Styling; +#nullable enable + namespace Avalonia.Controls { /// @@ -21,9 +21,9 @@ namespace Avalonia.Controls { private static readonly ITemplate DefaultPanel = new FuncTemplate(() => new StackPanel { Orientation = Orientation.Vertical }); - private Popup _popup; - private List _attachedControls; - private IInputElement _previousFocus; + private Popup? _popup; + private List? _attachedControls; + private IInputElement? _previousFocus; /// /// Initializes a new instance of the class. @@ -56,14 +56,14 @@ namespace Avalonia.Controls /// /// property is changing from false to true. /// - public event CancelEventHandler ContextMenuOpening; + public event CancelEventHandler? ContextMenuOpening; /// /// Occurs when the value of the /// /// property is changing from true to false. /// - public event CancelEventHandler ContextMenuClosing; + public event CancelEventHandler? ContextMenuClosing; /// /// Called when the property changes on a control. @@ -77,7 +77,7 @@ namespace Avalonia.Controls { control.PointerReleased -= ControlPointerReleased; oldMenu._attachedControls?.Remove(control); - ((ISetLogicalParent)oldMenu._popup)?.SetParent(null); + ((ISetLogicalParent?)oldMenu._popup)?.SetParent(null); } if (e.NewValue is ContextMenu newMenu) @@ -97,7 +97,7 @@ namespace Avalonia.Controls /// Opens a context menu on the specified control. /// /// The control. - public void Open(Control control) + public void Open(Control? control) { if (control is null && (_attachedControls is null || _attachedControls.Count == 0)) { @@ -113,7 +113,7 @@ namespace Avalonia.Controls nameof(control)); } - control ??= _attachedControls[0]; + control ??= _attachedControls![0]; if (IsOpen) { @@ -204,7 +204,7 @@ namespace Avalonia.Controls if (_attachedControls is null || _attachedControls.Count == 0) { - ((ISetLogicalParent)_popup).SetParent(null); + ((ISetLogicalParent)_popup!).SetParent(null); } // HACK: Reset the focus when the popup is closed. We need to fix this so it's automatic. From 372042b6f9afb67ec2934f5a76dd3f6442bad1fa Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 21 May 2020 11:39:12 +0200 Subject: [PATCH 007/169] Added positioning properties to ContextMenu. --- src/Avalonia.Controls/ContextMenu.cs | 69 ++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index a026c8fa72..5a60fd37af 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -19,6 +19,30 @@ namespace Avalonia.Controls /// public class ContextMenu : MenuBase, ISetterValue { + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementModeProperty = + Popup.PlacementModeProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty HorizontalOffsetProperty = + Popup.HorizontalOffsetProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty VerticalOffsetProperty = + Popup.VerticalOffsetProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementTargetProperty = + Popup.PlacementTargetProperty.AddOwner(); + private static readonly ITemplate DefaultPanel = new FuncTemplate(() => new StackPanel { Orientation = Orientation.Vertical }); private Popup? _popup; @@ -47,10 +71,47 @@ namespace Avalonia.Controls /// static ContextMenu() { - ItemsPanelProperty.OverrideDefaultValue(typeof(ContextMenu), DefaultPanel); + ItemsPanelProperty.OverrideDefaultValue(DefaultPanel); + PlacementModeProperty.OverrideDefaultValue(PlacementMode.Pointer); ContextMenuProperty.Changed.Subscribe(ContextMenuChanged); } + /// + /// Gets or sets the placement mode of the popup in relation to the . + /// + public PlacementMode PlacementMode + { + get { return GetValue(PlacementModeProperty); } + set { SetValue(PlacementModeProperty, value); } + } + + /// + /// Gets or sets the Horizontal offset of the popup in relation to the + /// + public double HorizontalOffset + { + get { return GetValue(HorizontalOffsetProperty); } + set { SetValue(HorizontalOffsetProperty, value); } + } + + /// + /// Gets or sets the Vertical offset of the popup in relation to the + /// + public double VerticalOffset + { + get { return GetValue(VerticalOffsetProperty); } + set { SetValue(VerticalOffsetProperty, value); } + } + + /// + /// Gets or sets the control that is used to determine the popup's position. + /// + public Control? PlacementTarget + { + get { return GetValue(PlacementTargetProperty); } + set { SetValue(PlacementTargetProperty, value); } + } + /// /// Occurs when the value of the /// @@ -124,8 +185,10 @@ namespace Avalonia.Controls { _popup = new Popup { - PlacementMode = PlacementMode.Pointer, - PlacementTarget = control, + HorizontalOffset = HorizontalOffset, + VerticalOffset = VerticalOffset, + PlacementMode = PlacementMode, + PlacementTarget = PlacementTarget ?? control, StaysOpen = false }; From 9bb71a248467f2827518b652797441e647d7ed30 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 21 May 2020 14:46:49 +0200 Subject: [PATCH 008/169] Added/tweaked docs. --- .../Primitives/IPopupHost.cs | 46 +++ .../PopupPositioning/IPopupPositioner.cs | 269 +++++++++++------- .../ManagedPopupPositioner.cs | 4 + 3 files changed, 218 insertions(+), 101 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/IPopupHost.cs b/src/Avalonia.Controls/Primitives/IPopupHost.cs index 74a3ca8818..bb4daf38e1 100644 --- a/src/Avalonia.Controls/Primitives/IPopupHost.cs +++ b/src/Avalonia.Controls/Primitives/IPopupHost.cs @@ -5,19 +5,65 @@ using Avalonia.VisualTree; namespace Avalonia.Controls.Primitives { + /// + /// Represents the top-level control opened by a . + /// + /// + /// A popup host can be either be a popup window created by the operating system + /// () or an which is created + /// on an . + /// public interface IPopupHost : IDisposable { + /// + /// Sets the control to display in the popup. + /// + /// void SetChild(IControl control); + + /// + /// Gets the presenter from the control's template. + /// IContentPresenter Presenter { get; } + + /// + /// Gets the root of the visual tree in the case where the popup is presented using a + /// separate visual tree. + /// IVisual HostedVisualTreeRoot { get; } + /// + /// Raised when the control's template is applied. + /// event EventHandler TemplateApplied; + /// + /// Configures the position of the popup according to a target control and a set of + /// placement parameters. + /// + /// The placement target. + /// The placement mode. + /// The offset, in device-independent pixels. + /// The anchor point. + /// The anchor gravity. void ConfigurePosition(IVisual target, PlacementMode placement, Point offset, PopupPositioningEdge anchor = PopupPositioningEdge.None, PopupPositioningEdge gravity = PopupPositioningEdge.None); + + /// + /// Shows the popup. + /// void Show(); + + /// + /// Hides the popup. + /// void Hide(); + + /// + /// Binds the constraints of the popup host to a set of properties, usally those present on + /// . + /// IDisposable BindConstraints(AvaloniaObject popup, StyledProperty widthProperty, StyledProperty minWidthProperty, StyledProperty maxWidthProperty, StyledProperty heightProperty, StyledProperty minHeightProperty, diff --git a/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs b/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs index f0358ec04f..d2a403b602 100644 --- a/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs +++ b/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs @@ -50,45 +50,47 @@ using Avalonia.VisualTree; namespace Avalonia.Controls.Primitives.PopupPositioning { /// - /// - /// The IPopupPositioner provides a collection of rules for the placement of a - /// a popup relative to its parent. Rules can be defined to ensure - /// the popup remains within the visible area's borders, and to - /// specify how the popup changes its position, such as sliding along - /// an axis, or flipping around a rectangle. These positioner-created rules are - /// constrained by the requirement that a popup must intersect with or - /// be at least partially adjacent to its parent surface. + /// Provides positioning parameters to . /// + /// + /// The IPopupPositioner provides a collection of rules for the placement of a a popup relative + /// to its parent. Rules can be defined to ensure the popup remains within the visible area's + /// borders, and to specify how the popup changes its position, such as sliding along an axis, + /// or flipping around a rectangle. These positioner-created rules are constrained by the + /// requirement that a popup must intersect with or be at least partially adjacent to its parent + /// surface. + /// public struct PopupPositionerParameters { private PopupPositioningEdge _gravity; private PopupPositioningEdge _anchor; /// - /// Set the size of the popup that is to be positioned with the positioner - /// object. The size is in scaled coordinates. + /// Set the size of the popup that is to be positioned with the positioner object, in device- + /// independent pixels. /// public Size Size { get; set; } /// - /// Specify the anchor rectangle within the parent that the popup - /// will be placed relative to. The rectangle is relative to the - /// parent geometry - /// - /// The anchor rectangle may not extend outside the window geometry of the - /// popup's parent. The anchor rectangle is in scaled coordinates + /// Specifies the anchor rectangle within the parent that the popup will be placed relative + /// to, in device-independent pixels. /// + /// + /// The rectangle is relative to the parent geometry and may not extend outside the window + /// geometry of the popup's parent. + /// public Rect AnchorRectangle { get; set; } - /// - /// Defines the anchor point for the anchor rectangle. The specified anchor - /// is used derive an anchor point that the popup will be - /// positioned relative to. If a corner anchor is set (e.g. 'TopLeft' or - /// 'BottomRight'), the anchor point will be at the specified corner; - /// otherwise, the derived anchor point will be centered on the specified - /// edge, or in the center of the anchor rectangle if no edge is specified. + /// Defines the anchor point for the anchor rectangle. /// + /// + /// The specified anchor is used derive an anchor point that the popup will be positioned + /// relative to. If a corner anchor is set (e.g. 'TopLeft' or 'BottomRight'), the anchor + /// point will be at the specified corner; otherwise, the derived anchor point will be + /// centered on the specified edge, or in the center of the anchor rectangle if no edge is + /// specified. + /// public PopupPositioningEdge Anchor { get => _anchor; @@ -100,13 +102,14 @@ namespace Avalonia.Controls.Primitives.PopupPositioning } /// - /// Defines in what direction a popup should be positioned, relative to - /// the anchor point of the parent. If a corner gravity is - /// specified (e.g. 'BottomRight' or 'TopLeft'), then the popup - /// will be placed towards the specified gravity; otherwise, the popup - /// will be centered over the anchor point on any axis that had no - /// gravity specified. + /// Defines in what direction a popup should be positioned, relative to the anchor point of + /// the parent. /// + /// + /// If a corner gravity is specified (e.g. 'BottomRight' or 'TopLeft'), then the popup will + /// be placed towards the specified gravity; otherwise, the popup will be centered over the + /// anchor point on any axis that had no gravity specified. + /// public PopupPositioningEdge Gravity { get => _gravity; @@ -118,48 +121,51 @@ namespace Avalonia.Controls.Primitives.PopupPositioning } /// - /// Specify how the popup should be positioned if the originally intended - /// position caused the popup to be constrained, meaning at least - /// partially outside positioning boundaries set by the positioner. The - /// adjustment is set by constructing a bitmask describing the adjustment to - /// be made when the popup is constrained on that axis. + /// Specify how the popup should be positioned if the originally intended position caused + /// the popup to be constrained. + /// + /// + /// Adjusts the popup position if the intended position caused the popup to be constrained; + /// meaning at least partially outside positioning boundaries set by the positioner. The + /// adjustment is set by constructing a bitmask describing the adjustment to be made when + /// the popup is constrained on that axis. /// - /// If no bit for one axis is set, the positioner will assume that the child - /// surface should not change its position on that axis when constrained. + /// If no bit for one axis is set, the positioner will assume that the child surface should + /// not change its position on that axis when constrained. /// - /// If more than one bit for one axis is set, the order of how adjustments - /// are applied is specified in the corresponding adjustment descriptions. + /// If more than one bit for one axis is set, the order of how adjustments are applied is + /// specified in the corresponding adjustment descriptions. /// /// The default adjustment is none. - /// + /// public PopupPositionerConstraintAdjustment ConstraintAdjustment { get; set; } - + /// /// Specify the popup position offset relative to the position of the - /// anchor on the anchor rectangle and the anchor on the popup. For - /// example if the anchor of the anchor rectangle is at (x, y), the popup - /// has the gravity bottom|right, and the offset is (ox, oy), the calculated - /// surface position will be (x + ox, y + oy). The offset position of the - /// surface is the one used for constraint testing. See - /// set_constraint_adjustment. - /// - /// An example use case is placing a popup menu on top of a user interface - /// element, while aligning the user interface element of the parent surface - /// with some user interface element placed somewhere in the popup. + /// anchor on the anchor rectangle and the anchor on the popup. /// + /// + /// For example if the anchor of the anchor rectangle is at (x, y), the popup has the + /// gravity bottom|right, and the offset is (ox, oy), the calculated surface position will + /// be (x + ox, y + oy). The offset position of the surface is the one used for constraint + /// testing. See set_constraint_adjustment. + /// + /// An example use case is placing a popup menu on top of a user interface element, while + /// aligning the user interface element of the parent surface with some user interface + /// element placed somewhere in the popup. + /// public Point Offset { get; set; } } - + /// - /// The constraint adjustment value define ways how popup position will - /// be adjusted if the unadjusted position would result in the popup - /// being partly constrained. - /// - /// Whether a popup is considered 'constrained' is left to the positioner - /// to determine. For example, the popup may be partly outside the - /// target platform defined 'work area', thus necessitating the popup's - /// position be adjusted until it is entirely inside the work area. + /// Defines how a popup position will be adjusted if the unadjusted position would result in + /// the popup being partly constrained. /// + /// + /// Whether a popup is considered 'constrained' is left to the positioner to determine. For + /// example, the popup may be partly outside the target platform defined 'work area', thus + /// necessitating the popup's position be adjusted until it is entirely inside the work area. + /// [Flags] public enum PopupPositionerConstraintAdjustment { @@ -171,62 +177,59 @@ namespace Avalonia.Controls.Primitives.PopupPositioning /// /// Slide the surface along the x axis until it is no longer constrained. - /// First try to slide towards the direction of the gravity on the x axis - /// until either the edge in the opposite direction of the gravity is - /// unconstrained or the edge in the direction of the gravity is - /// constrained. - /// - /// Then try to slide towards the opposite direction of the gravity on the - /// x axis until either the edge in the direction of the gravity is - /// unconstrained or the edge in the opposite direction of the gravity is - /// constrained. /// + /// + /// First try to slide towards the direction of the gravity on the x axis until either the + /// edge in the opposite direction of the gravity is unconstrained or the edge in the + /// direction of the gravity is constrained. + /// + /// Then try to slide towards the opposite direction of the gravity on the x axis until + /// either the edge in the direction of the gravity is unconstrained or the edge in the + /// opposite direction of the gravity is constrained. + /// SlideX = 1, - /// - /// Slide the surface along the y axis until it is no longer constrained. - /// - /// First try to slide towards the direction of the gravity on the y axis - /// until either the edge in the opposite direction of the gravity is - /// unconstrained or the edge in the direction of the gravity is - /// constrained. - /// - /// Then try to slide towards the opposite direction of the gravity on the - /// y axis until either the edge in the direction of the gravity is - /// unconstrained or the edge in the opposite direction of the gravity is - /// constrained. - /// */ + /// Slide the surface along the y axis until it is no longer constrained. /// + /// + /// First try to slide towards the direction of the gravity on the y axis until either the + /// edge in the opposite direction of the gravity is unconstrained or the edge in the + /// direction of the gravity is constrained. + /// + /// Then try to slide towards the opposite direction of the gravity on the y axis until + /// either the edge in the direction of the gravity is unconstrained or the edge in the + /// opposite direction of the gravity is constrained. + /// SlideY = 2, /// - /// Invert the anchor and gravity on the x axis if the surface is - /// constrained on the x axis. For example, if the left edge of the - /// surface is constrained, the gravity is 'left' and the anchor is - /// 'left', change the gravity to 'right' and the anchor to 'right'. - /// - /// If the adjusted position also ends up being constrained, the resulting - /// position of the flip_x adjustment will be the one before the - /// adjustment. + /// Invert the anchor and gravity on the x axis if the surface is constrained on the x axis. /// + /// + /// For example, if the left edge of the surface is constrained, the gravity is 'left' and + /// the anchor is 'left', change the gravity to 'right' and the anchor to 'right'. + /// + /// If the adjusted position also ends up being constrained, the resulting position of the + /// FlipX adjustment will be the one before the adjustment. + /// /// FlipX = 4, /// - /// Invert the anchor and gravity on the y axis if the surface is - /// constrained on the y axis. For example, if the bottom edge of the - /// surface is constrained, the gravity is 'bottom' and the anchor is - /// 'bottom', change the gravity to 'top' and the anchor to 'top'. + /// Invert the anchor and gravity on the y axis if the surface is constrained on the y axis. + /// + /// + /// For example, if the bottom edge of the surface is constrained, the gravity is 'bottom' + /// and the anchor is 'bottom', change the gravity to 'top' and the anchor to 'top'. /// - /// The adjusted position is calculated given the original anchor - /// rectangle and offset, but with the new flipped anchor and gravity - /// values. + /// The adjusted position is calculated given the original anchor rectangle and offset, but + /// with the new flipped anchor and gravity values. /// - /// If the adjusted position also ends up being constrained, the resulting - /// position of the flip_y adjustment will be the one before the - /// adjustment. - /// + /// If the adjusted position also ends up being constrained, the resulting position of the + /// FlipY adjustment will be the one before the adjustment. + /// FlipY = 8, + All = SlideX|SlideY|FlipX|FlipY } @@ -267,27 +270,91 @@ namespace Avalonia.Controls.Primitives.PopupPositioning } + /// + /// Defines the popup position edge for and + /// . + /// [Flags] public enum PopupPositioningEdge { + /// + /// The center of the anchor rectangle. + /// None, + + /// + /// The top edge of the anchor rectangle. + /// Top = 1, + + /// + /// The bottom edge of the anchor rectangle. + /// Bottom = 2, + + /// + /// The left edge of the anchor rectangle. + /// Left = 4, + + /// + /// The right edge of the anchor rectangle. + /// Right = 8, + + /// + /// The top-left corner of the anchor rectangle. + /// TopLeft = Top | Left, + + /// + /// The top-right corner of the anchor rectangle. + /// TopRight = Top | Right, + + /// + /// The bottom-left corner of the anchor rectangle. + /// BottomLeft = Bottom | Left, + + /// + /// The bottom-right corner of the anchor rectangle. + /// BottomRight = Bottom | Right, - + /// + /// A mask for the vertical component flags. + /// VerticalMask = Top | Bottom, + + /// + /// A mask for the horizontal component flags. + /// HorizontalMask = Left | Right, + + /// + /// A mask for all flags. + /// AllMask = VerticalMask|HorizontalMask } + /// + /// Positions an . + /// + /// + /// is an abstraction of the wayland xdg_positioner spec. + /// + /// The popup positioner implementation is determined by the platform implementation. A default + /// managed implementation is provided in for platforms + /// on which popups can be arbitrarily positioned. + /// public interface IPopupPositioner { + /// + /// Updates the position of the associated according to the + /// specified parameters. + /// + /// The positioning parameters. void Update(PopupPositionerParameters parameters); } diff --git a/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs b/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs index 07348cdf78..bd23a13e7f 100644 --- a/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs +++ b/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs @@ -25,6 +25,10 @@ namespace Avalonia.Controls.Primitives.PopupPositioning } } + /// + /// An implementation for platforms on which a popup can be + /// aritrarily positioned. + /// public class ManagedPopupPositioner : IPopupPositioner { private readonly IManagedPopupPositionerPopup _popup; From 07d1f16fa0f7eeb58c35c9977bab06f4c9a6fa15 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 21 May 2020 15:35:25 +0200 Subject: [PATCH 009/169] Added additional placement properties to PopupRoot. And split `PopupPositioningEdge` into `PopupAnchor` and `PopupGravity` so their values can be documented more clearly. --- .../Primitives/IPopupHost.cs | 10 +- .../Primitives/OverlayPopupHost.cs | 5 +- src/Avalonia.Controls/Primitives/Popup.cs | 97 +++++++++--- .../PopupPositioning/IPopupPositioner.cs | 138 +++++++++++++----- .../ManagedPopupPositioner.cs | 42 +++--- src/Avalonia.Controls/Primitives/PopupRoot.cs | 7 +- 6 files changed, 215 insertions(+), 84 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/IPopupHost.cs b/src/Avalonia.Controls/Primitives/IPopupHost.cs index bb4daf38e1..18fb45fd5a 100644 --- a/src/Avalonia.Controls/Primitives/IPopupHost.cs +++ b/src/Avalonia.Controls/Primitives/IPopupHost.cs @@ -45,10 +45,14 @@ namespace Avalonia.Controls.Primitives /// The placement mode. /// The offset, in device-independent pixels. /// The anchor point. - /// The anchor gravity. + /// The popup gravity. + /// + /// The anchor rect. If null, the bounds of will be used. + /// void ConfigurePosition(IVisual target, PlacementMode placement, Point offset, - PopupPositioningEdge anchor = PopupPositioningEdge.None, - PopupPositioningEdge gravity = PopupPositioningEdge.None); + PopupAnchor anchor = PopupAnchor.None, + PopupGravity gravity = PopupGravity.None, + Rect? rect = null); /// /// Shows the popup. diff --git a/src/Avalonia.Controls/Primitives/OverlayPopupHost.cs b/src/Avalonia.Controls/Primitives/OverlayPopupHost.cs index 3dc9d302db..15b09f29f1 100644 --- a/src/Avalonia.Controls/Primitives/OverlayPopupHost.cs +++ b/src/Avalonia.Controls/Primitives/OverlayPopupHost.cs @@ -71,10 +71,11 @@ namespace Avalonia.Controls.Primitives } public void ConfigurePosition(IVisual target, PlacementMode placement, Point offset, - PopupPositioningEdge anchor = PopupPositioningEdge.None, PopupPositioningEdge gravity = PopupPositioningEdge.None) + PopupAnchor anchor = PopupAnchor.None, PopupGravity gravity = PopupGravity.None, + Rect? rect = null) { _positionerParameters.ConfigurePosition((TopLevel)_overlayLayer.GetVisualRoot(), target, placement, offset, anchor, - gravity); + gravity, rect); UpdatePosition(); } diff --git a/src/Avalonia.Controls/Primitives/Popup.cs b/src/Avalonia.Controls/Primitives/Popup.cs index 66f2153b6c..ff1201286c 100644 --- a/src/Avalonia.Controls/Primitives/Popup.cs +++ b/src/Avalonia.Controls/Primitives/Popup.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Reactive.Disposables; using Avalonia.Controls.Presenters; +using Avalonia.Controls.Primitives.PopupPositioning; using Avalonia.Input; using Avalonia.Input.Raw; using Avalonia.Interactivity; @@ -34,12 +35,36 @@ namespace Avalonia.Controls.Primitives o => o.IsOpen, (o, v) => o.IsOpen = v); + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementAnchorProperty = + AvaloniaProperty.Register(nameof(PlacementAnchor)); + + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementGravityProperty = + AvaloniaProperty.Register(nameof(PlacementGravity)); + /// /// Defines the property. /// public static readonly StyledProperty PlacementModeProperty = AvaloniaProperty.Register(nameof(PlacementMode), defaultValue: PlacementMode.Bottom); + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementRectProperty = + AvaloniaProperty.Register(nameof(PlacementRect)); + + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementTargetProperty = + AvaloniaProperty.Register(nameof(PlacementTarget)); + #pragma warning disable 618 /// /// Defines the property. @@ -60,12 +85,6 @@ namespace Avalonia.Controls.Primitives public static readonly StyledProperty VerticalOffsetProperty = AvaloniaProperty.Register(nameof(VerticalOffset)); - /// - /// Defines the property. - /// - public static readonly StyledProperty PlacementTargetProperty = - AvaloniaProperty.Register(nameof(PlacementTarget)); - /// /// Defines the property. /// @@ -136,6 +155,26 @@ namespace Avalonia.Controls.Primitives set { SetAndRaise(IsOpenProperty, ref _isOpen, value); } } + /// + /// Gets or sets the anchor point on the when + /// is . + /// + public PopupAnchor PlacementAnchor + { + get { return GetValue(PlacementAnchorProperty); } + set { SetValue(PlacementAnchorProperty, value); } + } + + /// + /// Gets or sets a value which defines in what direction the popup should open + /// when is . + /// + public PopupGravity PlacementGravity + { + get { return GetValue(PlacementGravityProperty); } + set { SetValue(PlacementGravityProperty, value); } + } + /// /// Gets or sets the placement mode of the popup in relation to the . /// @@ -145,6 +184,32 @@ namespace Avalonia.Controls.Primitives set { SetValue(PlacementModeProperty, value); } } + /// + /// Gets or sets the the anchor rectangle within the parent that the popup will be placed + /// relative to when is . + /// + /// + /// The placement rect defines a rectangle relative to around + /// which the popup will be opened, with determining which edge + /// of the placement target is used. + /// + /// If unset, the anchor rectangle will be the bounds of the . + /// + public Rect? PlacementRect + { + get { return GetValue(PlacementRectProperty); } + set { SetValue(PlacementRectProperty, value); } + } + + /// + /// Gets or sets the control that is used to determine the popup's position. + /// + public Control? PlacementTarget + { + get { return GetValue(PlacementTargetProperty); } + set { SetValue(PlacementTargetProperty, value); } + } + [Obsolete("This property has no effect")] public bool ObeyScreenEdges { @@ -153,7 +218,7 @@ namespace Avalonia.Controls.Primitives } /// - /// Gets or sets the Horizontal offset of the popup in relation to the + /// Gets or sets the Horizontal offset of the popup in relation to the . /// public double HorizontalOffset { @@ -162,7 +227,7 @@ namespace Avalonia.Controls.Primitives } /// - /// Gets or sets the Vertical offset of the popup in relation to the + /// Gets or sets the Vertical offset of the popup in relation to the . /// public double VerticalOffset { @@ -170,15 +235,6 @@ namespace Avalonia.Controls.Primitives set { SetValue(VerticalOffsetProperty, value); } } - /// - /// Gets or sets the control that is used to determine the popup's position. - /// - public Control? PlacementTarget - { - get { return GetValue(PlacementTargetProperty); } - set { SetValue(PlacementTargetProperty, value); } - } - /// /// Gets or sets a value indicating whether the popup should stay open when the popup is /// pressed or loses focus. @@ -251,8 +307,11 @@ namespace Avalonia.Controls.Primitives popupHost.ConfigurePosition( placementTarget, - PlacementMode, - new Point(HorizontalOffset, VerticalOffset)); + PlacementMode, + new Point(HorizontalOffset, VerticalOffset), + PlacementAnchor, + PlacementGravity, + PlacementRect); DeferCleanup(SubscribeToEventHandler>(popupHost, RootTemplateApplied, (x, handler) => x.TemplateApplied += handler, diff --git a/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs b/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs index d2a403b602..d6e54b80c1 100644 --- a/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs +++ b/src/Avalonia.Controls/Primitives/PopupPositioning/IPopupPositioner.cs @@ -62,8 +62,8 @@ namespace Avalonia.Controls.Primitives.PopupPositioning /// public struct PopupPositionerParameters { - private PopupPositioningEdge _gravity; - private PopupPositioningEdge _anchor; + private PopupGravity _gravity; + private PopupAnchor _anchor; /// /// Set the size of the popup that is to be positioned with the positioner object, in device- @@ -91,7 +91,7 @@ namespace Avalonia.Controls.Primitives.PopupPositioning /// centered on the specified edge, or in the center of the anchor rectangle if no edge is /// specified. /// - public PopupPositioningEdge Anchor + public PopupAnchor Anchor { get => _anchor; set @@ -110,12 +110,12 @@ namespace Avalonia.Controls.Primitives.PopupPositioning /// be placed towards the specified gravity; otherwise, the popup will be centered over the /// anchor point on any axis that had no gravity specified. /// - public PopupPositioningEdge Gravity + public PopupGravity Gravity { get => _gravity; set { - PopupPositioningEdgeHelper.ValidateEdge(value); + PopupPositioningEdgeHelper.ValidateGravity(value); _gravity = value; } } @@ -235,18 +235,23 @@ namespace Avalonia.Controls.Primitives.PopupPositioning static class PopupPositioningEdgeHelper { - public static void ValidateEdge(this PopupPositioningEdge edge) + public static void ValidateEdge(this PopupAnchor edge) { - if (((edge & PopupPositioningEdge.Left) != 0 && (edge & PopupPositioningEdge.Right) != 0) + if (((edge & PopupAnchor.Left) != 0 && (edge & PopupAnchor.Right) != 0) || - ((edge & PopupPositioningEdge.Top) != 0 && (edge & PopupPositioningEdge.Bottom) != 0)) + ((edge & PopupAnchor.Top) != 0 && (edge & PopupAnchor.Bottom) != 0)) throw new ArgumentException("Opposite edges specified"); } - public static PopupPositioningEdge Flip(this PopupPositioningEdge edge) + public static void ValidateGravity(this PopupGravity gravity) + { + ValidateEdge((PopupAnchor)gravity); + } + + public static PopupAnchor Flip(this PopupAnchor edge) { - var hmask = PopupPositioningEdge.Left | PopupPositioningEdge.Right; - var vmask = PopupPositioningEdge.Top | PopupPositioningEdge.Bottom; + var hmask = PopupAnchor.Left | PopupAnchor.Right; + var vmask = PopupAnchor.Top | PopupAnchor.Bottom; if ((edge & hmask) != 0) edge ^= hmask; if ((edge & vmask) != 0) @@ -254,28 +259,36 @@ namespace Avalonia.Controls.Primitives.PopupPositioning return edge; } - public static PopupPositioningEdge FlipX(this PopupPositioningEdge edge) + public static PopupAnchor FlipX(this PopupAnchor edge) { - if ((edge & PopupPositioningEdge.HorizontalMask) != 0) - edge ^= PopupPositioningEdge.HorizontalMask; + if ((edge & PopupAnchor.HorizontalMask) != 0) + edge ^= PopupAnchor.HorizontalMask; return edge; } - public static PopupPositioningEdge FlipY(this PopupPositioningEdge edge) + public static PopupAnchor FlipY(this PopupAnchor edge) { - if ((edge & PopupPositioningEdge.VerticalMask) != 0) - edge ^= PopupPositioningEdge.VerticalMask; + if ((edge & PopupAnchor.VerticalMask) != 0) + edge ^= PopupAnchor.VerticalMask; return edge; } - + + public static PopupGravity FlipX(this PopupGravity gravity) + { + return (PopupGravity)FlipX((PopupAnchor)gravity); + } + + public static PopupGravity FlipY(this PopupGravity gravity) + { + return (PopupGravity)FlipY((PopupAnchor)gravity); + } } /// - /// Defines the popup position edge for and - /// . + /// Defines the edges around an anchor rectangle on which a popup will open. /// [Flags] - public enum PopupPositioningEdge + public enum PopupAnchor { /// /// The center of the anchor rectangle. @@ -338,6 +351,58 @@ namespace Avalonia.Controls.Primitives.PopupPositioning AllMask = VerticalMask|HorizontalMask } + /// + /// Defines the direction in which a popup will open. + /// + [Flags] + public enum PopupGravity + { + /// + /// The popup will be centered over the anchor edge. + /// + None, + + /// + /// The popup will be positioned above the anchor edge + /// + Top = 1, + + /// + /// The popup will be positioned below the anchor edge + /// + Bottom = 2, + + /// + /// The popup will be positioned to the left of the anchor edge + /// + Left = 4, + + /// + /// The popup will be positioned to the right of the anchor edge + /// + Right = 8, + + /// + /// The popup will be positioned to the top-left of the anchor edge + /// + TopLeft = Top | Left, + + /// + /// The popup will be positioned to the top-right of the anchor edge + /// + TopRight = Top | Right, + + /// + /// The popup will be positioned to the bottom-left of the anchor edge + /// + BottomLeft = Bottom | Left, + + /// + /// The popup will be positioned to the bottom-right of the anchor edge + /// + BottomRight = Bottom | Right, + } + /// /// Positions an . /// @@ -363,7 +428,7 @@ namespace Avalonia.Controls.Primitives.PopupPositioning public static void ConfigurePosition(ref this PopupPositionerParameters positionerParameters, TopLevel topLevel, IVisual target, PlacementMode placement, Point offset, - PopupPositioningEdge anchor, PopupPositioningEdge gravity) + PopupAnchor anchor, PopupGravity gravity, Rect? rect) { // We need a better way for tracking the last pointer position var pointer = topLevel.PointToClient(topLevel.PlatformImpl.MouseDevice.Position); @@ -373,8 +438,8 @@ namespace Avalonia.Controls.Primitives.PopupPositioning if (placement == PlacementMode.Pointer) { positionerParameters.AnchorRectangle = new Rect(pointer, new Size(1, 1)); - positionerParameters.Anchor = PopupPositioningEdge.TopLeft; - positionerParameters.Gravity = PopupPositioningEdge.BottomRight; + positionerParameters.Anchor = PopupAnchor.TopLeft; + positionerParameters.Gravity = PopupGravity.BottomRight; } else { @@ -384,32 +449,33 @@ namespace Avalonia.Controls.Primitives.PopupPositioning if (matrix == null) { if (target.GetVisualRoot() == null) - throw new InvalidCastException("Target control is not attached to the visual tree"); - throw new InvalidCastException("Target control is not in the same tree as the popup parent"); + throw new InvalidOperationException("Target control is not attached to the visual tree"); + throw new InvalidOperationException("Target control is not in the same tree as the popup parent"); } - positionerParameters.AnchorRectangle = new Rect(default, target.Bounds.Size) - .TransformToAABB(matrix.Value); + var bounds = new Rect(default, target.Bounds.Size); + var anchorRect = rect ?? bounds; + positionerParameters.AnchorRectangle = anchorRect.Intersect(bounds).TransformToAABB(matrix.Value); if (placement == PlacementMode.Right) { - positionerParameters.Anchor = PopupPositioningEdge.TopRight; - positionerParameters.Gravity = PopupPositioningEdge.BottomRight; + positionerParameters.Anchor = PopupAnchor.TopRight; + positionerParameters.Gravity = PopupGravity.BottomRight; } else if (placement == PlacementMode.Bottom) { - positionerParameters.Anchor = PopupPositioningEdge.BottomLeft; - positionerParameters.Gravity = PopupPositioningEdge.BottomRight; + positionerParameters.Anchor = PopupAnchor.BottomLeft; + positionerParameters.Gravity = PopupGravity.BottomRight; } else if (placement == PlacementMode.Left) { - positionerParameters.Anchor = PopupPositioningEdge.TopLeft; - positionerParameters.Gravity = PopupPositioningEdge.BottomLeft; + positionerParameters.Anchor = PopupAnchor.TopLeft; + positionerParameters.Gravity = PopupGravity.BottomLeft; } else if (placement == PlacementMode.Top) { - positionerParameters.Anchor = PopupPositioningEdge.TopLeft; - positionerParameters.Gravity = PopupPositioningEdge.TopRight; + positionerParameters.Anchor = PopupAnchor.TopLeft; + positionerParameters.Gravity = PopupGravity.TopRight; } else if (placement == PlacementMode.AnchorAndGravity) { diff --git a/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs b/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs index bd23a13e7f..04167bbd15 100644 --- a/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs +++ b/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositioner.cs @@ -39,38 +39,38 @@ namespace Avalonia.Controls.Primitives.PopupPositioning } - private static Point GetAnchorPoint(Rect anchorRect, PopupPositioningEdge edge) + private static Point GetAnchorPoint(Rect anchorRect, PopupAnchor edge) { double x, y; - if ((edge & PopupPositioningEdge.Left) != 0) + if ((edge & PopupAnchor.Left) != 0) x = anchorRect.X; - else if ((edge & PopupPositioningEdge.Right) != 0) + else if ((edge & PopupAnchor.Right) != 0) x = anchorRect.Right; else x = anchorRect.X + anchorRect.Width / 2; - if ((edge & PopupPositioningEdge.Top) != 0) + if ((edge & PopupAnchor.Top) != 0) y = anchorRect.Y; - else if ((edge & PopupPositioningEdge.Bottom) != 0) + else if ((edge & PopupAnchor.Bottom) != 0) y = anchorRect.Bottom; else y = anchorRect.Y + anchorRect.Height / 2; return new Point(x, y); } - private static Point Gravitate(Point anchorPoint, Size size, PopupPositioningEdge gravity) + private static Point Gravitate(Point anchorPoint, Size size, PopupGravity gravity) { double x, y; - if ((gravity & PopupPositioningEdge.Left) != 0) + if ((gravity & PopupGravity.Left) != 0) x = -size.Width; - else if ((gravity & PopupPositioningEdge.Right) != 0) + else if ((gravity & PopupGravity.Right) != 0) x = 0; else x = -size.Width / 2; - if ((gravity & PopupPositioningEdge.Top) != 0) + if ((gravity & PopupGravity.Top) != 0) y = -size.Height; - else if ((gravity & PopupPositioningEdge.Bottom) != 0) + else if ((gravity & PopupGravity.Bottom) != 0) y = 0; else y = -size.Height / 2; @@ -89,7 +89,7 @@ namespace Avalonia.Controls.Primitives.PopupPositioning private void Update(Size translatedSize, Size originalSize, - Rect anchorRect, PopupPositioningEdge anchor, PopupPositioningEdge gravity, + Rect anchorRect, PopupAnchor anchor, PopupGravity gravity, PopupPositionerConstraintAdjustment constraintAdjustment, Point offset) { var parentGeometry = _popup.ParentClientAreaScreenGeometry; @@ -116,28 +116,28 @@ namespace Avalonia.Controls.Primitives.PopupPositioning var bounds = GetBounds(); - bool FitsInBounds(Rect rc, PopupPositioningEdge edge = PopupPositioningEdge.AllMask) + bool FitsInBounds(Rect rc, PopupAnchor edge = PopupAnchor.AllMask) { - if ((edge & PopupPositioningEdge.Left) != 0 + if ((edge & PopupAnchor.Left) != 0 && rc.X < bounds.X) return false; - if ((edge & PopupPositioningEdge.Top) != 0 + if ((edge & PopupAnchor.Top) != 0 && rc.Y < bounds.Y) return false; - if ((edge & PopupPositioningEdge.Right) != 0 + if ((edge & PopupAnchor.Right) != 0 && rc.Right > bounds.Right) return false; - if ((edge & PopupPositioningEdge.Bottom) != 0 + if ((edge & PopupAnchor.Bottom) != 0 && rc.Bottom > bounds.Bottom) return false; return true; } - Rect GetUnconstrained(PopupPositioningEdge a, PopupPositioningEdge g) => + Rect GetUnconstrained(PopupAnchor a, PopupGravity g) => new Rect(Gravitate(GetAnchorPoint(anchorRect, a), translatedSize, g) + offset, translatedSize); @@ -145,11 +145,11 @@ namespace Avalonia.Controls.Primitives.PopupPositioning // If flipping geometry and anchor is allowed and helps, use the flipped one, // otherwise leave it as is - if (!FitsInBounds(geo, PopupPositioningEdge.HorizontalMask) + if (!FitsInBounds(geo, PopupAnchor.HorizontalMask) && (constraintAdjustment & PopupPositionerConstraintAdjustment.FlipX) != 0) { var flipped = GetUnconstrained(anchor.FlipX(), gravity.FlipX()); - if (FitsInBounds(flipped, PopupPositioningEdge.HorizontalMask)) + if (FitsInBounds(flipped, PopupAnchor.HorizontalMask)) geo = geo.WithX(flipped.X); } @@ -163,11 +163,11 @@ namespace Avalonia.Controls.Primitives.PopupPositioning // If flipping geometry and anchor is allowed and helps, use the flipped one, // otherwise leave it as is - if (!FitsInBounds(geo, PopupPositioningEdge.VerticalMask) + if (!FitsInBounds(geo, PopupAnchor.VerticalMask) && (constraintAdjustment & PopupPositionerConstraintAdjustment.FlipY) != 0) { var flipped = GetUnconstrained(anchor.FlipY(), gravity.FlipY()); - if (FitsInBounds(flipped, PopupPositioningEdge.VerticalMask)) + if (FitsInBounds(flipped, PopupAnchor.VerticalMask)) geo = geo.WithY(flipped.Y); } diff --git a/src/Avalonia.Controls/Primitives/PopupRoot.cs b/src/Avalonia.Controls/Primitives/PopupRoot.cs index 788fe03162..3b167d52e1 100644 --- a/src/Avalonia.Controls/Primitives/PopupRoot.cs +++ b/src/Avalonia.Controls/Primitives/PopupRoot.cs @@ -82,11 +82,12 @@ namespace Avalonia.Controls.Primitives } public void ConfigurePosition(IVisual target, PlacementMode placement, Point offset, - PopupPositioningEdge anchor = PopupPositioningEdge.None, - PopupPositioningEdge gravity = PopupPositioningEdge.None) + PopupAnchor anchor = PopupAnchor.None, + PopupGravity gravity = PopupGravity.None, + Rect? rect = null) { _positionerParameters.ConfigurePosition(_parent, target, - placement, offset, anchor, gravity); + placement, offset, anchor, gravity, rect); if (_positionerParameters.Size != default) UpdatePosition(); From 4334c124b9ad177228d64e3ce7f1c7de6f12b889 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 21 May 2020 15:43:17 +0200 Subject: [PATCH 010/169] Added additional placement properties to ContextMenu. --- src/Avalonia.Controls/ContextMenu.cs | 92 +++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index 5a60fd37af..286cb6d6d8 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using Avalonia.Controls.Generators; using Avalonia.Controls.Platform; using Avalonia.Controls.Primitives; +using Avalonia.Controls.Primitives.PopupPositioning; using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.Interactivity; @@ -19,11 +20,6 @@ namespace Avalonia.Controls /// public class ContextMenu : MenuBase, ISetterValue { - /// - /// Defines the property. - /// - public static readonly StyledProperty PlacementModeProperty = - Popup.PlacementModeProperty.AddOwner(); /// /// Defines the property. @@ -37,6 +33,30 @@ namespace Avalonia.Controls public static readonly StyledProperty VerticalOffsetProperty = Popup.VerticalOffsetProperty.AddOwner(); + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementAnchorProperty = + Popup.PlacementAnchorProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementGravityProperty = + Popup.PlacementGravityProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementModeProperty = + Popup.PlacementModeProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty PlacementRectProperty = + AvaloniaProperty.Register(nameof(PlacementRect)); + /// /// Defines the property. /// @@ -77,16 +97,7 @@ namespace Avalonia.Controls } /// - /// Gets or sets the placement mode of the popup in relation to the . - /// - public PlacementMode PlacementMode - { - get { return GetValue(PlacementModeProperty); } - set { SetValue(PlacementModeProperty, value); } - } - - /// - /// Gets or sets the Horizontal offset of the popup in relation to the + /// Gets or sets the Horizontal offset of the context menu in relation to the . /// public double HorizontalOffset { @@ -95,7 +106,7 @@ namespace Avalonia.Controls } /// - /// Gets or sets the Vertical offset of the popup in relation to the + /// Gets or sets the Vertical offset of the popup in relation to the . /// public double VerticalOffset { @@ -103,6 +114,52 @@ namespace Avalonia.Controls set { SetValue(VerticalOffsetProperty, value); } } + /// + /// Gets or sets the anchor point on the when + /// is . + /// + public PopupAnchor PlacementAnchor + { + get { return GetValue(PlacementAnchorProperty); } + set { SetValue(PlacementAnchorProperty, value); } + } + + /// + /// Gets or sets a value which defines in what direction the context menu should open + /// when is . + /// + public PopupGravity PlacementGravity + { + get { return GetValue(PlacementGravityProperty); } + set { SetValue(PlacementGravityProperty, value); } + } + + /// + /// Gets or sets the placement mode of the context menu in relation to the. + /// + public PlacementMode PlacementMode + { + get { return GetValue(PlacementModeProperty); } + set { SetValue(PlacementModeProperty, value); } + } + + /// + /// Gets or sets the the anchor rectangle within the parent that the popup will be placed + /// relative to when is . + /// + /// + /// The placement rect defines a rectangle relative to around + /// which the popup will be opened, with determining which edge + /// of the placement target is used. + /// + /// If unset, the anchor rectangle will be the bounds of the . + /// + public Rect? PlacementRect + { + get { return GetValue(PlacementRectProperty); } + set { SetValue(PlacementRectProperty, value); } + } + /// /// Gets or sets the control that is used to determine the popup's position. /// @@ -187,7 +244,10 @@ namespace Avalonia.Controls { HorizontalOffset = HorizontalOffset, VerticalOffset = VerticalOffset, + PlacementAnchor = PlacementAnchor, + PlacementGravity = PlacementGravity, PlacementMode = PlacementMode, + PlacementRect = PlacementRect, PlacementTarget = PlacementTarget ?? control, StaysOpen = false }; From 964f0b85d44c64231e3eabbee8db5180dda64be2 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 21 May 2020 16:04:51 +0200 Subject: [PATCH 011/169] Tweak doc comments. --- src/Avalonia.Controls/ContextMenu.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index 286cb6d6d8..456af7bdde 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -106,7 +106,7 @@ namespace Avalonia.Controls } /// - /// Gets or sets the Vertical offset of the popup in relation to the . + /// Gets or sets the Vertical offset of the context menu in relation to the . /// public double VerticalOffset { @@ -144,7 +144,7 @@ namespace Avalonia.Controls } /// - /// Gets or sets the the anchor rectangle within the parent that the popup will be placed + /// Gets or sets the the anchor rectangle within the parent that the context menu will be placed /// relative to when is . /// /// From 9391ddbab5285d0d555f714692bd00628c07db9b Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Fri, 22 May 2020 10:33:58 +0200 Subject: [PATCH 012/169] Add failing tests for shorthand color parsing. --- .../Media/ColorTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs index e17fd47ff8..f8bd15593a 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs @@ -17,6 +17,17 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0xff, result.A); } + [Fact] + public void Parse_Parses_RGB_Hash_Shorthand_Color() + { + var result = Color.Parse("#f84"); + + Assert.Equal(0xff, result.R); + Assert.Equal(0x88, result.G); + Assert.Equal(0x44, result.B); + Assert.Equal(0xff, result.A); + } + [Fact] public void Parse_Parses_ARGB_Hash_Color() { @@ -28,6 +39,17 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0x40, result.A); } + [Fact] + public void Parse_Parses_ARGB_Hash_Shorthand_Color() + { + var result = Color.Parse("#4f84"); + + Assert.Equal(0xff, result.R); + Assert.Equal(0x88, result.G); + Assert.Equal(0x44, result.B); + Assert.Equal(0x44, result.A); + } + [Fact] public void Parse_Parses_Named_Color_Lowercase() { From 34fa98c013cdb7b2c5b7b5b12078cee166bb593e Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Fri, 22 May 2020 11:58:18 +0200 Subject: [PATCH 013/169] Unify color parsing and expose ROS and string based variants properly. --- src/Avalonia.Visuals/Media/Color.cs | 126 +++++++++++++++++++++------- 1 file changed, 97 insertions(+), 29 deletions(-) diff --git a/src/Avalonia.Visuals/Media/Color.cs b/src/Avalonia.Visuals/Media/Color.cs index 2e06d2578f..d52642ff9c 100644 --- a/src/Avalonia.Visuals/Media/Color.cs +++ b/src/Avalonia.Visuals/Media/Color.cs @@ -89,33 +89,62 @@ namespace Avalonia.Media /// The . public static Color Parse(string s) { - if (s == null) throw new ArgumentNullException(nameof(s)); - if (s.Length == 0) throw new FormatException(); + if (TryParse(s, out Color color)) + { + return color; + } - if (s[0] == '#') + throw new FormatException($"Invalid color string: '{s}'."); + } + + /// + /// Parses a color string. + /// + /// The color string. + /// The . + public static Color Parse(ReadOnlySpan s) + { + if (TryParse(s, out Color color)) { - var or = 0u; + return color; + } - if (s.Length == 7) - { - or = 0xff000000; - } - else if (s.Length != 9) - { - throw new FormatException($"Invalid color string: '{s}'."); - } + throw new FormatException($"Invalid color string: '{s.ToString()}'."); + } + + /// + /// Parses a color string. + /// + /// The color string. + /// The parsed color + /// The status of the operation. + public static bool TryParse(string s, out Color color) + { + if (s == null) + { + throw new ArgumentNullException(nameof(s)); + } + + if (s.Length == 0) + { + throw new FormatException(); + } - return FromUInt32(uint.Parse(s.Substring(1), NumberStyles.HexNumber, CultureInfo.InvariantCulture) | or); + if (TryParseInternal(s.AsSpan(), out color)) + { + return true; } var knownColor = KnownColors.GetKnownColor(s); if (knownColor != KnownColor.None) { - return knownColor.ToColor(); + color = knownColor.ToColor(); + + return true; } - throw new FormatException($"Invalid color string: '{s}'."); + return false; } /// @@ -126,40 +155,79 @@ namespace Avalonia.Media /// The status of the operation. public static bool TryParse(ReadOnlySpan s, out Color color) { - color = default; - if (s == null) - return false; if (s.Length == 0) + { + color = default; + return false; + } if (s[0] == '#') { - var or = 0u; + return TryParseInternal(s, out color); + } + + var knownColor = KnownColors.GetKnownColor(s.ToString()); + + if (knownColor != KnownColor.None) + { + color = knownColor.ToColor(); + + return true; + } + + color = default; - if (s.Length == 7) + return false; + } + + private static bool TryParseInternal(ReadOnlySpan s, out Color color) + { + static bool TryParseCore(ReadOnlySpan input, ref Color color) + { + var alphaComponent = 0u; + + if (input.Length == 6) { - or = 0xff000000; + alphaComponent = 0xff000000; } - else if (s.Length != 9) + else if (input.Length != 8) { return false; } - if(!uint.TryParse(s.Slice(1).ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var parsed)) + // TODO: (netstandard 2.1) Can use allocation free parsing. + if (!uint.TryParse(input.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, + out var parsed)) + { return false; - color = FromUInt32(parsed| or); + } + + color = FromUInt32(parsed | alphaComponent); + return true; } - var knownColor = KnownColors.GetKnownColor(s.ToString()); + color = default; - if (knownColor != KnownColor.None) + ReadOnlySpan input = s.Slice(1); + + // Handle shorthand cases like #FFF (RGB) or #FFFF (ARGB) + if (input.Length == 3 || input.Length == 4) { - color = knownColor.ToColor(); - return true; + var extendedLength = 2 * input.Length; + Span extended = stackalloc char[extendedLength]; + + for (int i = 0; i < input.Length; i++) + { + extended[2 * i + 0] = input[i]; + extended[2 * i + 1] = input[i]; + } + + return TryParseCore(extended, ref color); } - return false; + return TryParseCore(input, ref color); } /// From 9c4bcbdea987b8a8b0a819cd78d2ba924973e91a Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Fri, 22 May 2020 11:58:35 +0200 Subject: [PATCH 014/169] Add more coverage for TryParse --- .../Media/ColorTests.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs index f8bd15593a..f3f3c9a4ca 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs @@ -17,6 +17,18 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0xff, result.A); } + [Fact] + public void Try_Parse_Parses_RGB_Hash_Color() + { + var success = Color.TryParse("#ff8844", out Color result); + + Assert.True(success); + Assert.Equal(0xff, result.R); + Assert.Equal(0x88, result.G); + Assert.Equal(0x44, result.B); + Assert.Equal(0xff, result.A); + } + [Fact] public void Parse_Parses_RGB_Hash_Shorthand_Color() { @@ -28,6 +40,18 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0xff, result.A); } + [Fact] + public void Try_Parse_Parses_RGB_Hash_Shorthand_Color() + { + var success = Color.TryParse("#f84", out Color result); + + Assert.True(success); + Assert.Equal(0xff, result.R); + Assert.Equal(0x88, result.G); + Assert.Equal(0x44, result.B); + Assert.Equal(0xff, result.A); + } + [Fact] public void Parse_Parses_ARGB_Hash_Color() { @@ -39,6 +63,18 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0x40, result.A); } + [Fact] + public void Try_Parse_Parses_ARGB_Hash_Color() + { + var success = Color.TryParse("#40ff8844", out Color result); + + Assert.True(success); + Assert.Equal(0xff, result.R); + Assert.Equal(0x88, result.G); + Assert.Equal(0x44, result.B); + Assert.Equal(0x40, result.A); + } + [Fact] public void Parse_Parses_ARGB_Hash_Shorthand_Color() { @@ -50,6 +86,18 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0x44, result.A); } + [Fact] + public void Try_Parse_Parses_ARGB_Hash_Shorthand_Color() + { + var success = Color.TryParse("#4f84", out Color result); + + Assert.True(success); + Assert.Equal(0xff, result.R); + Assert.Equal(0x88, result.G); + Assert.Equal(0x44, result.B); + Assert.Equal(0x44, result.A); + } + [Fact] public void Parse_Parses_Named_Color_Lowercase() { @@ -61,6 +109,18 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0xff, result.A); } + [Fact] + public void TryParse_Parses_Named_Color_Lowercase() + { + var success = Color.TryParse("red", out Color result); + + Assert.True(success); + Assert.Equal(0xff, result.R); + Assert.Equal(0x00, result.G); + Assert.Equal(0x00, result.B); + Assert.Equal(0xff, result.A); + } + [Fact] public void Parse_Parses_Named_Color_Uppercase() { @@ -72,22 +132,52 @@ namespace Avalonia.Visuals.UnitTests.Media Assert.Equal(0xff, result.A); } + [Fact] + public void TryParse_Parses_Named_Color_Uppercase() + { + var success = Color.TryParse("RED", out Color result); + + Assert.True(success); + Assert.Equal(0xff, result.R); + Assert.Equal(0x00, result.G); + Assert.Equal(0x00, result.B); + Assert.Equal(0xff, result.A); + } + [Fact] public void Parse_Hex_Value_Doesnt_Accept_Too_Few_Chars() { Assert.Throws(() => Color.Parse("#ff")); } + [Fact] + public void TryParse_Hex_Value_Doesnt_Accept_Too_Few_Chars() + { + Assert.False(Color.TryParse("#ff", out _)); + } + [Fact] public void Parse_Hex_Value_Doesnt_Accept_Too_Many_Chars() { Assert.Throws(() => Color.Parse("#ff5555555")); } + [Fact] + public void TryParse_Hex_Value_Doesnt_Accept_Too_Many_Chars() + { + Assert.False(Color.TryParse("#ff5555555", out _)); + } + [Fact] public void Parse_Hex_Value_Doesnt_Accept_Invalid_Number() { Assert.Throws(() => Color.Parse("#ff808g80")); } + + [Fact] + public void TryParse_Hex_Value_Doesnt_Accept_Invalid_Number() + { + Assert.False(Color.TryParse("#ff808g80", out _)); + } } } From cdce7d96348f27f15c758a299a521d522184719f Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Fri, 22 May 2020 13:24:47 +0200 Subject: [PATCH 015/169] Avoid redundant parsing of named colors in TryParse. --- src/Avalonia.Visuals/Media/Color.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Visuals/Media/Color.cs b/src/Avalonia.Visuals/Media/Color.cs index d52642ff9c..052ee5e1b7 100644 --- a/src/Avalonia.Visuals/Media/Color.cs +++ b/src/Avalonia.Visuals/Media/Color.cs @@ -130,7 +130,7 @@ namespace Avalonia.Media throw new FormatException(); } - if (TryParseInternal(s.AsSpan(), out color)) + if (s[0] == '#' && TryParseInternal(s.AsSpan(), out color)) { return true; } @@ -144,6 +144,8 @@ namespace Avalonia.Media return true; } + color = default; + return false; } @@ -212,7 +214,7 @@ namespace Avalonia.Media ReadOnlySpan input = s.Slice(1); - // Handle shorthand cases like #FFF (RGB) or #FFFF (ARGB) + // Handle shorthand cases like #FFF (RGB) or #FFFF (ARGB). if (input.Length == 3 || input.Length == 4) { var extendedLength = 2 * input.Length; From d69718ab61d75c5542449a436bca5202b33e8b3e Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Sat, 23 May 2020 21:00:41 +0200 Subject: [PATCH 016/169] Reimplement new child window support. --- samples/ControlCatalog/Pages/DialogsPage.xaml | 2 + .../ControlCatalog/Pages/DialogsPage.xaml.cs | 15 ++++ src/Avalonia.Controls/Window.cs | 84 ++++++++++++++++--- 3 files changed, 88 insertions(+), 13 deletions(-) diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml b/samples/ControlCatalog/Pages/DialogsPage.xaml index 0834e829d8..a0e82663bf 100644 --- a/samples/ControlCatalog/Pages/DialogsPage.xaml +++ b/samples/ControlCatalog/Pages/DialogsPage.xaml @@ -11,5 +11,7 @@ + + diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs index dcb94a89e7..cf6c771e34 100644 --- a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs +++ b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs @@ -93,6 +93,21 @@ namespace ControlCatalog.Pages window.ShowInTaskbar = false; window.ShowDialog(GetWindow()); }; + this.FindControl public class Window : WindowBase, IStyleable, IFocusScope, ILayoutRoot { - private List _children = new List(); + private readonly List<(Window child, bool isDialog)> _children = new List<(Window, bool)>(); /// /// Defines the property. @@ -376,7 +376,7 @@ namespace Avalonia.Controls private void CloseInternal() { - foreach (var child in _children.ToList()) + foreach (var (child, _) in _children.ToList()) { // if we HandleClosing() before then there will be no children. child.CloseInternal(); @@ -399,7 +399,7 @@ namespace Avalonia.Controls { bool canClose = true; - foreach (var child in _children.ToList()) + foreach (var (child, _) in _children.ToList()) { if (!child.HandleClosing()) { @@ -472,6 +472,27 @@ namespace Avalonia.Controls /// The window has already been closed. /// public override void Show() + { + ShowCore(null); + } + + /// + /// Shows the window as a child of . + /// + /// + /// The window has already been closed. + /// + public void Show(Window parent) + { + if (parent is null) + { + throw new ArgumentNullException(nameof(parent), "Showing a child window requires valid parent."); + } + + ShowCore(parent); + } + + private void ShowCore(Window parent) { if (PlatformImpl == null) { @@ -483,7 +504,7 @@ namespace Avalonia.Controls return; } - this.RaiseEvent(new RoutedEventArgs(WindowOpenedEvent)); + RaiseEvent(new RoutedEventArgs(WindowOpenedEvent)); EnsureInitialized(); IsVisible = true; @@ -504,6 +525,14 @@ namespace Avalonia.Controls using (BeginAutoSizing()) { + if (parent != null) + { + PlatformImpl?.SetParent(parent.PlatformImpl); + } + + Owner = parent; + parent?.AddChild(this, false); + PlatformImpl?.Show(); Renderer?.Start(); } @@ -571,9 +600,9 @@ namespace Avalonia.Controls using (BeginAutoSizing()) { - PlatformImpl.SetParent(owner.PlatformImpl); + PlatformImpl?.SetParent(owner.PlatformImpl); Owner = owner; - owner.AddChild(this); + owner.AddChild(this, true); PlatformImpl?.Show(); Renderer?.Start(); @@ -598,28 +627,57 @@ namespace Avalonia.Controls private void UpdateEnabled() { - PlatformImpl.SetEnabled(_children.Count == 0); + bool isEnabled = true; + + foreach (var (_, isDialog) in _children) + { + if (isDialog) + { + isEnabled = false; + break; + } + } + + PlatformImpl.SetEnabled(isEnabled); } - private void AddChild(Window window) + private void AddChild(Window window, bool isDialog) { - _children.Add(window); + _children.Add((window, isDialog)); UpdateEnabled(); } private void RemoveChild(Window window) { - _children.Remove(window); + for (int i = _children.Count - 1; i >= 0; i--) + { + var (child, _) = _children[i]; + + if (ReferenceEquals(child, window)) + { + _children.RemoveAt(i); + } + } + UpdateEnabled(); } private void OnGotInputWhenDisabled() { - var firstChild = _children.FirstOrDefault(); + Window firstDialogChild = null; + + foreach (var (child, isDialog) in _children) + { + if (isDialog) + { + firstDialogChild = child; + break; + } + } - if (firstChild != null) + if (firstDialogChild != null) { - firstChild.OnGotInputWhenDisabled(); + firstDialogChild.OnGotInputWhenDisabled(); } else { From a04e187fa09fd3255024e77183b43f484e101f40 Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Sat, 23 May 2020 21:02:43 +0200 Subject: [PATCH 017/169] Extra parameter doc. --- src/Avalonia.Controls/Window.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 24e0e90417..782dc69b39 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -479,6 +479,7 @@ namespace Avalonia.Controls /// /// Shows the window as a child of . /// + /// Window that will be a parent of the shown window. /// /// The window has already been closed. /// From cd37520b7141c8d0140be887e8ee0fcc85471c6a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 25 May 2020 16:43:22 +0200 Subject: [PATCH 018/169] Trim invalid selection when Source first assigned. `SelectionModel` can have a selection set before its `Source` is initialized. In this case, be sure to trim invalid selections from the model before continuing. Fixes #3919 --- src/Avalonia.Controls/IndexRange.cs | 47 +++++++++++ src/Avalonia.Controls/SelectionModel.cs | 7 +- .../SelectionModelChangeSet.cs | 2 +- src/Avalonia.Controls/SelectionNode.cs | 21 +++++ .../IndexRangeTests.cs | 82 +++++++++++++++++++ .../SelectionModelTests.cs | 44 +++++++--- 6 files changed, 187 insertions(+), 16 deletions(-) diff --git a/src/Avalonia.Controls/IndexRange.cs b/src/Avalonia.Controls/IndexRange.cs index 1dc161c699..e45d013af4 100644 --- a/src/Avalonia.Controls/IndexRange.cs +++ b/src/Avalonia.Controls/IndexRange.cs @@ -132,6 +132,53 @@ namespace Avalonia.Controls return result; } + public static int Intersect( + IList ranges, + IndexRange range, + IList? removed = null) + { + var result = 0; + + for (var i = 0; i < ranges.Count && range != s_invalid; ++i) + { + var existing = ranges[i]; + + if (existing.End < range.Begin || existing.Begin > range.End) + { + removed?.Add(existing); + ranges.RemoveAt(i--); + result += existing.Count; + } + else + { + if (existing.Begin < range.Begin) + { + var except = new IndexRange(existing.Begin, range.Begin - 1); + removed?.Add(except); + ranges[i] = existing = new IndexRange(range.Begin, existing.End); + result += except.Count; + } + + if (existing.End > range.End) + { + var except = new IndexRange(range.End + 1, existing.End); + removed?.Add(except); + ranges[i] = new IndexRange(existing.Begin, range.End); + result += except.Count; + } + } + } + + MergeRanges(ranges); + + if (removed is object) + { + MergeRanges(removed); + } + + return result; + } + public static int Remove( IList ranges, IndexRange range, diff --git a/src/Avalonia.Controls/SelectionModel.cs b/src/Avalonia.Controls/SelectionModel.cs index dd4934f9e5..7a64421326 100644 --- a/src/Avalonia.Controls/SelectionModel.cs +++ b/src/Avalonia.Controls/SelectionModel.cs @@ -45,12 +45,9 @@ namespace Avalonia.Controls if (_rootNode.Source != null) { - if (_rootNode.Source != null) + using (var operation = new Operation(this)) { - using (var operation = new Operation(this)) - { - ClearSelection(resetAnchor: true); - } + ClearSelection(resetAnchor: true); } } diff --git a/src/Avalonia.Controls/SelectionModelChangeSet.cs b/src/Avalonia.Controls/SelectionModelChangeSet.cs index 6e77dc5755..d1df38656a 100644 --- a/src/Avalonia.Controls/SelectionModelChangeSet.cs +++ b/src/Avalonia.Controls/SelectionModelChangeSet.cs @@ -135,7 +135,7 @@ namespace Avalonia.Controls if (index >= currentIndex && index < currentIndex + currentCount) { int targetIndex = GetIndexAt(getRanges(info), index - currentIndex); - item = info.Items?.GetAt(targetIndex); + item = info.Items?.Count > targetIndex ? info.Items?.GetAt(targetIndex) : null; break; } diff --git a/src/Avalonia.Controls/SelectionNode.cs b/src/Avalonia.Controls/SelectionNode.cs index 0b00db88c3..1a3bde1765 100644 --- a/src/Avalonia.Controls/SelectionNode.cs +++ b/src/Avalonia.Controls/SelectionNode.cs @@ -101,6 +101,7 @@ namespace Avalonia.Controls ItemsSourceView = newDataSource; + TrimInvalidSelections(); PopulateSelectedItemsFromSelectedIndices(); HookupCollectionChangedHandler(); OnSelectionChanged(); @@ -108,6 +109,26 @@ namespace Avalonia.Controls } } + private void TrimInvalidSelections() + { + if (_selected == null || ItemsSourceView == null) + { + return; + } + + var validRange = ItemsSourceView.Count > 0 ? new IndexRange(0, ItemsSourceView.Count - 1) : new IndexRange(-1, -1); + var removed = new List(); + var removedCount = IndexRange.Intersect(_selected, validRange, removed); + + if (removedCount > 0) + { + using var operation = _manager.Update(); + SelectedCount -= removedCount; + OnSelectionChanged(); + _operation!.Deselected(removed); + } + } + public ItemsSourceView? ItemsSourceView { get; private set; } public int DataCount => ItemsSourceView?.Count ?? 0; public int ChildrenNodeCount => _childrenNodes.Count; diff --git a/tests/Avalonia.Controls.UnitTests/IndexRangeTests.cs b/tests/Avalonia.Controls.UnitTests/IndexRangeTests.cs index e0f46d9fa9..e01c752658 100644 --- a/tests/Avalonia.Controls.UnitTests/IndexRangeTests.cs +++ b/tests/Avalonia.Controls.UnitTests/IndexRangeTests.cs @@ -127,6 +127,88 @@ namespace Avalonia.Controls.UnitTests Assert.Empty(selected); } + [Fact] + public void Intersect_Should_Remove_Items_From_Beginning() + { + var ranges = new List { new IndexRange(0, 10) }; + var removed = new List(); + var result = IndexRange.Intersect(ranges, new IndexRange(2, 12), removed); + + Assert.Equal(2, result); + Assert.Equal(new[] { new IndexRange(2, 10) }, ranges); + Assert.Equal(new[] { new IndexRange(0, 1) }, removed); + } + + [Fact] + public void Intersect_Should_Remove_Items_From_End() + { + var ranges = new List { new IndexRange(0, 10) }; + var removed = new List(); + var result = IndexRange.Intersect(ranges, new IndexRange(0, 8), removed); + + Assert.Equal(2, result); + Assert.Equal(new[] { new IndexRange(0, 8) }, ranges); + Assert.Equal(new[] { new IndexRange(9, 10) }, removed); + } + + [Fact] + public void Intersect_Should_Remove_Entire_Range_Start() + { + var ranges = new List { new IndexRange(0, 5), new IndexRange(6, 10) }; + var removed = new List(); + var result = IndexRange.Intersect(ranges, new IndexRange(6, 10), removed); + + Assert.Equal(6, result); + Assert.Equal(new[] { new IndexRange(6, 10) }, ranges); + Assert.Equal(new[] { new IndexRange(0, 5) }, removed); + } + + [Fact] + public void Intersect_Should_Remove_Entire_Range_End() + { + var ranges = new List { new IndexRange(0, 5), new IndexRange(6, 10) }; + var removed = new List(); + var result = IndexRange.Intersect(ranges, new IndexRange(0, 4), removed); + + Assert.Equal(6, result); + Assert.Equal(new[] { new IndexRange(0, 4) }, ranges); + Assert.Equal(new[] { new IndexRange(5, 10) }, removed); + } + + [Fact] + public void Intersect_Should_Remove_Entire_Range_Start_End() + { + var ranges = new List + { + new IndexRange(0, 2), + new IndexRange(3, 7), + new IndexRange(8, 10) + }; + var removed = new List(); + var result = IndexRange.Intersect(ranges, new IndexRange(3, 7), removed); + + Assert.Equal(6, result); + Assert.Equal(new[] { new IndexRange(3, 7) }, ranges); + Assert.Equal(new[] { new IndexRange(0, 2), new IndexRange(8, 10) }, removed); + } + + [Fact] + public void Intersect_Should_Remove_Entire_And_Partial_Range_Start_End() + { + var ranges = new List + { + new IndexRange(0, 2), + new IndexRange(3, 7), + new IndexRange(8, 10) + }; + var removed = new List(); + var result = IndexRange.Intersect(ranges, new IndexRange(4, 6), removed); + + Assert.Equal(8, result); + Assert.Equal(new[] { new IndexRange(4, 6) }, ranges); + Assert.Equal(new[] { new IndexRange(0, 3), new IndexRange(7, 10) }, removed); + } + [Fact] public void Remove_Should_Remove_Entire_Range() { diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index 211fc44caf..1dc5fead26 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -1775,7 +1775,17 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Assigning_Source_With_Less_Items_Than_Selection_Trims_Selection() + public void Initializing_Source_With_Less_Items_Than_Selection_Trims_Selection() + { + var data = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel(); + target.SelectedIndex = new IndexPath(4); + target.Source = data; + Assert.Empty(target.SelectedIndices); + } + + [Fact] + public void Initializing_Source_With_Less_Items_Than_Selection_Trims_Selection_RetainSelection() { var data = new[] { "foo", "bar", "baz" }; var target = new SelectionModel { RetainSelectionOnReset = true }; @@ -1785,7 +1795,7 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void Assigning_Source_With_Less_Items_Than_Multiple_Selection_Trims_Selection() + public void Initializing_Source_With_Less_Items_Than_Multiple_Selection_Trims_Selection() { var data = new[] { "foo", "bar", "baz" }; var target = new SelectionModel { RetainSelectionOnReset = true }; @@ -1796,17 +1806,31 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new IndexPath(2), target.SelectedIndices.First()); } - // test that going from non-null source to non-null source clears selection [Fact] - public void Changing_Source_With_Valid_IndexPath_Retains_Selection() + public void Initializing_Source_With_Less_Items_Than_Selection_Raises_SelectionChanged() { - var data = new[] { "foo", "bar", "baz", "boo", "hoo" }; - var smallerData = new[] { "foo", "bar", "baz" }; - var target = new SelectionModel { RetainSelectionOnReset = true }; + var data = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel(); + var raised = 0; + + target.SelectedIndex = new IndexPath(4); + + target.SelectionChanged += (s, e) => + { + if (raised == 0) + { + Assert.Equal(new[] { Path(4) }, e.DeselectedIndices); + Assert.Equal(new object[] { null }, e.DeselectedItems); + Assert.Empty(e.SelectedIndices); + Assert.Empty(e.SelectedItems); + } + + ++raised; + }; + target.Source = data; - target.SelectedIndex = new IndexPath(2); - target.Source = smallerData; - Assert.Equal(1, target.SelectedIndices.Count); + + Assert.Equal(2, raised); } private int GetSubscriberCount(AvaloniaList list) From 9dc9f9debae7cd082acaaf95276f2b49691754bc Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 26 May 2020 13:17:02 +0200 Subject: [PATCH 019/169] Ensure selection is reported correctly during batch update. --- src/Avalonia.Controls/SelectionModel.cs | 13 ++++++ .../SelectionModelTests.cs | 41 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Avalonia.Controls/SelectionModel.cs b/src/Avalonia.Controls/SelectionModel.cs index 0a74306e84..a40e0a62f4 100644 --- a/src/Avalonia.Controls/SelectionModel.cs +++ b/src/Avalonia.Controls/SelectionModel.cs @@ -629,6 +629,8 @@ namespace Avalonia.Controls { AnchorIndex = default; } + + OnSelectionChanged(); } private void OnSelectionChanged(SelectionModelSelectionChangedEventArgs? e = null) @@ -664,6 +666,8 @@ namespace Avalonia.Controls { AnchorIndex = new IndexPath(index); } + + OnSelectionChanged(); } private void SelectWithGroupImpl(int groupIndex, int itemIndex, bool select) @@ -680,6 +684,8 @@ namespace Avalonia.Controls { AnchorIndex = new IndexPath(groupIndex, itemIndex); } + + OnSelectionChanged(); } private void SelectWithPathImpl(IndexPath index, bool select) @@ -708,6 +714,8 @@ namespace Avalonia.Controls { AnchorIndex = index; } + + OnSelectionChanged(); } private void SelectRangeFromAnchorImpl(int index, bool select) @@ -721,6 +729,7 @@ namespace Avalonia.Controls } _rootNode.SelectRange(new IndexRange(anchorIndex, index), select); + OnSelectionChanged(); } private void SelectRangeFromAnchorWithGroupImpl(int endGroupIndex, int endItemIndex, bool select) @@ -754,6 +763,8 @@ namespace Avalonia.Controls int endIndex = groupIdx == endGroupIndex ? endItemIndex : groupNode.DataCount - 1; groupNode.SelectRange(new IndexRange(startIndex, endIndex), select); } + + OnSelectionChanged(); } private void SelectRangeImpl(IndexPath start, IndexPath end, bool select) @@ -781,6 +792,8 @@ namespace Avalonia.Controls info.ParentNode!.Select(info.Path.GetAt(info.Path.GetSize() - 1), select); } }); + + OnSelectionChanged(); } private void BeginOperation() diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index b9f93090b4..59552e7f93 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -1512,6 +1512,47 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(0, raised); } + [Fact] + public void Batch_Update_Selection_Is_Correct_Throughout() + { + var data = new[] { "foo", "bar", "baz", "qux" }; + var target = new SelectionModel { Source = data }; + var raised = 0; + + using (target.Update()) + { + target.Select(1); + + Assert.Equal(new IndexPath(1), target.SelectedIndex); + Assert.Equal(new[] { new IndexPath(1) }, target.SelectedIndices); + Assert.Equal("bar", target.SelectedItem); + Assert.Equal(new[] { "bar" }, target.SelectedItems); + + target.Deselect(1); + + Assert.Equal(new IndexPath(), target.SelectedIndex); + Assert.Empty(target.SelectedIndices); + Assert.Null(target.SelectedItem); + Assert.Empty(target.SelectedItems); + + target.SelectRange(new IndexPath(1), new IndexPath(1)); + + Assert.Equal(new IndexPath(1), target.SelectedIndex); + Assert.Equal(new[] { new IndexPath(1) }, target.SelectedIndices); + Assert.Equal("bar", target.SelectedItem); + Assert.Equal(new[] { "bar" }, target.SelectedItems); + + target.ClearSelection(); + + Assert.Equal(new IndexPath(), target.SelectedIndex); + Assert.Empty(target.SelectedIndices); + Assert.Null(target.SelectedItem); + Assert.Empty(target.SelectedItems); + } + + Assert.Equal(0, raised); + } + [Fact] public void AutoSelect_Selects_When_Enabled() { From 2255518851914fa1c6558d833aad837f88d531ea Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 26 May 2020 13:28:28 +0200 Subject: [PATCH 020/169] Apply auto-select at end of batch update. --- src/Avalonia.Controls/SelectionModel.cs | 41 +++++++++++++------ .../SelectionModelTests.cs | 24 +++++++++++ 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Controls/SelectionModel.cs b/src/Avalonia.Controls/SelectionModel.cs index a40e0a62f4..314c36d28d 100644 --- a/src/Avalonia.Controls/SelectionModel.cs +++ b/src/Avalonia.Controls/SelectionModel.cs @@ -46,14 +46,25 @@ namespace Avalonia.Controls if (_rootNode.Source != null) { - using (var operation = new Operation(this)) + // Temporarily prevent auto-select when switching source. + var restoreAutoSelect = _autoSelect; + _autoSelect = false; + + try + { + using (var operation = new Operation(this)) + { + ClearSelection(resetAnchor: true); + } + } + finally { - ClearSelection(resetAnchor: true); + _autoSelect = restoreAutoSelect; } } _rootNode.Source = value; - ApplyAutoSelect(); + ApplyAutoSelect(true); RaisePropertyChanged("Source"); @@ -111,7 +122,7 @@ namespace Avalonia.Controls if (_autoSelect != value) { _autoSelect = value; - ApplyAutoSelect(); + ApplyAutoSelect(true); } } } @@ -185,7 +196,6 @@ namespace Avalonia.Controls using var operation = new Operation(this); ClearSelection(resetAnchor: true); SelectWithPathImpl(value, select: true); - ApplyAutoSelect(); } } } @@ -381,21 +391,18 @@ namespace Avalonia.Controls { using var operation = new Operation(this); SelectImpl(index, select: false); - ApplyAutoSelect(); } public void Deselect(int groupIndex, int itemIndex) { using var operation = new Operation(this); SelectWithGroupImpl(groupIndex, itemIndex, select: false); - ApplyAutoSelect(); } public void DeselectAt(IndexPath index) { using var operation = new Operation(this); SelectWithPathImpl(index, select: false); - ApplyAutoSelect(); } public bool IsSelected(int index) => _rootNode.IsSelected(index); @@ -562,7 +569,6 @@ namespace Avalonia.Controls { using var operation = new Operation(this); ClearSelection(resetAnchor: true); - ApplyAutoSelect(); } public IDisposable Update() => new Operation(this); @@ -589,7 +595,7 @@ namespace Avalonia.Controls } OnSelectionChanged(e); - ApplyAutoSelect(); + ApplyAutoSelect(true); } internal IObservable? ResolvePath(object data, IndexPath dataIndexPath) @@ -816,6 +822,8 @@ namespace Avalonia.Controls if (--_operationCount == 0) { + ApplyAutoSelect(false); + var changes = new List(); _rootNode.EndOperation(changes); @@ -837,7 +845,7 @@ namespace Avalonia.Controls } } - private void ApplyAutoSelect() + private void ApplyAutoSelect(bool createOperation) { if (AutoSelect) { @@ -845,8 +853,15 @@ namespace Avalonia.Controls if (SelectedIndex == default && _rootNode.ItemsSourceView?.Count > 0) { - using var operation = new Operation(this); - SelectImpl(0, true); + if (createOperation) + { + using var operation = new Operation(this); + SelectImpl(0, true); + } + else + { + SelectImpl(0, true); + } } } } diff --git a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs index 59552e7f93..24e82a69d0 100644 --- a/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs @@ -1754,6 +1754,30 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(0, raised); } + [Fact] + public void AutoSelect_Is_Applied_At_End_Of_Batch_Update() + { + var data = new[] { "foo", "bar", "baz" }; + var target = new SelectionModel { AutoSelect = true, Source = data }; + + using (target.Update()) + { + target.ClearSelection(); + + Assert.Equal(new IndexPath(), target.SelectedIndex); + Assert.Empty(target.SelectedIndices); + Assert.Null(target.SelectedItem); + Assert.Empty(target.SelectedItems); + } + + Assert.Equal(new IndexPath(0), target.SelectedIndex); + Assert.Equal(new[] { new IndexPath(0) }, target.SelectedIndices); + Assert.Equal("foo", target.SelectedItem); + Assert.Equal(new[] { "foo" }, target.SelectedItems); + + Assert.Equal(new IndexPath(0), target.SelectedIndex); + } + [Fact] public void Can_Replace_Parent_Children_Collection() { From d504c861993bb002635e64fc470ba80509769a05 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 13 May 2020 13:00:51 +0200 Subject: [PATCH 021/169] Added a "Select Random Item" button to TreeView page. Causes an exception due to containers not being materialized. --- samples/ControlCatalog/Pages/TreeViewPage.xaml | 1 + .../ViewModels/TreeViewPageViewModel.cs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/samples/ControlCatalog/Pages/TreeViewPage.xaml b/samples/ControlCatalog/Pages/TreeViewPage.xaml index 6019d5f91f..789b45e62c 100644 --- a/samples/ControlCatalog/Pages/TreeViewPage.xaml +++ b/samples/ControlCatalog/Pages/TreeViewPage.xaml @@ -20,6 +20,7 @@ + Single diff --git a/samples/ControlCatalog/ViewModels/TreeViewPageViewModel.cs b/samples/ControlCatalog/ViewModels/TreeViewPageViewModel.cs index d396ef2b3d..5bc23e2fe5 100644 --- a/samples/ControlCatalog/ViewModels/TreeViewPageViewModel.cs +++ b/samples/ControlCatalog/ViewModels/TreeViewPageViewModel.cs @@ -23,12 +23,14 @@ namespace ControlCatalog.ViewModels AddItemCommand = ReactiveCommand.Create(AddItem); RemoveItemCommand = ReactiveCommand.Create(RemoveItem); + SelectRandomItemCommand = ReactiveCommand.Create(SelectRandomItem); } public ObservableCollection Items { get; } public SelectionModel Selection { get; } public ReactiveCommand AddItemCommand { get; } public ReactiveCommand RemoveItemCommand { get; } + public ReactiveCommand SelectRandomItemCommand { get; } public SelectionMode SelectionMode { @@ -74,6 +76,15 @@ namespace ControlCatalog.ViewModels } } + private void SelectRandomItem() + { + var random = new Random(); + var depth = random.Next(4); + var indexes = Enumerable.Range(0, 4).Select(x => random.Next(10)); + var path = new IndexPath(indexes); + Selection.SelectedIndex = path; + } + private void SelectionChanged(object sender, SelectionModelSelectionChangedEventArgs e) { var selected = string.Join(",", e.SelectedIndices); From 87868cd2bda806fd584a08860bb2222a05c0e7af Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 13 May 2020 14:30:22 +0200 Subject: [PATCH 022/169] Materialize TreeViewItems on selection. If we're selecting a particular tree view item, then materialize and expand the item's ancestors as `SelectionModel` requests children. Only do this if a particular item is being selected, not when an item is selected as part of a range select. To do this, needed to add a `FinalIndex` property to `SelectionModelChildrenRequestedEventArgs` in order to know if we're selecting a descendent of the item whose children are being requested. This is a massive hack, but I can't think of a better way to do it with the current `TreeView` implementation. --- src/Avalonia.Controls/IndexPath.cs | 20 ++++++++++++++ src/Avalonia.Controls/SelectionModel.cs | 27 ++++++++++++------- ...electionModelChildrenRequestedEventArgs.cs | 22 ++++++++++++++- src/Avalonia.Controls/SelectionNode.cs | 6 ++--- src/Avalonia.Controls/TreeView.cs | 9 ++++++- .../Utils/SelectionTreeHelper.cs | 8 +++--- 6 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/Avalonia.Controls/IndexPath.cs b/src/Avalonia.Controls/IndexPath.cs index 6c5aaf7ad1..73b75bc23d 100644 --- a/src/Avalonia.Controls/IndexPath.cs +++ b/src/Avalonia.Controls/IndexPath.cs @@ -123,6 +123,26 @@ namespace Avalonia.Controls } } + public bool IsAncestorOf(in IndexPath other) + { + if (other.GetSize() <= GetSize()) + { + return false; + } + + var size = GetSize(); + + for (int i = 0; i < size; i++) + { + if (GetAt(i) != other.GetAt(i)) + { + return false; + } + } + + return true; + } + public override string ToString() { if (_path != null) diff --git a/src/Avalonia.Controls/SelectionModel.cs b/src/Avalonia.Controls/SelectionModel.cs index 314c36d28d..ff1c0260bb 100644 --- a/src/Avalonia.Controls/SelectionModel.cs +++ b/src/Avalonia.Controls/SelectionModel.cs @@ -141,7 +141,7 @@ namespace Avalonia.Controls while (current?.AnchorIndex >= 0) { path.Add(current.AnchorIndex); - current = current.GetAt(current.AnchorIndex, false); + current = current.GetAt(current.AnchorIndex, false, default); } anchor = new IndexPath(path); @@ -420,7 +420,7 @@ namespace Avalonia.Controls for (int i = 0; i < path.GetSize() - 1; i++) { var childIndex = path.GetAt(i); - node = node.GetAt(childIndex, realizeChild: false); + node = node.GetAt(childIndex, false, default); if (node == null) { @@ -455,7 +455,7 @@ namespace Avalonia.Controls } var isSelected = (bool?)false; - var childNode = _rootNode.GetAt(groupIndex, realizeChild: false); + var childNode = _rootNode.GetAt(groupIndex, false, default); if (childNode != null) { @@ -474,7 +474,7 @@ namespace Avalonia.Controls for (int i = 0; i < path.GetSize() - 1; i++) { var childIndex = path.GetAt(i); - node = node.GetAt(childIndex, realizeChild: false); + node = node.GetAt(childIndex, false, default); if (node == null) { @@ -598,7 +598,10 @@ namespace Avalonia.Controls ApplyAutoSelect(true); } - internal IObservable? ResolvePath(object data, IndexPath dataIndexPath) + internal IObservable? ResolvePath( + object data, + IndexPath dataIndexPath, + IndexPath finalIndexPath) { IObservable? resolved = null; @@ -607,18 +610,22 @@ namespace Avalonia.Controls { if (_childrenRequestedEventArgs == null) { - _childrenRequestedEventArgs = new SelectionModelChildrenRequestedEventArgs(data, dataIndexPath, false); + _childrenRequestedEventArgs = new SelectionModelChildrenRequestedEventArgs( + data, + dataIndexPath, + finalIndexPath, + false); } else { - _childrenRequestedEventArgs.Initialize(data, dataIndexPath, false); + _childrenRequestedEventArgs.Initialize(data, dataIndexPath, finalIndexPath, false); } ChildrenRequested(this, _childrenRequestedEventArgs); resolved = _childrenRequestedEventArgs.Children; // Clear out the values in the args so that it cannot be used after the event handler call. - _childrenRequestedEventArgs.Initialize(null, default, true); + _childrenRequestedEventArgs.Initialize(null, default, default, true); } return resolved; @@ -683,7 +690,7 @@ namespace Avalonia.Controls ClearSelection(resetAnchor: true); } - var childNode = _rootNode.GetAt(groupIndex, realizeChild: true); + var childNode = _rootNode.GetAt(groupIndex, true, new IndexPath(groupIndex, itemIndex)); var selected = childNode!.Select(itemIndex, select); if (selected) @@ -764,7 +771,7 @@ namespace Avalonia.Controls for (int groupIdx = startGroupIndex; groupIdx <= endGroupIndex; groupIdx++) { - var groupNode = _rootNode.GetAt(groupIdx, realizeChild: true)!; + var groupNode = _rootNode.GetAt(groupIdx, true, new IndexPath(endGroupIndex, endItemIndex))!; int startIndex = groupIdx == startGroupIndex ? startItemIndex : 0; int endIndex = groupIdx == endGroupIndex ? endItemIndex : groupNode.DataCount - 1; groupNode.SelectRange(new IndexRange(startIndex, endIndex), select); diff --git a/src/Avalonia.Controls/SelectionModelChildrenRequestedEventArgs.cs b/src/Avalonia.Controls/SelectionModelChildrenRequestedEventArgs.cs index 974da0cf71..b1f3e0b2c4 100644 --- a/src/Avalonia.Controls/SelectionModelChildrenRequestedEventArgs.cs +++ b/src/Avalonia.Controls/SelectionModelChildrenRequestedEventArgs.cs @@ -16,15 +16,17 @@ namespace Avalonia.Controls { private object? _source; private IndexPath _sourceIndexPath; + private IndexPath _finalIndexPath; private bool _throwOnAccess; internal SelectionModelChildrenRequestedEventArgs( object source, IndexPath sourceIndexPath, + IndexPath finalIndexPath, bool throwOnAccess) { source = source ?? throw new ArgumentNullException(nameof(source)); - Initialize(source, sourceIndexPath, throwOnAccess); + Initialize(source, sourceIndexPath, finalIndexPath, throwOnAccess); } /// @@ -65,9 +67,26 @@ namespace Avalonia.Controls } } + /// + /// Gets the index of the final object which is being attempted to be retrieved. + /// + public IndexPath FinalIndex + { + get + { + if (_throwOnAccess) + { + throw new ObjectDisposedException(nameof(SelectionModelChildrenRequestedEventArgs)); + } + + return _finalIndexPath; + } + } + internal void Initialize( object? source, IndexPath sourceIndexPath, + IndexPath finalIndexPath, bool throwOnAccess) { if (!throwOnAccess && source == null) @@ -77,6 +96,7 @@ namespace Avalonia.Controls _source = source; _sourceIndexPath = sourceIndexPath; + _finalIndexPath = finalIndexPath; _throwOnAccess = throwOnAccess; } } diff --git a/src/Avalonia.Controls/SelectionNode.cs b/src/Avalonia.Controls/SelectionNode.cs index 1a3bde1765..d99606673e 100644 --- a/src/Avalonia.Controls/SelectionNode.cs +++ b/src/Avalonia.Controls/SelectionNode.cs @@ -162,7 +162,7 @@ namespace Avalonia.Controls // create a bunch of leaf node instances - instead i use the same instance m_leafNode to avoid // an explosion of node objects. However, I'm still creating the m_childrenNodes // collection unfortunately. - public SelectionNode? GetAt(int index, bool realizeChild) + public SelectionNode? GetAt(int index, bool realizeChild, IndexPath finalIndexPath) { SelectionNode? child = null; @@ -192,7 +192,7 @@ namespace Avalonia.Controls if (childData != null) { var childDataIndexPath = IndexPath.CloneWithChildIndex(index); - resolver = _manager.ResolvePath(childData, childDataIndexPath); + resolver = _manager.ResolvePath(childData, childDataIndexPath, finalIndexPath); } if (resolver != null) @@ -864,7 +864,7 @@ namespace Avalonia.Controls int notSelectedCount = 0; for (int i = 0; i < ChildrenNodeCount; i++) { - var child = GetAt(i, realizeChild: false); + var child = GetAt(i, false, default); if (child != null) { diff --git a/src/Avalonia.Controls/TreeView.cs b/src/Avalonia.Controls/TreeView.cs index 95e7437838..3e6ade39fc 100644 --- a/src/Avalonia.Controls/TreeView.cs +++ b/src/Avalonia.Controls/TreeView.cs @@ -395,10 +395,17 @@ namespace Avalonia.Controls private void OnSelectionModelChildrenRequested(object sender, SelectionModelChildrenRequestedEventArgs e) { - var container = ItemContainerGenerator.Index.ContainerFromItem(e.Source) as ItemsControl; + var container = ItemContainerGenerator.Index.ContainerFromItem(e.Source) as TreeViewItem; if (container is object) { + if (e.SourceIndex.IsAncestorOf(e.FinalIndex)) + { + container.IsExpanded = true; + container.ApplyTemplate(); + container.Presenter?.ApplyTemplate(); + } + e.Children = Observable.CombineLatest( container.GetObservable(TreeViewItem.IsExpandedProperty), container.GetObservable(ItemsProperty), diff --git a/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs b/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs index 430ecabbb8..5adf5bdeea 100644 --- a/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs +++ b/src/Avalonia.Controls/Utils/SelectionTreeHelper.cs @@ -28,7 +28,7 @@ namespace Avalonia.Controls.Utils if (depth < path.GetSize() - 1) { - node = node.GetAt(childIndex, realizeChildren)!; + node = node.GetAt(childIndex, realizeChildren, path)!; } } } @@ -50,7 +50,7 @@ namespace Avalonia.Controls.Utils int count = realizeChildren ? nextNode.Node.DataCount : nextNode.Node.ChildrenNodeCount; for (int i = count - 1; i >= 0; i--) { - var child = nextNode.Node.GetAt(i, realizeChildren); + var child = nextNode.Node.GetAt(i, realizeChildren, nextNode.Path); var childPath = nextNode.Path.CloneWithChildIndex(i); if (child != null) { @@ -90,7 +90,7 @@ namespace Avalonia.Controls.Utils for (int i = endIndex; i >= startIndex; i--) { - var child = node.GetAt(i, realizeChild: true); + var child = node.GetAt(i, true, end); if (child != null) { var childPath = currentPath.CloneWithChildIndex(i); @@ -112,7 +112,7 @@ namespace Avalonia.Controls.Utils int endIndex = depth < end.GetSize() && isEndPath ? end.GetAt(depth) : info.Node.DataCount - 1; for (int i = endIndex; i >= startIndex; i--) { - var child = info.Node.GetAt(i, realizeChild: true); + var child = info.Node.GetAt(i, true, end); if (child != null) { var childPath = info.Path.CloneWithChildIndex(i); From 9df84abbf3db84a74bc9fbee1b90e14b816a23d2 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 13 May 2020 14:55:33 +0200 Subject: [PATCH 023/169] Bring selected TreeViewItem into view after layout finishes. --- src/Avalonia.Controls/TreeView.cs | 2 +- .../TreeViewTests.cs | 548 ++++++++++-------- 2 files changed, 298 insertions(+), 252 deletions(-) diff --git a/src/Avalonia.Controls/TreeView.cs b/src/Avalonia.Controls/TreeView.cs index 3e6ade39fc..1e66bf4d69 100644 --- a/src/Avalonia.Controls/TreeView.cs +++ b/src/Avalonia.Controls/TreeView.cs @@ -346,7 +346,7 @@ namespace Avalonia.Controls if (container != null) { - container.BringIntoView(); + DispatcherTimer.RunOnce(container.BringIntoView, TimeSpan.Zero); } } } diff --git a/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs b/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs index 373f3e6861..c1bd45bcad 100644 --- a/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TreeViewTests.cs @@ -118,313 +118,340 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Clicking_Item_Should_Select_It() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var item = tree[0].Children[1].Children[0]; - var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item); + var item = tree[0].Children[1].Children[0]; + var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item); - Assert.NotNull(container); + Assert.NotNull(container); - _mouse.Click(container); + _mouse.Click(container); - Assert.Equal(item, target.SelectedItem); - Assert.True(container.IsSelected); + Assert.Equal(item, target.SelectedItem); + Assert.True(container.IsSelected); + } } [Fact] public void Clicking_WithControlModifier_Selected_Item_Should_Deselect_It() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var item = tree[0].Children[1].Children[0]; - var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item); + var item = tree[0].Children[1].Children[0]; + var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item); - Assert.NotNull(container); + Assert.NotNull(container); - target.SelectedItem = item; + target.SelectedItem = item; - Assert.True(container.IsSelected); + Assert.True(container.IsSelected); - _mouse.Click(container, modifiers: KeyModifiers.Control); + _mouse.Click(container, modifiers: KeyModifiers.Control); - Assert.Null(target.SelectedItem); - Assert.False(container.IsSelected); + Assert.Null(target.SelectedItem); + Assert.False(container.IsSelected); + } } [Fact] public void Clicking_WithControlModifier_Not_Selected_Item_Should_Select_It() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var item1 = tree[0].Children[1].Children[0]; - var container1 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1); + var item1 = tree[0].Children[1].Children[0]; + var container1 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1); - var item2 = tree[0].Children[1]; - var container2 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2); + var item2 = tree[0].Children[1]; + var container2 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2); - Assert.NotNull(container1); - Assert.NotNull(container2); + Assert.NotNull(container1); + Assert.NotNull(container2); - target.SelectedItem = item1; + target.SelectedItem = item1; - Assert.True(container1.IsSelected); + Assert.True(container1.IsSelected); - _mouse.Click(container2, modifiers: KeyModifiers.Control); - - Assert.Equal(item2, target.SelectedItem); - Assert.False(container1.IsSelected); - Assert.True(container2.IsSelected); + _mouse.Click(container2, modifiers: KeyModifiers.Control); + + Assert.Equal(item2, target.SelectedItem); + Assert.False(container1.IsSelected); + Assert.True(container2.IsSelected); + } } [Fact] public void Clicking_WithControlModifier_Selected_Item_Should_Deselect_And_Remove_From_SelectedItems() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - SelectionMode = SelectionMode.Multiple - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + SelectionMode = SelectionMode.Multiple + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var rootNode = tree[0]; + var rootNode = tree[0]; - var item1 = rootNode.Children[0]; - var item2 = rootNode.Children.Last(); + var item1 = rootNode.Children[0]; + var item2 = rootNode.Children.Last(); - var item1Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1); - var item2Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2); + var item1Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1); + var item2Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2); - ClickContainer(item1Container, KeyModifiers.Control); - Assert.True(item1Container.IsSelected); + ClickContainer(item1Container, KeyModifiers.Control); + Assert.True(item1Container.IsSelected); - ClickContainer(item2Container, KeyModifiers.Control); - Assert.True(item2Container.IsSelected); + ClickContainer(item2Container, KeyModifiers.Control); + Assert.True(item2Container.IsSelected); - Assert.Equal(new[] {item1, item2}, target.Selection.SelectedItems.OfType()); + Assert.Equal(new[] { item1, item2 }, target.Selection.SelectedItems.OfType()); - ClickContainer(item1Container, KeyModifiers.Control); - Assert.False(item1Container.IsSelected); + ClickContainer(item1Container, KeyModifiers.Control); + Assert.False(item1Container.IsSelected); - Assert.DoesNotContain(item1, target.Selection.SelectedItems.OfType()); + Assert.DoesNotContain(item1, target.Selection.SelectedItems.OfType()); + } } [Fact] public void Clicking_WithShiftModifier_DownDirection_Should_Select_Range_Of_Items() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - SelectionMode = SelectionMode.Multiple - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + SelectionMode = SelectionMode.Multiple + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var rootNode = tree[0]; + var rootNode = tree[0]; - var from = rootNode.Children[0]; - var to = rootNode.Children.Last(); + var from = rootNode.Children[0]; + var to = rootNode.Children.Last(); - var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); - var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); + var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); + var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); - ClickContainer(fromContainer, KeyModifiers.None); + ClickContainer(fromContainer, KeyModifiers.None); - Assert.True(fromContainer.IsSelected); + Assert.True(fromContainer.IsSelected); - ClickContainer(toContainer, KeyModifiers.Shift); - AssertChildrenSelected(target, rootNode); + ClickContainer(toContainer, KeyModifiers.Shift); + AssertChildrenSelected(target, rootNode); + } } [Fact] public void Clicking_WithShiftModifier_UpDirection_Should_Select_Range_Of_Items() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - SelectionMode = SelectionMode.Multiple - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + SelectionMode = SelectionMode.Multiple + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var rootNode = tree[0]; + var rootNode = tree[0]; - var from = rootNode.Children.Last(); - var to = rootNode.Children[0]; + var from = rootNode.Children.Last(); + var to = rootNode.Children[0]; - var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); - var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); + var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); + var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); - ClickContainer(fromContainer, KeyModifiers.None); + ClickContainer(fromContainer, KeyModifiers.None); - Assert.True(fromContainer.IsSelected); + Assert.True(fromContainer.IsSelected); - ClickContainer(toContainer, KeyModifiers.Shift); - AssertChildrenSelected(target, rootNode); + ClickContainer(toContainer, KeyModifiers.Shift); + AssertChildrenSelected(target, rootNode); + } } [Fact] public void Clicking_First_Item_Of_SelectedItems_Should_Select_Only_It() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - SelectionMode = SelectionMode.Multiple - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + SelectionMode = SelectionMode.Multiple + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var rootNode = tree[0]; + var rootNode = tree[0]; - var from = rootNode.Children.Last(); - var to = rootNode.Children[0]; + var from = rootNode.Children.Last(); + var to = rootNode.Children[0]; - var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); - var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); + var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); + var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); - ClickContainer(fromContainer, KeyModifiers.None); + ClickContainer(fromContainer, KeyModifiers.None); - ClickContainer(toContainer, KeyModifiers.Shift); - AssertChildrenSelected(target, rootNode); + ClickContainer(toContainer, KeyModifiers.Shift); + AssertChildrenSelected(target, rootNode); - ClickContainer(fromContainer, KeyModifiers.None); + ClickContainer(fromContainer, KeyModifiers.None); - Assert.True(fromContainer.IsSelected); + Assert.True(fromContainer.IsSelected); - foreach (var child in rootNode.Children) - { - if (child == from) + foreach (var child in rootNode.Children) { - continue; - } + if (child == from) + { + continue; + } - var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(child); + var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(child); - Assert.False(container.IsSelected); + Assert.False(container.IsSelected); + } } } [Fact] public void Setting_SelectedItem_Should_Set_Container_Selected() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var item = tree[0].Children[1].Children[0]; - var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item); + var item = tree[0].Children[1].Children[0]; + var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item); - Assert.NotNull(container); + Assert.NotNull(container); - target.SelectedItem = item; + target.SelectedItem = item; - Assert.True(container.IsSelected); + Assert.True(container.IsSelected); + } } [Fact] public void Setting_SelectedItem_Should_Raise_SelectedItemChanged_Event() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - ExpandAll(target); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + ExpandAll(target); - var item = tree[0].Children[1].Children[0]; + var item = tree[0].Children[1].Children[0]; - var called = false; - target.SelectionChanged += (s, e) => - { - Assert.Empty(e.RemovedItems); - Assert.Equal(1, e.AddedItems.Count); - Assert.Same(item, e.AddedItems[0]); - called = true; - }; + var called = false; + target.SelectionChanged += (s, e) => + { + Assert.Empty(e.RemovedItems); + Assert.Equal(1, e.AddedItems.Count); + Assert.Same(item, e.AddedItems[0]); + called = true; + }; - target.SelectedItem = item; - Assert.True(called); + target.SelectedItem = item; + Assert.True(called); + } } [Fact] @@ -564,7 +591,7 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Keyboard_Navigation_Should_Move_To_Last_Selected_Node() { - using (UnitTestApplication.Start(TestServices.RealFocus)) + using (Application()) { var focus = FocusManager.Instance; var navigation = AvaloniaLocator.Current.GetService(); @@ -647,7 +674,7 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Pressing_SelectAll_Gesture_With_Downward_Range_Selected_Should_Select_All_Nodes() { - using (UnitTestApplication.Start()) + using (Application()) { var tree = CreateTestTreeData(); var target = new TreeView @@ -694,7 +721,7 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Pressing_SelectAll_Gesture_With_Upward_Range_Selected_Should_Select_All_Nodes() { - using (UnitTestApplication.Start()) + using (Application()) { var tree = CreateTestTreeData(); var target = new TreeView @@ -768,97 +795,106 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Right_Click_On_UnselectedItem_Should_Clear_Existing_Selection() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - SelectionMode = SelectionMode.Multiple, - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + SelectionMode = SelectionMode.Multiple, + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]); - var rootNode = tree[0]; - var to = rootNode.Children[0]; - var then = rootNode.Children[1]; + var rootNode = tree[0]; + var to = rootNode.Children[0]; + var then = rootNode.Children[1]; - var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(rootNode); - var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); - var thenContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(then); + var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(rootNode); + var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); + var thenContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(then); - ClickContainer(fromContainer, KeyModifiers.None); - ClickContainer(toContainer, KeyModifiers.Shift); + ClickContainer(fromContainer, KeyModifiers.None); + ClickContainer(toContainer, KeyModifiers.Shift); - Assert.Equal(2, target.Selection.SelectedItems.Count); + Assert.Equal(2, target.Selection.SelectedItems.Count); - _mouse.Click(thenContainer, MouseButton.Right); + _mouse.Click(thenContainer, MouseButton.Right); - Assert.Equal(1, target.Selection.SelectedItems.Count); + Assert.Equal(1, target.Selection.SelectedItems.Count); + } } [Fact] public void Shift_Right_Click_Should_Not_Select_Multiple() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - SelectionMode = SelectionMode.Multiple, - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + SelectionMode = SelectionMode.Multiple, + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]); - var rootNode = tree[0]; - var from = rootNode.Children[0]; - var to = rootNode.Children[1]; - var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); - var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); + var rootNode = tree[0]; + var from = rootNode.Children[0]; + var to = rootNode.Children[1]; + var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); + var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); - _mouse.Click(fromContainer); - _mouse.Click(toContainer, MouseButton.Right, modifiers: KeyModifiers.Shift); + _mouse.Click(fromContainer); + _mouse.Click(toContainer, MouseButton.Right, modifiers: KeyModifiers.Shift); - Assert.Equal(1, target.Selection.SelectedItems.Count); + Assert.Equal(1, target.Selection.SelectedItems.Count); + } } [Fact] public void Ctrl_Right_Click_Should_Not_Select_Multiple() { - var tree = CreateTestTreeData(); - var target = new TreeView + using (Application()) { - Template = CreateTreeViewTemplate(), - Items = tree, - SelectionMode = SelectionMode.Multiple, - }; + var tree = CreateTestTreeData(); + var target = new TreeView + { + Template = CreateTreeViewTemplate(), + Items = tree, + SelectionMode = SelectionMode.Multiple, + }; - var visualRoot = new TestRoot(); - visualRoot.Child = target; + var visualRoot = new TestRoot(); + visualRoot.Child = target; - CreateNodeDataTemplate(target); - ApplyTemplates(target); - target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]); + CreateNodeDataTemplate(target); + ApplyTemplates(target); + target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]); - var rootNode = tree[0]; - var from = rootNode.Children[0]; - var to = rootNode.Children[1]; - var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); - var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); + var rootNode = tree[0]; + var from = rootNode.Children[0]; + var to = rootNode.Children[1]; + var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from); + var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to); - _mouse.Click(fromContainer); - _mouse.Click(toContainer, MouseButton.Right, modifiers: KeyModifiers.Control); + _mouse.Click(fromContainer); + _mouse.Click(toContainer, MouseButton.Right, modifiers: KeyModifiers.Control); - Assert.Equal(1, target.Selection.SelectedItems.Count); + Assert.Equal(1, target.Selection.SelectedItems.Count); + } } [Fact] @@ -944,7 +980,7 @@ namespace Avalonia.Controls.UnitTests public void Auto_Expanding_In_Style_Should_Not_Break_Range_Selection() { /// Issue #2980. - using (UnitTestApplication.Start(TestServices.RealStyler)) + using (Application()) { var target = new DerivedTreeView { @@ -1183,12 +1219,12 @@ namespace Avalonia.Controls.UnitTests } } - void ClickContainer(IControl container, KeyModifiers modifiers) + private void ClickContainer(IControl container, KeyModifiers modifiers) { _mouse.Click(container, modifiers: modifiers); } - void AssertChildrenSelected(TreeView treeView, Node rootNode) + private void AssertChildrenSelected(TreeView treeView, Node rootNode) { foreach (var child in rootNode.Children) { @@ -1198,6 +1234,16 @@ namespace Avalonia.Controls.UnitTests } } + private IDisposable Application() + { + return UnitTestApplication.Start( + TestServices.MockThreadingInterface.With( + focusManager: new FocusManager(), + keyboardDevice: () => new KeyboardDevice(), + keyboardNavigation: new KeyboardNavigationHandler(), + inputManager: new InputManager())); + } + private class Node : NotifyingBase { private IAvaloniaList _children; From a3d3690470aa6787a5c2531dab7c0208df6373ea Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 26 May 2020 18:20:19 +0200 Subject: [PATCH 024/169] Use SelectionModel for DevTools tree. --- .../Diagnostics/ViewModels/TreeNode.cs | 38 ++++++++++++++++++- .../ViewModels/TreePageViewModel.cs | 24 +++++++++--- .../Diagnostics/Views/TreePageView.xaml | 2 +- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreeNode.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreeNode.cs index c8a9da600d..aa27538abc 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreeNode.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreeNode.cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; using System.Collections.Specialized; using System.Reactive; using System.Reactive.Linq; using Avalonia.Collections; using Avalonia.Controls; using Avalonia.LogicalTree; -using Avalonia.Styling; using Avalonia.VisualTree; namespace Avalonia.Diagnostics.ViewModels @@ -82,5 +82,41 @@ namespace Avalonia.Diagnostics.ViewModels get; private set; } + + public IndexPath Index + { + get + { + var indices = new List(); + var child = this; + var parent = Parent; + + while (parent is object) + { + indices.Add(IndexOf(parent.Children, child)); + child = child.Parent; + parent = parent.Parent; + } + + indices.Add(0); + indices.Reverse(); + return new IndexPath(indices); + } + } + + private static int IndexOf(IReadOnlyList collection, TreeNode item) + { + var count = collection.Count; + + for (var i = 0; i < count; ++i) + { + if (collection[i] == item) + { + return i; + } + } + + throw new AvaloniaInternalException("TreeNode was not present in parent Children collection."); + } } } diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs index 26b5fe2524..4496cc0a1c 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/TreePageViewModel.cs @@ -6,28 +6,40 @@ namespace Avalonia.Diagnostics.ViewModels { internal class TreePageViewModel : ViewModelBase, IDisposable { - private TreeNode _selected; + private TreeNode _selectedNode; private ControlDetailsViewModel _details; private string _propertyFilter; public TreePageViewModel(TreeNode[] nodes) { Nodes = nodes; - } + Selection = new SelectionModel + { + SingleSelect = true, + Source = Nodes + }; + + Selection.SelectionChanged += (s, e) => + { + SelectedNode = (TreeNode)Selection.SelectedItem; + }; + } public TreeNode[] Nodes { get; protected set; } + public SelectionModel Selection { get; } + public TreeNode SelectedNode { - get => _selected; - set + get => _selectedNode; + private set { if (Details != null) { _propertyFilter = Details.PropertyFilter; } - if (RaiseAndSetIfChanged(ref _selected, value)) + if (RaiseAndSetIfChanged(ref _selectedNode, value)) { Details = value != null ? new ControlDetailsViewModel(value.Visual, _propertyFilter) : @@ -83,7 +95,7 @@ namespace Avalonia.Diagnostics.ViewModels if (node != null) { - SelectedNode = node; + Selection.SelectedIndex = node.Index; ExpandNode(node.Parent); } } diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml b/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml index a1e6ca7d37..4ddb320175 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml @@ -6,7 +6,7 @@ + Selection="{Binding Selection}"> From d6cce80809ad8303cc48b2fd827d9b1c93f65f5f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 26 May 2020 18:20:42 +0200 Subject: [PATCH 025/169] Add clarification. --- src/Avalonia.Controls/TreeView.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls/TreeView.cs b/src/Avalonia.Controls/TreeView.cs index 1e66bf4d69..a91655855c 100644 --- a/src/Avalonia.Controls/TreeView.cs +++ b/src/Avalonia.Controls/TreeView.cs @@ -119,9 +119,10 @@ namespace Avalonia.Controls /// /// Gets or sets the selected item. /// - /// - /// Gets or sets the selected item. - /// + /// + /// Note that setting this property only currently works if the item is expanded to be visible. + /// To select non-expanded nodes use `Selection.SelectedIndex`. + /// public object SelectedItem { get => Selection.SelectedItem; From a99963e3a0e4172d257ec63c3be765ac3086f389 Mon Sep 17 00:00:00 2001 From: amwx Date: Fri, 29 May 2020 04:08:07 -0500 Subject: [PATCH 026/169] Fluent Calendar style --- .../Accents/FluentControlResourcesDark.xaml | 28 ++ .../Accents/FluentControlResourcesLight.xaml | 28 ++ src/Avalonia.Themes.Fluent/Calendar.xaml | 13 +- .../CalendarButton.xaml | 124 ++++---- .../CalendarDayButton.xaml | 156 +++++----- src/Avalonia.Themes.Fluent/CalendarItem.xaml | 272 +++++++----------- 6 files changed, 321 insertions(+), 300 deletions(-) diff --git a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml index a0f3ef0d62..fd6203d82d 100644 --- a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml +++ b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml @@ -153,5 +153,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml index ec4f35664c..436b8af3d6 100644 --- a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml +++ b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml @@ -153,5 +153,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia.Themes.Fluent/Calendar.xaml b/src/Avalonia.Themes.Fluent/Calendar.xaml index 6bbee4ef17..2fbc705d83 100644 --- a/src/Avalonia.Themes.Fluent/Calendar.xaml +++ b/src/Avalonia.Themes.Fluent/Calendar.xaml @@ -7,11 +7,12 @@ - \ No newline at end of file + diff --git a/src/Avalonia.Themes.Fluent/CalendarButton.xaml b/src/Avalonia.Themes.Fluent/CalendarButton.xaml index 3314534b3b..2ba4489204 100644 --- a/src/Avalonia.Themes.Fluent/CalendarButton.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarButton.xaml @@ -7,75 +7,99 @@ - - --> + + - - - - - - - + + + - - + + + + diff --git a/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml b/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml index 2d79e62a75..1e5953ab2d 100644 --- a/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml @@ -7,110 +7,102 @@ - - - - + --> - - - - - - - - - - - - - - - - + diff --git a/src/Avalonia.Themes.Fluent/CalendarItem.xaml b/src/Avalonia.Themes.Fluent/CalendarItem.xaml index dfd89ed82f..41f3fcbc1f 100644 --- a/src/Avalonia.Themes.Fluent/CalendarItem.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarItem.xaml @@ -8,176 +8,124 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - From 3b48112e1d3f44beaf0df52eb738554e3bd81e57 Mon Sep 17 00:00:00 2001 From: amwx Date: Fri, 29 May 2020 04:24:07 -0500 Subject: [PATCH 027/169] Fluent Calendar Style --- src/Avalonia.Themes.Fluent/Calendar.xaml | 2 +- src/Avalonia.Themes.Fluent/CalendarButton.xaml | 9 ++++++--- src/Avalonia.Themes.Fluent/CalendarDayButton.xaml | 14 ++++++-------- src/Avalonia.Themes.Fluent/CalendarItem.xaml | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Themes.Fluent/Calendar.xaml b/src/Avalonia.Themes.Fluent/Calendar.xaml index 2fbc705d83..6ae334dbae 100644 --- a/src/Avalonia.Themes.Fluent/Calendar.xaml +++ b/src/Avalonia.Themes.Fluent/Calendar.xaml @@ -5,7 +5,7 @@ // All other rights reserved. --> - + - \ No newline at end of file + diff --git a/src/Avalonia.Themes.Fluent/CalendarButton.xaml b/src/Avalonia.Themes.Fluent/CalendarButton.xaml index 3314534b3b..2ba4489204 100644 --- a/src/Avalonia.Themes.Fluent/CalendarButton.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarButton.xaml @@ -7,75 +7,99 @@ - - --> + + - - - - - - - + + + - - + + + + diff --git a/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml b/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml index 2d79e62a75..1e5953ab2d 100644 --- a/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml @@ -7,110 +7,102 @@ - - - - + --> - - - - - - - - - - - - - - - - + diff --git a/src/Avalonia.Themes.Fluent/CalendarItem.xaml b/src/Avalonia.Themes.Fluent/CalendarItem.xaml index dfd89ed82f..41f3fcbc1f 100644 --- a/src/Avalonia.Themes.Fluent/CalendarItem.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarItem.xaml @@ -8,176 +8,124 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - From 5065e6c072020860312c01053365105759c5e7eb Mon Sep 17 00:00:00 2001 From: amwx Date: Fri, 29 May 2020 04:24:07 -0500 Subject: [PATCH 032/169] Fluent Calendar Style --- src/Avalonia.Themes.Fluent/Calendar.xaml | 2 +- src/Avalonia.Themes.Fluent/CalendarButton.xaml | 9 ++++++--- src/Avalonia.Themes.Fluent/CalendarDayButton.xaml | 14 ++++++-------- src/Avalonia.Themes.Fluent/CalendarItem.xaml | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Themes.Fluent/Calendar.xaml b/src/Avalonia.Themes.Fluent/Calendar.xaml index 2fbc705d83..6ae334dbae 100644 --- a/src/Avalonia.Themes.Fluent/Calendar.xaml +++ b/src/Avalonia.Themes.Fluent/Calendar.xaml @@ -5,7 +5,7 @@ // All other rights reserved. --> - + --> + + diff --git a/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml b/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml index 4f6f5951d2..ff72044c93 100644 --- a/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarDayButton.xaml @@ -19,12 +19,14 @@ + + + BorderThickness="0" ClipToBounds="True"> + BorderBrush="{TemplateBinding BorderBrush}"/> - + + + --> + diff --git a/src/Avalonia.Themes.Fluent/CalendarItem.xaml b/src/Avalonia.Themes.Fluent/CalendarItem.xaml index 7e17ee7075..df17da84dc 100644 --- a/src/Avalonia.Themes.Fluent/CalendarItem.xaml +++ b/src/Avalonia.Themes.Fluent/CalendarItem.xaml @@ -61,6 +61,9 @@ + + 1 + 11,9,11,10 + 11,4,11,7 + + + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml index 15e157f573..e6c6a1bb56 100644 --- a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml +++ b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml @@ -101,8 +101,8 @@ - - + + @@ -150,7 +150,7 @@ - + 0 @@ -169,6 +169,43 @@ + + 1 + 11,9,11,10 + 11,4,11,7 + + + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 9d5345e621534075273619ebda6f98bf98caad0f Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sun, 31 May 2020 18:15:27 +0800 Subject: [PATCH 037/169] Import Fluent Slider --- samples/ControlCatalog/Pages/SliderPage.xaml | 2 + src/Avalonia.Controls/Primitives/Track.cs | 14 +- src/Avalonia.Controls/Slider.cs | 155 ++++--- src/Avalonia.Controls/TickBar.cs | 416 ++++++++++++++++++ .../Accents/FluentBaseDark.xaml | 27 +- .../Accents/FluentBaseLight.xaml | 27 +- .../Accents/FluentControlResourcesDark.xaml | 56 ++- .../Accents/FluentControlResourcesLight.xaml | 56 ++- src/Avalonia.Themes.Fluent/Slider.xaml | 323 ++++++++++---- 9 files changed, 938 insertions(+), 138 deletions(-) create mode 100644 src/Avalonia.Controls/TickBar.cs diff --git a/samples/ControlCatalog/Pages/SliderPage.xaml b/samples/ControlCatalog/Pages/SliderPage.xaml index 58f7b881fe..c6f5521e60 100644 --- a/samples/ControlCatalog/Pages/SliderPage.xaml +++ b/samples/ControlCatalog/Pages/SliderPage.xaml @@ -9,12 +9,14 @@ diff --git a/src/Avalonia.Controls/Primitives/Track.cs b/src/Avalonia.Controls/Primitives/Track.cs index e104a8a664..1db47a13e7 100644 --- a/src/Avalonia.Controls/Primitives/Track.cs +++ b/src/Avalonia.Controls/Primitives/Track.cs @@ -41,13 +41,16 @@ namespace Avalonia.Controls.Primitives public static readonly StyledProperty IsDirectionReversedProperty = AvaloniaProperty.Register(nameof(IsDirectionReversed)); + public static readonly StyledProperty IgnoreThumbDragProperty = + AvaloniaProperty.Register(nameof(IsThumbDragHandled)); + private double _minimum; private double _maximum = 100.0; private double _value; static Track() { - ThumbProperty.Changed.AddClassHandler((x,e) => x.ThumbChanged(e)); + ThumbProperty.Changed.AddClassHandler((x, e) => x.ThumbChanged(e)); IncreaseButtonProperty.Changed.AddClassHandler((x, e) => x.ButtonChanged(e)); DecreaseButtonProperty.Changed.AddClassHandler((x, e) => x.ButtonChanged(e)); AffectsArrange(MinimumProperty, MaximumProperty, ValueProperty, OrientationProperty); @@ -113,6 +116,12 @@ namespace Avalonia.Controls.Primitives set { SetValue(IsDirectionReversedProperty, value); } } + public bool IsThumbDragHandled + { + get { return GetValue(IgnoreThumbDragProperty); } + set { SetValue(IgnoreThumbDragProperty, value); } + } + private double ThumbCenterOffset { get; set; } private double Density { get; set; } @@ -422,6 +431,9 @@ namespace Avalonia.Controls.Primitives private void ThumbDragged(object sender, VectorEventArgs e) { + if (IsThumbDragHandled) + return; + Value = MathUtilities.Clamp( Value + ValueFromDistance(e.Vector.X, e.Vector.Y), Minimum, diff --git a/src/Avalonia.Controls/Slider.cs b/src/Avalonia.Controls/Slider.cs index e92c8faf20..de15685e4d 100644 --- a/src/Avalonia.Controls/Slider.cs +++ b/src/Avalonia.Controls/Slider.cs @@ -1,12 +1,40 @@ using System; +using Avalonia.Controls.Mixins; using Avalonia.Controls.Primitives; -using Avalonia.Data; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Layout; +using Avalonia.Utilities; namespace Avalonia.Controls { + + /// + /// Enum which describes how to position the ticks in a . + /// + public enum TickPlacement + { + /// + /// No tick marks will appear. + /// + None, + + /// + /// Tick marks will appear above the track for a horizontal , or to the left of the track for a vertical . + /// + TopLeft, + + /// + /// Tick marks will appear below the track for a horizontal , or to the right of the track for a vertical . + /// + BottomRight, + + /// + /// Tick marks appear on both sides of either a horizontal or vertical . + /// + Outside + } + /// /// A control that lets the user select from a range of values by moving a Thumb control along a Track. /// @@ -30,19 +58,31 @@ namespace Avalonia.Controls public static readonly StyledProperty TickFrequencyProperty = AvaloniaProperty.Register(nameof(TickFrequency), 0.0); + /// + /// Defines the property. + /// + public static readonly StyledProperty TickPlacementProperty = + AvaloniaProperty.Register(nameof(TickPlacement), 0d); + // Slider required parts + private bool _isDragging = false; private Track _track; private Button _decreaseButton; private Button _increaseButton; + private IDisposable _decreaseButtonPressDispose; + private IDisposable _decreaseButtonReleaseDispose; + private IDisposable _increaseButtonSubscription; + private IDisposable _increaseButtonReleaseDispose; + private IDisposable _pointerMovedDispose; /// /// Initializes static members of the class. /// static Slider() { + PressedMixin.Attach(); OrientationProperty.OverrideDefaultValue(typeof(Slider), Orientation.Horizontal); Thumb.DragStartedEvent.AddClassHandler((x, e) => x.OnThumbDragStarted(e), RoutingStrategies.Bubble); - Thumb.DragDeltaEvent.AddClassHandler((x, e) => x.OnThumbDragDelta(e), RoutingStrategies.Bubble); Thumb.DragCompletedEvent.AddClassHandler((x, e) => x.OnThumbDragCompleted(e), RoutingStrategies.Bubble); } @@ -81,57 +121,83 @@ namespace Avalonia.Controls set { SetValue(TickFrequencyProperty, value); } } + /// + /// Gets or sets a value that indicates where to draw + /// tick marks in relation to the track. + /// + public TickPlacement TickPlacement + { + get { return GetValue(TickPlacementProperty); } + set { SetValue(TickPlacementProperty, value); } + } + /// protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { - if (_decreaseButton != null) - { - _decreaseButton.Click -= DecreaseClick; - } - - if (_increaseButton != null) - { - _increaseButton.Click -= IncreaseClick; - } + _decreaseButtonPressDispose?.Dispose(); + _decreaseButtonReleaseDispose?.Dispose(); + _increaseButtonSubscription?.Dispose(); + _increaseButtonReleaseDispose?.Dispose(); + _pointerMovedDispose?.Dispose(); _decreaseButton = e.NameScope.Find