Refactor Tab

This commit is contained in:
qinware
2025-08-21 14:38:14 +08:00
parent 7e2beab793
commit 45a3c8d99d
7 changed files with 26 additions and 73 deletions
@@ -89,17 +89,12 @@ public class BaseTabControl : AvaloniaTabControl,
string IControlSharedTokenResourcesHost.TokenId => TabControlToken.ID;
#endregion
private Border? _frame;
private Panel? _alignWrapper;
private Point _tabStripBorderStartPoint;
private Point _tabStripBorderEndPoint;
private CompositeDisposable? _resourceBindingsDisposable;
CompositeDisposable? IResourceBindingManager.ResourceBindingsDisposable
{
get => _resourceBindingsDisposable;
set => _resourceBindingsDisposable = value;
}
CompositeDisposable? IResourceBindingManager.ResourceBindingsDisposable { get; set; }
static BaseTabControl()
{
@@ -117,14 +112,7 @@ public class BaseTabControl : AvaloniaTabControl,
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_frame = e.NameScope.Find<Border>(TabControlThemeConstants.FramePart);
_alignWrapper = e.NameScope.Find<Panel>(TabControlThemeConstants.AlignWrapperPart);
if (_frame is not null)
{
this.AddResourceBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, BorderThicknessProperty,
SharedTokenKey.BorderThickness, BindingPriority.Template,
new RenderScaleAwareThicknessConfigure(this)));
}
}
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
@@ -139,8 +127,9 @@ public class BaseTabControl : AvaloniaTabControl,
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
this.AddResourceBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, TabAndContentGutterProperty,
TabControlTokenKey.TabAndContentGutter));
this.AddResourceBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, BorderThicknessProperty,
SharedTokenKey.BorderThickness, BindingPriority.Template,
new RenderScaleAwareThicknessConfigure(this)));
}
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
@@ -1,9 +1,5 @@
using System.Globalization;
using System.Reactive.Disposables;
using AtomUI.Controls.Themes;
using AtomUI.Theme;
using AtomUI.Theme.Data;
using AtomUI.Theme.Styling;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Converters;
@@ -22,8 +18,7 @@ namespace AtomUI.Controls;
[TemplatePart(TabScrollViewerThemeConstants.ScrollEndEdgeIndicatorPart, typeof(Control))]
[TemplatePart(TabScrollViewerThemeConstants.ScrollMenuIndicatorPart, typeof(IconButton))]
[TemplatePart(TabScrollViewerThemeConstants.ScrollViewContentPart, typeof(ScrollContentPresenter))]
internal abstract class BaseTabScrollViewer : ScrollViewer,
IResourceBindingManager
internal abstract class BaseTabScrollViewer : ScrollViewer
{
private const int EdgeIndicatorZIndex = 1000;
@@ -44,8 +39,8 @@ internal abstract class BaseTabScrollViewer : ScrollViewer,
o => o.MenuEdgeThickness,
(o, v) => o.MenuEdgeThickness = v);
internal static readonly StyledProperty<bool> IsMotionEnabledProperty
= MotionAwareControlProperty.IsMotionEnabledProperty.AddOwner<BaseTabScrollViewer>();
internal static readonly StyledProperty<bool> IsMotionEnabledProperty =
MotionAwareControlProperty.IsMotionEnabledProperty.AddOwner<BaseTabScrollViewer>();
private Dock _tabStripPlacement;
@@ -77,19 +72,12 @@ internal abstract class BaseTabScrollViewer : ScrollViewer,
set => SetValue(IsMotionEnabledProperty, value);
}
CompositeDisposable? IResourceBindingManager.ResourceBindingsDisposable
{
get => _resourceBindingsDisposable;
set => _resourceBindingsDisposable = value;
}
#endregion
private protected IconButton? _menuIndicator;
private protected Border? _startEdgeIndicator;
private protected Border? _endEdgeIndicator;
private protected MenuFlyout? _menuFlyout;
private CompositeDisposable? _resourceBindingsDisposable;
static BaseTabScrollViewer()
{
@@ -213,22 +201,6 @@ internal abstract class BaseTabScrollViewer : ScrollViewer,
SetupIndicatorsVisibility();
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_resourceBindingsDisposable = new CompositeDisposable();
this.AddResourceBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, EdgeShadowStartColorProperty,
SharedTokenKey.ColorFillSecondary));
this.AddResourceBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, MenuEdgeThicknessProperty,
TabControlTokenKey.MenuEdgeThickness));
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
this.DisposeTokenBindings();
}
protected override Size MeasureOverride(Size availableSize)
{
var size = base.MeasureOverride(availableSize);
@@ -1,5 +1,4 @@
using System.Reactive.Disposables;
using AtomUI.Controls.Themes;
using AtomUI.Data;
using AtomUI.Theme;
using AtomUI.Theme.Data;
@@ -7,7 +6,6 @@ using AtomUI.Theme.Styling;
using AtomUI.Theme.Utils;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.LogicalTree;
@@ -70,17 +68,10 @@ public abstract class BaseTabStrip : AvaloniaTabStrip,
Control IMotionAwareControl.PropertyBindTarget => this;
Control IControlSharedTokenResourcesHost.HostControl => this;
string IControlSharedTokenResourcesHost.TokenId => TabControlToken.ID;
CompositeDisposable? IResourceBindingManager.ResourceBindingsDisposable
{
get => _resourceBindingsDisposable;
set => _resourceBindingsDisposable = value;
}
CompositeDisposable? IResourceBindingManager.ResourceBindingsDisposable { get; set; }
#endregion
private Border? _frame;
private CompositeDisposable? _resourceBindingsDisposable;
static BaseTabStrip()
{
ItemsPanelProperty.OverrideDefaultValue<BaseTabStrip>(DefaultPanel);
@@ -94,19 +85,6 @@ public abstract class BaseTabStrip : AvaloniaTabStrip,
this.RegisterResources();
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_frame = e.NameScope.Find<Border>(TabStripThemeConstants.FramePart);
UpdatePseudoClasses();
if (_frame is not null)
{
this.AddResourceBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, BorderThicknessProperty,
SharedTokenKey.BorderThickness, BindingPriority.Template,
new RenderScaleAwareThicknessConfigure(this)));
}
}
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
{
base.PrepareContainerForItemOverride(container, item, index);
@@ -118,6 +96,14 @@ public abstract class BaseTabStrip : AvaloniaTabStrip,
}
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
this.AddResourceBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, BorderThicknessProperty,
SharedTokenKey.BorderThickness, BindingPriority.Template,
new RenderScaleAwareThicknessConfigure(this)));
}
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnDetachedFromLogicalTree(e);
@@ -8,6 +8,7 @@
<Style Selector="^:is(atom|BaseTabControl)">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static atom:SharedTokenKey.ColorBorderSecondary}}" />
<Setter Property="IsMotionEnabled" Value="{DynamicResource {x:Static atom:SharedTokenKey.EnableMotion}}" />
<Setter Property="TabAndContentGutter" Value="{DynamicResource {x:Static atom:TabControlTokenKey.TabAndContentGutter}}" />
<!-- 设置 items presenter 面板样式 -->
<!-- 分为上、右、下、左 -->
@@ -38,6 +38,8 @@
</Setter>
<Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="EdgeShadowStartColor" Value="{DynamicResource {x:Static atom:SharedTokenKey.ColorFillSecondary}}"/>
<Setter Property="MenuEdgeThickness" Value="{DynamicResource {x:Static atom:TabControlTokenKey.MenuEdgeThickness}}"/>
<Style Selector="^ /template/ atom|IconButton#PART_ScrollMenuIndicator">
<Setter Property="IconWidth" Value="{DynamicResource {x:Static atom:SharedTokenKey.IconSize}}"/>
@@ -10,7 +10,8 @@
</ControlTheme.BasedOn>
<Setter Property="Template">
<ControlTemplate>
<Border Name="{x:Static atom:TabControlThemeConstants.FramePart}">
<Border Name="{x:Static atom:TabControlThemeConstants.FramePart}"
BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel Name="{x:Static atom:TabControlThemeConstants.MainLayoutContainerPart}">
<Panel Name="{x:Static atom:TabControlThemeConstants.AlignWrapperPart}"
DockPanel.Dock="{TemplateBinding TabStripPlacement}"
@@ -6,7 +6,8 @@
x:ClassModifier="internal">
<Setter Property="Template">
<ControlTemplate TargetType="atom:TabStripItem">
<Border Name="{x:Static atom:TabStripItemThemeConstants.DecoratorPart}">
<Border Name="{x:Static atom:TabStripItemThemeConstants.DecoratorPart}"
Background="{TemplateBinding Background}">
<StackPanel Name="{x:Static atom:TabStripItemThemeConstants.ContentLayoutPart}"
Orientation="Horizontal">
<atom:IconPresenter Name="{x:Static atom:TabStripItemThemeConstants.ItemIconPresenterPart}"
@@ -26,6 +27,7 @@
<Style Selector="^:is(atom|TabStripItem)">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static atom:TabControlTokenKey.ItemColor}}" />
<Setter Property="Background" Value="Transparent" />
<Style Selector="^ /template/ atom|IconButton#PART_ItemCloseButton">
<Setter Property="Margin" Value="{DynamicResource {x:Static atom:TabControlTokenKey.CloseIconMargin}}" />