Skip to content

Commit

Permalink
Merge branch 'main' into niels9001/loading
Browse files Browse the repository at this point in the history
  • Loading branch information
niels9001 authored Dec 4, 2023
2 parents a6e4cfa + bef863c commit 1067264
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="SettingsControlsExperiment.Samples.ClickableSettingsCardSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -13,7 +13,10 @@
Header="A clickable SettingsCard"
HeaderIcon="{ui:FontIcon Glyph=&#xE799;}"
IsClickEnabled="True"
IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}" />
IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}">
<TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="This is content" />
</controls:SettingsCard>

<controls:SettingsCard ActionIcon="{ui:FontIcon Glyph=&#xE8A7;}"
ActionIconToolTip="Open in new window"
Expand Down
5 changes: 4 additions & 1 deletion components/SettingsControls/samples/SettingsCardSample.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="SettingsControlsExperiment.Samples.SettingsCardSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -8,6 +8,7 @@
xmlns:ui="using:CommunityToolkit.WinUI"
mc:Ignorable="d">
<StackPanel Spacing="4">

<controls:SettingsCard x:Name="settingsCard"
Description="This is a default card, with the Header, HeaderIcon, Description and Content set."
Header="This is the Header"
Expand Down Expand Up @@ -47,5 +48,7 @@
<Button Content="This control will wrap vertically!"
Style="{StaticResource AccentButtonStyle}" />
</controls:SettingsCard>

<controls:SettingsCard Header="This is a card with a Header only" />
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="SettingsControlsExperiment.Samples.SettingsExpanderItemsSourceSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down
69 changes: 67 additions & 2 deletions components/SettingsControls/src/SettingsCard/SettingsCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.ComponentModel.Design;

namespace CommunityToolkit.WinUI.Controls;

/// <summary>
Expand All @@ -13,17 +15,46 @@ namespace CommunityToolkit.WinUI.Controls;
[TemplatePart(Name = HeaderPresenter, Type = typeof(ContentPresenter))]
[TemplatePart(Name = DescriptionPresenter, Type = typeof(ContentPresenter))]
[TemplatePart(Name = HeaderIconPresenterHolder, Type = typeof(Viewbox))]

[TemplateVisualState(Name = NormalState, GroupName = CommonStates)]
[TemplateVisualState(Name = PointerOverState, GroupName = CommonStates)]
[TemplateVisualState(Name = PressedState, GroupName = CommonStates)]
[TemplateVisualState(Name = DisabledState, GroupName = CommonStates)]

[TemplateVisualState(Name = RightState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = RightWrappedState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = RightWrappedNoIconState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = LeftState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = VerticalState, GroupName = ContentAlignmentStates)]

[TemplateVisualState(Name = NoContentSpacingState, GroupName = ContentSpacingStates)]
[TemplateVisualState(Name = ContentSpacingState, GroupName = ContentSpacingStates)]

public partial class SettingsCard : ButtonBase
{
internal const string CommonStates = "CommonStates";
internal const string NormalState = "Normal";
internal const string PointerOverState = "PointerOver";
internal const string PressedState = "Pressed";
internal const string DisabledState = "Disabled";

internal const string ContentAlignmentStates = "ContentAlignmentStates";
internal const string RightState = "Right";
internal const string RightWrappedState = "RightWrapped";
internal const string RightWrappedNoIconState = "RightWrappedNoIcon";
internal const string LeftState = "Left";
internal const string VerticalState = "Vertical";

internal const string ContentSpacingStates = "ContentSpacingStates";
internal const string NoContentSpacingState = "NoContentSpacing";
internal const string ContentSpacingState = "ContentSpacing";

internal const string ActionIconPresenterHolder = "PART_ActionIconPresenterHolder";
internal const string HeaderPresenter = "PART_HeaderPresenter";
internal const string DescriptionPresenter = "PART_DescriptionPresenter";
internal const string HeaderIconPresenterHolder = "PART_HeaderIconPresenterHolder";


/// <summary>
/// Creates a new instance of the <see cref="SettingsCard"/> class.
/// </summary>
Expand All @@ -42,11 +73,24 @@ protected override void OnApplyTemplate()
OnHeaderIconChanged();
OnDescriptionChanged();
OnIsClickEnabledChanged();
VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, true);
CheckInitialVisualState();

RegisterAutomation();
IsEnabledChanged += OnIsEnabledChanged;
RegisterPropertyChangedCallback(ContentProperty, OnContentChanged);
IsEnabledChanged += OnIsEnabledChanged;
}

private void CheckInitialVisualState()
{
VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, true);

