forked from Flow-Launcher/Flow.Launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsControl.xaml.cs
More file actions
254 lines (213 loc) · 8.9 KB
/
Copy pathSettingsControl.xaml.cs
File metadata and controls
254 lines (213 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
namespace Flow.Launcher.Plugin.WebSearch
{
/// <summary>
/// Interaction logic for WebSearchesSetting.xaml
/// </summary>
public partial class SettingsControl : UserControl
{
private readonly Settings _settings;
private readonly PluginInitContext _context;
private Point _dragStartPoint;
public SettingsControl(PluginInitContext context, SettingsViewModel viewModel)
{
InitializeComponent();
_context = context;
_settings = viewModel.Settings;
DataContext = viewModel;
}
private void OnAddSearchSearchClick(object sender, RoutedEventArgs e)
{
var setting = new SearchSourceSettingWindow(_settings.SearchSources, _context);
setting.ShowDialog();
}
private void OnDeleteSearchSearchClick(object sender, RoutedEventArgs e)
{
if (_settings.SelectedSearchSource != null)
{
var selected = _settings.SelectedSearchSource;
var warning = _context.API.GetTranslation("flowlauncher_plugin_websearch_delete_warning");
var formated = string.Format(warning, selected.Title);
var result = _context.API.ShowMsgBox(formated, string.Empty, MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
var id = _context.CurrentPluginMetadata.ID;
_context.API.RemoveActionKeyword(id, selected.ActionKeyword);
_settings.SearchSources.Remove(selected);
}
}
}
private void OnEditSearchSourceClick(object sender, RoutedEventArgs e)
{
if (_settings.SelectedSearchSource != null)
{
var webSearch = new SearchSourceSettingWindow
(
_settings.SearchSources, _context, _settings.SelectedSearchSource
);
webSearch.ShowDialog();
}
}
GridViewColumnHeader _lastHeaderClicked = null;
ListSortDirection _lastDirection = ListSortDirection.Ascending;
private void SortByColumn(object sender, RoutedEventArgs e)
{
ListSortDirection direction;
if (e.OriginalSource is not GridViewColumnHeader headerClicked)
{
return;
}
if (headerClicked.Role == GridViewColumnHeaderRole.Padding)
{
return;
}
if (headerClicked != _lastHeaderClicked)
{
direction = ListSortDirection.Ascending;
}
else
{
if (_lastDirection == ListSortDirection.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
direction = ListSortDirection.Ascending;
}
}
var columnBinding = headerClicked.Column.DisplayMemberBinding as Binding;
var sortBy = columnBinding?.Path.Path ?? headerClicked.Column.Header as string;
if (sortBy != null)
{
Sort(sortBy, direction);
if (direction == ListSortDirection.Ascending)
{
headerClicked.Column.HeaderTemplate =
Resources["HeaderTemplateArrowUp"] as DataTemplate;
}
else
{
headerClicked.Column.HeaderTemplate =
Resources["HeaderTemplateArrowDown"] as DataTemplate;
}
// Remove arrow from previously sorted header
if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
{
_lastHeaderClicked.Column.HeaderTemplate = null;
}
_lastHeaderClicked = headerClicked;
_lastDirection = direction;
}
}
private void Sort(string sortBy, ListSortDirection direction)
{
ICollectionView dataView = CollectionViewSource.GetDefaultView(SearchSourcesListView.ItemsSource);
dataView.SortDescriptions.Clear();
SortDescription sd = new(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}
private void MouseDoubleClickItem(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (((FrameworkElement)e.OriginalSource).DataContext is SearchSource && _settings.SelectedSearchSource != null)
{
var webSearch = new SearchSourceSettingWindow
(
_settings.SearchSources, _context, _settings.SelectedSearchSource
);
webSearch.ShowDialog();
}
}
private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
{
var listView = sender as ListView;
var gView = listView.View as GridView;
var workingWidth =
listView.ActualWidth - SystemParameters.VerticalScrollBarWidth; // take into account vertical scrollbar
if (workingWidth <= 0) return;
var col1 = 0.08;
var col2 = 0.24;
var col3 = 0.26;
var col4 = 0.20;
var col5 = 0.22;
gView.Columns[0].Width = workingWidth * col1;
gView.Columns[1].Width = workingWidth * col2;
gView.Columns[2].Width = workingWidth * col3;
gView.Columns[3].Width = workingWidth * col4;
gView.Columns[4].Width = workingWidth * col5;
}
private void ListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_dragStartPoint = e.GetPosition(null);
}
private void ListView_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _dragStartPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listView = (ListView)sender;
ListViewItem listViewItem = FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null) return;
SearchSource item = (SearchSource)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);
if (item == null) return;
DragDrop.DoDragDrop(listViewItem, item, DragDropEffects.Move);
}
}
private void ListView_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(SearchSource)))
{
SearchSource droppedData = e.Data.GetData(typeof(SearchSource)) as SearchSource;
var listView = (ListView)sender;
var target = GetNearestContainer(e.OriginalSource);
if (target == null)
return;
SearchSource targetData = (SearchSource)listView.ItemContainerGenerator.ItemFromContainer(target);
if (targetData == null)
return;
var items = _settings.SearchSources;
int removedIdx = items.IndexOf(droppedData);
int targetIdx = items.IndexOf(targetData);
if (removedIdx == targetIdx)
return;
items.Move(removedIdx, targetIdx);
}
}
private ListViewItem GetNearestContainer(object source)
{
var element = source as UIElement;
while (element != null && !(element is ListViewItem))
element = VisualTreeHelper.GetParent(element) as UIElement;
return element as ListViewItem;
}
private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
{
while (current != null)
{
if (current is T)
return (T)current;
current = VisualTreeHelper.GetParent(current);
}
return null;
}
// This is used for NumberBox to force its value to be 1 when the user clears the value
private void NumberBox_ValueChanged(iNKORE.UI.WPF.Modern.Controls.NumberBox sender, iNKORE.UI.WPF.Modern.Controls.NumberBoxValueChangedEventArgs args)
{
if (double.IsNaN(args.NewValue))
{
sender.Value = 1;
_settings.MaxSuggestions = (int)sender.Value;
}
}
}
}