mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-03-02 15:50:49 +08:00
fix: Cannot set Background for default SolidButton theme
This commit is contained in:
@@ -52,6 +52,7 @@
|
||||
<Button Classes="Success" Theme="{DynamicResource SolidButton}">Success</Button>
|
||||
<Button Classes="Warning" Theme="{DynamicResource SolidButton}">Warning</Button>
|
||||
<Button Classes="Danger" Theme="{DynamicResource SolidButton}">Danger</Button>
|
||||
<Button Background="Purple" BorderBrush="Purple" Theme="{DynamicResource SolidButton}">Custom</Button>
|
||||
<Button
|
||||
Classes="Danger"
|
||||
IsEnabled="False"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<ResourceDictionary
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:semiConverters="clr-namespace:Semi.Avalonia.Converters"
|
||||
x:CompileBindings="True">
|
||||
<!-- Button Theme Key: Light, Solid, Outline, Borderless; Default is Light -->
|
||||
<!-- Button Default Classes: Primary, Secondary, Tertiary, Success, Warning, Danger; Default is Primary -->
|
||||
@@ -97,6 +98,8 @@
|
||||
<Setter Property="Padding" Value="{DynamicResource ButtonSmallPadding}" />
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
|
||||
<semiConverters:AdaptiveColorConverter x:Key="AdaptiveColorConverter"/>
|
||||
|
||||
<ControlTheme
|
||||
x:Key="SolidButton"
|
||||
@@ -108,20 +111,22 @@
|
||||
Classes="Solid" />
|
||||
</FocusAdornerTemplate>
|
||||
</Setter>
|
||||
<Setter Property="Background" Value="{DynamicResource ButtonSolidPrimaryBackground}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource ButtonSolidPrimaryBorderBrush}" />
|
||||
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
|
||||
<Setter Property="Foreground" Value="{DynamicResource ButtonSolidForeground}" />
|
||||
<Setter Property="Background" Value="{DynamicResource ButtonSolidPrimaryBackground}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource ButtonSolidPrimaryBorderBrush}" />
|
||||
<Setter Property="Background" Value="{TemplateBinding Background}" />
|
||||
<Setter Property="BorderBrush" Value="{TemplateBinding BorderBrush}" />
|
||||
</Style>
|
||||
|
||||
<Style Selector="^:pointerover /template/ ContentPresenter#PART_ContentPresenter">
|
||||
<Setter Property="Background" Value="{DynamicResource ButtonSolidPrimaryPointeroverBackground}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource ButtonSolidPrimaryPointeroverBorderBrush}" />
|
||||
<Setter Property="Background" Value="{TemplateBinding Background, Converter={StaticResource AdaptiveColorConverter}, ConverterParameter=0.2}" />
|
||||
<Setter Property="BorderBrush" Value="{TemplateBinding BorderBrush, Converter={StaticResource AdaptiveColorConverter}, ConverterParameter=0.2}" />
|
||||
</Style>
|
||||
<Style Selector="^:pressed /template/ ContentPresenter#PART_ContentPresenter">
|
||||
<Setter Property="Foreground" Value="{DynamicResource ButtonSolidPrimaryPressedForeground}" />
|
||||
<Setter Property="Background" Value="{DynamicResource ButtonSolidPrimaryPressedBackground}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource ButtonSolidPrimaryPressedBorderBrush}" />
|
||||
<Setter Property="Background" Value="{TemplateBinding Background, Converter={StaticResource AdaptiveColorConverter}, ConverterParameter=0.4}" />
|
||||
<Setter Property="BorderBrush" Value="{TemplateBinding BorderBrush, Converter={StaticResource AdaptiveColorConverter}, ConverterParameter=0.4}" />
|
||||
</Style>
|
||||
|
||||
<Style Selector="^.Primary">
|
||||
|
||||
40
src/Semi.Avalonia/Converters/AdaptiveColorConverter.cs
Normal file
40
src/Semi.Avalonia/Converters/AdaptiveColorConverter.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Data.Converters;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Media.Immutable;
|
||||
using Avalonia.Styling;
|
||||
|
||||
namespace Semi.Avalonia.Converters;
|
||||
|
||||
public class AdaptiveColorConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is IImmutableSolidColorBrush brush)
|
||||
{
|
||||
var color = brush.Color;
|
||||
var theme = Application.Current.ActualThemeVariant;
|
||||
var para = double.Parse(parameter?.ToString() ?? "0.2");
|
||||
|
||||
double factor = theme == ThemeVariant.Light ? -para : para;
|
||||
return AdjustColorBrightness(color, factor);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private IImmutableSolidColorBrush AdjustColorBrightness(Color color, double factor)
|
||||
{
|
||||
// 调整颜色亮度
|
||||
var r = (byte)Math.Min(255, Math.Max(0, color.R + color.R * factor));
|
||||
var g = (byte)Math.Min(255, Math.Max(0, color.G + color.G * factor));
|
||||
var b = (byte)Math.Min(255, Math.Max(0, color.B + color.B * factor));
|
||||
return new ImmutableSolidColorBrush(Color.FromRgb(r, g, b));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user