Skip to content

Commit 4684a38

Browse files
committed
1. Change propertyname match to switch,
2. Use begin/stop instead of resume/pause to reset position 3. Add a small delay when stopping the animation after hiding to make the animation more fluently
1 parent 797ff90 commit 4684a38

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 49 additions & 42 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,48 +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);
94+
isProgressBarStoryboardPaused = false;
95+
}
8796
}
8897

89-
if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused)
98+
if (!isProgressBarStoryboardPaused)
9099
{
91-
_progressBarStoryboard.Resume(ProgressBar);
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(ProgressBar);
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(ProgressBar);
108-
isProgressBarStoryboardPaused = true;
109-
}
110-
else if (_viewModel.MainWindowVisibility == Visibility.Visible && isProgressBarStoryboardPaused)
111-
{
112-
113-
_progressBarStoryboard.Resume(ProgressBar);
114-
isProgressBarStoryboardPaused = false;
115-
}
116-
}, 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);
120+
isProgressBarStoryboardPaused = false;
121+
}
122+
}, System.Windows.Threading.DispatcherPriority.Render);
123+
124+
break;
125+
}
117126
}
118127
};
119128
_settings.PropertyChanged += (o, e) =>
@@ -190,17 +199,15 @@ private void InitializeNotifyIcon()
190199

191200
private void InitProgressbarAnimation()
192201
{
193-
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)));
194204
var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
195205
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
196206
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
197207
_progressBarStoryboard.Children.Add(da);
198208
_progressBarStoryboard.Children.Add(da1);
199209
_progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
200-
201-
_progressBarStoryboard.Begin(ProgressBar, true);
202-
_progressBarStoryboard.Pause();
203-
210+
204211
_viewModel.ProgressBarVisibility = Visibility.Hidden;
205212
isProgressBarStoryboardPaused = true;
206213
}
@@ -214,10 +221,10 @@ private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e)
214221
{
215222
if (sender != null && e.OriginalSource != null)
216223
{
217-
var r = (ResultListBox)sender;
218-
var d = (DependencyObject)e.OriginalSource;
224+
var r = (ResultListBox) sender;
225+
var d = (DependencyObject) e.OriginalSource;
219226
var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem;
220-
var result = (ResultViewModel)item?.DataContext;
227+
var result = (ResultViewModel) item?.DataContext;
221228
if (result != null)
222229
{
223230
if (e.ChangedButton == MouseButton.Left)

0 commit comments

Comments
 (0)