Exemplo n.º 1
0
        public void Invalidating_Child_Should_Remeasure_Parent()
        {
            var layoutManager = new LayoutManager();

            using (AvaloniaLocator.EnterScope())
            {
                AvaloniaLocator.CurrentMutable.Bind<ILayoutManager>().ToConstant(layoutManager);

                Border border;
                StackPanel panel;

                var root = new TestLayoutRoot
                {
                    Child = panel = new StackPanel
                    {
                        Children = new Controls.Controls
                    {
                        (border = new Border())
                    }
                    }
                };

                layoutManager.ExecuteInitialLayoutPass(root);
                Assert.Equal(new Size(0, 0), root.DesiredSize);

                border.Width = 100;
                border.Height = 100;

                layoutManager.ExecuteLayoutPass();
                Assert.Equal(new Size(100, 100), panel.DesiredSize);
            }                
        }
Exemplo n.º 2
0
        public void Removing_From_Parent_Should_Invalidate_Measure_Of_Control_And_Descendents()
        {
            var panel = new StackPanel();
            var child2 = new Border();
            var child1 = new Border { Child = child2 };
            panel.Children.Add(child1);

            panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            Assert.True(child1.IsMeasureValid);
            Assert.True(child2.IsMeasureValid);

            panel.Children.Remove(child1);
            Assert.False(child1.IsMeasureValid);
            Assert.False(child2.IsMeasureValid);
        }
Exemplo n.º 3
0
        public void Tapped_Should_Be_Raised_Even_When_PointerPressed_Handled()
        {
            Border border = new Border();
            var decorator = new Decorator
            {
                Child = border
            };
            var result = new List<string>();

            border.AddHandler(Border.PointerPressedEvent, (s, e) => e.Handled = true);
            decorator.AddHandler(Gestures.TappedEvent, (s, e) => result.Add("dt"));
            border.AddHandler(Gestures.TappedEvent, (s, e) => result.Add("bt"));

            border.RaiseEvent(new PointerPressedEventArgs());
            border.RaiseEvent(new PointerReleasedEventArgs());

            Assert.Equal(new[] { "bt", "dt" }, result);
        }
Exemplo n.º 4
0
        public void Invalidating_Child_Should_Not_Invalidate_Parent()
        {
            var panel = new StackPanel();
            var child = new Border();
            panel.Children.Add(child);

            panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

            Assert.Equal(new Size(0, 0), panel.DesiredSize);

            child.Width = 100;
            child.Height = 100;

            Assert.True(panel.IsMeasureValid);
            Assert.False(child.IsMeasureValid);

            panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            Assert.Equal(new Size(0, 0), panel.DesiredSize);
        }
Exemplo n.º 5
0
        public void FindStyleResource_Should_Find_Correct_Resource()
        {
            Border target;

            var tree = new Decorator
            {
                Styles = new Styles
                {
                    new Style
                    {
                        Resources = new Dictionary<string, object>
                        {
                            { "Foo", "foo resource" },
                            { "Bar", "overridden" },
                        }
                    }
                },
                Child = target = new Border
                {
                    Styles = new Styles
                    {
                        new Style
                        {
                            Resources = new Dictionary<string, object>
                            {
                                { "Bar", "again overridden" },
                            }
                        },
                        new Style
                        {
                            Resources = new Dictionary<string, object>
                            {
                                { "Bar", "bar resource" },
                            }
                        }
                    }
                }
            };

            Assert.Equal("foo resource", target.FindStyleResource("Foo"));
            Assert.Equal("bar resource", target.FindStyleResource("Bar"));
        }
        public void GetVisualsAt_Should_Find_Control_Translated_Outside_Parent_Bounds()
        {
            using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
            {
                Border target;
                var container = new Panel
                {
                    Width = 200,
                    Height = 200,
                    ClipToBounds = false,
                    Children = new Controls.Controls
                    {
                        new Border
                        {
                            Width = 100,
                            Height = 100,
                            ZIndex = 1,
                            HorizontalAlignment = HorizontalAlignment.Left,
                            VerticalAlignment = VerticalAlignment.Top,
                            Child = target = new Border
                            {
                                Width = 50,
                                Height = 50,
                                HorizontalAlignment = HorizontalAlignment.Left,
                                VerticalAlignment = VerticalAlignment.Top,
                                RenderTransform = new TranslateTransform(110, 110),
                            }
                        },
                    }
                };

                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(120, 120));

                Assert.Equal(new IVisual[] { target, container }, result);
            }
        }
