fix: fix padding.

This commit is contained in:
rabbitism
2024-01-27 20:26:40 +08:00
parent b46eb8c456
commit bea0618fd2
3 changed files with 44 additions and 11 deletions

View File

@@ -24,7 +24,10 @@
IconPlacement="{Binding #placement.Value}" IconPlacement="{Binding #placement.Value}"
IsLoading="{Binding #loading.IsChecked}"> IsLoading="{Binding #loading.IsChecked}">
<u:IconButton.Icon> <u:IconButton.Icon>
<TextBlock Text="🐼" HorizontalAlignment="Center" FontSize="40" /> <TextBlock
HorizontalAlignment="Center"
FontSize="40"
Text="🐼" />
</u:IconButton.Icon> </u:IconButton.Icon>
</u:IconButton> </u:IconButton>
<u:IconButton <u:IconButton
@@ -90,5 +93,16 @@
Data="{StaticResource iconGlyph}" /> Data="{StaticResource iconGlyph}" />
</u:IconButton.Icon> </u:IconButton.Icon>
</u:IconButton> </u:IconButton>
<u:IconButton
Classes="Danger"
IsLoading="{Binding #loading.IsChecked}"
Theme="{DynamicResource SolidIconButton}">
<u:IconButton.Icon>
<TextBlock
HorizontalAlignment="Center"
FontSize="40"
Text="🐼" />
</u:IconButton.Icon>
</u:IconButton>
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -38,8 +38,7 @@
RowDefinitions="Auto, Auto"> RowDefinitions="Auto, Auto">
<Panel <Panel
Name="PART_IconRoot" Name="PART_IconRoot"
Grid.Column="0" Grid.Column="0">
Margin="0,0,8,0">
<Panel.IsVisible> <Panel.IsVisible>
<MultiBinding Converter="{x:Static BoolConverters.Or}"> <MultiBinding Converter="{x:Static BoolConverters.Or}">
<Binding Path="IsLoading" RelativeSource="{RelativeSource TemplatedParent}" /> <Binding Path="IsLoading" RelativeSource="{RelativeSource TemplatedParent}" />
@@ -61,6 +60,7 @@
<ContentPresenter <ContentPresenter
Name="PART_ContentPresenter" Name="PART_ContentPresenter"
Grid.Column="1" Grid.Column="1"
Margin="8 0 0 0"
HorizontalAlignment="Center" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalAlignment="Center"
Content="{TemplateBinding Content}" /> Content="{TemplateBinding Content}" />
@@ -107,16 +107,20 @@
<Setter Property="Background" Value="{DynamicResource ButtonDefaultDisabledBackground}" /> <Setter Property="Background" Value="{DynamicResource ButtonDefaultDisabledBackground}" />
<Setter Property="Foreground" Value="{DynamicResource ButtonDefaultDisabledForeground}" /> <Setter Property="Foreground" Value="{DynamicResource ButtonDefaultDisabledForeground}" />
</Style> </Style>
<Style Selector="^:empty[IsLoading=False] /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Margin" Value="0"></Setter>
</Style>
<Style Selector="^:right"> <Style Selector="^:right">
<Style Selector="^ /template/ Panel#PART_IconRoot"> <Style Selector="^ /template/ Panel#PART_IconRoot">
<Setter Property="Grid.Column" Value="1" /> <Setter Property="Grid.Column" Value="1" />
<Setter Property="Grid.Row" Value="0" /> <Setter Property="Grid.Row" Value="0" />
<Setter Property="Margin" Value="8 0 0 0" />
</Style> </Style>
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter"> <Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Grid.Column" Value="0" /> <Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.Row" Value="0" /> <Setter Property="Grid.Row" Value="0" />
<Setter Property="Margin" Value="0 0 8 0" />
</Style> </Style>
</Style> </Style>
@@ -124,11 +128,11 @@
<Style Selector="^ /template/ Panel#PART_IconRoot"> <Style Selector="^ /template/ Panel#PART_IconRoot">
<Setter Property="Grid.Column" Value="0" /> <Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.Row" Value="0" /> <Setter Property="Grid.Row" Value="0" />
<Setter Property="Margin" Value="0 0 8 0" />
</Style> </Style>
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter"> <Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Grid.Column" Value="1" /> <Setter Property="Grid.Column" Value="1" />
<Setter Property="Grid.Row" Value="0" /> <Setter Property="Grid.Row" Value="0" />
<Setter Property="Margin" Value="8 0 0 0" />
</Style> </Style>
</Style> </Style>
@@ -136,11 +140,11 @@
<Style Selector="^ /template/ Panel#PART_IconRoot"> <Style Selector="^ /template/ Panel#PART_IconRoot">
<Setter Property="Grid.Column" Value="0" /> <Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.Row" Value="0" /> <Setter Property="Grid.Row" Value="0" />
<Setter Property="Margin" Value="0 0 0 4" />
</Style> </Style>
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter"> <Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Grid.Column" Value="0" /> <Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.Row" Value="1" /> <Setter Property="Grid.Row" Value="1" />
<Setter Property="Margin" Value="0 4 0 0" />
</Style> </Style>
</Style> </Style>
@@ -148,11 +152,11 @@
<Style Selector="^ /template/ Panel#PART_IconRoot"> <Style Selector="^ /template/ Panel#PART_IconRoot">
<Setter Property="Grid.Column" Value="0" /> <Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.Row" Value="1" /> <Setter Property="Grid.Row" Value="1" />
<Setter Property="Margin" Value="0 4 0 0" />
</Style> </Style>
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter"> <Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Grid.Column" Value="0" /> <Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.Row" Value="0" /> <Setter Property="Grid.Row" Value="0" />
<Setter Property="Margin" Value="0 0 0 4" />
</Style> </Style>
</Style> </Style>

