public void Should_Not_Create_Items_Before_Added_To_Visual_Tree()
        {
            var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
            var target = new TestItemsPresenter(true)
            {
                Items = items,
                ItemsPanel = VirtualizingPanelTemplate(Orientation.Vertical),
                ItemTemplate = ItemTemplate(),
                VirtualizationMode = ItemVirtualizationMode.Simple,
            };

            var scroller = new ScrollContentPresenter
            {
                Content = target,
            };

            scroller.UpdateChild();
            target.ApplyTemplate();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Empty(target.Panel.Children);

            var root = new TestRoot
            {
                Child = scroller,
            };

            target.InvalidateMeasure();
            target.Panel.InvalidateMeasure();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(10, target.Panel.Children.Count);
        }
        public void Measure_Should_Pass_Unchanged_Bounds_To_IScrollable()
        {
            var scrollable = new TestScrollable();
            var target = new ScrollContentPresenter
            {
                Content = scrollable,
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));

            Assert.Equal(new Size(100, 100), scrollable.AvailableSize);
        }
        public void Content_Can_Be_Stretched()
        {
            Border content;
            var target = new ScrollContentPresenter
            {
                Content = content = new Border
                {
                    Padding = new Thickness(8),
                },
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Rect(0, 0, 100, 100), content.Bounds);
        }
        public void Content_Can_Be_Bottom_Aligned()
        {
            Border content;
            var target = new ScrollContentPresenter
            {
                Content = content = new Border
                {
                    Padding = new Thickness(8),
                    VerticalAlignment = VerticalAlignment.Bottom,
                },
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Rect(0, 84, 100, 16), content.Bounds);
        }
        public void Content_Can_Be_Right_Aligned()
        {
            Border content;
            var target = new ScrollContentPresenter
            {
                Content = content = new Border
                {
                    Padding = new Thickness(8),
                    HorizontalAlignment = HorizontalAlignment.Right
                },
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Rect(84, 0, 16, 100), content.Bounds);
        }
        public void Arrange_Should_Not_Set_Viewport_And_Extent_With_IScrollable()
        {
            var target = new ScrollContentPresenter
            {
                Content = new TestScrollable()
            };

            var changed = false;

            target.UpdateChild();
            target.Measure(new Size(100, 100));

            target.GetObservable(ScrollViewer.ViewportProperty).Skip(1).Subscribe(_ => changed = true);
            target.GetObservable(ScrollViewer.ExtentProperty).Skip(1).Subscribe(_ => changed = true);

            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.False(changed);
        }
        public void Arrange_Should_Offset_IScrollable_Bounds_When_Logical_Scroll_Disabled()
        {
            var scrollable = new TestScrollable
            {
                IsLogicalScrollEnabled = false,
            };

            var target = new ScrollContentPresenter
            {
                Content = scrollable,
                Offset = new Vector(25, 25),
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Rect(-25, -25, 150, 150), scrollable.Bounds);
        }
        public void Arrange_Should_Not_Offset_IScrollable_Bounds()
        {
            var scrollable = new TestScrollable
            {
                Extent = new Size(100, 100),
                Offset = new Vector(50, 50),
                Viewport = new Size(25, 25),
            };

            var target = new ScrollContentPresenter
            {
                Content = scrollable,
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Rect(0, 0, 100, 100), scrollable.Bounds);
        }
        public void Changing_Content_Should_Update_State()
        {
            var logicalScrollable = new TestScrollable
            {
                Extent = new Size(100, 100),
                Offset = new Vector(50, 50),
                Viewport = new Size(25, 25),
            };

            var nonLogicalScrollable = new TestScrollable
            {
                IsLogicalScrollEnabled = false,
            };

            var target = new ScrollContentPresenter
            {
                Content = logicalScrollable,
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(logicalScrollable.Extent, target.Extent);
            Assert.Equal(logicalScrollable.Offset, target.Offset);
            Assert.Equal(logicalScrollable.Viewport, target.Viewport);
            Assert.Equal(new Rect(0, 0, 100, 100), logicalScrollable.Bounds);

            target.Content = nonLogicalScrollable;
            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Size(150, 150), target.Extent);
            Assert.Equal(new Vector(0, 0), target.Offset);
            Assert.Equal(new Size(100, 100), target.Viewport);
            Assert.Equal(new Rect(0, 0, 150, 150), nonLogicalScrollable.Bounds);

            target.Content = logicalScrollable;
            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(logicalScrollable.Extent, target.Extent);
            Assert.Equal(logicalScrollable.Offset, target.Offset);
            Assert.Equal(logicalScrollable.Viewport, target.Viewport);
            Assert.Equal(new Rect(0, 0, 100, 100), logicalScrollable.Bounds);
        }
        public void Toggling_IsLogicalScrollEnabled_Should_Update_State()
        {
            var scrollable = new TestScrollable
            {
                Extent = new Size(100, 100),
                Offset = new Vector(50, 50),
                Viewport = new Size(25, 25),
            };

            var target = new ScrollContentPresenter
            {
                Content = scrollable,
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(scrollable.Extent, target.Extent);
            Assert.Equal(scrollable.Offset, target.Offset);
            Assert.Equal(scrollable.Viewport, target.Viewport);
            Assert.Equal(new Rect(0, 0, 100, 100), scrollable.Bounds);

            scrollable.IsLogicalScrollEnabled = false;
            scrollable.InvalidateScroll();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Size(150, 150), target.Extent);
            Assert.Equal(new Vector(0, 0), target.Offset);
            Assert.Equal(new Size(100, 100), target.Viewport);
            Assert.Equal(new Rect(0, 0, 150, 150), scrollable.Bounds);

            scrollable.IsLogicalScrollEnabled = true;
            scrollable.InvalidateScroll();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(scrollable.Extent, target.Extent);
            Assert.Equal(scrollable.Offset, target.Offset);
            Assert.Equal(scrollable.Viewport, target.Viewport);
            Assert.Equal(new Rect(0, 0, 100, 100), scrollable.Bounds);
        }
        public void Offset_Should_Not_Be_Written_To_IScrollable_After_Removal()
        {
            var scrollable = new TestScrollable
            {
                Extent = new Size(100, 100),
                Offset = new Vector(50, 50),
            };

            var target = new ScrollContentPresenter
            {
                Content = scrollable
            };

            target.Content = null;
            target.Offset = new Vector(25, 25);

            Assert.Equal(new Vector(50, 50), scrollable.Offset);
        }
        public void Offset_Should_Be_Written_To_IScrollable()
        {
            var scrollable = new TestScrollable
            {
                Extent = new Size(100, 100),
                Offset = new Vector(50, 50),
            };

            var target = new ScrollContentPresenter
            {
                Content = scrollable
            };

            target.UpdateChild();
            target.Offset = new Vector(25, 25);

            Assert.Equal(target.Offset, scrollable.Offset);
        }
        public void Content_Can_Be_Offset()
        {
            Border content;
            var target = new ScrollContentPresenter
            {
                Content = content = new Border
                {
                    Width = 150,
                    Height = 150,
                },
                Offset = new Vector(25, 25),
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Rect(-25, -25, 150, 150), content.Bounds);
        }
        public void Extent_Offset_And_Viewport_Should_Be_Read_From_IScrollable()
        {
            var scrollable = new TestScrollable
            {
                Extent = new Size(100, 100),
                Offset = new Vector(50, 50),
                Viewport = new Size(25, 25),
            };

            var target = new ScrollContentPresenter
            {
                Content = scrollable
            };

            target.UpdateChild();

            Assert.Equal(scrollable.Extent, target.Extent);
            Assert.Equal(scrollable.Offset, target.Offset);
            Assert.Equal(scrollable.Viewport, target.Viewport);

            scrollable.Extent = new Size(200, 200);
            scrollable.Offset = new Vector(100, 100);
            scrollable.Viewport = new Size(50, 50);

            Assert.Equal(scrollable.Extent, target.Extent);
            Assert.Equal(scrollable.Offset, target.Offset);
            Assert.Equal(scrollable.Viewport, target.Viewport);
        }
        public void Arrange_Should_Set_Viewport_And_Extent_In_That_Order()
        {
            var target = new ScrollContentPresenter
            {
                Content = new Border { Width = 40, Height = 50 }
            };

            var set = new List<string>();

            target.UpdateChild();
            target.Measure(new Size(100, 100));

            target.GetObservable(ScrollViewer.ViewportProperty).Skip(1).Subscribe(_ => set.Add("Viewport"));
            target.GetObservable(ScrollViewer.ExtentProperty).Skip(1).Subscribe(_ => set.Add("Extent"));

            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new[] { "Viewport", "Extent" }, set);
        }
        public void GetVisualsAt_Should_Not_Find_Control_Outside_Scroll_Viewport()
        {
            using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
            {
                Border target;
                Border item1;
                Border item2;
                ScrollContentPresenter scroll;

                var container = new Panel
                {
                    Width = 100,
                    Height = 200,
                    Children = new Controls.Controls
                    {
                        (target = new Border()
                        {
                            Width = 100,
                            Height = 100
                        }),
                        new Border()
                        {
                            Width = 100,
                            Height = 100,
                            Margin = new Thickness(0, 100, 0, 0),
                            Child = scroll = new ScrollContentPresenter()
                            {
                                Content = new StackPanel()
                                {
                                    Children = new Controls.Controls
                                    {
                                        (item1 = new Border()
                                        {
                                            Width = 100,
                                            Height = 100,
                                        }),
                                        (item2 = new Border()
                                        {
                                            Width = 100,
                                            Height = 100,
                                        }),
                                    }
                                }
                            }
                        }
                    }
                };

                scroll.UpdateChild();

                container.Measure(Size.Infinity);
                container.Arrange(new Rect(container.DesiredSize));

                var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
                context.Render(container);

                var result = container.GetVisualsAt(new Point(50, 150)).First();

                Assert.Equal(item1, result);

                result = container.GetVisualsAt(new Point(50, 50)).First();

                Assert.Equal(target, result);

                scroll.Offset = new Vector(0, 100);

                //we don't have setup LayoutManager so we will make it manually
                scroll.Parent.InvalidateArrange();
                container.InvalidateArrange();

                container.Arrange(new Rect(container.DesiredSize));
                context.Render(container);

                result = container.GetVisualsAt(new Point(50, 150)).First();

                Assert.Equal(item2, result);

                result = container.GetVisualsAt(new Point(50, 50)).First();

                Assert.NotEqual(item1, result);
                Assert.Equal(target, result);
            }
        }
        public void Measure_Should_Pass_Bounded_X_If_CannotScrollHorizontally()
        {
            var child = new TestControl();
            var target = new ScrollContentPresenter
            {
                Content = child,
                [ScrollContentPresenter.CanScrollHorizontallyProperty] = false,
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));

            Assert.Equal(new Size(100, double.PositiveInfinity), child.AvailableSize);
        }
        public void BringDescendentIntoView_Should_Handle_Child_Margin()
        {
            Border border;
            var target = new ScrollContentPresenter
            {
                Width = 100,
                Height = 100,
                Content = new Decorator
                {
                    Margin = new Thickness(50),
                    Child = border = new Border
                    {
                        Width = 200,
                        Height = 200,
                    }
                }
            };

            target.UpdateChild();
            target.Measure(Size.Infinity);
            target.Arrange(new Rect(0, 0, 100, 100));
            target.BringDescendentIntoView(border, new Rect(200, 200, 0, 0));

            Assert.Equal(new Vector(150, 150), target.Offset);
        }
        public void BringDescendentIntoView_Should_Update_Offset()
        {
            var target = new ScrollContentPresenter
            {
                Width = 100,
                Height = 100,
                Content = new Border
                {
                    Width = 200,
                    Height = 200,
                }
            };

            target.UpdateChild();
            target.Measure(Size.Infinity);
            target.Arrange(new Rect(0, 0, 100, 100));
            target.BringDescendentIntoView(target.Child, new Rect(200, 200, 0, 0));

            Assert.Equal(new Vector(100, 100), target.Offset);
        }
        public void Setting_Offset_Should_Invalidate_Arrange()
        {
            var target = new ScrollContentPresenter
            {
                Content = new Border { Width = 140, Height = 150 }
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));
            target.Offset = new Vector(10, 100);

            Assert.True(target.IsMeasureValid);
            Assert.False(target.IsArrangeValid);
        }
        public void Content_Can_Be_Larger_Than_Viewport()
        {
            TestControl content;
            var target = new ScrollContentPresenter
            {
                Content = content = new TestControl(),
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));
            target.Arrange(new Rect(0, 0, 100, 100));

            Assert.Equal(new Rect(0, 0, 150, 150), content.Bounds);
        }
        public void Measure_Should_Pass_Unbounded_X_If_CanScrollHorizontally()
        {
            var child = new TestControl();
            var target = new ScrollContentPresenter
            {
                Content = child,
            };

            target.UpdateChild();
            target.Measure(new Size(100, 100));

            Assert.Equal(Size.Infinity, child.AvailableSize);
        }
        public void InvalidateScroll_Should_Be_Set_When_Set_As_Content()
        {
            var scrollable = new TestScrollable();
            var target = new ScrollContentPresenter
            {
                Content = scrollable
            };

            target.UpdateChild();

            Assert.NotNull(scrollable.InvalidateScroll);
        }
        public void InvalidateScroll_Should_Be_Cleared_When_Removed_From_Content()
        {
            var scrollable = new TestScrollable();
            var target = new ScrollContentPresenter
            {
                Content = scrollable
            };

            target.UpdateChild();
            target.Content = null;
            target.UpdateChild();

            Assert.Null(scrollable.InvalidateScroll);
        }
示例#25
0
        private static Control ScrollViewerTemplate(ScrollViewer control)
        {
            var result = new ScrollContentPresenter
            {
                Name = "PART_ContentPresenter",
                [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
            };

            return result;
        }
        private static ItemsPresenter CreateTarget(
            Orientation orientation = Orientation.Vertical,
            bool useContainers = true,
            int itemCount = 20,
            bool useAvaloniaList = false)
        {
            ItemsPresenter result;
            var itemsSource = Enumerable.Range(0, itemCount).Select(x => $"Item {x}");
            var items = useAvaloniaList ?
                (IEnumerable)new AvaloniaList<string>(itemsSource) :
                (IEnumerable)new ObservableCollection<string>(itemsSource);

            var scroller = new ScrollContentPresenter
            {
                Content = result = new TestItemsPresenter(useContainers)
                {
                    Items = items,
                    ItemsPanel = VirtualizingPanelTemplate(orientation),
                    ItemTemplate = ItemTemplate(),
                    VirtualizationMode = ItemVirtualizationMode.Simple,
                }
            };

            scroller.UpdateChild();

            return result;
        }