Skip to content

Commit 7882e4a

Browse files
committed
Refactor SplitButtonToggleDropDownBehavior
1 parent 4e89ea5 commit 7882e4a

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
using System.Windows.Input;
5+
using System.Windows.Media;
6+
using System.Windows.Media.Animation;
7+
8+
using HandyControl.Controls;
9+
10+
using IPConfig.Languages;
11+
12+
namespace IPConfig.Behaviors;
13+
14+
public class CopyContentsSplitButtonBehavior : SplitButtonToggleDropDownBehavior
15+
{
16+
private bool _canPlayTextAnimation;
17+
18+
protected override void OnAssociatedObjectClick(object sender, RoutedEventArgs e)
19+
{
20+
if (_canPlayTextAnimation)
21+
{
22+
AnimateText();
23+
24+
_canPlayTextAnimation = false;
25+
}
26+
}
27+
28+
protected override void OnAssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e, HitTestResult hitTestResult)
29+
{
30+
if (hitTestResult?.VisualHit is Border { Child: null } or TextBlock)
31+
{
32+
_canPlayTextAnimation = true;
33+
}
34+
}
35+
36+
private void AnimateText()
37+
{
38+
NameScope.SetNameScope(AssociatedObject, new NameScope());
39+
string name = $"SplitButton_{Guid.NewGuid().ToString().Replace('-', '_')}";
40+
AssociatedObject.RegisterName(name, AssociatedObject);
41+
42+
var keyFrames = new StringAnimationUsingKeyFrames {
43+
Duration = new(TimeSpan.FromSeconds(1))
44+
};
45+
46+
var frame = new DiscreteStringKeyFrame(Lang.Copied, KeyTime.FromTimeSpan(TimeSpan.Zero));
47+
keyFrames.KeyFrames.Add(frame);
48+
49+
var story = new Storyboard { FillBehavior = FillBehavior.Stop };
50+
Storyboard.SetTargetName(keyFrames, name);
51+
Storyboard.SetTargetProperty(keyFrames, new PropertyPath(SplitButton.ContentProperty));
52+
story.Children.Add(keyFrames);
53+
story.Begin(AssociatedObject);
54+
}
55+
}

IPConfig/Behaviors/SplitButtonToggleDropDownBehavior.cs

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
using System;
2-
using System.Windows;
1+
using System.Windows;
32
using System.Windows.Controls;
3+
using System.Windows.Input;
44
using System.Windows.Media;
5-
using System.Windows.Media.Animation;
65
using System.Windows.Shapes;
76

87
using HandyControl.Controls;
98

10-
using IPConfig.Languages;
11-
129
using Microsoft.Xaml.Behaviors;
1310

1411
namespace IPConfig.Behaviors;
@@ -18,7 +15,11 @@ namespace IPConfig.Behaviors;
1815
/// </summary>
1916
public class SplitButtonToggleDropDownBehavior : Behavior<SplitButton>
2017
{
21-
private bool _canPlayTextAnimation;
18+
protected virtual void OnAssociatedObjectClick(object sender, RoutedEventArgs e)
19+
{ }
20+
21+
protected virtual void OnAssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e, HitTestResult hitTestResult)
22+
{ }
2223

2324
protected override void OnAttached()
2425
{
@@ -36,51 +37,22 @@ protected override void OnDetaching()
3637
base.OnDetaching();
3738
}
3839

39-
private void AnimateText()
40-
{
41-
NameScope.SetNameScope(AssociatedObject, new NameScope());
42-
string name = $"SplitButton_{Guid.NewGuid().ToString().Replace('-', '_')}";
43-
AssociatedObject.RegisterName(name, AssociatedObject);
44-
45-
var keyFrames = new StringAnimationUsingKeyFrames {
46-
Duration = new(TimeSpan.FromSeconds(1))
47-
};
48-
49-
var frame = new DiscreteStringKeyFrame(Lang.Copied, KeyTime.FromTimeSpan(TimeSpan.Zero));
50-
keyFrames.KeyFrames.Add(frame);
51-
52-
var story = new Storyboard { FillBehavior = FillBehavior.Stop };
53-
Storyboard.SetTargetName(keyFrames, name);
54-
Storyboard.SetTargetProperty(keyFrames, new PropertyPath(SplitButton.ContentProperty));
55-
story.Children.Add(keyFrames);
56-
story.Begin(AssociatedObject);
57-
}
58-
5940
private void AssociatedObject_Click(object sender, RoutedEventArgs e)
6041
{
61-
if (_canPlayTextAnimation)
62-
{
63-
AnimateText();
64-
65-
_canPlayTextAnimation = false;
66-
}
42+
OnAssociatedObjectClick(sender, e);
6743
}
6844

69-
private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
45+
private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
7046
{
71-
var result = VisualTreeHelper.HitTest(AssociatedObject, e.GetPosition(AssociatedObject));
72-
73-
if (result?.VisualHit is Border { Child: null } or TextBlock)
74-
{
75-
_canPlayTextAnimation = true;
76-
}
47+
var hitTestResult = VisualTreeHelper.HitTest(AssociatedObject, e.GetPosition(AssociatedObject));
48+
OnAssociatedObjectPreviewMouseLeftButtonDown(sender, e, hitTestResult);
7749

7850
// 如果 VisualHit 为 Path,则鼠标在点击 SplitButton 的 DropDown 位置,
7951
// 如果为 TextBlock,则鼠标在点击 SplitButton 的非 DropDown 位置。
8052
// 如果为 Border,并且其 Child 不为 null,则鼠标在点击 DropDown 位置,否则,鼠标在点击 非 DropDown 位置。
8153
// 如果为 null,则鼠标在点击其他位置。
8254
// 如果不判断 VisualHit,则在点击下拉项前,此事件将被调用,下拉项被关闭,导致下拉项无法被点击到。
83-
if (AssociatedObject.IsDropDownOpen && result?.VisualHit is Border or Path or TextBlock)
55+
if (AssociatedObject.IsDropDownOpen && hitTestResult?.VisualHit is Border or Path or TextBlock)
8456
{
8557
AssociatedObject.IsDropDownOpen = false;
8658
e.Handled = true;

IPConfig/Views/IPConfigDetailView.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@
154154
Command="{Binding PrimarySaveOrApplyCommand}"
155155
Content="{Binding PrimarySaveOrApplyString, Mode=OneWay}"
156156
Style="{StaticResource SplitButtonPrimary}">
157+
<i:Interaction.Behaviors>
158+
<b:SplitButtonToggleDropDownBehavior />
159+
</i:Interaction.Behaviors>
160+
157161
<hc:SplitButton.DropDownContent>
158162
<Menu Style="{StaticResource SplitButtonDropDownMenuStyle}">
159163
<MenuItem Command="{Binding ValidateCommand}"

IPConfig/Views/NicConfigDetailView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@
527527
Content="{lang:Lang {x:Static lang:LangKey.CopyContents_}}"
528528
Style="{StaticResource SplitButtonPrimary}">
529529
<i:Interaction.Behaviors>
530-
<b:SplitButtonToggleDropDownBehavior />
530+
<b:CopyContentsSplitButtonBehavior />
531531
</i:Interaction.Behaviors>
532532

533533
<hc:SplitButton.DropDownContent>

0 commit comments

Comments
 (0)