Skip to content

Commit

Permalink
✨ Add toggle switch styles
Browse files Browse the repository at this point in the history
  • Loading branch information
russkyc committed Dec 31, 2024
1 parent 6820185 commit 2f28b62
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 5 deletions.
1 change: 1 addition & 0 deletions Russkyc.UI.Avalonia.Gallery/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public MainViewModel()
_pages = [
new ButtonView(),
new ToggleButtonView(),
new ToggleSwitchView(),
new RadioButtonView(),
new TextBoxView(),
new ComboBoxView(),
Expand Down
60 changes: 60 additions & 0 deletions Russkyc.UI.Avalonia.Gallery/Views/Pages/ToggleSwitchView.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<UserControl
x:Class="Russkyc.UI.Avalonia.Gallery.Views.Pages.ToggleSwitchView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignWidth="800"
mc:Ignorable="d">
<StackPanel Margin="24" Spacing="24">
<StackPanel Spacing="-2">
<TextBlock
FontSize="24"
FontWeight="Bold"
Text="ToggleSwitch" />
<TextBlock Text="Displays a toggle switch." />
</StackPanel>
<StackPanel Spacing="8">
<TextBlock
FontSize="16"
FontWeight="SemiBold"
Text="ToggleSwitch Variants Preview" />
<Border Height="200" Classes="Card">
<WrapPanel
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<WrapPanel.Styles>
<Style Selector="ToggleSwitch">
<Setter Property="Margin" Value="4" />
</Style>
</WrapPanel.Styles>
<ToggleSwitch VerticalAlignment="Bottom" />
<ToggleSwitch
VerticalAlignment="Bottom"
OffContent="Off Content"
OnContent="On Content" />
<ToggleSwitch
VerticalAlignment="Bottom"
IsEnabled="False"
OffContent="Disabled"
OnContent="Disabled" />
</WrapPanel>
</Border>
</StackPanel>
<StackPanel Spacing="8">
<TextBlock
FontSize="16"
FontWeight="SemiBold"
Text="Code" />
<ThemeVariantScope RequestedThemeVariant="Dark">
<TextBox
Name="CodeBlock"
Padding="12"
CornerRadius="12"
FontSize="16"
IsReadOnly="True" />
</ThemeVariantScope>
</StackPanel>
</StackPanel>
</UserControl>
35 changes: 35 additions & 0 deletions Russkyc.UI.Avalonia.Gallery/Views/Pages/ToggleSwitchView.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Avalonia.Controls;

namespace Russkyc.UI.Avalonia.Gallery.Views.Pages;

public partial class ToggleSwitchView : UserControl
{
public ToggleSwitchView()
{
InitializeComponent();
CodeBlock.Text = """
<Border Height="200" Classes="Card">
<WrapPanel
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<WrapPanel.Styles>
<Style Selector="ToggleSwitch">
<Setter Property="Margin" Value="4" />
</Style>
</WrapPanel.Styles>
<ToggleSwitch VerticalAlignment="Bottom" />
<ToggleSwitch
VerticalAlignment="Bottom"
OffContent="Off Content"
OnContent="On Content" />
<ToggleSwitch
VerticalAlignment="Bottom"
IsEnabled="False"
OffContent="Disabled"
OnContent="Disabled" />
</WrapPanel>
</Border>
""";
}
}
16 changes: 12 additions & 4 deletions Russkyc.UI.Avalonia.Gallery/Views/Sidebar.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,42 @@
<RadioButton
Classes="Toggle Navigation"
Command="{Binding OpenPageCommand}"
Content="RadioButton">
Content="ToggleSwitch">
<RadioButton.CommandParameter>
<x:Int32>2</x:Int32>
</RadioButton.CommandParameter>
</RadioButton>
<RadioButton
Classes="Toggle Navigation"
Command="{Binding OpenPageCommand}"
Content="Textbox">
Content="RadioButton">
<RadioButton.CommandParameter>
<x:Int32>3</x:Int32>
</RadioButton.CommandParameter>
</RadioButton>
<RadioButton
Classes="Toggle Navigation"
Command="{Binding OpenPageCommand}"
Content="ComboBox">
Content="Textbox">
<RadioButton.CommandParameter>
<x:Int32>4</x:Int32>
</RadioButton.CommandParameter>
</RadioButton>
<RadioButton
Classes="Toggle Navigation"
Command="{Binding OpenPageCommand}"
Content="Card">
Content="ComboBox">
<RadioButton.CommandParameter>
<x:Int32>5</x:Int32>
</RadioButton.CommandParameter>
</RadioButton>
<RadioButton
Classes="Toggle Navigation"
Command="{Binding OpenPageCommand}"
Content="Card">
<RadioButton.CommandParameter>
<x:Int32>6</x:Int32>
</RadioButton.CommandParameter>
</RadioButton>
</StackPanel>
</UserControl>
3 changes: 2 additions & 1 deletion Russkyc.UI.Avalonia/Controls.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
</ControlTheme>
</ResourceDictionary>
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/Button.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/Border.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/RadioButton.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/ToggleButton.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/ToggleSwitch.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/ScrollBar.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/TextBox.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/ComboBox.axaml" />
<ResourceInclude Source="avares://Russkyc.UI.Avalonia/Controls/Border.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
188 changes: 188 additions & 0 deletions Russkyc.UI.Avalonia/Controls/ToggleSwitch.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Design.PreviewWith>
<StackPanel
Width="250"
Margin="20"
Spacing="24">
<StackPanel Orientation="Horizontal">
<ToggleSwitch VerticalAlignment="Bottom" />
<ToggleSwitch
VerticalAlignment="Bottom"
OffContent="Off Content"
OnContent="On Content" />
<ToggleSwitch
VerticalAlignment="Bottom"
IsEnabled="False"
OffContent="Disabled"
OnContent="Disabled" />
</StackPanel>
</StackPanel>
</Design.PreviewWith>

<Thickness x:Key="ToggleSwitchTopHeaderMargin">0,0,0,4</Thickness>
<GridLength x:Key="ToggleSwitchPreContentMargin">0</GridLength>
<GridLength x:Key="ToggleSwitchPostContentMargin">0</GridLength>
<x:Double x:Key="ToggleSwitchThemeMinWidth">0</x:Double>

<ControlTheme x:Key="{x:Type ToggleSwitch}" TargetType="ToggleSwitch">
<Setter Property="Foreground" Value="{DynamicResource Foreground}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="OffContent" Value="" />
<Setter Property="OnContent" Value="" />
<Setter Property="KnobTransitions">
<Transitions>
<DoubleTransition
Easing="CubicEaseOut"
Property="Canvas.Left"
Duration="0:0:0.2" />
</Transitions>
</Setter>
<Setter Property="Template">
<ControlTemplate>
<Grid Background="{TemplateBinding Background}" RowDefinitions="Auto,*">
<ContentPresenter
x:Name="PART_ContentPresenter"
Grid.Row="0"
Margin="{DynamicResource ToggleSwitchTopHeaderMargin}"
VerticalAlignment="Top"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
RecognizesAccessKey="True" />
<Grid
Grid.Row="1"
MinWidth="{DynamicResource ToggleSwitchThemeMinWidth}"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="{DynamicResource ToggleSwitchPreContentMargin}" />
<RowDefinition Height="Auto" />
<RowDefinition Height="{DynamicResource ToggleSwitchPostContentMargin}" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="8" MaxWidth="24" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid
x:Name="SwitchAreaGrid"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="0"
Grid.ColumnSpan="3"
TemplatedControl.IsTemplateFocusTarget="True" />
<ContentPresenter
x:Name="PART_OffContentPresenter"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="2"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding OffContent}"
ContentTemplate="{TemplateBinding OffContentTemplate}" />
<ContentPresenter
x:Name="PART_OnContentPresenter"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="2"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding OnContent}"
ContentTemplate="{TemplateBinding OnContentTemplate}" />
<Border
x:Name="OuterBorder"
Grid.Row="1"
Grid.Column="0"
Width="40"
Height="26"
Background="{DynamicResource BorderBrush}"
BorderBrush="{DynamicResource BorderBrush}"
BorderThickness="0"
CornerRadius="14" />
<Border
x:Name="SwitchKnobBounds"
Grid.Row="1"
Grid.Column="0"
Width="39"
Height="26"
Background="{DynamicResource PrimaryBrush}"
BorderBrush="{DynamicResource BorderBrush}"
BorderThickness="0"
CornerRadius="14" />
<Canvas
x:Name="PART_SwitchKnob"
Grid.Row="1"
Grid.Column="0"
Width="13"
Height="20"
HorizontalAlignment="Left">
<Grid
x:Name="PART_MovingKnobs"
Width="26"
Height="20">
<Ellipse
x:Name="SwitchKnobOn"
Width="18"
Height="18"
VerticalAlignment="Center"
Fill="{DynamicResource BackgroundBrush}" />
<Ellipse
x:Name="SwitchKnobOff"
Width="18"
Height="18"
VerticalAlignment="Center"
Fill="{DynamicResource BackgroundBrush}" />
</Grid>
</Canvas>
</Grid>
</Grid>
</ControlTemplate>
</Setter>

