Refactor TabItem

This commit is contained in:
qinware
2025-08-21 14:11:28 +08:00
parent 23f586c81c
commit a7ca926852
2 changed files with 61 additions and 86 deletions
+31 -44
View File
@@ -1,30 +1,24 @@
using System.Diagnostics;
using System.Reactive.Disposables;
using AtomUI.Animations;
using AtomUI.Controls.Themes;
using AtomUI.Controls.Utils;
using AtomUI.IconPkg;
using AtomUI.IconPkg.AntDesign;
using AtomUI.Reflection;
using AtomUI.Theme;
using AtomUI.Theme.Data;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Rendering;
using Avalonia.Styling;
using Avalonia.VisualTree;
namespace AtomUI.Controls;
using AvaloniaTabItem = Avalonia.Controls.TabItem;
public class TabItem : AvaloniaTabItem,
ICustomHitTest,
IResourceBindingManager
public class TabItem : AvaloniaTabItem
{
#region 公共属性定义
@@ -86,16 +80,9 @@ public class TabItem : AvaloniaTabItem,
set => SetValue(IsMotionEnabledProperty, value);
}
CompositeDisposable? IResourceBindingManager.ResourceBindingsDisposable
{
get => _resourceBindingsDisposable;
set => _resourceBindingsDisposable = value;
}
#endregion
private IconButton? _closeButton;
private CompositeDisposable? _resourceBindingsDisposable;
private Border? _decorator;
private void SetupDefaultCloseIcon()
@@ -183,7 +170,7 @@ public class TabItem : AvaloniaTabItem,
{
if (change.Property == ShapeProperty)
{
SetupShapeThemeBindings();
SetupShapeThemeBindings(true);
}
}
@@ -214,35 +201,35 @@ public class TabItem : AvaloniaTabItem,
}
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
private void SetupShapeThemeBindings(bool force = false)
{
if (force || Theme == null)
{
string? resourceKey = null;
if (Shape == TabSharp.Line)
{
resourceKey = TabItemThemeConstants.TabItemThemeId;
}
else
{
resourceKey = TabItemThemeConstants.CardTabItemThemeId;
}
if (Application.Current != null)
{
if (Application.Current.TryFindResource(resourceKey, out var resource))
{
if (resource is ControlTheme theme)
{
Theme = theme;
}
}
}
}
}
public override void EndInit()
{
_resourceBindingsDisposable = new CompositeDisposable();
SetupShapeThemeBindings();
base.OnAttachedToLogicalTree(e);
}
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnDetachedFromLogicalTree(e);
this.DisposeTokenBindings();
}
private void SetupShapeThemeBindings()
{
if (Shape == TabSharp.Line)
{
this.AddResourceBindingDisposable(
TokenResourceBinder.CreateTokenBinding(this, ThemeProperty, TabItemThemeConstants.TabItemThemeId));
}
else
{
this.AddResourceBindingDisposable(
TokenResourceBinder.CreateTokenBinding(this, ThemeProperty, TabItemThemeConstants.CardTabItemThemeId));
}
}
public bool HitTest(Point point)
{
return true;
base.EndInit();
}
}
@@ -1,20 +1,16 @@
using System.Diagnostics;
using System.Reactive.Disposables;
using AtomUI.Animations;
using AtomUI.Controls.Themes;
using AtomUI.Controls.Utils;
using AtomUI.IconPkg;
using AtomUI.IconPkg.AntDesign;
using AtomUI.Theme;
using AtomUI.Theme.Data;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Rendering;
using Avalonia.Styling;
using Avalonia.VisualTree;
namespace AtomUI.Controls;
@@ -27,9 +23,7 @@ public enum TabSharp
Card
}
public class TabStripItem : AvaloniaTabStripItem,
ICustomHitTest,
IResourceBindingManager
public class TabStripItem : AvaloniaTabStripItem
{
#region 公共属性定义
@@ -102,16 +96,9 @@ public class TabStripItem : AvaloniaTabStripItem,
set => SetValue(IsMotionEnabledProperty, value);
}
CompositeDisposable? IResourceBindingManager.ResourceBindingsDisposable
{
get => _resourceBindingsDisposable;
set => _resourceBindingsDisposable = value;
}
#endregion
private IconButton? _closeButton;
private CompositeDisposable? _resourceBindingsDisposable;
private Border? _decorator;
private void SetupDefaultCloseIcon()
@@ -200,7 +187,7 @@ public class TabStripItem : AvaloniaTabStripItem,
{
if (change.Property == ShapeProperty)
{
SetupShapeThemeBindings();
SetupShapeThemeBindings(true);
}
}
@@ -222,35 +209,36 @@ public class TabStripItem : AvaloniaTabStripItem,
}
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
private void SetupShapeThemeBindings(bool force = false)
{
_resourceBindingsDisposable = new CompositeDisposable();
SetupShapeThemeBindings();
base.OnAttachedToLogicalTree(e);
if (force || Theme == null)
{
string? resourceKey = null;
if (Shape == TabSharp.Line)
{
resourceKey = TabStripThemeConstants.TabStripItemThemeId;
}
else
{
resourceKey = TabStripThemeConstants.CardTabStripItemThemeId;
}
if (Application.Current != null)
{
if (Application.Current.TryFindResource(resourceKey, out var resource))
{
if (resource is ControlTheme theme)
{
Theme = theme;
}
}
}
}
}
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
public override void EndInit()
{
base.OnDetachedFromLogicalTree(e);
this.DisposeTokenBindings();
}
private void SetupShapeThemeBindings()
{
if (Shape == TabSharp.Line)
{
this.AddResourceBindingDisposable(
TokenResourceBinder.CreateTokenBinding(this, ThemeProperty, TabStripThemeConstants.TabStripItemThemeId));
}
else
{
this.AddResourceBindingDisposable(
TokenResourceBinder.CreateTokenBinding(this, ThemeProperty, TabStripThemeConstants.CardTabStripItemThemeId));
}
}
public bool HitTest(Point point)
{
return true;
SetupShapeThemeBindings();
base.EndInit();
}
}