feat: introduce class helper here.

This commit is contained in:
rabbitism
2024-01-25 13:19:48 +08:00
parent 430dccc958
commit 87bb47b4e1
5 changed files with 136 additions and 22 deletions

View File

@@ -3,8 +3,10 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
Padding="24"
Background="Aqua"
x:Class="Ursa.Demo.Dialogs.PlainDialog">
<StackPanel Margin="8">
<StackPanel>
<Calendar SelectedDate="{Binding Date}" ></Calendar>
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,24 @@
using Avalonia;
namespace Ursa.Themes.Semi;
internal class ClassHelper: AvaloniaObject
{
static ClassHelper()
{
ClassesProperty.Changed.AddClassHandler<StyledElement>(OnClassesChanged);
}
public static readonly AttachedProperty<string> ClassesProperty =
AvaloniaProperty.RegisterAttached<ClassHelper, StyledElement, string>("Classes");
public static void SetClasses(AvaloniaObject obj, string value) => obj.SetValue(ClassesProperty, value);
public static string GetClasses(AvaloniaObject obj) => obj.GetValue(ClassesProperty);
private static void OnClassesChanged(StyledElement sender, AvaloniaPropertyChangedEventArgs value)
{
IEnumerable<string> classes = value.GetNewValue<IEnumerable<string>>();
sender.Classes.Clear();
sender.Classes.AddRange(classes);
}
}

View File