Exemplo n.º 7
0
        public void FindStyleResource_Should_Return_UnsetValue_For_Not_Found()
        {
            Border target;

            var tree = target = new Border
            {
                Styles = new Styles
                    {
                        new Style
                        {
                            Resources = new Dictionary<string, object>
                            {
                                { "Foo", "foo" },
                            }
                        },
                    }
            };

            Assert.Equal(AvaloniaProperty.UnsetValue, target.FindStyleResource("Baz"));
        }
Exemplo n.º 8
0
        public void Grandchild_Size_Changed()
        {
            using (var context = AvaloniaLocator.EnterScope())
            {
                RegisterServices();

                Border border;
                TextBlock textBlock;

                var window = new Window()
                {
                    SizeToContent = SizeToContent.WidthAndHeight,
                    Content = border = new Border
                    {
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment = VerticalAlignment.Center,
                        Child = new Border
                        {
                            Child = textBlock = new TextBlock
                            {
                                Width = 400,
                                Height = 400,
                                Text = "Hello World!",
                            },
                        }
                    }
                };

                LayoutManager.Instance.ExecuteInitialLayoutPass(window);

                Assert.Equal(new Size(400, 400), border.Bounds.Size);
                textBlock.Width = 200;
                LayoutManager.Instance.ExecuteLayoutPass();

                Assert.Equal(new Size(200, 400), border.Bounds.Size);
            }
        }
Exemplo n.º 9
0
        public void DoubleTapped_Should_Follow_Pointer_Pressed_Released_Pressed()
        {
            Border border = new Border();
            var decorator = new Decorator
            {
                Child = border
            };
            var result = new List<string>();

            decorator.AddHandler(Border.PointerPressedEvent, (s, e) => result.Add("dp"));
            decorator.AddHandler(Border.PointerReleasedEvent, (s, e) => result.Add("dr"));
            decorator.AddHandler(Gestures.TappedEvent, (s, e) => result.Add("dt"));
            decorator.AddHandler(Gestures.DoubleTappedEvent, (s, e) => result.Add("ddt"));
            border.AddHandler(Border.PointerPressedEvent, (s, e) => result.Add("bp"));
            border.AddHandler(Border.PointerReleasedEvent, (s, e) => result.Add("br"));
            border.AddHandler(Gestures.TappedEvent, (s, e) => result.Add("bt"));
            border.AddHandler(Gestures.DoubleTappedEvent, (s, e) => result.Add("bdt"));

            border.RaiseEvent(new PointerPressedEventArgs());
            border.RaiseEvent(new PointerReleasedEventArgs());
            border.RaiseEvent(new PointerPressedEventArgs { ClickCount = 2 });

            Assert.Equal(new[] { "bp", "dp", "br", "dr", "bt", "dt", "bp", "dp", "bdt", "ddt" }, result);
        }
