feat: add generic thickness/cornerradius converter.

This commit is contained in:
rabbitism
2023-07-28 00:34:06 +08:00
parent 4c597a5d1c
commit 45b2f5eb41
5 changed files with 191 additions and 11 deletions

View File

@@ -1,7 +1,8 @@
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
xmlns:u="https://irihi.tech/ursa"
xmlns:converters="clr-namespace:Ursa.Converters;assembly=Ursa">
<ControlTheme x:Key="{x:Type u:DualBadge}" TargetType="u:DualBadge">
<Setter Property="u:DualBadge.CornerRadius" Value="{DynamicResource DualBadgeDefaultCornerRadius}" />
<Setter Property="u:DualBadge.FontSize" Value="{DynamicResource DualBadgeDefaultFontSize}" />
@@ -30,10 +31,9 @@
Name="{x:Static u:DualBadge.PART_Icon}"
Width="14"
Height="14"
Margin="2 0 0 0"
IsVisible="{TemplateBinding Icon,Converter={x:Static ObjectConverters.IsNotNull}}"
Foreground="{TemplateBinding IconForeground}"
Padding="{TemplateBinding Padding}"
Margin="{TemplateBinding Padding, Converter={x:Static converters:ThicknessExcludeConverter.Right}}"
Content="{TemplateBinding Icon}"
ContentTemplate="{TemplateBinding IconTemplate}" />
<ContentPresenter
@@ -41,7 +41,7 @@
TextElement.FontSize="{TemplateBinding FontSize}"
IsVisible="{TemplateBinding Header,Converter={x:Static ObjectConverters.IsNotNull}}"
Foreground="{TemplateBinding HeaderForeground}"
Padding="{TemplateBinding Padding}"
Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}" />
</DockPanel>
@@ -84,7 +84,6 @@
BasedOn="{StaticResource {x:Type u:DualBadge}}"
TargetType="u:DualBadge">
<Setter Property="CornerRadius" Value="0" />
<Setter Property="Padding" Value="12 6" />
<Setter Property="Padding" Value="6 6" />
</ControlTheme>
</ResourceDictionary>

View File