if (GetTemplateChild("ContentAlignmentStates") is VisualStateGroup contentAlignmentStatesGroup)
{
contentAlignmentStatesGroup.CurrentStateChanged -= this.ContentAlignmentStates_Changed;
CheckVerticalSpacingState(contentAlignmentStatesGroup.CurrentState);
contentAlignmentStatesGroup.CurrentStateChanged += this.ContentAlignmentStates_Changed;
}
}
private void RegisterAutomation()
{
if (Header is string headerString && headerString != string.Empty)
Expand Down Expand Up @@ -207,6 +251,7 @@ private void OnDescriptionChanged()
? Visibility.Visible
: Visibility.Collapsed;
}

}

private void OnHeaderChanged()
Expand All @@ -217,6 +262,26 @@ private void OnHeaderChanged()
? Visibility.Visible
: Visibility.Collapsed;
}

}

private void ContentAlignmentStates_Changed(object sender, VisualStateChangedEventArgs e)
{
CheckVerticalSpacingState(e.NewState);
}

private void CheckVerticalSpacingState(VisualState s)
{
// On state change, checking if the Content should be wrapped (e.g. when the card is made smaller or the ContentAlignment is set to Vertical). If the Content and the Header or Description are not null, we add spacing between the Content and the Header/Description.

if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (Header != null || Description != null))
{
VisualStateManager.GoToState(this, ContentSpacingState, true);
}
else
{
VisualStateManager.GoToState(this, NoContentSpacingState, true);
}
}

private FrameworkElement? GetFocusedElement()
Expand Down
16 changes: 11 additions & 5 deletions components/SettingsControls/src/SettingsCard/SettingsCard.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CommunityToolkit.WinUI.Controls"
Expand Down Expand Up @@ -106,7 +106,7 @@
<Thickness x:Key="SettingsCardHeaderIconMargin">2,0,20,0</Thickness>
<Thickness x:Key="SettingsCardActionIconMargin">14,0,0,0</Thickness>
<x:Double x:Key="SettingsCardActionIconMaxSize">13</x:Double>
<Thickness x:Key="SettingsCardVerticalHeaderContentSpacing">0,8,0,0</Thickness>
<x:Double x:Key="SettingsCardVerticalHeaderContentSpacing">8</x:Double>
<x:Double x:Key="SettingsCardWrapThreshold">476</x:Double>
<x:Double x:Key="SettingsCardWrapNoIconThreshold">286</x:Double>

Expand Down Expand Up @@ -310,7 +310,6 @@
<Setter Target="PART_ContentPresenter.(Grid.Column)" Value="1" />
<Setter Target="PART_ContentPresenter.HorizontalAlignment" Value="Stretch" />
<Setter Target="PART_ContentPresenter.HorizontalContentAlignment" Value="Left" />
<Setter Target="PART_ContentPresenter.Margin" Value="{ThemeResource SettingsCardVerticalHeaderContentSpacing}" />
<Setter Target="HeaderPanel.Margin" Value="0" />
</VisualState.Setters>
</VisualState>
Expand All @@ -327,7 +326,6 @@
<Setter Target="PART_ContentPresenter.(Grid.Column)" Value="1" />
<Setter Target="PART_ContentPresenter.HorizontalAlignment" Value="Stretch" />
<Setter Target="PART_ContentPresenter.HorizontalContentAlignment" Value="Left" />
<Setter Target="PART_ContentPresenter.Margin" Value="{ThemeResource SettingsCardVerticalHeaderContentSpacing}" />
<Setter Target="HeaderPanel.Margin" Value="0" />
</VisualState.Setters>
</VisualState>
Expand Down Expand Up @@ -359,7 +357,6 @@
<Setter Target="PART_ContentPresenter.(Grid.Column)" Value="1" />
<Setter Target="PART_ContentPresenter.HorizontalAlignment" Value="Stretch" />
<Setter Target="PART_ContentPresenter.HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Target="PART_ContentPresenter.Margin" Value="{ThemeResource SettingsCardVerticalHeaderContentSpacing}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
Expand All @@ -376,6 +373,15 @@
</VisualState.Setters>
</VisualState>
</VisualStateGroup>

<VisualStateGroup x:Name="ContentSpacingStates">
<VisualState x:Name="NoContentSpacing" />
<VisualState x:Name="ContentSpacing">
<VisualState.Setters>
<Setter Target="PART_RootGrid.RowSpacing" Value="{ThemeResource SettingsCardVerticalHeaderContentSpacing}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<Viewbox x:Name="PART_HeaderIconPresenterHolder"
Expand Down

0 comments on commit 1067264

Please sign in to comment.