From 48aaf3a7db730a49bcec473eb6b38086364a765b Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Fri, 18 Nov 2022 23:15:02 -0600 Subject: [PATCH] fix right click not working (and refactor left click) --- Flow.Launcher/MainWindow.xaml | 246 ++++++++++---------- Flow.Launcher/MainWindow.xaml.cs | 22 -- Flow.Launcher/ResultListBox.xaml | 4 +- Flow.Launcher/ResultListBox.xaml.cs | 48 +++- Flow.Launcher/ViewModel/MainViewModel.cs | 31 +-- Flow.Launcher/ViewModel/ResultsViewModel.cs | 5 + 6 files changed, 192 insertions(+), 164 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml b/Flow.Launcher/MainWindow.xaml index 3bf855da7..17444aae8 100644 --- a/Flow.Launcher/MainWindow.xaml +++ b/Flow.Launcher/MainWindow.xaml @@ -1,5 +1,4 @@ - - - - - - + + + + - - - - - - - - - - - - + + - - - + + - - - - - - - - + - - - - - - - - - - - - + - - - - + + + - - + - + - + - + - - + - + @@ -243,23 +228,19 @@ - - - - - - - - - - + + + - + LeftClickResultCommand="{Binding LeftClickResultCommand}" + RightClickResultCommand="{Binding RightClickResultCommand}" /> - - - + + + - + LeftClickResultCommand="{Binding LeftClickResultCommand}" + RightClickResultCommand="{Binding RightClickResultCommand}" /> - - - + + + - + LeftClickResultCommand="{Binding LeftClickResultCommand}" + RightClickResultCommand="{Binding RightClickResultCommand}" /> diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 747509d77..10b22e33a 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -405,28 +405,6 @@ namespace Flow.Launcher if (e.ChangedButton == MouseButton.Left) DragMove(); } - private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e) - { - if (sender != null && e.OriginalSource != null) - { - var r = (ResultListBox)sender; - var d = (DependencyObject)e.OriginalSource; - var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem; - var result = (ResultViewModel)item?.DataContext; - if (result != null) - { - if (e.ChangedButton == MouseButton.Left) - { - _viewModel.OpenResultCommand.Execute(null); - } - else if (e.ChangedButton == MouseButton.Right) - { - _viewModel.LoadContextMenuCommand.Execute(null); - } - } - } - } - private void OnPreviewDragOver(object sender, DragEventArgs e) { e.Handled = true; diff --git a/Flow.Launcher/ResultListBox.xaml b/Flow.Launcher/ResultListBox.xaml index 1ece390d1..d6cf3462b 100644 --- a/Flow.Launcher/ResultListBox.xaml +++ b/Flow.Launcher/ResultListBox.xaml @@ -27,7 +27,9 @@ Visibility="{Binding Visbility}" mc:Ignorable="d" PreviewMouseMove="ResultList_MouseMove" - PreviewMouseLeftButtonDown="ResultList_PreviewMouseLeftButtonDown"> + PreviewMouseLeftButtonDown="ResultList_PreviewMouseLeftButtonDown" + PreviewMouseLeftButtonUp="ResultListBox_OnPreviewMouseUp" + PreviewMouseRightButtonDown="ResultListBox_OnPreviewMouseRightButtonDown"> diff --git a/Flow.Launcher/ResultListBox.xaml.cs b/Flow.Launcher/ResultListBox.xaml.cs index d1415f8ab..6bd1490c3 100644 --- a/Flow.Launcher/ResultListBox.xaml.cs +++ b/Flow.Launcher/ResultListBox.xaml.cs @@ -17,6 +17,36 @@ namespace Flow.Launcher InitializeComponent(); } + public static readonly DependencyProperty RightClickResultCommandProperty = + DependencyProperty.Register("RightClickResultCommand", typeof(ICommand), typeof(ResultListBox), new UIPropertyMetadata(null)); + + public ICommand RightClickResultCommand + { + get + { + return (ICommand)GetValue(RightClickResultCommandProperty); + } + set + { + SetValue(RightClickResultCommandProperty, value); + } + } + + public static readonly DependencyProperty LeftClickResultCommandProperty = + DependencyProperty.Register("LeftClickResultCommand", typeof(ICommand), typeof(ResultListBox), new UIPropertyMetadata(null)); + + public ICommand LeftClickResultCommand + { + get + { + return (ICommand)GetValue(LeftClickResultCommandProperty); + } + set + { + SetValue(LeftClickResultCommandProperty, value); + } + } + private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0 && e.AddedItems[0] != null) @@ -98,10 +128,24 @@ namespace Flow.Launcher path }); DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Move | DragDropEffects.Copy); - + App.API.ChangeQuery(query, true); - + e.Handled = true; } + private void ResultListBox_OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) + { + if (Mouse.DirectlyOver is not FrameworkElement { DataContext: ResultViewModel result }) + return; + + RightClickResultCommand?.Execute(result.Result); + } + private void ResultListBox_OnPreviewMouseUp(object sender, MouseButtonEventArgs e) + { + if (Mouse.DirectlyOver is not FrameworkElement { DataContext: ResultViewModel result }) + return; + + LeftClickResultCommand?.Execute(null); + } } } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 73be0bbed..99d2a0990 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -77,12 +77,22 @@ namespace Flow.Launcher.ViewModel _userSelectedRecord = _userSelectedRecordStorage.Load(); _topMostRecord = _topMostRecordStorage.Load(); - ContextMenu = new ResultsViewModel(Settings); - Results = new ResultsViewModel(Settings); - History = new ResultsViewModel(Settings); + InitializeKeyCommands(); + + ContextMenu = new ResultsViewModel(Settings) + { + LeftClickResultCommand = OpenResultCommand, RightClickResultCommand = LoadContextMenuCommand + }; + Results = new ResultsViewModel(Settings) + { + LeftClickResultCommand = OpenResultCommand, RightClickResultCommand = LoadContextMenuCommand + }; + History = new ResultsViewModel(Settings) + { + LeftClickResultCommand = OpenResultCommand, RightClickResultCommand = LoadContextMenuCommand + }; _selectedResults = Results; - InitializeKeyCommands(); RegisterViewUpdate(); RegisterResultsUpdatedEvent(); @@ -199,15 +209,10 @@ namespace Flow.Launcher.ViewModel PluginManager.API.OpenUrl("https://github.com/Flow-Launcher/Flow.Launcher/wiki/Flow-Launcher/"); }); OpenSettingCommand = new RelayCommand(_ => { App.API.OpenSettingDialog(); }); - OpenResultCommand = new RelayCommand(async index => + OpenResultCommand = new AsyncRelayCommand(async _ => { var results = SelectedResults; - if (index != null) - { - results.SelectedIndex = int.Parse(index.ToString()!); - } - var result = results.SelectedItem?.Result; if (result == null) { @@ -363,9 +368,9 @@ namespace Flow.Launcher.ViewModel { if (MainWindowWidth + 100 > 1920 || Settings.WindowSize == 1920) { - Settings.WindowSize = 1920; + Settings.WindowSize = 1920; } - else + else { Settings.WindowSize += 100; Settings.WindowLeft -= 50; @@ -648,7 +653,7 @@ namespace Flow.Launcher.ViewModel if (currentCancellationToken.IsCancellationRequested) return; - + // handle the exclusiveness of plugin using action keyword RemoveOldQueryResults(query); diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index db24825d0..1820ea56b 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -8,6 +8,8 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; +using System.Windows.Input; +using JetBrains.Annotations; namespace Flow.Launcher.ViewModel { @@ -49,6 +51,9 @@ namespace Flow.Launcher.ViewModel public ResultViewModel SelectedItem { get; set; } public Thickness Margin { get; set; } public Visibility Visbility { get; set; } = Visibility.Collapsed; + + public ICommand RightClickResultCommand { get; init; } + public ICommand LeftClickResultCommand { get; init; } #endregion