Skip to content

Commit 3e28698

Browse files
committed
chore: add avalonia TabControlSliding demo.
1 parent 045ced1 commit 3e28698

File tree

6 files changed

+423
-12
lines changed

6 files changed

+423
-12
lines changed

src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeTabControlDemo.axaml

+87
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,93 @@
99
ItemHeight="200"
1010
MaxWidth="832"
1111
ChildWrapping="Wrap">
12+
<TabControl SelectedIndex="3"
13+
Theme="{StaticResource TabControlSliding}">
14+
<TabItem Header="Title1">
15+
<Border>
16+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
17+
<Run Text="Text" />
18+
<Run Text="1" />
19+
</TextBlock>
20+
</Border>
21+
</TabItem>
22+
<TabItem Header="Title22">
23+
<Border>
24+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
25+
<Run Text="Text" />
26+
<Run Text="2" />
27+
</TextBlock>
28+
</Border>
29+
</TabItem>
30+
<TabItem Header="Title333">
31+
<Border>
32+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
33+
<Run Text="Text" />
34+
<Run Text="3" />
35+
</TextBlock>
36+
</Border>
37+
</TabItem>
38+
<TabItem Header="Title4444">
39+
<Border>
40+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
41+
<Run Text="Text" />
42+
<Run Text="4" />
43+
</TextBlock>
44+
</Border>
45+
</TabItem>
46+
<TabItem Header="Title55555">
47+
<Border>
48+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
49+
<Run Text="Text" />
50+
<Run Text="5" />
51+
</TextBlock>
52+
</Border>
53+
</TabItem>
54+
</TabControl>
55+
<TabControl Theme="{StaticResource TabControlSliding}"
56+
TabStripPlacement="Bottom">
57+
<TabItem Header="Title1">
58+
<Border>
59+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
60+
<Run Text="Text" />
61+
<Run Text="1" />
62+
</TextBlock>
63+
</Border>
64+
</TabItem>
65+
<TabItem Header="Title22"
66+
IsEnabled="False">
67+
<Border>
68+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
69+
<Run Text="Text" />
70+
<Run Text="2" />
71+
</TextBlock>
72+
</Border>
73+
</TabItem>
74+
<TabItem Header="Title333">
75+
<Border>
76+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
77+
<Run Text="Text" />
78+
<Run Text="3" />
79+
</TextBlock>
80+
</Border>
81+
</TabItem>
82+
<TabItem Header="Title4444">
83+
<Border>
84+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
85+
<Run Text="Text" />
86+
<Run Text="4" />
87+
</TextBlock>
88+
</Border>
89+
</TabItem>
90+
<TabItem Header="Title55555">
91+
<Border>
92+
<TextBlock Theme="{StaticResource TextBlockLargeBold}">
93+
<Run Text="Text" />
94+
<Run Text="5" />
95+
</TextBlock>
96+
</Border>
97+
</TabItem>
98+
</TabControl>
1299
<TabControl TabStripPlacement="Right">
13100
<TabItem Header="Title1">
14101
<Border>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using Avalonia;
3+
using Avalonia.Animation;
4+
using Avalonia.Animation.Easings;
5+
using Avalonia.Controls;
6+
using Avalonia.Controls.Metadata;
7+
using Avalonia.Controls.Primitives;
8+
using Avalonia.Interactivity;
9+
using Avalonia.Layout;
10+
using Avalonia.Media;
11+
using Avalonia.Styling;
12+
using HandyControl.Tools;
13+
14+
namespace HandyControl.Controls;
15+
16+
[TemplatePart(Name = ElementSliding, Type = typeof(Layoutable))]
17+
public class SlidingTabContainer : ContentControl
18+
{
19+
private const string ElementSliding = "PART_Sliding";
20+
21+
private Control? _sliding;
22+
private TabControl? _tabControl;
23+
24+
protected override void OnLoaded(RoutedEventArgs e)
25+
{
26+
base.OnLoaded(e);
27+
OnSelectionChanged(null, null!);
28+
}
29+
30+
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
31+
{
32+
if (_tabControl is not null)
33+
{
34+
_tabControl.SelectionChanged -= OnSelectionChanged;
35+
}
36+
37+
base.OnApplyTemplate(e);
38+
39+
_tabControl = VisualHelper.GetParent<TabControl>(this);
40+
if (_tabControl is null)
41+
{
42+
return;
43+
}
44+
45+
_tabControl.SelectionChanged += OnSelectionChanged;
46+
47+
_sliding = e.NameScope.Find<Control>(ElementSliding);
48+
if (_sliding is null)
49+
{
50+
return;
51+
}
52+
53+
if (IsLoaded)
54+
{
55+
OnSelectionChanged(null, null!);
56+
}
57+
}
58+
59+
protected override void OnSizeChanged(SizeChangedEventArgs e)
60+
{
61+
base.OnSizeChanged(e);
62+
63+
if (_tabControl is null)
64+
{
65+
return;
66+
}
67+
68+
OnSelectionChanged(null, null!);
69+
}
70+
71+
private void OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
72+
{
73+
const int animationSpeed = 200;
74+
75+
if (_sliding is null || _tabControl?.SelectedItem is null || !IsLoaded)
76+
{
77+
return;
78+
}
79+
80+
if (_tabControl.ContainerFromItem(_tabControl.SelectedItem) is not TabItem tabItem)
81+
{
82+
return;
83+
}
84+
85+
var padding = Padding;
86+
Point? offset = tabItem.TranslatePoint(new Point(-padding.Left, -padding.Top), this);
87+
if (offset is null)
88+
{
89+
return;
90+
}
91+
92+
new Animation
93+
{
94+
FillMode = FillMode.Forward,
95+
Duration = TimeSpan.FromMilliseconds(animationSpeed),
96+
Easing = new QuadraticEaseOut(),
97+
Children =
98+
{
99+
new KeyFrame
100+
{
101+
Setters =
102+
{
103+
new Setter(WidthProperty, tabItem.Bounds.Width),
104+
new Setter(HeightProperty, tabItem.Bounds.Height),
105+
new Setter(TranslateTransform.XProperty, offset.Value.X),
106+
new Setter(TranslateTransform.YProperty, offset.Value.Y),
107+
},
108+
Cue = new Cue(1),
109+
}
110+
}
111+
}.RunAsync(_sliding);
112+
}
113+
}

0 commit comments

Comments
 (0)