diff --git a/src/Avalonia.Base/Layout/Layoutable.cs b/src/Avalonia.Base/Layout/Layoutable.cs index e0c316c60aa..b6beb1ba581 100644 --- a/src/Avalonia.Base/Layout/Layoutable.cs +++ b/src/Avalonia.Base/Layout/Layoutable.cs @@ -545,6 +545,8 @@ protected virtual Size MeasureCore(Size availableSize) { if (IsVisible) { + ApplyStyling(); + if (!IsVisible) return new Size(); var margin = Margin; var useLayoutRounding = UseLayoutRounding; var scale = 1.0; @@ -555,7 +557,6 @@ protected virtual Size MeasureCore(Size availableSize) margin = LayoutHelper.RoundLayoutThickness(margin, scale); } - ApplyStyling(); ApplyTemplate(); var minMax = new MinMax(this); diff --git a/src/Avalonia.Controls/StackPanel.cs b/src/Avalonia.Controls/StackPanel.cs index 007a35c0778..63984592dac 100644 --- a/src/Avalonia.Controls/StackPanel.cs +++ b/src/Avalonia.Controls/StackPanel.cs @@ -261,6 +261,9 @@ protected override Size MeasureOverride(Size availableSize) // Get next child. var child = children[i]; + // Measure the child. + child.Measure(layoutSlotSize); + bool isVisible = child.IsVisible; if (isVisible && !hasVisibleChild) @@ -268,8 +271,6 @@ protected override Size MeasureOverride(Size availableSize) hasVisibleChild = true; } - // Measure the child. - child.Measure(layoutSlotSize); Size childDesiredSize = child.DesiredSize; // Accumulate child size. diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index 646dad30ec8..d191f3da985 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs @@ -1642,6 +1642,83 @@ public void WindowDecorationsTheme_Should_Apply_To_Decorations() } } + [Fact] + public void IsVisible_Setter_Should_Affect_Measurements_Inside_WindowDrawnDecorationsContent() + { + using var app = UnitTestApplication.Start(TestServices.StyledWindow); + + var windowImpl = MockWindowingPlatform.CreateWindowMock(); + windowImpl.Setup(x => x.NeedsManagedDecorations).Returns(true); + windowImpl.Setup(x => x.RequestedDrawnDecorations).Returns(PlatformRequestedDrawnDecoration.TitleBar); + + var window = new Window(windowImpl.Object); + + var stackPanel = new StackPanel + { + Width = 32, + Spacing = 2, + Children = + { + new Control { Height = 32 }, + new Control + { + Height = 32, + Classes = { "hidden-by-style" } + }, + } + }; + + var contentControl = new ContentControl + { + Content = new Control + { + Height = 32, + Width = 32, + Classes = { "hidden-by-style" } + } + }; + + var content = new WindowDrawnDecorationsContent + { + Overlay = new ContentControl + { + Content = new Panel + { + Children = { stackPanel, contentControl } + } + } + }; + + var template = new WindowDrawnDecorationsTemplate + { + Content = (IServiceProvider? _) => new TemplateResult(content, new NameScope()) + }; + + var theme = new ControlTheme(typeof(WindowDrawnDecorations)) + { + Setters = + { + new Setter(WindowDrawnDecorations.TemplateProperty, template) + } + }; + + var style = new Style(x => x.Is().Template().OfType().Class("hidden-by-style")) + { + Setters = + { + new Setter(Visual.IsVisibleProperty, false) + } + }; + + window.WindowDecorationsTheme = theme; + window.Styles.Add(style); + window.Show(); + window.Measure(Size.Infinity); + + Assert.Equal(new Size(), contentControl.DesiredSize); + Assert.Equal(new Size(32, 32), stackPanel.DesiredSize); + } + private class TopmostWindow : Window { static TopmostWindow()