Skip to content

Commit fc188e2

Browse files
committed
code_review: PR #1484
- Check `Type` instead of `Name` while selecting external shell/terminal - External merge tool named `Custom` is not supported - Add `try-catch` statement to other file pickers in `Preferences` window Signed-off-by: leo <longshuang@msn.cn>
1 parent 1188949 commit fc188e2

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

src/ViewModels/ExecuteCustomAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class CustomActionControlComboBox : ObservableObject, ICustomActionContro
7777
public string Label { get; set; } = string.Empty;
7878
public string Description { get; set; } = string.Empty;
7979
public List<string> Options { get; set; } = [];
80-
80+
8181
public string Value
8282
{
8383
get => _value;

src/Views/Preferences.axaml.cs

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,15 @@ private async void SelectThemeOverrideFile(object _, RoutedEventArgs e)
214214
AllowMultiple = false,
215215
};
216216

217-
var selected = await StorageProvider.OpenFilePickerAsync(options);
218-
if (selected.Count == 1)
217+
try
219218
{
220-
ViewModels.Preferences.Instance.ThemeOverrides = selected[0].Path.LocalPath;
219+
var selected = await StorageProvider.OpenFilePickerAsync(options);
220+
if (selected is { Count: 1 })
221+
ViewModels.Preferences.Instance.ThemeOverrides = selected[0].Path.LocalPath;
222+
}
223+
catch (Exception ex)
224+
{
225+
App.RaiseException(string.Empty, $"Failed to select theme: {ex.Message}");
221226
}
222227

223228
e.Handled = true;
@@ -232,11 +237,18 @@ private async void SelectGitExecutable(object _, RoutedEventArgs e)
232237
AllowMultiple = false,
233238
};
234239

235-
var selected = await StorageProvider.OpenFilePickerAsync(options);
236-
if (selected.Count == 1)
240+
try
241+
{
242+
var selected = await StorageProvider.OpenFilePickerAsync(options);
243+
if (selected is { Count: 1 })
244+
{
245+
ViewModels.Preferences.Instance.GitInstallPath = selected[0].Path.LocalPath;
246+
UpdateGitVersion();
247+
}
248+
}
249+
catch (Exception ex)
237250
{
238-
ViewModels.Preferences.Instance.GitInstallPath = selected[0].Path.LocalPath;
239-
UpdateGitVersion();
251+
App.RaiseException(string.Empty, $"Failed to select git executable: {ex.Message}");
240252
}
241253

242254
e.Handled = true;
@@ -277,10 +289,15 @@ private async void SelectGPGExecutable(object _, RoutedEventArgs e)
277289
AllowMultiple = false,
278290
};
279291

280-
var selected = await StorageProvider.OpenFilePickerAsync(options);
281-
if (selected.Count == 1)
292+
try
293+
{
294+
var selected = await StorageProvider.OpenFilePickerAsync(options);
295+
if (selected is { Count: 1 })
296+
GPGExecutableFile = selected[0].Path.LocalPath;
297+
}
298+
catch (Exception ex)
282299
{
283-
GPGExecutableFile = selected[0].Path.LocalPath;
300+
App.RaiseException(string.Empty, $"Failed to select gpg program: {ex.Message}");
284301
}
285302

286303
e.Handled = true;
@@ -293,9 +310,8 @@ private async void SelectShellOrTerminal(object _, RoutedEventArgs e)
293310
return;
294311

295312
var shell = Models.ShellOrTerminal.Supported[type];
296-
297313
var options = new FilePickerOpenOptions() { AllowMultiple = false };
298-
if (shell.Name != "Custom")
314+
if (shell.Type != "custom")
299315
{
300316
options = new FilePickerOpenOptions()
301317
{
@@ -304,59 +320,47 @@ private async void SelectShellOrTerminal(object _, RoutedEventArgs e)
304320
};
305321
}
306322

307-
IReadOnlyList<IStorageFile> selected = null;
308323
try
309324
{
310-
selected = await StorageProvider.OpenFilePickerAsync(options);
325+
var selected = await StorageProvider.OpenFilePickerAsync(options);
326+
if (selected is { Count: 1 })
327+
ViewModels.Preferences.Instance.ShellOrTerminalPath = selected[0].Path.LocalPath;
311328
}
312329
catch (Exception ex)
313330
{
314331
App.RaiseException(string.Empty, $"Failed to select shell/terminal: {ex.Message}");
315332
}
316333

317-
if (selected is { Count: 1 })
318-
{
319-
ViewModels.Preferences.Instance.ShellOrTerminalPath = selected[0].Path.LocalPath;
320-
}
321-
322334
e.Handled = true;
323335
}
324336

325337
private async void SelectExternalMergeTool(object _, RoutedEventArgs e)
326338
{
327339
var type = ViewModels.Preferences.Instance.ExternalMergeToolType;
328-
if (type < 0 || type >= Models.ExternalMerger.Supported.Count)
340+
if (type <= 0 || type >= Models.ExternalMerger.Supported.Count)
329341
{
330342
ViewModels.Preferences.Instance.ExternalMergeToolType = 0;
331343
e.Handled = true;
332344
return;
333345
}
334346

335347
var tool = Models.ExternalMerger.Supported[type];
336-
var options = new FilePickerOpenOptions() { AllowMultiple = false };
337-
if (tool.Name != "Custom")
348+
var options = new FilePickerOpenOptions()
338349
{
339-
options = new FilePickerOpenOptions()
340-
{
341-
FileTypeFilter = [new FilePickerFileType(tool.Name) { Patterns = tool.GetPatterns() }],
342-
AllowMultiple = false,
343-
};
344-
}
350+
FileTypeFilter = [new FilePickerFileType(tool.Name) { Patterns = tool.GetPatterns() }],
351+
AllowMultiple = false,
352+
};
345353

346-
IReadOnlyList<IStorageFile> selected = null;
347354
try
348355
{
349-
selected = await StorageProvider.OpenFilePickerAsync(options);
356+
var selected = await StorageProvider.OpenFilePickerAsync(options);
357+
if (selected is { Count: 1 })
358+
ViewModels.Preferences.Instance.ExternalMergeToolPath = selected[0].Path.LocalPath;
350359
}
351360
catch (Exception ex)
352361
{
353362
App.RaiseException(string.Empty, $"Failed to select merge tool: {ex.Message}");
354363
}
355-
356-
if (selected is { Count: 1 })
357-
{
358-
ViewModels.Preferences.Instance.ExternalMergeToolPath = selected[0].Path.LocalPath;
359-
}
360364

361365
e.Handled = true;
362366
}
@@ -425,9 +429,16 @@ private async void SelectExecutableForCustomAction(object sender, RoutedEventArg
425429
AllowMultiple = false,
426430
};
427431

428-
var selected = await StorageProvider.OpenFilePickerAsync(options);
429-
if (selected.Count == 1 && sender is Button { DataContext: Models.CustomAction action })
430-
action.Executable = selected[0].Path.LocalPath;
432+
try
433+
{
434+
var selected = await StorageProvider.OpenFilePickerAsync(options);
435+
if (selected is { Count: 1 } && sender is Button { DataContext: Models.CustomAction action })
436+
action.Executable = selected[0].Path.LocalPath;
437+
}
438+
catch (Exception ex)
439+
{
440+
App.RaiseException(string.Empty, $"Failed to select program for custom action: {ex.Message}");
441+
}
431442

432443
e.Handled = true;
433444
}

0 commit comments

Comments
 (0)