misc: refactor converters to inherit from MarkupValueConverter and update namespaces.

This commit is contained in:
Zhang Dian
2025-09-19 14:44:19 +08:00
parent 25d5e43781
commit 5b804a9767
15 changed files with 35 additions and 67 deletions

View File

@@ -4,7 +4,6 @@
xmlns:u="https://irihi.tech/ursa" xmlns:u="https://irihi.tech/ursa"
xmlns:iri="https://irihi.tech/shared" xmlns:iri="https://irihi.tech/shared"
xmlns:converters="clr-namespace:Ursa.Themes.Semi.Converters"> xmlns:converters="clr-namespace:Ursa.Themes.Semi.Converters">
<converters:TreeLevelToPaddingConverter x:Key="LevelToPaddingConverter" />
<ControlTheme x:Key="{x:Type u:Anchor}" TargetType="{x:Type u:Anchor}"> <ControlTheme x:Key="{x:Type u:Anchor}" TargetType="{x:Type u:Anchor}">
<Setter Property="Background" Value="Transparent" /> <Setter Property="Background" Value="Transparent" />
<Setter Property="VerticalAlignment" Value="Top" /> <Setter Property="VerticalAlignment" Value="Top" />
@@ -51,7 +50,7 @@
Content="{TemplateBinding Header}" Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"> ContentTemplate="{TemplateBinding HeaderTemplate}">
<ContentPresenter.Padding> <ContentPresenter.Padding>
<MultiBinding Converter="{StaticResource LevelToPaddingConverter}"> <MultiBinding Converter="{converters:TreeLevelToPaddingConverter}">
<Binding Path="Level" RelativeSource="{RelativeSource AncestorType={x:Type u:AnchorItem}}" /> <Binding Path="Level" RelativeSource="{RelativeSource AncestorType={x:Type u:AnchorItem}}" />
<DynamicResource ResourceKey="AnchorIndent" /> <DynamicResource ResourceKey="AnchorIndent" />
</MultiBinding> </MultiBinding>

View File

@@ -28,8 +28,6 @@
</StackPanel> </StackPanel>
</Design.PreviewWith> </Design.PreviewWith>
<converters:BadgeContentOverflowConverter x:Key="BadgeContentConverter" />
<ControlTheme x:Key="{x:Type u:Badge}" TargetType="{x:Type u:Badge}"> <ControlTheme x:Key="{x:Type u:Badge}" TargetType="{x:Type u:Badge}">
<!-- Set a very large corner radius to achieve pill look. --> <!-- Set a very large corner radius to achieve pill look. -->
<Setter Property="CornerRadius" Value="{DynamicResource BadgeCornerRadius}" /> <Setter Property="CornerRadius" Value="{DynamicResource BadgeCornerRadius}" />
@@ -85,7 +83,7 @@
TextElement.Foreground="{TemplateBinding Foreground}" TextElement.Foreground="{TemplateBinding Foreground}"
ContentTemplate="{TemplateBinding HeaderTemplate}"> ContentTemplate="{TemplateBinding HeaderTemplate}">
<ContentPresenter.Content> <ContentPresenter.Content>
<MultiBinding Converter="{StaticResource BadgeContentConverter}"> <MultiBinding Converter="{converters:BadgeContentOverflowConverter}">
<Binding Path="Header" RelativeSource="{RelativeSource TemplatedParent}" /> <Binding Path="Header" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="OverflowCount" RelativeSource="{RelativeSource TemplatedParent}" /> <Binding Path="OverflowCount" RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding> </MultiBinding>

View File

