Skip to content

Commit 477d988

Browse files
authored
Merge pull request #474 from taooceros/fixStoryboardPause
fix storyboard animation control error
2 parents 4b5e62d + bd66e56 commit 477d988

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.ComponentModel;
3+
using System.Threading.Tasks;
34
using System.Windows;
45
using System.Windows.Input;
56
using System.Windows.Media.Animation;
@@ -22,7 +23,6 @@ namespace Flow.Launcher
2223
{
2324
public partial class MainWindow
2425
{
25-
2626
#region Private Fields
2727

2828
private readonly Storyboard _progressBarStoryboard = new Storyboard();
@@ -40,6 +40,7 @@ public MainWindow(Settings settings, MainViewModel mainVM)
4040
_settings = settings;
4141
InitializeComponent();
4242
}
43+
4344
public MainWindow()
4445
{
4546
InitializeComponent();
@@ -53,7 +54,6 @@ private void OnClosing(object sender, CancelEventArgs e)
5354

5455
private void OnInitialized(object sender, EventArgs e)
5556
{
56-
5757
}
5858

5959
private void OnLoaded(object sender, RoutedEventArgs _)
@@ -72,47 +72,57 @@ private void OnLoaded(object sender, RoutedEventArgs _)
7272

7373
_viewModel.PropertyChanged += (o, e) =>
7474
{
75-
if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility))
75+
switch (e.PropertyName)
7676
{
77-
if (_viewModel.MainWindowVisibility == Visibility.Visible)
77+
case nameof(MainViewModel.MainWindowVisibility):
7878
{
79-
Activate();
80-
QueryTextBox.Focus();
81-
UpdatePosition();
82-
_settings.ActivateTimes++;
83-
if (!_viewModel.LastQuerySelected)
79+
if (_viewModel.MainWindowVisibility == Visibility.Visible)
8480
{
85-
QueryTextBox.SelectAll();
86-
_viewModel.LastQuerySelected = true;
81+
Activate();
82+
QueryTextBox.Focus();
83+
UpdatePosition();
84+
_settings.ActivateTimes++;
85+
if (!_viewModel.LastQuerySelected)
86+
{
87+
QueryTextBox.SelectAll();
88+
_viewModel.LastQuerySelected = true;
89+
}
90+
91+
if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused)
92+
{
93+
_progressBarStoryboard.Begin(ProgressBar, true);
94+
isProgressBarStoryboardPaused = false;
95+
}
8796
}
8897

89-
if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused)
98+
if (!isProgressBarStoryboardPaused)
9099
{
91-
_progressBarStoryboard.Resume();
92-
isProgressBarStoryboardPaused = false;
100+
_progressBarStoryboard.Stop(ProgressBar);
101+
isProgressBarStoryboardPaused = true;
93102
}
103+
104+
break;
94105
}
95-
else if (!isProgressBarStoryboardPaused)
106+
case nameof(MainViewModel.ProgressBarVisibility):
96107
{
97-
_progressBarStoryboard.Pause();
98-
isProgressBarStoryboardPaused = true;
99-
}
100-
}
101-
else if (e.PropertyName == nameof(MainViewModel.ProgressBarVisibility))
102-
{
103-
Dispatcher.Invoke(() =>
104-
{
105-
if (_viewModel.ProgressBarVisibility == Visibility.Hidden && !isProgressBarStoryboardPaused)
108+
Dispatcher.Invoke(async () =>
106109
{
107-
_progressBarStoryboard.Pause();
108-
isProgressBarStoryboardPaused = true;
109-
}
110-
else if (_viewModel.MainWindowVisibility == Visibility.Visible && isProgressBarStoryboardPaused)
111-
{
112-
_progressBarStoryboard.Resume();
113-
isProgressBarStoryboardPaused = false;
114-
}
115-
}, System.Windows.Threading.DispatcherPriority.Render);
110+
if (_viewModel.ProgressBarVisibility == Visibility.Hidden && !isProgressBarStoryboardPaused)
111+
{
112+
await Task.Delay(50);
113+
_progressBarStoryboard.Stop(ProgressBar);
114+
isProgressBarStoryboardPaused = true;
115+
}
116+
else if (_viewModel.MainWindowVisibility == Visibility.Visible &&
117+
isProgressBarStoryboardPaused)
118+
{
119+
_progressBarStoryboard.Begin(ProgressBar, true);
120+
isProgressBarStoryboardPaused = false;
121+
}
122+
}, System.Windows.Threading.DispatcherPriority.Render);
123+
124+
break;
125+
}
116126
}
117127
};
118128
_settings.PropertyChanged += (o, e) =>
@@ -189,14 +199,15 @@ private void InitializeNotifyIcon()
189199

190200
private void InitProgressbarAnimation()
191201
{
192-
var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
202+
var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100,
203+
new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
193204
var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
194205
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
195206
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
196207
_progressBarStoryboard.Children.Add(da);
197208
_progressBarStoryboard.Children.Add(da1);
198209
_progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
199-
ProgressBar.BeginStoryboard(_progressBarStoryboard);
210+
200211
_viewModel.ProgressBarVisibility = Visibility.Hidden;
201212
isProgressBarStoryboardPaused = true;
202213
}
@@ -210,10 +221,10 @@ private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e)
210221
{
211222
if (sender != null && e.OriginalSource != null)
212223
{
213-
var r = (ResultListBox)sender;
214-
var d = (DependencyObject)e.OriginalSource;
224+
var r = (ResultListBox) sender;
225+
var d = (DependencyObject) e.OriginalSource;
215226
var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem;
216-
var result = (ResultViewModel)item?.DataContext;
227+
var result = (ResultViewModel) item?.DataContext;
217228
if (result != null)
218229
{
219230
if (e.ChangedButton == MouseButton.Left)

0 commit comments

Comments
 (0)