View File

@@ -8,13 +8,14 @@ using Ursa.Common;
namespace Ursa.Controls; namespace Ursa.Controls;
[PseudoClasses(PC_Right, PC_Left, PC_Top, PC_Bottom)] [PseudoClasses(PC_Right, PC_Left, PC_Top, PC_Bottom, PC_Empty)]
public class IconButton: Button public class IconButton: Button
{ {
public const string PC_Right = ":right"; public const string PC_Right = ":right";
public const string PC_Left = ":left"; public const string PC_Left = ":left";
public const string PC_Top = ":top"; public const string PC_Top = ":top";
public const string PC_Bottom = ":bottom"; public const string PC_Bottom = ":bottom";
public const string PC_Empty = ":empty";
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<IconButton, object?>( public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<IconButton, object?>(
nameof(Icon)); nameof(Icon));
@@ -56,18 +57,32 @@ public class IconButton: Button
{ {
IconPlacementProperty.Changed.AddClassHandler<IconButton, IconPlacement>((o, e) => IconPlacementProperty.Changed.AddClassHandler<IconButton, IconPlacement>((o, e) =>
{ {
o.SetPlacement(e.NewValue.Value); o.SetPlacement(e.NewValue.Value, o.Icon);
});
IconProperty.Changed.AddClassHandler<IconButton, object?>((o, e) =>
{
o.SetPlacement(o.IconPlacement, e.NewValue.Value);
}); });
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
SetPlacement(IconPlacement); SetPlacement(IconPlacement, Icon);
} }
private void SetPlacement(IconPlacement placement) private void SetPlacement(IconPlacement placement, object? icon)
{ {
if (icon is null)
{
PseudoClasses.Set(PC_Empty, true);
PseudoClasses.Set(PC_Left, false);
PseudoClasses.Set(PC_Right, false);
PseudoClasses.Set(PC_Top, false);
PseudoClasses.Set(PC_Bottom, false);
return;
}
PseudoClasses.Set(PC_Empty, false);
PseudoClasses.Set(PC_Left, placement == IconPlacement.Left); PseudoClasses.Set(PC_Left, placement == IconPlacement.Left);
PseudoClasses.Set(PC_Right, placement == IconPlacement.Right); PseudoClasses.Set(PC_Right, placement == IconPlacement.Right);
PseudoClasses.Set(PC_Top, placement == IconPlacement.Top); PseudoClasses.Set(PC_Top, placement == IconPlacement.Top);