@@ -29,7 +29,6 @@
<Button Theme="{DynamicResource BorderlessButton}" Classes="Danger" Content="Button" /> <Button Theme="{DynamicResource BorderlessButton}" Classes="Danger" Content="Button" />
</StackPanel> </StackPanel>
</Design.PreviewWith> </Design.PreviewWith>
<converters:BooleansToOpacityConverter x:Key="OpacityConverter" />
<ControlTheme x:Key="{x:Type u:IconButton}" TargetType="u:IconButton"> <ControlTheme x:Key="{x:Type u:IconButton}" TargetType="u:IconButton">
<Setter Property="Padding" Value="{DynamicResource ButtonDefaultPadding}" /> <Setter Property="Padding" Value="{DynamicResource ButtonDefaultPadding}" />
<Setter Property="MinHeight" Value="{DynamicResource ButtonDefaultHeight}" /> <Setter Property="MinHeight" Value="{DynamicResource ButtonDefaultHeight}" />
@@ -75,7 +74,7 @@
ContentTemplate="{TemplateBinding IconTemplate}" ContentTemplate="{TemplateBinding IconTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Foreground="{Binding #PART_ContentPresenter.Foreground}" Foreground="{Binding #PART_ContentPresenter.Foreground}"
Opacity="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=!IsLoading, Converter={StaticResource OpacityConverter}}"> Opacity="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=!IsLoading, Converter={converters:BooleansToOpacityConverter}}">
<ContentPresenter.DataTemplates> <ContentPresenter.DataTemplates>
<DataTemplate DataType="Geometry"> <DataTemplate DataType="Geometry">
<PathIcon <PathIcon

View File

@@ -9,7 +9,6 @@
<u:LoadingIcon Classes="Large" /> <u:LoadingIcon Classes="Large" />
</StackPanel> </StackPanel>
</Design.PreviewWith> </Design.PreviewWith>
<converters:BrushToColorConverter x:Key="BrushToColorConverter" />
<ControlTheme x:Key="{x:Type u:LoadingIcon}" TargetType="u:LoadingIcon"> <ControlTheme x:Key="{x:Type u:LoadingIcon}" TargetType="u:LoadingIcon">
<Setter Property="IsLoading" Value="True" /> <Setter Property="IsLoading" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource LoadingIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource LoadingIconForeground}" />
@@ -29,8 +28,8 @@
<ConicGradientBrush Angle="50"> <ConicGradientBrush Angle="50">
<GradientStops> <GradientStops>
<GradientStop Offset="0.4" Color="Transparent" /> <GradientStop Offset="0.4" Color="Transparent" />
<GradientStop Offset="0.8" Color="{Binding Foreground, Converter={StaticResource BrushToColorConverter}, RelativeSource={RelativeSource TemplatedParent}}" /> <GradientStop Offset="0.8" Color="{Binding Foreground, Converter={converters:BrushToColorConverter}, RelativeSource={RelativeSource TemplatedParent}}" />
<GradientStop Offset="1.0" Color="{Binding Foreground, Converter={StaticResource BrushToColorConverter}, RelativeSource={RelativeSource TemplatedParent}}" /> <GradientStop Offset="1.0" Color="{Binding Foreground, Converter={converters:BrushToColorConverter}, RelativeSource={RelativeSource TemplatedParent}}" />
</GradientStops> </GradientStops>
</ConicGradientBrush> </ConicGradientBrush>
</Arc.Stroke> </Arc.Stroke>

View File

@@ -3,7 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 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.Themes.Semi.Converters"> xmlns:converters="clr-namespace:Ursa.Themes.Semi.Converters">
<converters:NavMenuMarginConverter x:Key="NavMarginConverter" />
<ControlTheme x:Key="{x:Type u:NavMenu}" TargetType="u:NavMenu"> <ControlTheme x:Key="{x:Type u:NavMenu}" TargetType="u:NavMenu">
<Setter Property="Grid.IsSharedSizeScope" Value="True" /> <Setter Property="Grid.IsSharedSizeScope" Value="True" />
@@ -67,7 +66,7 @@
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Center"> VerticalAlignment="Center">
<Grid.Margin> <Grid.Margin>
<MultiBinding Converter="{StaticResource NavMarginConverter}"> <MultiBinding Converter="{converters:NavMenuMarginConverter}">
<Binding Path="SubMenuIndent" RelativeSource="{RelativeSource TemplatedParent}" /> <Binding Path="SubMenuIndent" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="Level" RelativeSource="{RelativeSource TemplatedParent}" /> <Binding Path="Level" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="IsHorizontalCollapsed" RelativeSource="{RelativeSource TemplatedParent}" /> <Binding Path="IsHorizontalCollapsed" RelativeSource="{RelativeSource TemplatedParent}" />

