Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SplitView :closed pseudoclass not being added on control initialization #17176 #17178

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Avalonia.Controls/SplitView/SplitView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
_pane = e.NameScope.Find<Panel>("PART_PaneRoot");

UpdateVisualStateForDisplayMode(DisplayMode);
UpdatePaneStatePseudoClass(IsPaneOpen);
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
Expand Down Expand Up @@ -331,19 +332,13 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
else if (change.Property == IsPaneOpenProperty)
{
bool isPaneOpen = change.GetNewValue<bool>();

UpdatePaneStatePseudoClass(isPaneOpen);
if (isPaneOpen)
{
PseudoClasses.Add(pcOpen);
PseudoClasses.Remove(pcClosed);

OnPaneOpened(new RoutedEventArgs(PaneOpenedEvent, this));
}
else
{
PseudoClasses.Add(pcClosed);
PseudoClasses.Remove(pcOpen);

OnPaneClosed(new RoutedEventArgs(PaneClosedEvent, this));
}
}
Expand Down Expand Up @@ -579,6 +574,20 @@ private void TopLevelBackRequested(object? sender, RoutedEventArgs e)
e.Handled = true;
}

private void UpdatePaneStatePseudoClass(bool isPaneOpen)
{
if (isPaneOpen)
{
PseudoClasses.Add(pcOpen);
PseudoClasses.Remove(pcClosed);
}
else
{
PseudoClasses.Add(pcClosed);
PseudoClasses.Remove(pcOpen);
}
}

/// <summary>
/// Coerces/validates the <see cref="IsPaneOpen"/> property value.
/// </summary>
Expand Down
27 changes: 25 additions & 2 deletions tests/Avalonia.Controls.UnitTests/SplitViewTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Avalonia.Input;
using Avalonia.Animation;
using Avalonia.Input;
using Avalonia.UnitTests;
using Moq;
using Xunit;

namespace Avalonia.Controls.UnitTests
Expand Down Expand Up @@ -65,7 +67,8 @@ public void SplitView_Cancel_Close_Should_Prevent_Pane_From_Closing()
[Fact]
public void SplitView_TemplateSettings_Are_Correct_For_Display_Modes()
{
using var app = UnitTestApplication.Start(TestServices.StyledWindow);
using var app = UnitTestApplication.Start(TestServices.StyledWindow
.With(globalClock: new MockGlobalClock()));
var wnd = new Window
{
Width = 1280,
Expand Down Expand Up @@ -280,5 +283,25 @@ public void Top_Level_Back_Requested_Closes_Light_Dismissable_Pane()

Assert.True(splitView.IsPaneOpen);
}

[Fact]
public void With_Default_IsPaneOpen_Value_Should_Have_Closed_Pseudo_Class_Set()
{
// Testing this control Pseudo Classes requires placing the SplitView on a window
// prior to asserting them, because some of the pseudo classes are set either when
// the template is applied or the control is attached to the visual tree
using var app = UnitTestApplication.Start(TestServices.StyledWindow
.With(globalClock: new MockGlobalClock()));
var wnd = new Window
{
Width = 1280,
Height = 720
};
var splitView = new SplitView();
wnd.Content = splitView;
wnd.Show();

Assert.Contains(splitView.Classes, ":closed".Equals);
}
}
}
Loading