feat: extract common button style.

This commit is contained in:
rabbitism
2024-01-23 19:55:23 +08:00
parent 7c29320ad9
commit 61ebba897b
9 changed files with 137 additions and 49 deletions

View File

@@ -7,18 +7,29 @@
<Setter Property="Template">
<ControlTemplate TargetType="u:DialogControl">
<Border
Classes="Shadow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Classes="Hover"
IsHitTestVisible="True"
Padding="2"
Theme="{DynamicResource CardBorder}">
<Grid RowDefinitions="Auto, *, Auto">
<StackPanel Grid.Row="0" HorizontalAlignment="Right">
<Button Name="{x:Static u:DialogControl.PART_CloseButton}" >Close</Button>
</StackPanel>
<DockPanel
Name="{x:Static u:DialogControl.PART_TitleArea}"
Grid.Row="0"
LastChildFill="False"
Background="Transparent">
<TextBlock DockPanel.Dock="Left" Text="Title" Margin="8 8 0 0" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
DockPanel.Dock="Right"
Margin="0,4,4,0"
Theme="{DynamicResource CloseButton}">
</Button>
</DockPanel>
<ContentPresenter
Grid.Row="0"
Grid.RowSpan="2"
Grid.RowSpan="3"
Content="{TemplateBinding Content}" />
</Grid>
</Border>
@@ -58,8 +69,16 @@
<Panel Margin="{TemplateBinding WindowDecorationMargin}" Background="Transparent" />
<ChromeOverlayLayer />
<Grid RowDefinitions="Auto, *, Auto">
<Button Name="{x:Static u:DialogWindow.PART_CloseButton}" VerticalAlignment="Top" HorizontalAlignment="Right">Close</Button>
<ContentPresenter Grid.Row="0" Grid.RowSpan="2" Content="{TemplateBinding Content}" />
<Button
Name="{x:Static u:DialogWindow.PART_CloseButton}"
HorizontalAlignment="Right"
VerticalAlignment="Top">
Close
</Button>
<ContentPresenter
Grid.Row="0"
Grid.RowSpan="2"
Content="{TemplateBinding Content}" />
</Grid>
</Panel>
</ControlTemplate>

View File

@@ -0,0 +1,42 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Add Resources Here -->
<ControlTheme x:Key="CloseButton" TargetType="Button">
<Setter Property="CornerRadius" Value="6" />
<Setter Property="Margin" Value="0, 4" />
<Setter Property="Padding" Value="4" />
<Setter Property="Height" Value="28" />
<Setter Property="Width" Value="28" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Template">
<ControlTemplate TargetType="Button">
<Border
Name="PART_Border"
Padding="{TemplateBinding Padding}"
Background="Transparent"
CornerRadius="{TemplateBinding CornerRadius}">
<PathIcon
Width="12"
Height="12"
Data="{DynamicResource WindowCloseIconGlyph}"/>
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:pointerover /template/ Border">
<Setter Property="Background" Value="{DynamicResource CaptionButtonClosePointeroverBackground}" />
</Style>
<Style Selector="^:pointerover /template/ PathIcon">
<Setter Property="Foreground" Value="White" />
</Style>
<Style Selector="^:pressed /template/ Border">
<Setter Property="Background" Value="{DynamicResource CaptionButtonClosePressedBackground}" />
</Style>
<Style Selector="^:pressed /template/ PathIcon">
<Setter Property="Foreground" Value="White" />
</Style>
</ControlTheme>
</ResourceDictionary>

View File

@@ -45,23 +45,7 @@
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Column="1"
Margin="0,4,4,0"
Background="{DynamicResource CaptionButtonClosePointeroverBackground}"
BorderBrush="{DynamicResource CaptionButtonClosePressedBackground}"
Theme="{DynamicResource CaptionButton}">
<Button.Styles>
<Style Selector="Button:pointerover">
<Setter Property="Foreground" Value="White" />
</Style>
<Style Selector="Button:pressed">
<Setter Property="Foreground" Value="White" />
</Style>
</Button.Styles>
<PathIcon
Width="12"
Height="12"
Data="{DynamicResource WindowCloseIconGlyph}"
Foreground="{Binding $parent[Button].Foreground}" />
</Button>
Theme="{DynamicResource CloseButton}"/>
</Grid>
<Grid
Grid.Row="1"

