From 797ff90195b9422d3eca9d3195384a017c26d504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BC=98=E9=9F=AC?= Date: Tue, 15 Jun 2021 16:41:27 +0800 Subject: [PATCH 1/3] fix storyboard animation control --- Flow.Launcher/MainWindow.xaml.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 04a1063f857..49e14fa4666 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -88,13 +88,13 @@ private void OnLoaded(object sender, RoutedEventArgs _) if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused) { - _progressBarStoryboard.Resume(); + _progressBarStoryboard.Resume(ProgressBar); isProgressBarStoryboardPaused = false; } } else if (!isProgressBarStoryboardPaused) { - _progressBarStoryboard.Pause(); + _progressBarStoryboard.Pause(ProgressBar); isProgressBarStoryboardPaused = true; } } @@ -104,12 +104,13 @@ private void OnLoaded(object sender, RoutedEventArgs _) { if (_viewModel.ProgressBarVisibility == Visibility.Hidden && !isProgressBarStoryboardPaused) { - _progressBarStoryboard.Pause(); + _progressBarStoryboard.Pause(ProgressBar); isProgressBarStoryboardPaused = true; } else if (_viewModel.MainWindowVisibility == Visibility.Visible && isProgressBarStoryboardPaused) { - _progressBarStoryboard.Resume(); + + _progressBarStoryboard.Resume(ProgressBar); isProgressBarStoryboardPaused = false; } }, System.Windows.Threading.DispatcherPriority.Render); @@ -196,7 +197,10 @@ private void InitProgressbarAnimation() _progressBarStoryboard.Children.Add(da); _progressBarStoryboard.Children.Add(da1); _progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever; - ProgressBar.BeginStoryboard(_progressBarStoryboard); + + _progressBarStoryboard.Begin(ProgressBar, true); + _progressBarStoryboard.Pause(); + _viewModel.ProgressBarVisibility = Visibility.Hidden; isProgressBarStoryboardPaused = true; } From 4684a38883b36df2509ab75f56ec2bc78e02e617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BC=98=E9=9F=AC?= Date: Tue, 15 Jun 2021 17:27:21 +0800 Subject: [PATCH 2/3] 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 --- Flow.Launcher/MainWindow.xaml.cs | 91 +++++++++++++++++--------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 49e14fa4666..51e1574f96a 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media.Animation; @@ -22,7 +23,6 @@ namespace Flow.Launcher { public partial class MainWindow { - #region Private Fields private readonly Storyboard _progressBarStoryboard = new Storyboard(); @@ -40,6 +40,7 @@ public MainWindow(Settings settings, MainViewModel mainVM) _settings = settings; InitializeComponent(); } + public MainWindow() { InitializeComponent(); @@ -53,7 +54,6 @@ private void OnClosing(object sender, CancelEventArgs e) private void OnInitialized(object sender, EventArgs e) { - } private void OnLoaded(object sender, RoutedEventArgs _) @@ -72,48 +72,57 @@ private void OnLoaded(object sender, RoutedEventArgs _) _viewModel.PropertyChanged += (o, e) => { - if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility)) + switch (e.PropertyName) { - if (_viewModel.MainWindowVisibility == Visibility.Visible) + case nameof(MainViewModel.MainWindowVisibility): { - Activate(); - QueryTextBox.Focus(); - UpdatePosition(); - _settings.ActivateTimes++; - if (!_viewModel.LastQuerySelected) + if (_viewModel.MainWindowVisibility == Visibility.Visible) { - QueryTextBox.SelectAll(); - _viewModel.LastQuerySelected = true; + Activate(); + QueryTextBox.Focus(); + UpdatePosition(); + _settings.ActivateTimes++; + if (!_viewModel.LastQuerySelected) + { + QueryTextBox.SelectAll(); + _viewModel.LastQuerySelected = true; + } + + if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused) + { + _progressBarStoryboard.Begin(ProgressBar); + isProgressBarStoryboardPaused = false; + } } - if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused) + if (!isProgressBarStoryboardPaused) { - _progressBarStoryboard.Resume(ProgressBar); - isProgressBarStoryboardPaused = false; + _progressBarStoryboard.Stop(ProgressBar); + isProgressBarStoryboardPaused = true; } + + break; } - else if (!isProgressBarStoryboardPaused) + case nameof(MainViewModel.ProgressBarVisibility): { - _progressBarStoryboard.Pause(ProgressBar); - isProgressBarStoryboardPaused = true; - } - } - else if (e.PropertyName == nameof(MainViewModel.ProgressBarVisibility)) - { - Dispatcher.Invoke(() => - { - if (_viewModel.ProgressBarVisibility == Visibility.Hidden && !isProgressBarStoryboardPaused) + Dispatcher.Invoke(async () => { - _progressBarStoryboard.Pause(ProgressBar); - isProgressBarStoryboardPaused = true; - } - else if (_viewModel.MainWindowVisibility == Visibility.Visible && isProgressBarStoryboardPaused) - { - - _progressBarStoryboard.Resume(ProgressBar); - isProgressBarStoryboardPaused = false; - } - }, System.Windows.Threading.DispatcherPriority.Render); + if (_viewModel.ProgressBarVisibility == Visibility.Hidden && !isProgressBarStoryboardPaused) + { + await Task.Delay(50); + _progressBarStoryboard.Stop(ProgressBar); + isProgressBarStoryboardPaused = true; + } + else if (_viewModel.MainWindowVisibility == Visibility.Visible && + isProgressBarStoryboardPaused) + { + _progressBarStoryboard.Begin(ProgressBar); + isProgressBarStoryboardPaused = false; + } + }, System.Windows.Threading.DispatcherPriority.Render); + + break; + } } }; _settings.PropertyChanged += (o, e) => @@ -190,17 +199,15 @@ private void InitializeNotifyIcon() private void InitProgressbarAnimation() { - var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); + var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, + new Duration(new TimeSpan(0, 0, 0, 0, 1600))); var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)")); Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)")); _progressBarStoryboard.Children.Add(da); _progressBarStoryboard.Children.Add(da1); _progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever; - - _progressBarStoryboard.Begin(ProgressBar, true); - _progressBarStoryboard.Pause(); - + _viewModel.ProgressBarVisibility = Visibility.Hidden; isProgressBarStoryboardPaused = true; } @@ -214,10 +221,10 @@ private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e) { if (sender != null && e.OriginalSource != null) { - var r = (ResultListBox)sender; - var d = (DependencyObject)e.OriginalSource; + var r = (ResultListBox) sender; + var d = (DependencyObject) e.OriginalSource; var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem; - var result = (ResultViewModel)item?.DataContext; + var result = (ResultViewModel) item?.DataContext; if (result != null) { if (e.ChangedButton == MouseButton.Left) From bd66e56e7cb514dad646069420123d4e65a2bc99 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Wed, 16 Jun 2021 23:27:34 +0800 Subject: [PATCH 3/3] add controllable argument to allow stop --- Flow.Launcher/MainWindow.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 51e1574f96a..4355cda154b 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -90,7 +90,7 @@ private void OnLoaded(object sender, RoutedEventArgs _) if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused) { - _progressBarStoryboard.Begin(ProgressBar); + _progressBarStoryboard.Begin(ProgressBar, true); isProgressBarStoryboardPaused = false; } } @@ -116,7 +116,7 @@ private void OnLoaded(object sender, RoutedEventArgs _) else if (_viewModel.MainWindowVisibility == Visibility.Visible && isProgressBarStoryboardPaused) { - _progressBarStoryboard.Begin(ProgressBar); + _progressBarStoryboard.Begin(ProgressBar, true); isProgressBarStoryboardPaused = false; } }, System.Windows.Threading.DispatcherPriority.Render);