View File

@@ -1,11 +1,11 @@
using System.Globalization; using System.Globalization;
using Avalonia.Data.Converters; using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Themes.Semi.Converters; namespace Ursa.Themes.Semi.Converters;
public class BooleansToOpacityConverter : IValueConverter public class BooleansToOpacityConverter : MarkupValueConverter
{ {
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) public override object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
if (value is bool b) if (value is bool b)
{ {
@@ -14,9 +14,4 @@ public class BooleansToOpacityConverter : IValueConverter
return 1; return 1;
} }
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} }

View File

@@ -1,12 +1,12 @@
using System.Globalization; using System.Globalization;
using Avalonia.Data.Converters;
using Avalonia.Media; using Avalonia.Media;
using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Themes.Semi.Converters; namespace Ursa.Themes.Semi.Converters;
public class BrushToColorConverter : IValueConverter public class BrushToColorConverter : MarkupValueConverter
{ {
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) public override object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
if (value is ISolidColorBrush b) if (value is ISolidColorBrush b)
{ {
@@ -15,9 +15,4 @@ public class BrushToColorConverter : IValueConverter
return Colors.Transparent; return Colors.Transparent;
} }
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} }

View File

@@ -1,15 +1,15 @@
using System.Globalization; using System.Globalization;
using Avalonia.Data.Converters; using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Themes.Semi.Converters; namespace Ursa.Themes.Semi.Converters;
public class ClockHandLengthConverter(double ratio) : IValueConverter public class ClockHandLengthConverter(double ratio) : MarkupValueConverter
{ {
public static ClockHandLengthConverter Hour { get; } = new(1 - 0.618); public static ClockHandLengthConverter Hour { get; } = new(1 - 0.618);
public static ClockHandLengthConverter Minute { get; } = new(0.618); public static ClockHandLengthConverter Minute { get; } = new(0.618);
public static ClockHandLengthConverter Second { get; } = new(1); public static ClockHandLengthConverter Second { get; } = new(1);
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) public override object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
if (value is double d) if (value is double d)
{ {
@@ -18,9 +18,4 @@ public class ClockHandLengthConverter(double ratio) : IValueConverter
return 0.0; return 0.0;
} }
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} }

View File

@@ -1,10 +1,10 @@
using System.Globalization; using System.Globalization;
using Avalonia.Data.Converters;
using Avalonia.Layout; using Avalonia.Layout;
using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Themes.Semi.Converters; namespace Ursa.Themes.Semi.Converters;
public class FormContentHeightToAlignmentConverter : IValueConverter public class FormContentHeightToAlignmentConverter : MarkupValueConverter
{ {
public static FormContentHeightToAlignmentConverter Instance = new(32); public static FormContentHeightToAlignmentConverter Instance = new(32);
public double Threshold { get; set; } public double Threshold { get; set; }
@@ -21,15 +21,9 @@ public class FormContentHeightToAlignmentConverter : IValueConverter
Threshold = threshold; Threshold = threshold;
} }
public override object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
if (value is not double d) return VerticalAlignment.Center; if (value is not double d) return VerticalAlignment.Center;
return d > Threshold ? VerticalAlignment.Top : VerticalAlignment.Center; return d > Threshold ? VerticalAlignment.Top : VerticalAlignment.Center;
} }
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} }

View File