<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter:empty">
<Setter Property="Margin" Value="0" />
</Style>
<!-- DisabledState -->
<Style Selector="^:disabled">
<Setter Property="Foreground" Value="{DynamicResource MutedForegroundBrush}" />
<Style Selector="^ /template/ Border#OuterBorder">
<Setter Property="Background" Value="{DynamicResource MutedForegroundBrush}" />
</Style>
</Style>
<!-- CheckedState -->
<Style Selector="^:checked">
<Style Selector="^ /template/ Ellipse#SwitchKnobOff">
<Setter Property="Opacity" Value="0" />
</Style>
<Style Selector="^ /template/ Ellipse#SwitchKnobOn">
<Setter Property="Opacity" Value="1" />
</Style>
<Style Selector="^ /template/ ContentPresenter#PART_OffContentPresenter">
<Setter Property="Opacity" Value="0" />
</Style>
<Style Selector="^ /template/ ContentPresenter#PART_OnContentPresenter">
<Setter Property="Opacity" Value="1" />
</Style>
</Style>
<!-- UncheckedState -->
<Style Selector="^:unchecked">
<Style Selector="^ /template/ Ellipse#SwitchKnobOff">
<Setter Property="Opacity" Value="1" />
</Style>
<Style Selector="^ /template/ Ellipse#SwitchKnobOn">
<Setter Property="Opacity" Value="0" />
</Style>
<Style Selector="^ /template/ Border#SwitchKnobBounds">
<Setter Property="Opacity" Value="0" />
</Style>
<Style Selector="^ /template/ ContentPresenter#PART_OffContentPresenter">
<Setter Property="Opacity" Value="1" />
</Style>
<Style Selector="^ /template/ ContentPresenter#PART_OnContentPresenter">
<Setter Property="Opacity" Value="0" />
</Style>
</Style>
</ControlTheme>
</ResourceDictionary>

0 comments on commit 2f28b62

Please sign in to comment.