This commit is contained in:
GG Z
2025-07-31 20:12:24 +08:00
parent 4f6cd2137c
commit f209e7d3ad
426 changed files with 15854 additions and 6612 deletions

View File

@@ -0,0 +1,382 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace AntDesignWPF.Controls;
/// <summary>
/// To trigger an operation.
/// </summary>
[TemplatePart(Name = PART_Border, Type = typeof(FrameworkElement))]
[TemplateVisualState(Name = "Loaded", GroupName = "LoadStates")]
[TemplateVisualState(Name = "Unloaded", GroupName = "LoadStates")]
public class AntButton : Button
{
#region Fields
private const string PART_Border = "PART_Border";
private FrameworkElement border;
private VisualState mouseOverState;
private VisualState pressedState;
private VisualState focusedState;
#endregion
#region Properties
public static readonly DependencyProperty GhostProperty =
DependencyProperty.Register(
"Ghost",
typeof(bool),
typeof(AntButton),
new PropertyMetadata(false, OnEffectBrushChanged));
/// <summary>
/// Gets/sets whether to make the background transparent and invert text and border colors.
/// </summary>
public bool Ghost { get { return (bool)GetValue(GhostProperty); } set { SetValue(GhostProperty, value); } }
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(string), typeof(AntButton), new PropertyMetadata(null));
/// <summary>
/// Gets/sets the icon type of the button.
/// </summary>
public string Icon { get { return (string)GetValue(IconProperty); } set { SetValue(IconProperty, value); } }
public static readonly DependencyProperty LoadingProperty =
DependencyProperty.Register(
"Loading",
typeof(bool),
typeof(AntButton),
new PropertyMetadata(false, OnLoadingChanged));
/// <summary>
/// Gets/sets the loading state of the button.
/// </summary>
public bool Loading { get { return (bool)GetValue(LoadingProperty); } set { SetValue(LoadingProperty, value); } }
private static void OnLoadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ (d as AntButton).SetLoadVisualState(); }
private void SetLoadVisualState() { VisualStateManager.GoToState(this, (Loading ? "Loaded" : "Unloaded"), true); }
public static readonly DependencyProperty ShapeProperty =
DependencyProperty.Register("Shape", typeof(Shapes), typeof(AntButton), new PropertyMetadata(Shapes.Square));
/// <summary>
/// Gets/sets the shape of button.
/// </summary>
public Shapes Shape { get { return (Shapes)GetValue(ShapeProperty); } set { SetValue(ShapeProperty, value); } }
public static readonly DependencyProperty SizeProperty =
DependencyProperty.Register("Size", typeof(Sizes?), typeof(AntButton), new PropertyMetadata(null));
/// <summary>
/// Gets/sets the size of the button.
/// </summary>
public Sizes? Size { get { return (Sizes?)GetValue(SizeProperty); } set { SetValue(SizeProperty, value); } }
public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register(
"Color",
typeof(AccentColor),
typeof(AntButton),
new PropertyMetadata(AccentColor.Default));
/// <summary>
/// 颜色
/// </summary>
public AccentColor Color
{
get { return (AccentColor)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
public static readonly DependencyProperty VariantProperty =
DependencyProperty.Register("Variant",
typeof(VariantType),
typeof(AntButton),
new PropertyMetadata(VariantType.Solid));
/// <summary>
/// Gets/sets the type of the button.
/// </summary>
public VariantType? Variant
{
get { return (VariantType?)GetValue(VariantProperty); }
set { SetValue(VariantProperty, value); }
}
public static readonly DependencyProperty EffectBrushProperty = DependencyProperty.Register(
"EffectBrush",
typeof(Brush),
typeof(AntButton),
new FrameworkPropertyMetadata(
Brushes.Transparent,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits,
OnEffectBrushChanged));
/// <summary>
/// Gets/sets the border effect brush of the button.
/// </summary>
public Brush EffectBrush
{
get { return (Brush)GetValue(EffectBrushProperty); }
set { SetValue(EffectBrushProperty, value); }
}
public static readonly DependencyProperty ShadowBrushProperty =
DependencyProperty.Register(
"ShadowBrush",
typeof(Brush),
typeof(AntButton),
new PropertyMetadata(Brushes.Transparent));
/// <summary>
/// 阴影颜色
/// </summary>
public Brush ShadowBrush
{
get { return (Brush)GetValue(ShadowBrushProperty); }
set { SetValue(ShadowBrushProperty, value); }
}
private static void OnEffectBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ (d as AntButton).SetVisualStateAnimation(); }
/// <summary>
/// Force background transparent in Ghost state.
/// </summary>
private static object OnBackgroundCoerceValue(DependencyObject d, object baseValue)
{
var button = d as AntButton;
if(button.Ghost)
{
return Brushes.Transparent;
}
return baseValue;
}
#endregion
#region Constructors
static AntButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AntButton), new FrameworkPropertyMetadata(typeof(AntButton)));
BackgroundProperty.OverrideMetadata(
typeof(AntButton),
new FrameworkPropertyMetadata { CoerceValueCallback = OnBackgroundCoerceValue });
}
#endregion
#region Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
border = GetTemplateChild(PART_Border) as FrameworkElement;
mouseOverState = GetTemplateChild("MouseOver") as VisualState;
focusedState = GetTemplateChild("Focused") as VisualState;
pressedState = GetTemplateChild("Pressed") as VisualState;
SetVisualStateAnimation();
SetLoadVisualState();
}
#endregion
#region Private Methods
private void SetVisualStateAnimation()
{
// No initialization or no need for me to handle.
if(border == null || mouseOverState == null && focusedState == null && pressedState == null)
return;
// Unable to extract color.
if(EffectBrush is SolidColorBrush brush)
{
var isShape = border is Shape;
Func<AntButton, Color, int, bool, bool, Duration?, Storyboard> func;
if(!Variant.HasValue || Variant.Value == VariantType.Dashed)
{
func = CreateDefaultStoryboard;
}
else if(Variant.Value == VariantType.Solid)
{
func = CreatePrimaryStoryboard;
}
else
{
// Danger
func = CreateDangerStoryboard;
}
if(mouseOverState != null)
{
mouseOverState.Storyboard = func(this, brush.Color, 5, isShape, false, null);
}
if(focusedState != null)
{
focusedState.Storyboard = func(this, brush.Color, 5, isShape, true, null);
}
if(pressedState != null)
{
pressedState.Storyboard = func(this, brush.Color, 7, isShape, false, TimeSpan.FromSeconds(0));
}
}
}
private static Storyboard CreateDefaultStoryboard(
AntButton button,
Color color,
int index,
bool IsShape,
bool focused,
Duration? duration = null)
{
var storyboard = new Storyboard();
var children = storyboard.Children;
color = ColorPalette.Toning(color, index);
children.Add(CreateForegroundAnimation(button, color, duration));
children.Add(CreateBorderAnimation(PART_Border, color, IsShape, duration));
return storyboard;
}
private static Storyboard CreatePrimaryStoryboard(
AntButton button,
Color color,
int index,
bool IsShape,
bool focused,
Duration? duration = null)
{
var storyboard = new Storyboard();
var children = storyboard.Children;
color = ColorPalette.Toning(color, index);
if(button.Ghost)
{
children.Add(CreateForegroundAnimation(button, color, duration));
}
else
{
children.Add(CreateBackgroundAnimation(PART_Border, color, IsShape, duration));
}
children.Add(CreateBorderAnimation(PART_Border, color, IsShape, duration));
return storyboard;
}
private static Storyboard CreateDangerStoryboard(
AntButton button,
Color color,
int index,
bool IsShape,
bool focused,
Duration? duration = null)
{
var storyboard = new Storyboard();
var children = storyboard.Children;
Color foreground;
color = ColorPalette.Toning(color, index);
if(button.Ghost)
{
foreground = color;
}
else
{
Color background;
if(focused)
{
foreground = color;
background = Colors.White;
}
else
{
foreground = Colors.White;
background = color;
}
children.Add(CreateBackgroundAnimation(PART_Border, background, IsShape, duration));
}
children.Add(CreateForegroundAnimation(button, foreground, duration));
children.Add(CreateBorderAnimation(PART_Border, color, IsShape, duration));
return storyboard;
}
private static Timeline CreateForegroundAnimation(DependencyObject target, Color color, Duration? duration = null)
{ return CreateColorAnimation(target, "(Control.Foreground).(SolidColorBrush.Color)", color, duration); }
private static Timeline CreateBackgroundAnimation(
string target,
Color color,
bool IsShape,
Duration? duration = null)
{ return CreateColorAnimation(target, (IsShape ? "Fill" : "Background") + ".Color", color, duration); }
private static Timeline CreateBorderAnimation(string target, Color color, bool IsShape, Duration? duration = null)
{ return CreateColorAnimation(target, (IsShape ? "Stroke" : "BorderBrush") + ".Color", color, duration); }
private static Timeline CreateColorAnimation(object target, string path, Color color, Duration? duration)
{
var animation = new ColorAnimation() { To = color };
if(duration.HasValue)
{
animation.Duration = duration.Value;
}
if(target is DependencyObject)
{
Storyboard.SetTarget(animation, (DependencyObject)target);
}
else
{
Storyboard.SetTargetName(animation, (string)target);
}
Storyboard.SetTargetProperty(animation, new PropertyPath(path));
return animation;
}
#endregion
}
public enum VariantType
{
Solid,
Outlined,
Dashed,
Filled,
Text,
Link
}
public enum AccentColor
{
Default,
Primary,
Danger,
Pink,
Purple,
Cyan
}