@@ -1,10 +1,10 @@
using System.Globalization; using System.Globalization;
using Avalonia; using Avalonia;
using Avalonia.Data.Converters; using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Themes.Semi.Converters; namespace Ursa.Themes.Semi.Converters;
public class FormContentHeightToMarginConverter : IValueConverter public class FormContentHeightToMarginConverter : MarkupValueConverter
{ {
public static FormContentHeightToMarginConverter Instance = new(); public static FormContentHeightToMarginConverter Instance = new();
public double Threshold { get; set; } public double Threshold { get; set; }
@@ -21,14 +21,9 @@ public class FormContentHeightToMarginConverter : IValueConverter
Threshold = threshold; Threshold = threshold;
} }
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) public override object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
if (value is not double d) return new Thickness(0); if (value is not double d) return new Thickness(0);
return d > Threshold ? new Thickness(0, 8, 8, 0) : new Thickness(0, 0, 8, 0); return d > Threshold ? new Thickness(0, 8, 8, 0) : new Thickness(0, 0, 8, 0);
} }
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} }

View File

@@ -1,12 +1,12 @@
using System.Globalization; using System.Globalization;
using Avalonia; using Avalonia;
using Avalonia.Data.Converters; using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Themes.Semi.Converters; namespace Ursa.Themes.Semi.Converters;
public class NavMenuMarginConverter : IMultiValueConverter public class NavMenuMarginConverter : MarkupMultiValueConverter
{ {
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture) public override object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{ {
if (values[0] is double indent && values[1] is int level && values[2] is bool b) if (values[0] is double indent && values[1] is int level && values[2] is bool b)
{ {

View File

@@ -1,12 +1,12 @@
using System.Globalization; using System.Globalization;
using Avalonia; using Avalonia;
using Avalonia.Data.Converters; using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Themes.Semi.Converters; namespace Ursa.Themes.Semi.Converters;
public class TreeLevelToPaddingConverter : IMultiValueConverter public class TreeLevelToPaddingConverter : MarkupMultiValueConverter
{ {
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture) public override object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{ {
if (values[0] is int i && values[1] is Thickness indent) if (values[0] is int i && values[1] is Thickness indent)
{ {

View File

@@ -1,11 +1,11 @@
using System.Globalization; using System.Globalization;
using Avalonia.Data.Converters; using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Converters; namespace Ursa.Converters;
public class BadgeContentOverflowConverter : IMultiValueConverter public class BadgeContentOverflowConverter : MarkupMultiValueConverter
{ {
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture) public override object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{ {
var overflowMark = parameter as string ?? "+"; var overflowMark = parameter as string ?? "+";
if (double.TryParse(values[0]?.ToString(), out var b) && values[1] is int count and > 0) if (double.TryParse(values[0]?.ToString(), out var b) && values[1] is int count and > 0)

View File

@@ -1,14 +1,14 @@
using System.Globalization; using System.Globalization;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Data.Converters; using Irihi.Avalonia.Shared.Converters;
namespace Ursa.Converters; namespace Ursa.Converters;
public class SelectionBoxTemplateConverter : IMultiValueConverter public class SelectionBoxTemplateConverter : MarkupMultiValueConverter
{ {
public static SelectionBoxTemplateConverter Instance { get; } = new(); public static SelectionBoxTemplateConverter Instance { get; } = new();
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture) public override object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{ {
for (int i = 0; i < values.Count; i++) for (int i = 0; i < values.Count; i++)
{ {

View File

@@ -15,7 +15,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)"/> <PackageReference Include="Avalonia" Version="$(AvaloniaVersion)"/>
<PackageReference Include="Irihi.Avalonia.Shared" Version="0.3.0" /> <PackageReference Include="Irihi.Avalonia.Shared" Version="0.3.1" />
<PackageReference Include="Irihi.Avalonia.Shared.Contracts" Version="0.3.0" /> <PackageReference Include="Irihi.Avalonia.Shared.Contracts" Version="0.3.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>