@@ -1,12 +1,14 @@
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:theme="clr-namespace:Ursa.Themes.Semi"
xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here -->
<ControlTheme x:Key="{x:Type u:OverlayDialogHost}" TargetType="u:OverlayDialogHost">
<Setter Property="OverlayMaskBrush" Value="{DynamicResource OverlayDialogMaskBrush}" />
</ControlTheme>
<ControlTheme x:Key="{x:Type u:DialogControl}" TargetType="u:DialogControl">
<Setter Property="CornerRadius" Value="12"></Setter>
<Setter Property="Template">
<ControlTemplate TargetType="u:DialogControl">
<Border
@@ -14,7 +16,8 @@
HorizontalAlignment="Center"
VerticalAlignment="Center"
Classes="Shadow"
CornerRadius="12"
CornerRadius="{TemplateBinding CornerRadius}"
ClipToBounds="True"
IsHitTestVisible="True"
Theme="{DynamicResource CardBorder}">
<Grid RowDefinitions="Auto, *">
@@ -92,6 +95,7 @@
</Style>
</ControlTheme>
<ControlTheme x:Key="{x:Type u:DefaultDialogControl}" TargetType="u:DefaultDialogControl">
<Setter Property="CornerRadius" Value="12"></Setter>
<Setter Property="Template">
<ControlTemplate TargetType="u:DefaultDialogControl">
<Border
@@ -99,7 +103,8 @@
HorizontalAlignment="Center"
VerticalAlignment="Center"
Classes="Shadow"
CornerRadius="12"
CornerRadius="{TemplateBinding CornerRadius}"
ClipToBounds="True"
IsHitTestVisible="True"
Theme="{DynamicResource CardBorder}">
<Grid Margin="24" RowDefinitions="Auto, *, Auto">
@@ -107,23 +112,29 @@
Name="PART_ContentPresenter"
Grid.Row="1"
Content="{TemplateBinding Content}" />
<Grid Grid.Row="0" ColumnDefinitions="*, Auto">
<Grid Grid.Row="0" ColumnDefinitions="Auto, *, Auto">
<Panel
Name="{x:Static u:DialogControl.PART_TitleArea}"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.ColumnSpan="3"
Background="Transparent" />
<TextBlock
<PathIcon
Name="PART_Icon"
Grid.Column="0"
Margin="0 0 8 0"
Width="16"
Height="16" />
<TextBlock
Grid.Column="1"
Margin="8,8,0,0"
Classes="Strong"
DockPanel.Dock="Left"
TextWrapping="Wrap"
IsVisible="{TemplateBinding Title,
Converter={x:Static ObjectConverters.IsNotNull}}"
Text="{TemplateBinding Title}" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Column="1"
Grid.Column="2"
Margin="0,4,4,0"
DockPanel.Dock="Right"
Theme="{DynamicResource CloseButton}" />
@@ -137,27 +148,47 @@
Name="{x:Static u:DefaultDialogControl.PART_CancelButton}"
Margin="8,0,0,0"
Classes="Tertiary"
Content="Cancel" />
Content="取消" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_NoButton}"
Margin="8,0,0,0"
Classes="Danger"
Content="No" />
Content="" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_YesButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="Yes" />
Theme="{DynamicResource SolidButton}"
Content="是" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_OKButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="OK" />
Theme="{DynamicResource SolidButton}"
Content="确认" />
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^[Icon=None]">
<Style Selector="^ /template/ PathIcon#PART_Icon">
<Setter Property="IsVisible" Value="False"></Setter>
</Style>
<Style Selector="^ /template/ Button#PART_OKButton">
<Setter Property="theme:ClassHelper.Classes" Value="Primary"></Setter>
</Style>
</Style>
<Style Selector="^[Icon=Info]">
<Style Selector="^ /template/ PathIcon#PART_Icon">
<Setter Property="IsVisible" Value="True"></Setter>
<Setter Property="Foreground" Value="{DynamicResource SemiBlue6}"></Setter>
</Style>
<Style Selector="^ /template/ Button#PART_OKButton">
<Setter Property="theme:ClassHelper.Classes" Value="Primary"></Setter>
</Style>
</Style>
<Style Selector="^:not(:modal) /template/ Panel#PART_TitleArea">
<Setter Property="ContextFlyout">
<MenuFlyout>
@@ -211,6 +242,63 @@
</ControlTheme>
<ControlTheme x:Key="{x:Type u:DialogWindow}" TargetType="u:DialogWindow">
<Setter Property="Title" Value="{x:Null}" />
<Setter Property="Background" Value="{DynamicResource WindowDefaultBackground}" />
<Setter Property="TransparencyBackgroundFallback" Value="{DynamicResource WindowDefaultBackground}" />
<Setter Property="Foreground" Value="{DynamicResource WindowDefaultForeground}" />
<Setter Property="FontSize" Value="{DynamicResource DefaultFontSize}" />
<Setter Property="FontFamily" Value="{DynamicResource DefaultFontFamily}" />
<Setter Property="Padding" Value="48 24" />
<Setter Property="SizeToContent" Value="WidthAndHeight" />
<Setter Property="WindowStartupLocation" Value="CenterOwner" />
<Setter Property="ExtendClientAreaTitleBarHeightHint" Value="1" />
<Setter Property="ExtendClientAreaToDecorationsHint" Value="True" />
<Setter Property="ExtendClientAreaChromeHints" Value="SystemChrome" />
<Setter Property="SystemDecorations">
<OnPlatform>
<OnPlatform.Windows>
<SystemDecorations>Full</SystemDecorations>
</OnPlatform.Windows>
<OnPlatform.Default>
<SystemDecorations>BorderOnly</SystemDecorations>
</OnPlatform.Default>
</OnPlatform>
</Setter>
<Setter Property="CanResize" Value="False" />
<Setter Property="Template">
<ControlTemplate TargetType="u:DialogWindow">
<Panel>
<Border Name="PART_TransparencyFallback" IsHitTestVisible="False" />
<Border Background="{TemplateBinding Background}" IsHitTestVisible="False" />
<Panel Margin="{TemplateBinding WindowDecorationMargin}" Background="Transparent" />
<ChromeOverlayLayer />
<Grid RowDefinitions="Auto, *">
<ContentPresenter
Grid.Row="0"
Grid.RowSpan="2"
Content="{TemplateBinding Content}" />
<Grid Grid.Row="0" ColumnDefinitions="*, Auto">
<TextBlock
Grid.Column="0"
Margin="8,8,0,0"
FontSize="14"
FontWeight="Bold"
Text="{TemplateBinding Title}"
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Column="1"
Margin="0,24,24,0"
Theme="{DynamicResource CloseButton}" />
</Grid>
</Grid>
</Panel>
</ControlTemplate>
</Setter>
</ControlTheme>
<ControlTheme x:Key="{x:Type u:DefaultDialogWindow}" TargetType="u:DefaultDialogWindow">
<Setter Property="Title" Value="{x:Null}" />
<Setter Property="Background" Value="{DynamicResource WindowDefaultBackground}" />
<Setter Property="TransparencyBackgroundFallback" Value="{DynamicResource WindowDefaultBackground}" />
@@ -267,25 +355,25 @@
HorizontalAlignment="Right"
Orientation="Horizontal">
<Button
Name="{x:Static u:DefaultDialogControl.PART_CancelButton}"
Name="{x:Static u:DefaultDialogWindow.PART_CancelButton}"
Margin="8,0,0,0"
Classes="Tertiary"
Content="Cancel" />
Content="取消" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_NoButton}"
Name="{x:Static u:DefaultDialogWindow.PART_NoButton}"
Margin="8,0,0,0"
Classes="Danger"
Content="No" />
Content="" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_YesButton}"
Name="{x:Static u:DefaultDialogWindow.PART_YesButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="Yes" />
Content="" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_OKButton}"
Name="{x:Static u:DefaultDialogWindow.PART_OKButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="OK" />
Content="确认" />
</StackPanel>
</Grid>
</Panel>

View File

@@ -13,7 +13,7 @@
<ResourceInclude Source="IPv4Box.axaml" />
<ResourceInclude Source="KeyGestureInput.axaml" />
<ResourceInclude Source="Loading.axaml" />
<ResourceInclude Source="MessageBoxWindow.axaml" />
<ResourceInclude Source="MessageBox.axaml" />
<ResourceInclude Source="Navigation.axaml" />
<ResourceInclude Source="NumericUpDown.axaml" />
<ResourceInclude Source="Pagination.axaml" />