forked from Flow-Launcher/Flow.Launcher
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettingsViewModel.cs
More file actions
596 lines (508 loc) · 20.1 KB
/
SettingsViewModel.cs
File metadata and controls
596 lines (508 loc) · 20.1 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Plugin.Explorer.Search;
using Flow.Launcher.Plugin.Explorer.Search.Everything;
using Flow.Launcher.Plugin.Explorer.Search.Everything.Exceptions;
using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
using Flow.Launcher.Plugin.Explorer.Views;
namespace Flow.Launcher.Plugin.Explorer.ViewModels
{
public partial class SettingsViewModel : BaseModel
{
public Settings Settings { get; set; }
internal PluginInitContext Context { get; set; }
public IReadOnlyList<EnumBindingModel<Settings.IndexSearchEngineOption>> IndexSearchEngines { get; set; }
public IReadOnlyList<EnumBindingModel<Settings.ContentIndexSearchEngineOption>> ContentIndexSearchEngines { get; set; }
public IReadOnlyList<EnumBindingModel<Settings.PathEnumerationEngineOption>> PathEnumerationEngines { get; set; }
public SettingsViewModel(PluginInitContext context, Settings settings)
{
Context = context;
Settings = settings;
InitializeEngineSelection();
InitializeActionKeywordModels();
}
public void Save()
{
Context.API.SaveSettingJsonStorage<Settings>();
}
#region Engine Selection
private EnumBindingModel<Settings.IndexSearchEngineOption> _selectedIndexSearchEngine;
private EnumBindingModel<Settings.ContentIndexSearchEngineOption> _selectedContentSearchEngine;
private EnumBindingModel<Settings.PathEnumerationEngineOption> _selectedPathEnumerationEngine;
public EnumBindingModel<Settings.IndexSearchEngineOption> SelectedIndexSearchEngine
{
get => _selectedIndexSearchEngine;
set
{
_selectedIndexSearchEngine = value;
Settings.IndexSearchEngine = value.Value;
OnPropertyChanged();
}
}
public EnumBindingModel<Settings.ContentIndexSearchEngineOption> SelectedContentSearchEngine
{
get => _selectedContentSearchEngine;
set
{
_selectedContentSearchEngine = value;
Settings.ContentSearchEngine = value.Value;
OnPropertyChanged();
}
}
public EnumBindingModel<Settings.PathEnumerationEngineOption> SelectedPathEnumerationEngine
{
get => _selectedPathEnumerationEngine;
set
{
_selectedPathEnumerationEngine = value;
Settings.PathEnumerationEngine = value.Value;
OnPropertyChanged();
}
}
[MemberNotNull(nameof(IndexSearchEngines),
nameof(ContentIndexSearchEngines),
nameof(PathEnumerationEngines),
nameof(_selectedIndexSearchEngine),
nameof(_selectedContentSearchEngine),
nameof(_selectedPathEnumerationEngine))]
private void InitializeEngineSelection()
{
IndexSearchEngines = EnumBindingModel<Settings.IndexSearchEngineOption>.CreateList();
ContentIndexSearchEngines = EnumBindingModel<Settings.ContentIndexSearchEngineOption>.CreateList();
PathEnumerationEngines = EnumBindingModel<Settings.PathEnumerationEngineOption>.CreateList();
_selectedIndexSearchEngine = IndexSearchEngines.First(x => x.Value == Settings.IndexSearchEngine);
_selectedContentSearchEngine = ContentIndexSearchEngines.First(x => x.Value == Settings.ContentSearchEngine);
_selectedPathEnumerationEngine = PathEnumerationEngines.First(x => x.Value == Settings.PathEnumerationEngine);
}
#endregion
#region Native Context Menu
public bool ShowWindowsContextMenu
{
get => Settings.ShowInlinedWindowsContextMenu;
set
{
Settings.ShowInlinedWindowsContextMenu = value;
OnPropertyChanged();
}
}
public string WindowsContextMenuIncludedItems
{
get => Settings.WindowsContextMenuIncludedItems;
set
{
Settings.WindowsContextMenuIncludedItems = value;
OnPropertyChanged();
}
}
public string WindowsContextMenuExcludedItems
{
get => Settings.WindowsContextMenuExcludedItems;
set
{
Settings.WindowsContextMenuExcludedItems = value;
OnPropertyChanged();
}
}
#endregion
#region Preview Panel
public bool ShowFileSizeInPreviewPanel
{
get => Settings.ShowFileSizeInPreviewPanel;
set
{
Settings.ShowFileSizeInPreviewPanel = value;
OnPropertyChanged();
}
}
public bool ShowCreatedDateInPreviewPanel
{
get => Settings.ShowCreatedDateInPreviewPanel;
set
{
Settings.ShowCreatedDateInPreviewPanel = value;
OnPropertyChanged();
OnPropertyChanged(nameof(ShowPreviewPanelDateTimeChoices));
OnPropertyChanged(nameof(PreviewPanelDateTimeChoicesVisibility));
}
}
public bool ShowModifiedDateInPreviewPanel
{
get => Settings.ShowModifiedDateInPreviewPanel;
set
{
Settings.ShowModifiedDateInPreviewPanel = value;
OnPropertyChanged();
OnPropertyChanged(nameof(ShowPreviewPanelDateTimeChoices));
OnPropertyChanged(nameof(PreviewPanelDateTimeChoicesVisibility));
}
}
public bool ShowFileAgeInPreviewPanel
{
get => Settings.ShowFileAgeInPreviewPanel;
set
{
Settings.ShowFileAgeInPreviewPanel = value;
OnPropertyChanged();
OnPropertyChanged(nameof(ShowPreviewPanelDateTimeChoices));
OnPropertyChanged(nameof(PreviewPanelDateTimeChoicesVisibility));
}
}
public string PreviewPanelDateFormat
{
get => Settings.PreviewPanelDateFormat;
set
{
Settings.PreviewPanelDateFormat = value;
OnPropertyChanged();
OnPropertyChanged(nameof(PreviewPanelDateFormatDemo));
}
}
public string PreviewPanelTimeFormat
{
get => Settings.PreviewPanelTimeFormat;
set
{
Settings.PreviewPanelTimeFormat = value;
OnPropertyChanged();
OnPropertyChanged(nameof(PreviewPanelTimeFormatDemo));
}
}
public string PreviewPanelDateFormatDemo => DateTime.Now.ToString(PreviewPanelDateFormat, CultureInfo.CurrentCulture);
public string PreviewPanelTimeFormatDemo => DateTime.Now.ToString(PreviewPanelTimeFormat, CultureInfo.CurrentCulture);
public bool ShowPreviewPanelDateTimeChoices => ShowCreatedDateInPreviewPanel || ShowModifiedDateInPreviewPanel;
public Visibility PreviewPanelDateTimeChoicesVisibility => ShowCreatedDateInPreviewPanel || ShowModifiedDateInPreviewPanel ? Visibility.Visible : Visibility.Collapsed;
public List<string> TimeFormatList { get; } = new()
{
"h:mm",
"hh:mm",
"H:mm",
"HH:mm",
"tt h:mm",
"tt hh:mm",
"h:mm tt",
"hh:mm tt",
"hh:mm:ss tt",
"HH:mm:ss"
};
public List<string> DateFormatList { get; } = new()
{
"dd/MM/yyyy",
"dd/MM/yyyy ddd",
"dd/MM/yyyy, dddd",
"dd-MM-yyyy",
"dd-MM-yyyy ddd",
"dd-MM-yyyy, dddd",
"dd.MM.yyyy",
"dd.MM.yyyy ddd",
"dd.MM.yyyy, dddd",
"MM/dd/yyyy",
"MM/dd/yyyy ddd",
"MM/dd/yyyy, dddd",
"yyyy-MM-dd",
"yyyy-MM-dd ddd",
"yyyy-MM-dd, dddd",
};
#endregion
#region ActionKeyword
[MemberNotNull(nameof(ActionKeywordsModels))]
private void InitializeActionKeywordModels()
{
ActionKeywordsModels = new List<ActionKeywordModel>
{
new(Settings.ActionKeyword.SearchActionKeyword,
"plugin_explorer_actionkeywordview_search"),
new(Settings.ActionKeyword.FileContentSearchActionKeyword,
"plugin_explorer_actionkeywordview_filecontentsearch"),
new(Settings.ActionKeyword.PathSearchActionKeyword,
"plugin_explorer_actionkeywordview_pathsearch"),
new(Settings.ActionKeyword.IndexSearchActionKeyword,
"plugin_explorer_actionkeywordview_indexsearch"),
new(Settings.ActionKeyword.QuickAccessActionKeyword,
"plugin_explorer_actionkeywordview_quickaccess")
};
}
public IReadOnlyList<ActionKeywordModel> ActionKeywordsModels { get; set; }
public ActionKeywordModel? SelectedActionKeyword { get; set; }
[RelayCommand]
private void EditActionKeyword(object obj)
{
if (SelectedActionKeyword is not { } actionKeyword)
{
ShowUnselectedMessage();
return;
}
var actionKeywordWindow = new ActionKeywordSetting(actionKeyword, Context.API);
if (!(actionKeywordWindow.ShowDialog() ?? false))
{
return;
}
switch (actionKeyword.Enabled, actionKeywordWindow.KeywordEnabled)
{
case (true, false):
Context.API.RemoveActionKeyword(Context.CurrentPluginMetadata.ID, actionKeyword.Keyword);
break;
case (true, true):
// same keyword will have dialog result false
Context.API.RemoveActionKeyword(Context.CurrentPluginMetadata.ID, actionKeyword.Keyword);
Context.API.AddActionKeyword(Context.CurrentPluginMetadata.ID, actionKeywordWindow.ActionKeyword);
break;
case (false, true):
Context.API.AddActionKeyword(Context.CurrentPluginMetadata.ID, actionKeywordWindow.ActionKeyword);
break;
case (false, false):
throw new ArgumentException(
$"Both false in {nameof(actionKeyword)}.{nameof(actionKeyword.Enabled)} and {nameof(actionKeywordWindow)}.{nameof(actionKeywordWindow.KeywordEnabled)} should suggest that the ShowDialog() result is false");
}
(actionKeyword.Keyword, actionKeyword.Enabled) = (actionKeywordWindow.ActionKeyword, actionKeywordWindow.KeywordEnabled);
}
#endregion
#region AccessLinks
public AccessLink? SelectedQuickAccessLink { get; set; }
public AccessLink? SelectedIndexSearchExcludedPath { get; set; }
public void AppendLink(string containerName, AccessLink link)
{
var container = containerName switch
{
"QuickAccessLink" => Settings.QuickAccessLinks,
"IndexSearchExcludedPaths" => Settings.IndexSearchExcludedSubdirectoryPaths,
_ => throw new ArgumentException($"Unknown container name: {containerName}")
};
container.Add(link);
}
[RelayCommand]
private void EditLink(object commandParameter)
{
var (selectedLink, collection) = commandParameter switch
{
"QuickAccessLink" => (SelectedQuickAccessLink, Settings.QuickAccessLinks),
"IndexSearchExcludedPaths" => (SelectedIndexSearchExcludedPath, Settings.IndexSearchExcludedSubdirectoryPaths),
_ => throw new ArgumentOutOfRangeException(nameof(commandParameter))
};
if (selectedLink is null)
{
ShowUnselectedMessage();
return;
}
var path = PromptUserSelectPath(selectedLink.Type,
selectedLink.Type == ResultType.Folder
? selectedLink.Path
: Path.GetDirectoryName(selectedLink.Path));
if (path is null)
return;
collection.Remove(selectedLink);
collection.Add(new AccessLink
{
Path = path, Type = selectedLink.Type,
});
}
private void ShowUnselectedMessage()
{
var warning = Context.API.GetTranslation("plugin_explorer_make_selection_warning");
Context.API.ShowMsgBox(warning);
}
[RelayCommand]
private void AddLink(object commandParameter)
{
var container = commandParameter switch
{
"QuickAccessLink" => Settings.QuickAccessLinks,
"IndexSearchExcludedPaths" => Settings.IndexSearchExcludedSubdirectoryPaths,
_ => throw new ArgumentOutOfRangeException(nameof(commandParameter))
};
ArgumentNullException.ThrowIfNull(container);
var folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
return;
var newAccessLink = new AccessLink
{
Path = folderBrowserDialog.SelectedPath
};
container.Add(newAccessLink);
}
[RelayCommand]
private void RemoveLink(object obj)
{
if (obj is not string container) return;
switch (container)
{
case "QuickAccessLink":
if (SelectedQuickAccessLink == null) return;
Settings.QuickAccessLinks.Remove(SelectedQuickAccessLink);
break;
case "IndexSearchExcludedPaths":
if (SelectedIndexSearchExcludedPath == null) return;
Settings.IndexSearchExcludedSubdirectoryPaths.Remove(SelectedIndexSearchExcludedPath);
break;
}
Save();
}
#endregion
private string? PromptUserSelectPath(ResultType type, string? initialDirectory = null)
{
string? path = null;
if (type is ResultType.Folder)
{
var folderBrowserDialog = new FolderBrowserDialog();
if (initialDirectory is not null)
folderBrowserDialog.InitialDirectory = initialDirectory;
if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
return path;
path = folderBrowserDialog.SelectedPath;
}
else if (type is ResultType.File)
{
var openFileDialog = new OpenFileDialog();
if (initialDirectory is not null)
openFileDialog.InitialDirectory = initialDirectory;
if (openFileDialog.ShowDialog() != DialogResult.OK)
return path;
path = openFileDialog.FileName;
}
return path;
}
internal static void OpenWindowsIndexingOptions()
{
var psi = new ProcessStartInfo
{
FileName = "control.exe",
UseShellExecute = true,
Arguments = Constants.WindowsIndexingOptions
};
Process.Start(psi);
}
[RelayCommand]
private void OpenFileEditorPath()
{
var path = PromptUserSelectPath(ResultType.File, Settings.EditorPath != null ? Path.GetDirectoryName(Settings.EditorPath) : null);
if (path is null)
return;
FileEditorPath = path;
}
[RelayCommand]
private void OpenFolderEditorPath()
{
var path = PromptUserSelectPath(ResultType.File, Settings.FolderEditorPath != null ? Path.GetDirectoryName(Settings.FolderEditorPath) : null);
if (path is null)
return;
FolderEditorPath = path;
}
[RelayCommand]
private void OpenShellPath()
{
var path = PromptUserSelectPath(ResultType.File, Settings.EditorPath != null ? Path.GetDirectoryName(Settings.EditorPath) : null);
if (path is null)
return;
ShellPath = path;
}
public string FileEditorPath
{
get => Settings.EditorPath;
set
{
Settings.EditorPath = value;
OnPropertyChanged();
}
}
public string FolderEditorPath
{
get => Settings.FolderEditorPath;
set
{
Settings.FolderEditorPath = value;
OnPropertyChanged();
}
}
public string ShellPath
{
get => Settings.ShellPath;
set
{
Settings.ShellPath = value;
OnPropertyChanged();
}
}
public string ExcludedFileTypes
{
get => Settings.ExcludedFileTypes;
set
{
// remove spaces and dots from the string before saving
string sanitized = string.IsNullOrEmpty(value) ? "" : value.Replace(" ", "").Replace(".", "");
Settings.ExcludedFileTypes = sanitized;
OnPropertyChanged();
}
}
public int MaxResultLowerLimit => 1;
public int MaxResultUpperLimit => 100000;
public int MaxResult
{
get => Settings.MaxResult;
set
{
Settings.MaxResult = Math.Clamp(value, MaxResultLowerLimit, MaxResultUpperLimit);
OnPropertyChanged();
}
}
#region Everything FastSortWarning
public Visibility FastSortWarningVisibility
{
get
{
try
{
return EverythingApi.IsFastSortOption(Settings.SortOption) ? Visibility.Collapsed : Visibility.Visible;
}
catch (IPCErrorException)
{
// this error occurs if the Everything service is not running, in this instance show the warning and
// update the message to let user know in the settings panel.
return Visibility.Visible;
}
catch (DllNotFoundException)
{
return Visibility.Collapsed;
}
}
}
public string SortOptionWarningMessage
{
get
{
try
{
// this method is used to determine if Everything service is running because as at Everything v1.4.1
// the sdk does not provide a dedicated interface to determine if it is running.
return EverythingApi.IsFastSortOption(Settings.SortOption) ? string.Empty
: Context.API.GetTranslation("flowlauncher_plugin_everything_nonfastsort_warning");
}
catch (IPCErrorException)
{
return Context.API.GetTranslation("flowlauncher_plugin_everything_is_not_running");
}
catch (DllNotFoundException)
{
return Context.API.GetTranslation("flowlauncher_plugin_everything_sdk_issue");
}
}
}
public string EverythingInstalledPath
{
get => Settings.EverythingInstalledPath;
set
{
Settings.EverythingInstalledPath = value;
OnPropertyChanged();
}
}
#endregion
}
}