Exemplo n.º 10
0
        private static TabItem AnimationsTab()
        {
            Border border1;
            Border border2;
            RotateTransform rotate;
            Button button1;

            var result = new TabItem
            {
                Header = "Animations",
                Content = new StackPanel
                {
                    Orientation = Orientation.Vertical,
                    Gap = 4,
                    Margin = new Thickness(10),
                    Children = new Controls
                    {
                        new TextBlock
                        {
                            Text = "Animations",
                            FontWeight = FontWeight.Medium,
                            FontSize = 20,
                            Foreground = Brush.Parse("#212121"),
                        },
                        new TextBlock
                        {
                            Text = "A few animations showcased below",
                            FontSize = 13,
                            Foreground = Brush.Parse("#727272"),
                            Margin = new Thickness(0, 0, 0, 10)
                        },
                        (button1 = new Button
                        {
                            Content = "Animate",
                            Width = 120,
                            [Grid.ColumnProperty] = 1,
                            [Grid.RowProperty] = 1,
                        }),
                        new Canvas
                        {
                            ClipToBounds = false,
                            Children = new Controls
                            {
                                (border1 = new Border
                                {
                                    Width = 100,
                                    Height = 100,
                                    HorizontalAlignment = HorizontalAlignment.Center,
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Background = Brushes.Crimson,
                                    RenderTransform = new RotateTransform(),
                                    Child = new Grid
                                    {
                                        Children = new Controls
                                        {
                                            new Ellipse()
                                            {
                                                Width = 100,
                                                Height = 100,
                                                Fill =
                                                    new RadialGradientBrush()
                                                    {
                                                        GradientStops =
                                                        {
                                                            new GradientStop(Colors.Blue, 0),
                                                            new GradientStop(Colors.Green, 1)
                                                        },
                                                        Radius = 75
                                                    }
                                            },
                                            new Avalonia.Controls.Shapes.Path
                                            {
                                                Data =
                                                    StreamGeometry.Parse(
                                                        "F1 M 16.6309,18.6563C 17.1309,8.15625 29.8809,14.1563 29.8809,14.1563C 30.8809,11.1563 34.1308,11.4063 34.1308,11.4063C 33.5,12 34.6309,13.1563 34.6309,13.1563C 32.1309,13.1562 31.1309,14.9062 31.1309,14.9062C 41.1309,23.9062 32.6309,27.9063 32.6309,27.9062C 24.6309,24.9063 21.1309,22.1562 16.6309,18.6563 Z M 16.6309,19.9063C 21.6309,24.1563 25.1309,26.1562 31.6309,28.6562C 31.6309,28.6562 26.3809,39.1562 18.3809,36.1563C 18.3809,36.1563 18,38 16.3809,36.9063C 15,36 16.3809,34.9063 16.3809,34.9063C 16.3809,34.9063 10.1309,30.9062 16.6309,19.9063 Z"),
                                                Fill =
                                                    new LinearGradientBrush()
                                                    {
                                                        GradientStops =
                                                        {
                                                            new GradientStop(Colors.Green, 0),
                                                            new GradientStop(Colors.LightSeaGreen, 1)
                                                        }
                                                    },
                                                HorizontalAlignment = HorizontalAlignment.Center,
                                                VerticalAlignment = VerticalAlignment.Center,
                                                RenderTransform = new MatrixTransform(Matrix.CreateScale(2, 2))
                                            }
                                        }
                                    },
                                    [Canvas.LeftProperty] = 100,
                                    [Canvas.TopProperty] = 100,
                                }),
                                (border2 = new Border
                                {
                                    Width = 100,
                                    Height = 100,
                                    HorizontalAlignment = HorizontalAlignment.Center,
                                    VerticalAlignment = VerticalAlignment.Center,
                                    Background = Brushes.Coral,
                                    Child = new Image
                                    {
                                        Source = new Bitmap(GetImage("github_icon.png")),
                                        HorizontalAlignment = HorizontalAlignment.Center,
                                        VerticalAlignment = VerticalAlignment.Center,
                                    },
                                    RenderTransform = (rotate = new RotateTransform
                                    {
                                        PropertyTransitions = new PropertyTransitions
                                        {
                                            RotateTransform.AngleProperty.Transition(500),
                                        }
                                    }),
                                    PropertyTransitions = new PropertyTransitions
                                    {
                                        Layoutable.WidthProperty.Transition(300),
                                        Layoutable.HeightProperty.Transition(1000),
                                    },
                                    [Canvas.LeftProperty] = 400,
                                    [Canvas.TopProperty] = 100,
                                }),
                            }
                        }
                    },
                },
            };

            button1.Click += (s, e) =>
            {
                if (border2.Width == 100)
                {
                    border2.Width = border2.Height = 400;
                    rotate.Angle = 180;
                }
                else
                {
                    border2.Width = border2.Height = 100;
                    rotate.Angle = 0;
                }
            };

            var start = Animate.Stopwatch.Elapsed;
            var degrees = Animate.Timer
                .Select(x =>
                {
                    var elapsed = (x - start).TotalSeconds;
                    var cycles = elapsed / 4;
                    var progress = cycles % 1;
                    return 360.0 * progress;
                });

            border1.RenderTransform.Bind(
                RotateTransform.AngleProperty,
                degrees,
                BindingPriority.Animation);

            return result;
        }
        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 GetVisualsAt_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
        {
            using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
            {
                Border target;

                var container = new Panel
                {
                    Width = 100,
                    Height = 200,
                    Children = new Controls.Controls
                    {
                        new Panel()
                        {
                            Width = 100,
                            Height = 100,
                            Margin = new Thickness(0, 100, 0, 0),
                            ClipToBounds = true,
                            Children = new Controls.Controls
                            {
                                (target = new Border()
                                {
                                    Width = 100,
                                    Height = 100,
                                    Margin = new Thickness(0, -100, 0, 0)
                                })
                            }
                        }
                    }
                };

                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, 50));

                Assert.Equal(new[] { container }, result);
            }
        }
Exemplo n.º 13
0
 /// <inheritdoc/>
 protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
 {
     _indicator = e.NameScope.Get<Border>("PART_Indicator");
     UpdateIndicator(Bounds.Size);
 }
Exemplo n.º 14
0
        public void Style_Should_Detach_When_Removed_From_Logical_Tree()
        {
            Border border;

            var style = new Style(x => x.OfType<Border>())
            {
                Setters = new[]
                {
                    new Setter(Border.BorderThicknessProperty, 4),
                }
            };

            var root = new TestRoot
            {
                Child = border = new Border(),
            };

            style.Attach(border, null);

            Assert.Equal(4, border.BorderThickness);
            root.Child = null;
            Assert.Equal(0, border.BorderThickness);
        }