Skip to content

Commit 02c8dfd

Browse files
authored
Merge pull request #4499 from Flow-Launcher/fix-InitProgressbarAnimation-avoid-style-creation
Refactor InitProgressbarAnimation & avoid style cast issue
2 parents 071994e + 238162a commit 02c8dfd

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

Flow.Launcher/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@
337337
</Border>
338338
<Line
339339
x:Name="ProgressBar"
340+
Style="{DynamicResource PendingLineStyle}"
340341
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth}"
341342
Height="2"
342343
Margin="12 0 12 0"

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public partial class MainWindow : IDisposable
7575
// Window Animation
7676
private const double DefaultRightMargin = 66; //* this value from base.xaml
7777
private bool _isClockPanelAnimating = false;
78+
private Storyboard _progressBarStoryboard;
7879

7980
// IDisposable
8081
private bool _disposed = false;
@@ -1120,49 +1121,58 @@ private double VerticalTop(MonitorInfo screen)
11201121

11211122
private void InitProgressbarAnimation()
11221123
{
1123-
var progressBarStoryBoard = new Storyboard();
1124+
_progressBarStoryboard = new Storyboard();
11241125

1125-
var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100,
1126-
new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
1127-
var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth + 0,
1128-
new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
1129-
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
1130-
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
1131-
progressBarStoryBoard.Children.Add(da);
1132-
progressBarStoryBoard.Children.Add(da1);
1133-
progressBarStoryBoard.RepeatBehavior = RepeatBehavior.Forever;
1126+
var animationDuration = new Duration(TimeSpan.FromMilliseconds(1600));
1127+
var progressBarLength = ProgressBar.X2 - ProgressBar.X1;
11341128

1135-
da.Freeze();
1136-
da1.Freeze();
1137-
1138-
const string progressBarAnimationName = "ProgressBarAnimation";
1139-
var beginStoryboard = new BeginStoryboard
1129+
var lineEndAnimation = new DoubleAnimation
11401130
{
1141-
Name = progressBarAnimationName, Storyboard = progressBarStoryBoard
1131+
From = ProgressBar.X2,
1132+
To = ProgressBar.ActualWidth + progressBarLength,
1133+
Duration = animationDuration
11421134
};
1143-
1144-
var stopStoryboard = new StopStoryboard()
1135+
var lineStartAnimation = new DoubleAnimation
11451136
{
1146-
BeginStoryboardName = progressBarAnimationName
1137+
From = ProgressBar.X1,
1138+
To = ProgressBar.ActualWidth,
1139+
Duration = animationDuration
11471140
};
1141+
1142+
Storyboard.SetTarget(lineEndAnimation, ProgressBar);
1143+
Storyboard.SetTargetProperty(lineEndAnimation, new PropertyPath("(Line.X2)"));
1144+
1145+
Storyboard.SetTarget(lineStartAnimation, ProgressBar);
1146+
Storyboard.SetTargetProperty(lineStartAnimation, new PropertyPath("(Line.X1)"));
1147+
1148+
_progressBarStoryboard.Children.Add(lineEndAnimation);
1149+
_progressBarStoryboard.Children.Add(lineStartAnimation);
1150+
_progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
1151+
1152+
lineEndAnimation.Freeze();
1153+
lineStartAnimation.Freeze();
1154+
1155+
ProgressBar.IsVisibleChanged -= ProgressBar_IsVisibleChanged;
1156+
ProgressBar.IsVisibleChanged += ProgressBar_IsVisibleChanged;
11481157

1149-
var trigger = new Trigger
1150-
{
1151-
Property = VisibilityProperty, Value = Visibility.Visible
1152-
};
1153-
trigger.EnterActions.Add(beginStoryboard);
1154-
trigger.ExitActions.Add(stopStoryboard);
1158+
_viewModel.ProgressBarVisibility = Visibility.Hidden;
1159+
}
11551160

1156-
var progressStyle = new Style(typeof(Line))
1161+
private void ProgressBar_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
1162+
{
1163+
if (_progressBarStoryboard == null)
11571164
{
1158-
BasedOn = FindResource("PendingLineStyle") as Style
1159-
};
1160-
progressStyle.RegisterName(progressBarAnimationName, beginStoryboard);
1161-
progressStyle.Triggers.Add(trigger);
1162-
1163-
ProgressBar.Style = progressStyle;
1165+
return;
1166+
}
11641167

1165-
_viewModel.ProgressBarVisibility = Visibility.Hidden;
1168+
if (ProgressBar.IsVisible)
1169+
{
1170+
_progressBarStoryboard.Begin(ProgressBar, true);
1171+
}
1172+
else
1173+
{
1174+
_progressBarStoryboard.Stop(ProgressBar);
1175+
}
11661176
}
11671177

11681178
private void WindowAnimation()

0 commit comments

Comments
 (0)