feat: add placement.

This commit is contained in:
rabbitism
2024-01-06 20:44:01 +08:00
parent e5e7f020e3
commit 1312ab6128
4 changed files with 70 additions and 7 deletions

View File

@@ -22,6 +22,14 @@
Data="{StaticResource iconGlyph}" />
</u:IconButton.Icon>
</u:IconButton>
<u:IconButton Content="GitHub" IsLoading="{Binding #loading.IsChecked}" IconPlacement="Right">
<u:IconButton.Icon>
<PathIcon
Width="14"
Height="14"
Data="{StaticResource iconGlyph}" />
</u:IconButton.Icon>
</u:IconButton>
<u:IconButton
Width="150"
Content="GitHub"

View File

@@ -33,12 +33,13 @@
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
ColumnDefinitions="Auto, Auto">
<Panel Grid.Column="0" Margin="0,0,4,0">
<Panel
Name="PART_IconRoot"
Grid.Column="0"
Margin="0,0,8,0">
<Panel.IsVisible>
<MultiBinding Converter="{x:Static BoolConverters.Or}">
<Binding
Path="IsLoading"
RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="IsLoading" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding
Converter="{x:Static ObjectConverters.IsNotNull}"
Path="Icon"
@@ -54,7 +55,10 @@
Foreground="{TemplateBinding Foreground}"
IsVisible="{TemplateBinding IsLoading}" />
</Panel>
<ContentPresenter Grid.Column="1" Content="{TemplateBinding Content}" />
<ContentPresenter
Name="PART_ContentPresenter"
Grid.Column="1"
Content="{TemplateBinding Content}" />
</Grid>
</Border>
</ControlTemplate>
@@ -99,6 +103,16 @@
<Setter Property="Foreground" Value="{DynamicResource ButtonDefaultDisabledForeground}" />
</Style>
<Style Selector="^:right">
<Style Selector="^ /template/ Panel#PART_IconRoot">
<Setter Property="Grid.Column" Value="1" />
<Setter Property="Margin" Value="8 0 0 0" />
</Style>
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Grid.Column" Value="0" />
</Style>
</Style>
<Style Selector="^.Large">
<Setter Property="Padding" Value="{DynamicResource ButtonLargePadding}" />
</Style>
@@ -106,8 +120,8 @@
<Setter Property="Padding" Value="{DynamicResource ButtonSmallPadding}" />
</Style>
</ControlTheme>
<ControlTheme
<ControlTheme
x:Key="SolidIconButton"
BasedOn="{StaticResource {x:Type u:IconButton}}"
TargetType="u:IconButton">

View File

@@ -0,0 +1,7 @@
namespace Ursa.Common;
public enum IconPlacement
{
Left,
Right,
}

View File

@@ -1,11 +1,17 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Ursa.Common;
namespace Ursa.Controls;
[PseudoClasses(PC_Right)]
public class IconButton: Button
{
public const string PC_Right = ":right";
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<IconButton, object?>(
nameof(Icon));
@@ -32,4 +38,32 @@ public class IconButton: Button
get => GetValue(IsLoadingProperty);
set => SetValue(IsLoadingProperty, value);
}
public static readonly StyledProperty<IconPlacement> IconPlacementProperty = AvaloniaProperty.Register<IconButton, IconPlacement>(
nameof(IconPlacement), defaultValue: IconPlacement.Left);
public IconPlacement IconPlacement
{
get => GetValue(IconPlacementProperty);
set => SetValue(IconPlacementProperty, value);
}
static IconButton()
{
IconPlacementProperty.Changed.AddClassHandler<IconButton, IconPlacement>((o, e) =>
{
o.SetPlacement(e.NewValue.Value);
});
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
SetPlacement(IconPlacement);
}
private void SetPlacement(IconPlacement placement)
{
PseudoClasses.Set(PC_Right, placement == IconPlacement.Right);
}
}