Skip to content

Commit 00d64d5

Browse files
committed
chore: add avalonia ToggleButton demo.
1 parent 4713903 commit 00d64d5

File tree

17 files changed

+1564
-242
lines changed

17 files changed

+1564
-242
lines changed

src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml

Lines changed: 602 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace HandyControlDemo.UserControl;
2+
3+
public partial class ToggleButtonDemoCtl : Avalonia.Controls.UserControl
4+
{
5+
public ToggleButtonDemoCtl()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Avalonia;
2+
using Avalonia.Media;
3+
4+
namespace HandyControl.Controls;
5+
6+
public class IconSwitchElement
7+
{
8+
public static readonly AttachedProperty<Geometry> GeometryProperty =
9+
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, Geometry>("Geometry");
10+
11+
public static void SetGeometry(AvaloniaObject element, Geometry value) => element.SetValue(GeometryProperty, value);
12+
13+
public static Geometry GetGeometry(AvaloniaObject element) => element.GetValue(GeometryProperty);
14+
15+
public static readonly AttachedProperty<double> WidthProperty =
16+
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, double>("Width", defaultValue: double.NaN);
17+
18+
public static void SetWidth(AvaloniaObject element, double value) => element.SetValue(WidthProperty, value);
19+
20+
public static double GetWidth(AvaloniaObject element) => element.GetValue(WidthProperty);
21+
22+
public static readonly AttachedProperty<double> HeightProperty =
23+
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, double>("Height", defaultValue: double.NaN);
24+
25+
public static void SetHeight(AvaloniaObject element, double value) => element.SetValue(HeightProperty, value);
26+
public static double GetHeight(AvaloniaObject element) => element.GetValue(HeightProperty);
27+
28+
public static readonly AttachedProperty<Geometry> GeometrySelectedProperty =
29+
AvaloniaProperty.RegisterAttached<IconSwitchElement, AvaloniaObject, Geometry>("GeometrySelected");
30+
31+
public static void SetGeometrySelected(AvaloniaObject element, Geometry value) =>
32+
element.SetValue(GeometrySelectedProperty, value);
33+
34+
public static Geometry GetGeometrySelected(AvaloniaObject element) => element.GetValue(GeometrySelectedProperty);
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Avalonia;
2+
3+
namespace HandyControl.Controls;
4+
5+
public class StatusSwitchElement
6+
{
7+
public static readonly AttachedProperty<object> CheckedElementProperty =
8+
AvaloniaProperty.RegisterAttached<StatusSwitchElement, AvaloniaObject, object>("CheckedElement");
9+
10+
public static void SetCheckedElement(AvaloniaObject element, object value) =>
11+
element.SetValue(CheckedElementProperty, value);
12+
13+
public static object GetCheckedElement(AvaloniaObject element) => element.GetValue(CheckedElementProperty);
14+
15+
public static readonly AttachedProperty<object> HideUncheckedElementProperty =
16+
AvaloniaProperty.RegisterAttached<StatusSwitchElement, AvaloniaObject, object>("HideUncheckedElement");
17+
18+
public static void SetHideUncheckedElement(AvaloniaObject element, object value) =>
19+
element.SetValue(HideUncheckedElementProperty, value);
20+
21+
public static object GetHideUncheckedElement(AvaloniaObject element) =>
22+
element.GetValue(HideUncheckedElementProperty);
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Avalonia;
2+
3+
namespace HandyControl.Controls;
4+
5+
public class ToggleButtonAttach
6+
{
7+
public static readonly AttachedProperty<bool> ShowLabelProperty =
8+
AvaloniaProperty.RegisterAttached<ToggleButtonAttach, AvaloniaObject, bool>("ShowLabel", inherits: true);
9+
10+
public static void SetShowLabel(AvaloniaObject element, bool value) => element.SetValue(ShowLabelProperty, value);
11+
12+
public static bool GetShowLabel(AvaloniaObject element) => element.GetValue(ShowLabelProperty);
13+
}

src/Avalonia/HandyControl_Avalonia/Themes/Basic/Converters.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:converter="clr-namespace:HandyControl.Tools.Converter">
44

5-
<converter:Boolean2BooleanReConverter x:Key="Boolean2BooleanReConverter" />
65
<converter:BorderClipConverter x:Key="BorderClipConverter" />
76
<converter:BorderCircularConverter x:Key="BorderCircularConverter" />
7+
<converter:BorderCircularClipConverter x:Key="BorderCircularClipConverter" />
88
<converter:GeometrySpacingConverter x:Key="GeometrySpacingConverter" />
99

1010
</ResourceDictionary>

src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,19 @@
111111
</Setter.Value>
112112
</Setter>
113113
</ControlTheme>
114+
115+
<ControlTheme x:Key="BorderCircularClip"
116+
BasedOn="{StaticResource BorderCircular}"
117+
TargetType="Border">
118+
<Setter Property="Clip">
119+
<Setter.Value>
120+
<MultiBinding Converter="{StaticResource BorderCircularClipConverter}">
121+
<Binding Path="Bounds"
122+
RelativeSource="{RelativeSource Self}" />
123+
<Binding Path="CornerRadius"
124+
RelativeSource="{RelativeSource Self}" />
125+
</MultiBinding>
126+
</Setter.Value>
127+
</Setter>
128+
</ControlTheme>
114129
</ResourceDictionary>

src/Avalonia/HandyControl_Avalonia/Themes/Styles/RadioButton.axaml

Lines changed: 25 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
CornerRadius="8"
5252
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
5353
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
54-
IsVisible="{TemplateBinding IsChecked, Converter={StaticResource Boolean2BooleanReConverter}}" />
54+
IsVisible="{TemplateBinding IsChecked, Converter={x:Static BoolConverters.Not}}" />
5555
<ContentPresenter Grid.Column="1"
5656
Focusable="False"
5757
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
@@ -101,174 +101,53 @@
101101
BasedOn="{StaticResource RadioButtonBaseStyle}"
102102
TargetType="RadioButton" />
103103

104-
<ControlTheme x:Key="RadioButtonSameAsButtonBaseStyle"
105-
BasedOn="{StaticResource ButtonBaseStyle}"
106-
TargetType="RadioButton">
107-
<Setter Property="Template">
108-
<ControlTemplate>
109-
<Panel>
110-
<Border Background="{TemplateBinding Background}"
111-
CornerRadius="{TemplateBinding CornerRadius}" />
112-
<Border BorderThickness="{TemplateBinding BorderThickness}"
113-
BorderBrush="{TemplateBinding BorderBrush}"
114-
CornerRadius="{TemplateBinding CornerRadius}">
115-
<StackPanel Orientation="Horizontal"
116-
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
117-
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
118-
Margin="{TemplateBinding Padding}">
119-
<Path Width="{TemplateBinding hc:IconElement.Width}"
120-
Height="{TemplateBinding hc:IconElement.Height}"
121-
Fill="{TemplateBinding Foreground}"
122-
IsVisible="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}"
123-
Stretch="Uniform"
124-
Data="{TemplateBinding hc:IconElement.Geometry}" />
125-
<ContentPresenter RecognizesAccessKey="True"
126-
VerticalAlignment="Center"
127-
Content="{TemplateBinding Content}"
128-
ContentTemplate="{TemplateBinding ContentTemplate}"
129-
IsVisible="{Binding $self.Content, Converter={x:Static ObjectConverters.IsNotNull}}"
130-
Margin="{Binding Path=(hc:IconElement.Geometry), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometrySpacingConverter}, ConverterParameter='6,0,0,0'}" />
131-
</StackPanel>
132-
</Border>
133-
<Border Margin="-1"
134-
IsVisible="{TemplateBinding IsChecked}"
135-
CornerRadius="{TemplateBinding CornerRadius}"
136-
BorderThickness="4"
137-
BorderBrush="{DynamicResource DarkMaskBrush}" />
138-
</Panel>
139-
</ControlTemplate>
140-
</Setter>
141-
</ControlTheme>
142-
143104
<ControlTheme x:Key="RadioButtonSameAsButtonDefault"
144-
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
145-
TargetType="RadioButton">
146-
<Setter Property="Background"
147-
Value="{DynamicResource RegionBrush}" />
148-
<Setter Property="Foreground"
149-
Value="{DynamicResource PrimaryTextBrush}" />
150-
151-
<Style Selector="^:pointerover">
152-
<Setter Property="Background"
153-
Value="{DynamicResource SecondaryRegionBrush}" />
154-
</Style>
155-
156-
<Style Selector="^:pressed">
157-
<Setter Property="Background"
158-
Value="{DynamicResource BorderBrush}" />
159-
</Style>
160-
</ControlTheme>
105+
BasedOn="{StaticResource ToggleButtonDefault}"
106+
TargetType="RadioButton" />
161107

162108
<ControlTheme x:Key="RadioButtonSameAsButtonDefault.Small"
163-
BasedOn="{StaticResource RadioButtonSameAsButtonDefault}"
164-
TargetType="RadioButton">
165-
<Setter Property="Height"
166-
Value="20" />
167-
<Setter Property="Padding"
168-
Value="6,1" />
169-
<Setter Property="hc:IconElement.Height"
170-
Value="12" />
171-
</ControlTheme>
109+
BasedOn="{StaticResource ToggleButtonDefault.Small}"
110+
TargetType="RadioButton" />
172111

173112
<ControlTheme x:Key="RadioButtonSameAsButtonPrimary"
174-
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
175-
TargetType="RadioButton">
176-
<Setter Property="Background"
177-
Value="{DynamicResource PrimaryBrush}" />
178-
<Setter Property="BorderBrush"
179-
Value="{DynamicResource PrimaryBrush}" />
180-
</ControlTheme>
113+
BasedOn="{StaticResource ToggleButtonPrimary}"
114+
TargetType="RadioButton" />
181115

182116
<ControlTheme x:Key="RadioButtonSameAsButtonPrimary.Small"
183-
BasedOn="{StaticResource RadioButtonSameAsButtonPrimary}"
184-
TargetType="RadioButton">
185-
<Setter Property="Height"
186-
Value="20" />
187-
<Setter Property="Padding"
188-
Value="6,1" />
189-
<Setter Property="hc:IconElement.Height"
190-
Value="12" />
191-
</ControlTheme>
117+
BasedOn="{StaticResource ToggleButtonPrimary.Small}"
118+
TargetType="RadioButton" />
192119

193120
<ControlTheme x:Key="RadioButtonSameAsButtonSuccess"
194-
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
195-
TargetType="RadioButton">
196-
<Setter Property="Background"
197-
Value="{DynamicResource SuccessBrush}" />
198-
<Setter Property="BorderBrush"
199-
Value="{DynamicResource SuccessBrush}" />
200-
</ControlTheme>
121+
BasedOn="{StaticResource ToggleButtonSuccess}"
122+
TargetType="RadioButton" />
201123

202124
<ControlTheme x:Key="RadioButtonSameAsButtonSuccess.Small"
203-
BasedOn="{StaticResource RadioButtonSameAsButtonSuccess}"
204-
TargetType="RadioButton">
205-
<Setter Property="Height"
206-
Value="20" />
207-
<Setter Property="Padding"
208-
Value="6,1" />
209-
<Setter Property="hc:IconElement.Height"
210-
Value="12" />
211-
</ControlTheme>
125+
BasedOn="{StaticResource ToggleButtonSuccess.Small}"
126+
TargetType="RadioButton" />
212127

213128
<ControlTheme x:Key="RadioButtonSameAsButtonInfo"
214-
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
215-
TargetType="RadioButton">
216-
<Setter Property="Background"
217-
Value="{DynamicResource InfoBrush}" />
218-
<Setter Property="BorderBrush"
219-
Value="{DynamicResource InfoBrush}" />
220-
</ControlTheme>
129+
BasedOn="{StaticResource ToggleButtonInfo}"
130+
TargetType="RadioButton" />
221131

222132
<ControlTheme x:Key="RadioButtonSameAsButtonInfo.Small"
223-
BasedOn="{StaticResource RadioButtonSameAsButtonInfo}"
224-
TargetType="RadioButton">
225-
<Setter Property="Height"
226-
Value="20" />
227-
<Setter Property="Padding"
228-
Value="6,1" />
229-
<Setter Property="hc:IconElement.Height"
230-
Value="12" />
231-
</ControlTheme>
133+
BasedOn="{StaticResource ToggleButtonInfo.Small}"
134+
TargetType="RadioButton" />
232135

233136
<ControlTheme x:Key="RadioButtonSameAsButtonWarning"
234-
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
235-
TargetType="RadioButton">
236-
<Setter Property="Background"
237-
Value="{DynamicResource WarningBrush}" />
238-
<Setter Property="BorderBrush"
239-
Value="{DynamicResource WarningBrush}" />
240-
</ControlTheme>
137+
BasedOn="{StaticResource ToggleButtonWarning}"
138+
TargetType="RadioButton" />
241139

242140
<ControlTheme x:Key="RadioButtonSameAsButtonWarning.Small"
243-
BasedOn="{StaticResource RadioButtonSameAsButtonWarning}"
244-
TargetType="RadioButton">
245-
<Setter Property="Height"
246-
Value="20" />
247-
<Setter Property="Padding"
248-
Value="6,1" />
249-
<Setter Property="hc:IconElement.Height"
250-
Value="12" />
251-
</ControlTheme>
141+
BasedOn="{StaticResource ToggleButtonWarning.Small}"
142+
TargetType="RadioButton" />
252143

253144
<ControlTheme x:Key="RadioButtonSameAsButtonDanger"
254-
BasedOn="{StaticResource RadioButtonSameAsButtonBaseStyle}"
255-
TargetType="RadioButton">
256-
<Setter Property="Background"
257-
Value="{DynamicResource DangerBrush}" />
258-
<Setter Property="BorderBrush"
259-
Value="{DynamicResource DangerBrush}" />
260-
</ControlTheme>
145+
BasedOn="{StaticResource ToggleButtonDanger}"
146+
TargetType="RadioButton" />
261147

262148
<ControlTheme x:Key="RadioButtonSameAsButtonDanger.Small"
263-
BasedOn="{StaticResource RadioButtonSameAsButtonDanger}"
264-
TargetType="RadioButton">
265-
<Setter Property="Height"
266-
Value="20" />
267-
<Setter Property="Padding"
268-
Value="6,1" />
269-
<Setter Property="hc:IconElement.Height"
270-
Value="12" />
271-
</ControlTheme>
149+
BasedOn="{StaticResource ToggleButtonDanger.Small}"
150+
TargetType="RadioButton" />
272151

273152
<ControlTheme x:Key="RadioButtonIconBaseStyle"
274153
BasedOn="{StaticResource ButtonBaseStyle}"

src/Avalonia/HandyControl_Avalonia/Themes/Styles/Style.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Window.axaml" />
1616
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Button.axaml" />
1717
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/RepeatButton.axaml" />
18+
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/ToggleButton.axaml" />
1819
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/RadioButton.axaml" />
1920
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/Border.axaml" />
2021
<MergeResourceInclude Source="avares://HandyControl/Themes/Styles/ItemsControl.axaml" />

0 commit comments

Comments
 (0)