Browse Source

Don't apply styling in TopLevel ctor.

Applying styling in the constructor isn't a good idea as demonstrated by #8549..

Instead apply styling when showing a window, or if it's needed call `ApplyStyling` manually, e.g. in unit tests.

Fixes #8549
pull/9411/head
Steven Kirk 3 years ago
parent
commit
086c2c7e70
  1. 3
      src/Avalonia.Base/PropertyStore/ValueStore.cs
  2. 2
      src/Avalonia.Controls/TopLevel.cs
  3. 2
      src/Avalonia.Controls/Window.cs
  4. 1
      src/Avalonia.Controls/WindowBase.cs
  5. 1
      tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs
  6. 10
      tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs
  7. 3
      tests/Avalonia.Controls.UnitTests/MenuItemTests.cs
  8. 1
      tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs
  9. 2
      tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
  10. 2
      tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs
  11. 8
      tests/Avalonia.Controls.UnitTests/ToolTipTests.cs
  12. 7
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs

3
src/Avalonia.Base/PropertyStore/ValueStore.cs

@ -610,8 +610,7 @@ namespace Avalonia.PropertyStore
private int InsertFrame(ValueFrame frame)
{
// Uncomment this line when #8549 is fixed.
//Debug.Assert(!_frames.Contains(frame));
Debug.Assert(!_frames.Contains(frame));
var index = BinarySearchFrame(frame.Priority);
_frames.Insert(index, frame);

2
src/Avalonia.Controls/TopLevel.cs

@ -182,8 +182,6 @@ namespace Avalonia.Controls
_globalStyles.GlobalStylesRemoved += ((IStyleHost)this).StylesRemoved;
}
ApplyStyling();
ClientSize = impl.ClientSize;
FrameSize = impl.FrameSize;

2
src/Avalonia.Controls/Window.cs

@ -647,6 +647,7 @@ namespace Avalonia.Controls
RaiseEvent(new RoutedEventArgs(WindowOpenedEvent));
EnsureInitialized();
ApplyStyling();
IsVisible = true;
var initialSize = new Size(
@ -726,6 +727,7 @@ namespace Avalonia.Controls
RaiseEvent(new RoutedEventArgs(WindowOpenedEvent));
EnsureInitialized();
ApplyStyling();
IsVisible = true;
var initialSize = new Size(

1
src/Avalonia.Controls/WindowBase.cs

@ -149,6 +149,7 @@ namespace Avalonia.Controls
try
{
EnsureInitialized();
ApplyStyling();
IsVisible = true;
if (!_hasExecutedInitialLayoutPass)

1
tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs

@ -1056,6 +1056,7 @@ namespace Avalonia.Controls.UnitTests
control.Items = CreateSimpleStringArray();
TextBox textBox = GetTextBox(control);
var window = new Window {Content = control};
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
Dispatcher.UIThread.RunJobs();

10
tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs

@ -29,6 +29,7 @@ namespace Avalonia.Controls.UnitTests
};
var window = new Window { Content = target };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -61,6 +62,7 @@ namespace Avalonia.Controls.UnitTests
};
var window = new Window { Content = target };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -130,6 +132,7 @@ namespace Avalonia.Controls.UnitTests
};
var window = new Window { Content = target };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -158,6 +161,7 @@ namespace Avalonia.Controls.UnitTests
};
var window = new Window { Content = target };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -186,6 +190,7 @@ namespace Avalonia.Controls.UnitTests
};
var window = new Window { Content = target };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -207,6 +212,7 @@ namespace Avalonia.Controls.UnitTests
};
var window = new Window { Content = target };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -390,7 +396,8 @@ namespace Avalonia.Controls.UnitTests
var sp = new StackPanel { Children = { target1, target2 } };
var window = new Window { Content = sp };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -594,6 +601,7 @@ namespace Avalonia.Controls.UnitTests
windowImpl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>())).Returns(renderer.Object);
var w = new Window(windowImpl.Object) { Content = content };
w.ApplyStyling();
w.ApplyTemplate();
w.Presenter.ApplyTemplate();
return w;

3
tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

@ -193,6 +193,7 @@ namespace Avalonia.Controls.UnitTests
var target = new MenuItem();
var contextMenu = new ContextMenu { Items = new AvaloniaList<MenuItem> { target } };
var window = new Window { Content = new Panel { ContextMenu = contextMenu } };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -232,6 +233,7 @@ namespace Avalonia.Controls.UnitTests
var flyout = new MenuFlyout { Items = new AvaloniaList<MenuItem> { target } };
var button = new Button { Flyout = flyout };
var window = new Window { Content = button };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -271,6 +273,7 @@ namespace Avalonia.Controls.UnitTests
var parentMenuItem = new MenuItem { Items = new AvaloniaList<MenuItem> { target } };
var contextMenu = new ContextMenu { Items = new AvaloniaList<MenuItem> { parentMenuItem } };
var window = new Window { Content = new Panel { ContextMenu = contextMenu } };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
contextMenu.Open();

1
tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs

@ -50,6 +50,7 @@ namespace Avalonia.Controls.UnitTests
var control = CreateControl();
TextBox textBox = GetTextBox(control);
var window = new Window { Content = control };
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
Dispatcher.UIThread.RunJobs();

2
tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs

@ -51,6 +51,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
};
window.Content = target;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
target.ApplyTemplate();
@ -177,6 +178,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
};
window.Content = target;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
target.ApplyTemplate();

2
tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs

@ -569,6 +569,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
windowImpl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>())).Returns(renderer.Object);
var window = new Window(windowImpl.Object);
window.ApplyStyling();
window.ApplyTemplate();
var target = new Popup()
@ -1090,6 +1091,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
private Window PreparedWindow(object content = null)
{
var w = new Window { Content = content };
w.ApplyStyling();
w.ApplyTemplate();
return w;
}

8
tests/Avalonia.Controls.UnitTests/ToolTipTests.cs

@ -51,6 +51,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = panel;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -114,6 +115,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = target;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -140,6 +142,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = target;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -183,6 +186,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = target;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -215,6 +219,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = decorator;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -237,6 +242,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = decorator;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -261,6 +267,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = decorator;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();
@ -286,6 +293,7 @@ namespace Avalonia.Controls.UnitTests
window.Content = target;
window.ApplyStyling();
window.ApplyTemplate();
window.Presenter.ApplyTemplate();

7
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs

@ -720,7 +720,12 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
//ensure binding is set and operational first
Assert.Equal(100.0, tracker.Tag);
Assert.Equal("EndInit 0", tracker.Order.Last());
// EndInit should be second-to-last operation, as last operation will be
// caused by styling being applied on EndInit.
Assert.Equal("EndInit 0", tracker.Order[tracker.Order.Count - 2]);
// Caused by styling.
Assert.Equal("Property Foreground Changed", tracker.Order[tracker.Order.Count - 1]);
}
}

Loading…
Cancel
Save