@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../Package.props"/>
<Import Project="../Package.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
@@ -13,11 +13,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)"/>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ursa\Ursa.csproj"/>
<ProjectReference Include="..\Ursa\Ursa.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,90 @@
using System.Globalization;
using Avalonia;
using Avalonia.Data.Converters;
namespace Ursa.Converters;
[Flags]
public enum CornerRadiusPosition
{
TopLeft = 1,
TopRight = 2,
BottomLeft = 4,
BottomRight = 8,
Top = 3,
Left = 5,
Right = 10,
Bottom = 12,
}
public class CornerRadiusIncludeConverter: IValueConverter
{
public static CornerRadiusIncludeConverter TopLeft { get; } = new( CornerRadiusPosition.TopLeft );
public static CornerRadiusIncludeConverter TopRight { get; } = new( CornerRadiusPosition.TopRight );
public static CornerRadiusIncludeConverter BottomLeft { get; } = new( CornerRadiusPosition.BottomLeft );
public static CornerRadiusIncludeConverter BottomRight { get; } = new( CornerRadiusPosition.BottomRight );
public static CornerRadiusIncludeConverter Top { get; } = new( CornerRadiusPosition.Top );
public static CornerRadiusIncludeConverter Left { get; } = new( CornerRadiusPosition.Left );
public static CornerRadiusIncludeConverter Right { get; } = new( CornerRadiusPosition.Right );
public static CornerRadiusIncludeConverter Bottom { get; } = new( CornerRadiusPosition.Bottom );
private readonly CornerRadiusPosition _position;
public CornerRadiusIncludeConverter(CornerRadiusPosition position)
{
_position = position;
}
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is CornerRadius r)
{
double topLeft = _position.HasFlag(CornerRadiusPosition.TopLeft) ? r.TopLeft : 0;
double topRight = _position.HasFlag(CornerRadiusPosition.TopRight) ? r.TopRight : 0;
double bottomLeft = _position.HasFlag(CornerRadiusPosition.BottomLeft) ? r.BottomLeft : 0;
double bottomRight = _position.HasFlag(CornerRadiusPosition.BottomRight) ? r.BottomRight : 0;
return new CornerRadius(topLeft, topRight, bottomRight, bottomLeft);
}
return AvaloniaProperty.UnsetValue;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class CornerRadiusExcludeConverter : IValueConverter
{
public static CornerRadiusExcludeConverter TopLeft { get; } = new( CornerRadiusPosition.TopLeft );
public static CornerRadiusExcludeConverter TopRight { get; } = new( CornerRadiusPosition.TopRight );
public static CornerRadiusExcludeConverter BottomLeft { get; } = new( CornerRadiusPosition.BottomLeft );
public static CornerRadiusExcludeConverter BottomRight { get; } = new( CornerRadiusPosition.BottomRight );
public static CornerRadiusExcludeConverter Top { get; } = new( CornerRadiusPosition.Top );
public static CornerRadiusExcludeConverter Left { get; } = new( CornerRadiusPosition.Left );
public static CornerRadiusExcludeConverter Right { get; } = new( CornerRadiusPosition.Right );
public static CornerRadiusExcludeConverter Bottom { get; } = new( CornerRadiusPosition.Bottom );
private readonly CornerRadiusPosition _position;
public CornerRadiusExcludeConverter(CornerRadiusPosition position)
{
_position = position;
}
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is CornerRadius r)
{
double topLeft = _position.HasFlag(CornerRadiusPosition.TopLeft) ? 0 : r.TopLeft;
double topRight = _position.HasFlag(CornerRadiusPosition.TopRight) ? 0 : r.TopRight;
double bottomLeft = _position.HasFlag(CornerRadiusPosition.BottomLeft) ? 0 : r.BottomLeft;
double bottomRight = _position.HasFlag(CornerRadiusPosition.BottomRight) ? 0 : r.BottomRight;
return new CornerRadius(topLeft, topRight, bottomRight, bottomLeft);
}
return AvaloniaProperty.UnsetValue;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,91 @@
using System.Globalization;
using Avalonia;
using Avalonia.Data.Converters;
namespace Ursa.Converters;
[Flags]
public enum ThicknessPosition
{
Left = 1,
Top = 2,
Right = 4,
Bottom = 8,
TopLeft = 3,
TopRight = 6,
BottomLeft = 9,
BottomRight = 12,
}
public class ThicknessExcludeConverter: IValueConverter
{
public static ThicknessExcludeConverter Left { get; } = new( ThicknessPosition.Left );
public static ThicknessExcludeConverter Top { get; } = new( ThicknessPosition.Top );
public static ThicknessExcludeConverter Right { get; } = new( ThicknessPosition.Right );
public static ThicknessExcludeConverter Bottom { get; } = new( ThicknessPosition.Bottom );
public static ThicknessExcludeConverter TopLeft { get; } = new( ThicknessPosition.TopLeft );
public static ThicknessExcludeConverter TopRight { get; } = new( ThicknessPosition.TopRight );
public static ThicknessExcludeConverter BottomLeft { get; } = new( ThicknessPosition.BottomLeft );
public static ThicknessExcludeConverter BottomRight { get; } = new( ThicknessPosition.BottomRight );
private readonly ThicknessPosition _position;
public ThicknessExcludeConverter(ThicknessPosition position)
{
_position = position;
}
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is Thickness t)
{
double left = _position.HasFlag(ThicknessPosition.Left) ? 0d: t.Left;
double top = _position.HasFlag(ThicknessPosition.Top) ? 0d : t.Top;
double right = _position.HasFlag(ThicknessPosition.Right) ? 0d : t.Right;
double bottom = _position.HasFlag(ThicknessPosition.Bottom) ? 0d : t.Bottom;
return new Thickness(left, top, right, bottom);
}
return AvaloniaProperty.UnsetValue;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ThicknessIncludeConverter: IValueConverter
{
public static ThicknessIncludeConverter Left { get; } = new( ThicknessPosition.Left );
public static ThicknessIncludeConverter Top { get; } = new( ThicknessPosition.Top );
public static ThicknessIncludeConverter Right { get; } = new( ThicknessPosition.Right );
public static ThicknessIncludeConverter Bottom { get; } = new( ThicknessPosition.Bottom );
public static ThicknessIncludeConverter TopLeft { get; } = new( ThicknessPosition.TopLeft );
public static ThicknessIncludeConverter TopRight { get; } = new( ThicknessPosition.TopRight );
public static ThicknessIncludeConverter BottomLeft { get; } = new( ThicknessPosition.BottomLeft );
public static ThicknessIncludeConverter BottomRight { get; } = new( ThicknessPosition.BottomRight );
private readonly ThicknessPosition _position;
public ThicknessIncludeConverter(ThicknessPosition position)
{
_position = position;
}
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is Thickness t)
{
var left = _position.HasFlag(ThicknessPosition.Left) ? t.Left : 0d;
var top = _position.HasFlag(ThicknessPosition.Top) ? t.Top : 0d;
var right = _position.HasFlag(ThicknessPosition.Right) ? t.Right : 0d;
var bottom = _position.HasFlag(ThicknessPosition.Bottom) ? t.Bottom : 0d;
return new Thickness(left, top, right, bottom);
}
return AvaloniaProperty.UnsetValue;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

View File

@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../Package.props"/>
<Import Project="../Package.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
@@ -13,7 +13,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)"/>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
</ItemGroup>
</Project>