View File

@@ -5,6 +5,7 @@
<ResourceInclude Source="Banner.axaml" />
<ResourceInclude Source="ButtonGroup.axaml" />
<ResourceInclude Source="Dialog.axaml" />
<ResourceInclude Source="DialogShared.axaml" />
<ResourceInclude Source="Divider.axaml" />
<ResourceInclude Source="DualBadge.axaml" />
<ResourceInclude Source="IconButton.axaml" />

View File

@@ -0,0 +1,11 @@
namespace Ursa.Common;
public enum DialogButton
{
None,
OK,
OKCancel,
YesNo,
YesNoCancel,
}

View File

@@ -0,0 +1,15 @@
namespace Ursa.Common;
public enum DialogIcon
{
Asterisk, // Same as Information
Error,
Exclamation, // Same as Warning
Hand, // Same as Error
Information,
None,
Question,
Stop, // Same as Error
Warning,
Success,
}

View File

@@ -0,0 +1,10 @@
namespace Ursa.Common;
public enum DialogResult
{
Cancel,
No,
None,
OK,
Yes,
}

View File

@@ -2,16 +2,20 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace Ursa.Controls;
[TemplatePart(PART_CloseButton, typeof(Button))]
[TemplatePart(PART_TitleArea, typeof(Panel))]
public class DialogControl: ContentControl
{
public const string PART_CloseButton = "PART_CloseButton";
public const string PART_TitleArea = "PART_TitleArea";
private Button? _closeButton;
private Panel? _titleArea;
public event EventHandler<object?>? OnClose;
static DialogControl()
@@ -32,6 +36,7 @@ public class DialogControl: ContentControl
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
@@ -40,13 +45,30 @@ public class DialogControl: ContentControl
{
_closeButton.Click -= Close;
}
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
if (_closeButton is not null)
{
_closeButton.Click += Close;
}
_titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble);
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
}
private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e)
{
e.Source = this;
}
private void OnTitlePointerMove(object sender, PointerEventArgs e)
{
e.Source = this;
}
public Task<T> ShowAsync<T>()
{

View File

@@ -15,12 +15,6 @@ public class OverlayDialogHost: Canvas
{
private readonly List<DialogControl> _dialogs = new();
private readonly List<DialogControl> _modalDialogs = new();
private Rectangle _overlayMask = new()
{
Fill = new SolidColorBrush(new Color(1, 0, 0, 0)),
[Rectangle.ZIndexProperty] = 0,
};
public static readonly StyledProperty<string> HostIdProperty = AvaloniaProperty.Register<OverlayDialogHost, string>(
nameof(HostId));
@@ -66,37 +60,27 @@ public class OverlayDialogHost: Canvas
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
if (e.Source is Control item)
if (e.Source is DialogControl item)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
var parent = item.FindAncestorOfType<DialogControl>();
if (parent is null)
{
return;
}
var p = e.GetPosition(this);
var left= p.X - _lastPoint.X;
var top = p.Y - _lastPoint.Y;
left = MathUtilities.Clamp(left, 0, Bounds.Width - parent.Bounds.Width);
top = MathUtilities.Clamp(top, 0, Bounds.Height - parent.Bounds.Height);
Canvas.SetLeft(parent, left);
Canvas.SetTop(parent, top);
left = MathUtilities.Clamp(left, 0, Bounds.Width - item.Bounds.Width);
top = MathUtilities.Clamp(top, 0, Bounds.Height - item.Bounds.Height);
Canvas.SetLeft(item, left);
Canvas.SetTop(item, top);
}
}
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (e.Source is Control item)
// base.OnPointerPressed(e);
if (e.Source is DialogControl item)
{
var parent = item.FindAncestorOfType<DialogControl>();
if (parent is null)
{
return;
}
_lastPoint = e.GetPosition(parent);
_lastPoint = e.GetPosition(item);
}
}