View File

@@ -0,0 +1,266 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:AntDesignWPF.Controls"
xmlns:helpers="clr-namespace:AntDesignWPF.Helpers"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/AntDesignWPF;component/Themes/Converters.xaml" />
<ResourceDictionary Source="pack://application:,,,/AntDesignWPF;component/Themes/Animations.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type controls:AntButton}">
<Setter Property="Background" Value="{DynamicResource AntDesign.Brush.BackgroundContainer}" />
<Setter Property="BorderBrush" Value="{DynamicResource AntDesign.Brush.BorderPrimary}" />
<Setter Property="BorderThickness" Value="{DynamicResource BorderThicknessBase}" />
<!-- <Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Color="Black"
BlurRadius="10"
Direction="270"
Opacity="0.4"
ShadowDepth="5" />
</Setter.Value>
</Setter>-->
<Setter Property="ShadowBrush">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AntDesign.Color.Primary}" Opacity="0.2"/>
</Setter.Value>
</Setter>
<Setter Property="EffectBrush" Value="{DynamicResource AntDesign.Brush.Primary}" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="FontSize" Value="{DynamicResource FontSizeBase}" />
<Setter Property="FontWeight" Value="{DynamicResource ButtonFontWeight}" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.TextPrimary}" />
<Setter Property="Height" Value="{DynamicResource ButtonHeightBase}" />
<Setter Property="helpers:AntControl.BorderStyle" Value="{DynamicResource BorderStyleBase}" />
<Setter Property="helpers:AntControl.CornerRadius" Value="{DynamicResource ButtonBorderRadiusBase}" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="{DynamicResource ButtonPaddingBase}" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:AntButton}">
<Grid>
<Border x:Name="Shadow"
Background="{TemplateBinding ShadowBrush}"
Margin="0,0,0,-1"
CornerRadius="{TemplateBinding helpers:AntControl.CornerRadius}" >
<!--<Border.Clip>
<RectangleGeometry Rect="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:AntButton}}, Path=RenderSize}" />
</Border.Clip>-->
</Border>
<!-- Effect -->
<Border
BorderBrush="{TemplateBinding EffectBrush}"
BorderThickness="0"
CornerRadius="{TemplateBinding helpers:AntControl.CornerRadius}"
Focusable="False"
Margin="-1"
Opacity="0.4"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
x:Name="Effect" />
<!-- Border -->
<controls:AntBorder
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderStyle="{TemplateBinding helpers:AntControl.BorderStyle}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding helpers:AntControl.CornerRadius}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
UseLayoutRounding="True"
x:Name="PART_Border">
<!-- Content -->
<Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid.ColumnDefinitions>
<!-- Icon -->
<ColumnDefinition Width="Auto" />
<!-- Content -->
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<controls:Icon Grid.Column="0"
Focusable="False"
Margin="0,0,8,0"
VerticalAlignment="Center"
Type="{TemplateBinding Icon}"
x:Name="Icon" />
<ContentPresenter
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
Grid.Column="1"
RecognizesAccessKey="True"
x:Name="Content" />
</Grid>
</controls:AntBorder>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0" To="Pressed" />
<VisualTransition GeneratedDuration="0:0:0.3" GeneratedEasingFunction="{StaticResource EaseInOut}" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed" />
<VisualState x:Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.3" GeneratedEasingFunction="{StaticResource EaseInOut}" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Focused" />
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
<VisualStateGroup x:Name="LoadStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.2" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Loaded">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.65" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unloaded" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
<ControlTemplate.Triggers>
<!-- Loading State -->
<Trigger Property="Loading" Value="True">
<Setter Property="Type" TargetName="Icon" Value="loading" />
</Trigger>
<!-- Content State -->
<Trigger Property="Content" Value="{x:Null}">
<!--<Setter Property="Grid.ColumnSpan" TargetName="Icon" Value="2" />-->
<Setter Property="Visibility" TargetName="Content" Value="Collapsed" />
<Setter Property="Margin" TargetName="Icon" Value="0" />
</Trigger>
<Trigger Property="Type" SourceName="Icon" Value="{x:Null}">
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
</Trigger>
<!-- Shape Circle -->
<Trigger Property="Shape" Value="Circle">
<Setter Property="FontSize" TargetName="Icon" Value="{Binding FontSize, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource AdditionConverter}, ConverterParameter=2}" />
</Trigger>
<EventTrigger RoutedEvent="ButtonBase.Click">
<BeginStoryboard Storyboard="{StaticResource Ant.ClickAnimating}" />
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="VerticalContentAlignment" Value="Center" />
<Style.Triggers>
<!-- Ghost State -->
<Trigger Property="Ghost" Value="True">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="White" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<!-- Primary -->
<Trigger Property="Variant" Value="Solid">
<Setter Property="Background" Value="{DynamicResource AntDesign.Brush.Primary}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="EffectBrush" Value="{DynamicResource AntDesign.Brush.Primary}" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.TextOnPrimary}" />
</Trigger>
<!-- Outlined -->
<Trigger Property="Variant" Value="Outlined">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="{DynamicResource AntDesign.Brush.Primary}" />
<Setter Property="EffectBrush" Value="{DynamicResource AntDesign.Brush.Primary}" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.Primary}" />
</Trigger>
<!-- Dashed -->
<Trigger Property="Variant" Value="Dashed">
<Setter Property="helpers:AntControl.BorderStyle" Value="Dashed" />
<Setter Property="ShadowBrush" Value="{DynamicResource AntDesign.Brush.Primary}" />
</Trigger>
<!-- Filled -->
<Trigger Property="Variant" Value="Filled">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AntDesign.Color.Primary}" Opacity="0.2"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="EffectBrush" Value="{DynamicResource AntDesign.Brush.BackgroundLayout}" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.TextOnPrimary}" />
</Trigger>
<Trigger Property="Variant" Value="Text">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="EffectBrush" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.Primary}" />
</Trigger>
<!-- Link -->
<Trigger Property="Variant" Value="Link">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="EffectBrush" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.Primary}" />
</Trigger>
<!-- Size -->
<Trigger Property="Size" Value="Large">
<Setter Property="FontSize" Value="{DynamicResource ButtonFontSizeLarge}" />
<Setter Property="Height" Value="{DynamicResource ButtonHeightLarge}" />
<Setter Property="Padding" Value="{DynamicResource ButtonPaddingLarge}" />
</Trigger>
<Trigger Property="Size" Value="Small">
<Setter Property="FontSize" Value="{DynamicResource ButtonFontSizeSmall}" />
<Setter Property="Height" Value="{DynamicResource ButtonHeightSmall}" />
<Setter Property="helpers:AntControl.CornerRadius" Value="{DynamicResource ButtonBorderRadiusSmall}" />
<Setter Property="Padding" Value="{DynamicResource ButtonPaddingSmall}" />
</Trigger>
<!-- Shape Circle -->
<Trigger Property="Shape" Value="Circle">
<Setter Property="helpers:AntControl.CornerRadius" Value="{Binding Height, Mode=OneWay, RelativeSource={RelativeSource Self}, Converter={StaticResource DoubleToCornerRadiusConverter}, ConverterParameter=2}" />
<Setter Property="Padding" Value="0" />
<Setter Property="Width" Value="{Binding Height, Mode=OneWay, RelativeSource={RelativeSource Self}}" />
</Trigger>
<!-- Loading State -->
<Trigger Property="Loading" Value="True">
<Setter Property="IsEnabled" Value="False" />
</Trigger>
<!-- Ghost States -->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Ghost" Value="True" />
<Condition Property="Variant" Value="Solid" />
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.Primary}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Variant" Value="Solid" />
<Condition Property="Color" Value="Danger" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource AntDesign.Brush.Error}" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="EffectBrush" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.TextPrimary}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Ghost" Value="True" />
<!--<Condition Property="Type" Value="Danger" />-->
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" Value="{DynamicResource AntDesign.Brush.BackgroundLayout}" />
</MultiTrigger>
<!-- Disabled -->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Loading" Value="False" />
<Condition Property="IsEnabled" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource AntDesign.Brush.BackgroundLayout}" />
<Setter Property="BorderBrush" Value="{DynamicResource AntDesign.Brush.BorderPrimary}" />
<Setter Property="Foreground" Value="{DynamicResource AntDesign.Brush.TextQuaternary}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>