Skip to content

Commit

Permalink
Merge pull request #3 from timunie/feat/more-pseudo-classes
Browse files Browse the repository at this point in the history
More pseudo-classes
  • Loading branch information
punker76 authored Oct 30, 2024
2 parents 0a92b41 + 5f32b8c commit 3163a62
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/IconPacks.Avalonia.BoxIcons/PackIconBoxIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
{
UpdateData();
}

base.UpdateIconPseudoClasses(true, false, true);
}

protected override void SetKind<TKind>(TKind iconKind)
Expand Down
29 changes: 29 additions & 0 deletions src/IconPacks.Avalonia.Core/PackIconControlBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Media;
Expand All @@ -16,8 +17,29 @@ namespace IconPacks.Avalonia
/// <summary>
/// Class PackIconControlBase which is the base class for any PackIcon control.
/// </summary>
[PseudoClasses(IconDataFlippedVerticallyPseudoClass)]
[PseudoClasses(IconFilledPseudoClass)]
[PseudoClasses(IconOutlinedPseudoClass)]
public abstract class PackIconControlBase : PackIconBase
{
/// <summary>
/// A string representing the pseudo-class when the icon data is flipped vertically
/// </summary>
/// <returns>":icon-data-flipped-vertically"</returns>
public const string IconDataFlippedVerticallyPseudoClass = ":icon-data-flipped-vertically";

/// <summary>
/// A string representing the pseudo-class when the icon data is drawn filled
/// </summary>
/// <returns>":icon-filled"</returns>
public const string IconFilledPseudoClass = ":icon-filled";

/// <summary>
/// A string representing the pseudo-class when the icon data is drawn outlined
/// </summary>
/// <returns>":icon-outlided"</returns>
public const string IconOutlinedPseudoClass = ":icon-outlined";

protected PackIconControlBase()
{
AffectsRender<PackIconControlBase>(SpinProperty, SpinDurationProperty, OpacityProperty, SpinEasingFunctionProperty, FlipProperty, RotationAngleProperty);
Expand Down Expand Up @@ -281,5 +303,12 @@ public bool SpinAutoReverse
get { return this.GetValue(SpinAutoReverseProperty); }
set { this.SetValue(SpinAutoReverseProperty, value); }
}

protected void UpdateIconPseudoClasses(bool filled, bool outlined, bool flipped)
{
PseudoClasses.Set(IconFilledPseudoClass, filled);
PseudoClasses.Set(IconOutlinedPseudoClass, outlined);
PseudoClasses.Set(IconDataFlippedVerticallyPseudoClass, flipped);
}
}
}
10 changes: 2 additions & 8 deletions src/IconPacks.Avalonia/PackIconControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ namespace IconPacks.Avalonia
{
/// <summary>
/// </summary>
[PseudoClasses(IconDataFlippedVerticallyPseudoClass)]
public class PackIconControl : PackIconControlBase
{
/// <summary>
/// A string representing the pseudo-class when the icon data is flipped vertically
/// </summary>
/// <returns>":icon-data-flipped-vertically"</returns>
public const string IconDataFlippedVerticallyPseudoClass = ":icon-data-flipped-vertically";

public static readonly StyledProperty<Enum> KindProperty
= AvaloniaProperty.Register<PackIconControl, Enum>(nameof(Kind));
Expand Down Expand Up @@ -53,11 +47,11 @@ protected override void UpdateData()
switch(Kind)
{
case PackIconBoxIconsKind:
PseudoClasses.Set(IconDataFlippedVerticallyPseudoClass, true);
UpdateIconPseudoClasses(true, false, true);
break;

default:
PseudoClasses.Set(IconDataFlippedVerticallyPseudoClass, false);
UpdateIconPseudoClasses(true, false, false);
break;
}

Expand Down
18 changes: 15 additions & 3 deletions src/IconPacks.Avalonia/PackIconControl.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:iconPacks="urn:iconpacks-avalonia"
xmlns:converter="clr-namespace:IconPacks.Avalonia.Converter;assembly=IconPacks.Avalonia.Core"
x:ClassModifier="internal">

<Design.PreviewWith>
<iconPacks:PackIconBoxIcons Kind="RegularBowlingBall" />
<iconPacks:PackIconControl Kind="{x:Static iconPacks:PackIconBoxIconsKind.SolidBowlHot}" />
</Design.PreviewWith>


Expand All @@ -21,7 +21,6 @@
Margin="{TemplateBinding Padding}">
<Path x:Name="PART_IconPath"
Data="{Binding Data, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, Converter={x:Static converter:NullToUnsetValueConverter.Instance}}"
Fill="{TemplateBinding Foreground}"
Stretch="Uniform"
UseLayoutRounding="False" />
</Viewbox>
Expand Down Expand Up @@ -166,6 +165,19 @@
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="UseLayoutRounding" Value="False" />
<Setter Property="Template" Value="{DynamicResource IconPacks.Avalonia.PackIconControl.Template}" />

<!-- icon is filled -->
<Style Selector="^:icon-filled /template/ Path#PART_IconPath">
<Setter Property="Fill" Value="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
</Style>

<!-- icon is outlined -->
<Style Selector="^:icon-outlined /template/ Path#PART_IconPath">
<Setter Property="Stroke" Value="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
<Setter Property="StrokeThickness" Value="2" />
<Setter Property="StrokeLineCap" Value="Round" />
<Setter Property="StrokeJoin" Value="Round" />
</Style>

<!-- flipped icon data -->
<Style Selector="^:icon-data-flipped-vertically /template/ Viewbox#PART_ViewBox">
Expand Down

0 comments on commit 3163a62

Please sign in to comment.