Refactor HyperLinkTextBlock Theme

This commit is contained in:
CHINBOY
2025-06-19 08:56:44 +08:00
parent 55f7547b5c
commit 68bb6bf956
6 changed files with 46 additions and 84 deletions
@@ -36,6 +36,7 @@
<ResourceInclude Source="Slider/Themes/SliderThemes.axaml" />
<ResourceInclude Source="PopupConfirm/Themes/PopupConfirmContainerTheme.axaml" />
<ResourceInclude Source="Tag/Themes/TagTheme.axaml" />
<ResourceInclude Source="TextBlock/Themes/HyperLinkTextBlockTheme.axaml" />
<ResourceInclude Source="NumericUpDown/Themes/NumericUpDownTheme.axaml" />
<ResourceInclude Source="Tooltip/Themes/ToolTipTheme.axaml" />
<ResourceInclude Source="TreeView/Themes/TreeViewThemes.axaml" />
@@ -14,7 +14,6 @@ namespace AtomUI.Theme
themes.Add(new AtomUI.Controls.CardTabItemTheme());
themes.Add(new AtomUI.Controls.CardTabStripItemTheme());
themes.Add(new AtomUI.Controls.CardTabStripTheme());
themes.Add(new AtomUI.Controls.HyperLinkTextBlockTheme());
themes.Add(new AtomUI.Controls.TabControlTheme());
themes.Add(new AtomUI.Controls.TabItemTheme());
themes.Add(new AtomUI.Controls.TabStripItemTheme());
@@ -1,5 +1,4 @@
using System.Windows.Input;
using AtomUI.Theme;
using AtomUI.Theme.Utils;
using Avalonia;
using Avalonia.Controls;
@@ -283,7 +282,7 @@ public class HyperLinkTextBlock : TemplatedControl, ICustomHitTest, IMotionAware
var e = new RoutedEventArgs(ClickEvent);
RaiseEvent(e);
(var command, var parameter) = (Command, CommandParameter);
var (command, parameter) = (Command, CommandParameter);
if (!e.Handled && command is not null && command.CanExecute(parameter))
{
command.Execute(parameter);
@@ -1,81 +0,0 @@
using AtomUI.Animations;
using AtomUI.Controls.Utils;
using AtomUI.Theme;
using AtomUI.Theme.Styling;
using Avalonia.Animation;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Styling;
namespace AtomUI.Controls;
[ControlThemeProvider]
internal class HyperLinkTextBlockTheme : BaseControlTheme
{
public const string TextPart = "PART_Text";
public HyperLinkTextBlockTheme()
: base(typeof(HyperLinkTextBlock))
{
}
protected override IControlTemplate BuildControlTemplate()
{
return new FuncControlTemplate<HyperLinkTextBlock>((hyperLinkTextBlock, scope) =>
{
var textBlock = new TextBlock
{
Name = TextPart
};
CreateTemplateParentBinding(textBlock, TextBlock.FontWeightProperty, HyperLinkTextBlock.FontWeightProperty);
CreateTemplateParentBinding(textBlock, TextBlock.FontStyleProperty, HyperLinkTextBlock.FontStyleProperty);
CreateTemplateParentBinding(textBlock, TextBlock.FontSizeProperty, HyperLinkTextBlock.FontSizeProperty);
CreateTemplateParentBinding(textBlock, TextBlock.TextProperty, HyperLinkTextBlock.TextProperty);
return textBlock;
});
}
protected override void BuildStyles()
{
var commonStyle = new Style(selector => selector.Nesting());
{
var isMotionEnabledStyle = new Style(selector => selector.Nesting().PropertyEquals(HyperLinkTextBlock.IsMotionEnabledProperty, true));
isMotionEnabledStyle.Add(HyperLinkTextBlock.TransitionsProperty, new SetterValueFactory<Transitions>(() => new Transitions()
{
TransitionUtils.CreateTransition<SolidColorBrushTransition>(ContentPresenter.ForegroundProperty)
}));
commonStyle.Add(isMotionEnabledStyle);
}
commonStyle.Add(TemplatedControl.CursorProperty, new SetterValueFactory<Cursor>(() => new Cursor(StandardCursorType.Hand)));
commonStyle.Add(TemplatedControl.BackgroundProperty, SharedTokenKey.ColorBgContainer);
var enabledStyle = new Style(selector => selector.Nesting().PropertyEquals(HyperLinkTextBlock.IsEnabledProperty, true));
// 正常状态
enabledStyle.Add(TemplatedControl.ForegroundProperty, SharedTokenKey.ColorLink);
// 正常 hover
{
var hoverStyle = new Style(selector => selector.Nesting().Class(StdPseudoClass.PointerOver));
hoverStyle.Add(TemplatedControl.ForegroundProperty, SharedTokenKey.ColorLinkHover);
enabledStyle.Add(hoverStyle);
}
// 正常按下
{
var pressedStyle = new Style(selector =>
selector.Nesting().Class(StdPseudoClass.PointerOver).Class(StdPseudoClass.Pressed));
pressedStyle.Add(TemplatedControl.ForegroundProperty, SharedTokenKey.ColorLinkActive);
enabledStyle.Add(pressedStyle);
}
commonStyle.Add(enabledStyle);
Add(commonStyle);
var disabledStyle = new Style(selector => selector.Nesting().Class(StdPseudoClass.Disabled));
disabledStyle.Add(TemplatedControl.ForegroundProperty, SharedTokenKey.ColorTextDisabled);
Add(disabledStyle);
}
}
@@ -0,0 +1,38 @@
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:atom="https://atomui.net">
<ControlTheme x:Key="{x:Type atom:HyperLinkTextBlock}" TargetType="atom:HyperLinkTextBlock">
<Setter Property="Template">
<ControlTemplate>
<TextBlock Name="{x:Static atom:HyperLinkTextBlockThemeConstants.TextPart}"
FontWeight="{TemplateBinding FontWeight}"
FontStyle="{TemplateBinding FontStyle}"
FontSize="{TemplateBinding FontSize}"
Text="{TemplateBinding Text}">
</TextBlock>
</ControlTemplate>
</Setter>
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Background" Value="{DynamicResource {x:Static atom:SharedTokenKey.ColorBgContainer}}" />
<Style Selector="^[IsEnabled=True]">
<Setter Property="Foreground" Value="{DynamicResource {x:Static atom:SharedTokenKey.ColorLink}}" />
<Style Selector="^:pointerover">
<Setter Property="Foreground" Value="{DynamicResource {x:Static atom:SharedTokenKey.ColorLinkHover}}" />
</Style>
<Style Selector="^:pressed">
<Setter Property="Foreground" Value="{DynamicResource {x:Static atom:SharedTokenKey.ColorLinkActive}}" />
</Style>
</Style>
<Style Selector="^:disabled">
<Setter Property="Foreground" Value="{DynamicResource {x:Static atom:SharedTokenKey.ColorTextDisabled}}" />
</Style>
</ControlTheme>
</ResourceDictionary>
@@ -0,0 +1,6 @@
namespace AtomUI.Controls.Themes;
internal class HyperLinkTextBlockThemeConstants
{
public const string TextPart = "PART_Text";
}