Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Progressbar Dispatcher Exception #1445

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Flow.Launcher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@
Height="2"
HorizontalAlignment="Right"
StrokeThickness="1"
Style="{DynamicResource PendingLineStyle}"
Visibility="{Binding ProgressBarVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
X1="-150"
X2="-50"
Expand Down
185 changes: 97 additions & 88 deletions Flow.Launcher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public partial class MainWindow
{
#region Private Fields

private readonly Storyboard _progressBarStoryboard = new Storyboard();
private bool isProgressBarStoryboardPaused;
private Settings _settings;
private NotifyIcon _notifyIcon;
private ContextMenu contextMenu;
Expand All @@ -57,7 +55,7 @@ public MainWindow(Settings settings, MainViewModel mainVM)
DataContext = mainVM;
_viewModel = mainVM;
_settings = settings;

InitializeComponent();
InitializePosition();
animationSound.Open(new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\open.wav"));
Expand All @@ -67,7 +65,7 @@ public MainWindow()
{
InitializeComponent();
}

private void OnCopy(object sender, ExecutedRoutedEventArgs e)
{
if (QueryTextBox.SelectionLength == 0)
Expand Down Expand Up @@ -114,59 +112,29 @@ private void OnLoaded(object sender, RoutedEventArgs _)
switch (e.PropertyName)
{
case nameof(MainViewModel.MainWindowVisibilityStatus):
{
if (_viewModel.MainWindowVisibilityStatus)
{
if (_viewModel.MainWindowVisibilityStatus)
if (_settings.UseSound)
{
if (_settings.UseSound)
{
animationSound.Position = TimeSpan.Zero;
animationSound.Play();
}
UpdatePosition();
Activate();
QueryTextBox.Focus();
_settings.ActivateTimes++;
if (!_viewModel.LastQuerySelected)
{
QueryTextBox.SelectAll();
_viewModel.LastQuerySelected = true;
}

if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused)
{
_progressBarStoryboard.Begin(ProgressBar, true);
isProgressBarStoryboardPaused = false;
}

if(_settings.UseAnimation)
WindowAnimator();
animationSound.Position = TimeSpan.Zero;
animationSound.Play();
}
else if (!isProgressBarStoryboardPaused)
UpdatePosition();
Activate();
QueryTextBox.Focus();
_settings.ActivateTimes++;
if (!_viewModel.LastQuerySelected)
{
_progressBarStoryboard.Stop(ProgressBar);
isProgressBarStoryboardPaused = true;
QueryTextBox.SelectAll();
_viewModel.LastQuerySelected = true;
}

break;
}
case nameof(MainViewModel.ProgressBarVisibility):
{
Dispatcher.Invoke(() =>
{
if (_viewModel.ProgressBarVisibility == Visibility.Hidden && !isProgressBarStoryboardPaused)
{
_progressBarStoryboard.Stop(ProgressBar);
isProgressBarStoryboardPaused = true;
}
else if (_viewModel.MainWindowVisibilityStatus &&
isProgressBarStoryboardPaused)
{
_progressBarStoryboard.Begin(ProgressBar, true);
isProgressBarStoryboardPaused = false;
}
});
break;
if (_settings.UseAnimation)
WindowAnimator();
}
break;
}
case nameof(MainViewModel.QueryTextCursorMovedToEnd):
if (_viewModel.QueryTextCursorMovedToEnd)
{
Expand Down Expand Up @@ -249,35 +217,45 @@ private void InitializeNotifyIcon()

contextMenu = new ContextMenu();

var openIcon = new FontIcon { Glyph = "\ue71e" };
var openIcon = new FontIcon
{
Glyph = "\ue71e"
};
var open = new MenuItem
{
Header = InternationalizationManager.Instance.GetTranslation("iconTrayOpen") + " (" + _settings.Hotkey + ")",
Icon = openIcon
Header = InternationalizationManager.Instance.GetTranslation("iconTrayOpen") + " (" + _settings.Hotkey + ")", Icon = openIcon
};
var gamemodeIcon = new FontIcon
{
Glyph = "\ue7fc"
};
var gamemodeIcon = new FontIcon { Glyph = "\ue7fc" };
var gamemode = new MenuItem
{
Header = InternationalizationManager.Instance.GetTranslation("GameMode"),
Icon = gamemodeIcon
Header = InternationalizationManager.Instance.GetTranslation("GameMode"), Icon = gamemodeIcon
};
var positionresetIcon = new FontIcon
{
Glyph = "\ue73f"
};
var positionresetIcon = new FontIcon { Glyph = "\ue73f" };
var positionreset = new MenuItem
{
Header = InternationalizationManager.Instance.GetTranslation("PositionReset"),
Icon = positionresetIcon
Header = InternationalizationManager.Instance.GetTranslation("PositionReset"), Icon = positionresetIcon
};
var settingsIcon = new FontIcon
{
Glyph = "\ue713"
};
var settingsIcon = new FontIcon { Glyph = "\ue713" };
var settings = new MenuItem
{
Header = InternationalizationManager.Instance.GetTranslation("iconTraySettings"),
Icon = settingsIcon
Header = InternationalizationManager.Instance.GetTranslation("iconTraySettings"), Icon = settingsIcon
};
var exitIcon = new FontIcon
{
Glyph = "\ue7e8"
};
var exitIcon = new FontIcon { Glyph = "\ue7e8" };
var exit = new MenuItem
{
Header = InternationalizationManager.Instance.GetTranslation("iconTrayExit"),
Icon = exitIcon
Header = InternationalizationManager.Instance.GetTranslation("iconTrayExit"), Icon = exitIcon
};

open.Click += (o, e) => _viewModel.ToggleFlowLauncher();
Expand Down Expand Up @@ -340,24 +318,55 @@ private void ToggleGameMode()
}
private async void PositionReset()
{
_viewModel.Show();
await Task.Delay(300); // If don't give a time, Positioning will be weird.
Left = HorizonCenter();
Top = VerticalCenter();
_viewModel.Show();
await Task.Delay(300); // If don't give a time, Positioning will be weird.
Left = HorizonCenter();
Top = VerticalCenter();
}
private void InitProgressbarAnimation()
{
var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 150,
new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
var progressBarStoryBoard = new Storyboard();

var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 150, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth + 50, 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.Children.Add(da);
progressBarStoryBoard.Children.Add(da1);
progressBarStoryBoard.RepeatBehavior = RepeatBehavior.Forever;

da.Freeze();
da1.Freeze();

const string progressBarAnimationName = "ProgressBarAnimation";
var beginStoryboard = new BeginStoryboard
{
Name = progressBarAnimationName, Storyboard = progressBarStoryBoard
};

var stopStoryboard = new StopStoryboard()
{
BeginStoryboardName = progressBarAnimationName
};

var trigger = new Trigger
{
Property = VisibilityProperty, Value = Visibility.Visible
};
trigger.EnterActions.Add(beginStoryboard);
trigger.ExitActions.Add(stopStoryboard);


var progressStyle = new Style(typeof(System.Windows.Shapes.Line))
{
BasedOn = FindResource("PendingLineStyle") as Style
};
progressStyle.RegisterName(progressBarAnimationName, beginStoryboard);
progressStyle.Triggers.Add(trigger);

ProgressBar.Style = progressStyle;

_viewModel.ProgressBarVisibility = Visibility.Hidden;
isProgressBarStoryboardPaused = true;
}
public void WindowAnimator()
{
Expand Down Expand Up @@ -390,11 +399,11 @@ public void WindowAnimator()
};
var IconMotion = new DoubleAnimation
{
From = 12,
To = 0,
EasingFunction = easing,
Duration = TimeSpan.FromSeconds(0.36),
FillBehavior = FillBehavior.Stop
From = 12,
To = 0,
EasingFunction = easing,
Duration = TimeSpan.FromSeconds(0.36),
FillBehavior = FillBehavior.Stop
};

var ClockOpacity = new DoubleAnimation
Expand Down Expand Up @@ -466,10 +475,10 @@ private void OnPreviewDragOver(object sender, DragEventArgs e)
private async void OnContextMenusForSettingsClick(object sender, RoutedEventArgs e)
{
_viewModel.Hide();
if(_settings.UseAnimation)

if (_settings.UseAnimation)
await Task.Delay(100);

App.API.OpenSettingDialog();
}

Expand All @@ -487,7 +496,7 @@ private async void OnDeactivated(object sender, EventArgs e)
// and always after Settings window is closed.
if (_settings.UseAnimation)
await Task.Delay(100);

if (_settings.HideWhenDeactive)
{
_viewModel.Hide();
Expand Down Expand Up @@ -525,7 +534,7 @@ public void HideStartup()
_viewModel.Show();
}
}

public double HorizonCenter()
{
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
Expand Down Expand Up @@ -607,9 +616,9 @@ private void OnKeyDown(object sender, KeyEventArgs e)
&& QueryTextBox.Text.Length > 0
&& QueryTextBox.CaretIndex == QueryTextBox.Text.Length)
{
var queryWithoutActionKeyword =
var queryWithoutActionKeyword =
QueryBuilder.Build(QueryTextBox.Text.Trim(), PluginManager.NonGlobalPlugins).Search;

if (FilesFolders.IsLocationPathString(queryWithoutActionKeyword))
{
_viewModel.BackspaceCommand.Execute(null);
Expand Down Expand Up @@ -645,7 +654,7 @@ public void InitializeColorScheme()

private void QueryTextBox_KeyUp(object sender, KeyEventArgs e)
{
if(_viewModel.QueryText != QueryTextBox.Text)
if (_viewModel.QueryText != QueryTextBox.Text)
{
BindingExpression be = QueryTextBox.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);
be.UpdateSource();
Expand Down