Skip to content

Commit

Permalink
chore: add avalonia ToggleButton demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Dec 1, 2024
1 parent 4713903 commit 00d64d5
Show file tree
Hide file tree
Showing 17 changed files with 1,564 additions and 242 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HandyControlDemo.UserControl;

public partial class ToggleButtonDemoCtl : Avalonia.Controls.UserControl
{
public ToggleButtonDemoCtl()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Avalonia;
using Avalonia.Media;

namespace HandyControl.Controls;

public class IconSwitchElement
{
public static readonly AttachedProperty<Geometry> GeometryProperty =
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, Geometry>("Geometry");

public static void SetGeometry(AvaloniaObject element, Geometry value) => element.SetValue(GeometryProperty, value);

public static Geometry GetGeometry(AvaloniaObject element) => element.GetValue(GeometryProperty);

public static readonly AttachedProperty<double> WidthProperty =
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, double>("Width", defaultValue: double.NaN);

public static void SetWidth(AvaloniaObject element, double value) => element.SetValue(WidthProperty, value);

public static double GetWidth(AvaloniaObject element) => element.GetValue(WidthProperty);

public static readonly AttachedProperty<double> HeightProperty =
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, double>("Height", defaultValue: double.NaN);

public static void SetHeight(AvaloniaObject element, double value) => element.SetValue(HeightProperty, value);
public static double GetHeight(AvaloniaObject element) => element.GetValue(HeightProperty);

public static readonly AttachedProperty<Geometry> GeometrySelectedProperty =
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, Geometry>("GeometrySelected");

public static void SetGeometrySelected(AvaloniaObject element, Geometry value) =>
element.SetValue(GeometrySelectedProperty, value);

public static Geometry GetGeometrySelected(AvaloniaObject element) => element.GetValue(GeometrySelectedProperty);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Avalonia;

namespace HandyControl.Controls;

public class StatusSwitchElement
{
public static readonly AttachedProperty<object> CheckedElementProperty =
AvaloniaProperty.RegisterAttached<StatusSwitchElement, AvaloniaObject, object>("CheckedElement");

public static void SetCheckedElement(AvaloniaObject element, object value) =>
element.SetValue(CheckedElementProperty, value);

public static object GetCheckedElement(AvaloniaObject element) => element.GetValue(CheckedElementProperty);

public static readonly AttachedProperty<object> HideUncheckedElementProperty =
AvaloniaProperty.RegisterAttached<StatusSwitchElement, AvaloniaObject, object>("HideUncheckedElement");

public static void SetHideUncheckedElement(AvaloniaObject element, object value) =>
element.SetValue(HideUncheckedElementProperty, value);

public static object GetHideUncheckedElement(AvaloniaObject element) =>
element.GetValue(HideUncheckedElementProperty);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Avalonia;

namespace HandyControl.Controls;

public class ToggleButtonAttach
{
public static readonly AttachedProperty<bool> ShowLabelProperty =
AvaloniaProperty.RegisterAttached<ToggleButtonAttach, AvaloniaObject, bool>("ShowLabel", inherits: true);

public static void SetShowLabel(AvaloniaObject element, bool value) => element.SetValue(ShowLabelProperty, value);

public static bool GetShowLabel(AvaloniaObject element) => element.GetValue(ShowLabelProperty);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converter="clr-namespace:HandyControl.Tools.Converter">

<converter:Boolean2BooleanReConverter x:Key="Boolean2BooleanReConverter" />
<converter:BorderClipConverter x:Key="BorderClipConverter" />
<converter:BorderCircularConverter x:Key="BorderCircularConverter" />
<converter:BorderCircularClipConverter x:Key="BorderCircularClipConverter" />
<converter:GeometrySpacingConverter x:Key="GeometrySpacingConverter" />

</ResourceDictionary>
15 changes: 15 additions & 0 deletions src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,19 @@
</Setter.Value>
</Setter>
</ControlTheme>

<ControlTheme x:Key="BorderCircularClip"
BasedOn="{StaticResource BorderCircular}"
TargetType="Border">
<Setter Property="Clip">
<Setter.Value>
<MultiBinding Converter="{StaticResource BorderCircularClipConverter}">
<Binding Path="Bounds"
RelativeSource="{RelativeSource Self}" />
<Binding Path="CornerRadius"
RelativeSource="{RelativeSource Self}" />
</MultiBinding>
</Setter.Value>
</Setter>
</ControlTheme>
</ResourceDictionary>
171 changes: 25 additions & 146 deletions src/Avalonia/HandyControl_Avalonia/Themes/Styles/RadioButton.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
CornerRadius="8"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsVisible="{TemplateBinding IsChecked, Converter={StaticResource Boolean2BooleanReConverter}}" />
IsVisible="{TemplateBinding IsChecked, Converter={x:Static BoolConverters.Not}}" />
<ContentPresenter Grid.Column="1"
Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Expand Down Expand Up @@ -101,174 +101,53 @@
BasedOn="{StaticResource RadioButtonBaseStyle}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonBaseStyle"
BasedOn="{StaticResource ButtonBaseStyle}"
TargetType="RadioButton">
<Setter Property="Template">
<ControlTemplate>
<Panel>
<Border Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}" />
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="{TemplateBinding CornerRadius}">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}">
<Path Width="{TemplateBinding hc:IconElement.Width}"
Height="{TemplateBinding hc:IconElement.Height}"
Fill="{TemplateBinding Foreground}"
IsVisible="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}"
Stretch="Uniform"
Data="{TemplateBinding hc:IconElement.Geometry}" />
<ContentPresenter RecognizesAccessKey="True"
VerticalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
IsVisible="{Binding $self.Content, Converter={x:Static ObjectConverters.IsNotNull}}"
Margin="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometrySpacingConverter}, ConverterParameter='6,0,0,0'}" />
</StackPanel>
</Border>
<Border Margin="-1"
IsVisible="{TemplateBinding IsChecked}"
CornerRadius="{TemplateBinding CornerRadius}"
BorderThickness="4"
BorderBrush="{DynamicResource DarkMaskBrush}" />
</Panel>
</ControlTemplate>
</Setter>
</ControlTheme>

<ControlTheme x:Key="RadioButtonSameAsButtonDefault"
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
TargetType="RadioButton">
<Setter Property="Background"
Value="{DynamicResource RegionBrush}" />
<Setter Property="Foreground"
Value="{DynamicResource PrimaryTextBrush}" />

<Style Selector="^:pointerover">
<Setter Property="Background"
Value="{DynamicResource SecondaryRegionBrush}" />
</Style>

<Style Selector="^:pressed">
<Setter Property="Background"
Value="{DynamicResource BorderBrush}" />
</Style>
</ControlTheme>
BasedOn="{StaticResource ToggleButtonDefault}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonDefault.Small"
BasedOn="{StaticResource RadioButtonSameAsButtonDefault}"
TargetType="RadioButton">
<Setter Property="Height"
Value="20" />
<Setter Property="Padding"
Value="6,1" />
<Setter Property="hc:IconElement.Height"
Value="12" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonDefault.Small}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonPrimary"
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
TargetType="RadioButton">
<Setter Property="Background"
Value="{DynamicResource PrimaryBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource PrimaryBrush}" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonPrimary}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonPrimary.Small"
BasedOn="{StaticResource RadioButtonSameAsButtonPrimary}"
TargetType="RadioButton">
<Setter Property="Height"
Value="20" />
<Setter Property="Padding"
Value="6,1" />
<Setter Property="hc:IconElement.Height"
Value="12" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonPrimary.Small}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonSuccess"
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
TargetType="RadioButton">
<Setter Property="Background"
Value="{DynamicResource SuccessBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource SuccessBrush}" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonSuccess}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonSuccess.Small"
BasedOn="{StaticResource RadioButtonSameAsButtonSuccess}"
TargetType="RadioButton">
<Setter Property="Height"
Value="20" />
<Setter Property="Padding"
Value="6,1" />
<Setter Property="hc:IconElement.Height"
Value="12" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonSuccess.Small}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonInfo"
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
TargetType="RadioButton">
<Setter Property="Background"
Value="{DynamicResource InfoBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource InfoBrush}" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonInfo}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonInfo.Small"
BasedOn="{StaticResource RadioButtonSameAsButtonInfo}"
TargetType="RadioButton">
<Setter Property="Height"
Value="20" />
<Setter Property="Padding"
Value="6,1" />
<Setter Property="hc:IconElement.Height"
Value="12" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonInfo.Small}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonWarning"
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
TargetType="RadioButton">
<Setter Property="Background"
Value="{DynamicResource WarningBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource WarningBrush}" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonWarning}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonWarning.Small"
BasedOn="{StaticResource RadioButtonSameAsButtonWarning}"
TargetType="RadioButton">
<Setter Property="Height"
Value="20" />
<Setter Property="Padding"
Value="6,1" />
<Setter Property="hc:IconElement.Height"
Value="12" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonWarning.Small}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonDanger"
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
TargetType="RadioButton">
<Setter Property="Background"
Value="{DynamicResource DangerBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource DangerBrush}" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonDanger}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonSameAsButtonDanger.Small"
BasedOn="{StaticResource RadioButtonSameAsButtonDanger}"
TargetType="RadioButton">
<Setter Property="Height"
Value="20" />
<Setter Property="Padding"
Value="6,1" />
<Setter Property="hc:IconElement.Height"
Value="12" />
</ControlTheme>
BasedOn="{StaticResource ToggleButtonDanger.Small}"
TargetType="RadioButton" />

<ControlTheme x:Key="RadioButtonIconBaseStyle"
BasedOn="{StaticResource ButtonBaseStyle}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Window.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Button.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/RepeatButton.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/ToggleButton.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/RadioButton.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Border.axaml" />
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/ItemsControl.axaml" />
Expand Down
Loading

0 comments on commit 00d64d5

Please sign in to comment.