|
2 | 2 | using System.Windows;
|
3 | 3 | using System.Windows.Controls;
|
4 | 4 | using Flow.Launcher.Core.Plugin;
|
| 5 | +using System.ComponentModel; |
| 6 | +using System.Windows.Data; |
5 | 7 |
|
6 | 8 | namespace Flow.Launcher.Plugin.WebSearch
|
7 | 9 | {
|
@@ -88,5 +90,85 @@ private void OnBrowserPathTextChanged(object sender, TextChangedEventArgs e)
|
88 | 90 | {
|
89 | 91 | _settings.BrowserPath = browserPathBox.Text;
|
90 | 92 | }
|
| 93 | + |
| 94 | + GridViewColumnHeader _lastHeaderClicked = null; |
| 95 | + ListSortDirection _lastDirection = ListSortDirection.Ascending; |
| 96 | + |
| 97 | + private void SortByColumn(object sender, RoutedEventArgs e) |
| 98 | + { |
| 99 | + ListSortDirection direction; |
| 100 | + |
| 101 | + if (e.OriginalSource is not GridViewColumnHeader headerClicked) |
| 102 | + { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if (headerClicked.Role == GridViewColumnHeaderRole.Padding) |
| 107 | + { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (headerClicked != _lastHeaderClicked) |
| 112 | + { |
| 113 | + direction = ListSortDirection.Ascending; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + if (_lastDirection == ListSortDirection.Ascending) |
| 118 | + { |
| 119 | + direction = ListSortDirection.Descending; |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + direction = ListSortDirection.Ascending; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + var columnBinding = headerClicked.Column.DisplayMemberBinding as Binding; |
| 128 | + var sortBy = columnBinding?.Path.Path ?? headerClicked.Column.Header as string; |
| 129 | + |
| 130 | + Sort(sortBy, direction); |
| 131 | + |
| 132 | + if (direction == ListSortDirection.Ascending) |
| 133 | + { |
| 134 | + headerClicked.Column.HeaderTemplate = |
| 135 | + Resources["HeaderTemplateArrowUp"] as DataTemplate; |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + headerClicked.Column.HeaderTemplate = |
| 140 | + Resources["HeaderTemplateArrowDown"] as DataTemplate; |
| 141 | + } |
| 142 | + |
| 143 | + // Remove arrow from previously sorted header |
| 144 | + if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked) |
| 145 | + { |
| 146 | + _lastHeaderClicked.Column.HeaderTemplate = null; |
| 147 | + } |
| 148 | + |
| 149 | + _lastHeaderClicked = headerClicked; |
| 150 | + _lastDirection = direction; |
| 151 | + } |
| 152 | + private void Sort(string sortBy, ListSortDirection direction) |
| 153 | + { |
| 154 | + ICollectionView dataView = CollectionViewSource.GetDefaultView(SearchSourcesListView.ItemsSource); |
| 155 | + dataView.SortDescriptions.Clear(); |
| 156 | + SortDescription sd = new(sortBy, direction); |
| 157 | + dataView.SortDescriptions.Add(sd); |
| 158 | + dataView.Refresh(); |
| 159 | + } |
| 160 | + |
| 161 | + private void MouseDoubleClickItem(object sender, System.Windows.Input.MouseButtonEventArgs e) |
| 162 | + { |
| 163 | + if (((FrameworkElement)e.OriginalSource).DataContext is SearchSource && _settings.SelectedSearchSource != null) |
| 164 | + { |
| 165 | + var webSearch = new SearchSourceSettingWindow |
| 166 | + ( |
| 167 | + _settings.SearchSources, _context, _settings.SelectedSearchSource |
| 168 | + ); |
| 169 | + |
| 170 | + webSearch.ShowDialog(); |
| 171 | + } |
| 172 | + } |
91 | 173 | }
|
92 | 174 | }
|
0 commit comments