From ead8591bfb59350fb6e2fa2b0a657d06f436a7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=9B=E5=B0=98=E7=A9=BA=E5=BF=A7?= Date: Wed, 15 Jan 2025 17:00:16 +0800 Subject: [PATCH] change:IsCancelingPickerAlsoTriggers ->OmitCommandOnCancel feat:add property ClearSelectionOnCancel. support for not clearing the selection if the file picker is canceled. --- demo/Ursa.Demo/Pages/PathPickerDemo.axaml | 15 ++++++--- src/Ursa/Controls/PathPicker/PathPicker.cs | 38 ++++++++++++++++------ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/demo/Ursa.Demo/Pages/PathPickerDemo.axaml b/demo/Ursa.Demo/Pages/PathPickerDemo.axaml index 8ffe642..50fa8c1 100644 --- a/demo/Ursa.Demo/Pages/PathPickerDemo.axaml +++ b/demo/Ursa.Demo/Pages/PathPickerDemo.axaml @@ -32,7 +32,11 @@ - + + @@ -53,7 +57,8 @@ SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPaths="{Binding Paths,Mode=OneWayToSource}" Command="{Binding SelectedCommand}" - IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}"> + IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}" + IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}"> @@ -68,7 +73,8 @@ SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPaths="{Binding Paths,Mode=OneWayToSource}" Command="{Binding SelectedCommand}" - IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}"> + IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}" + IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}"> @@ -83,7 +89,8 @@ SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPaths="{Binding Paths,Mode=OneWayToSource}" Command="{Binding SelectedCommand}" - IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}"> + IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}" + IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}"> diff --git a/src/Ursa/Controls/PathPicker/PathPicker.cs b/src/Ursa/Controls/PathPicker/PathPicker.cs index 10e96d4..e4e69d2 100644 --- a/src/Ursa/Controls/PathPicker/PathPicker.cs +++ b/src/Ursa/Controls/PathPicker/PathPicker.cs @@ -54,14 +54,24 @@ public class PathPicker : TemplatedControl AvaloniaProperty.Register( nameof(SelectedPathsText), defaultBindingMode: BindingMode.TwoWay); - public static readonly StyledProperty IsCancelingPickerAlsoTriggersProperty = + public static readonly StyledProperty IsOmitCommandOnCancelProperty = AvaloniaProperty.Register( - nameof(IsCancelingPickerAlsoTriggers)); + nameof(IsOmitCommandOnCancel)); - public bool IsCancelingPickerAlsoTriggers + public static readonly StyledProperty IsClearSelectionOnCancelProperty = + AvaloniaProperty.Register( + nameof(IsClearSelectionOnCancel)); + + public bool IsClearSelectionOnCancel { - get => GetValue(IsCancelingPickerAlsoTriggersProperty); - set => SetValue(IsCancelingPickerAlsoTriggersProperty, value); + get => GetValue(IsClearSelectionOnCancelProperty); + set => SetValue(IsClearSelectionOnCancelProperty, value); + } + + public bool IsOmitCommandOnCancel + { + get => GetValue(IsOmitCommandOnCancelProperty); + set => SetValue(IsOmitCommandOnCancelProperty, value); } public string? SelectedPathsText @@ -246,7 +256,7 @@ public class PathPicker : TemplatedControl FileTypeFilter = ParseFileTypes(FileFilter) }; var resFiles = await storageProvider.OpenFilePickerAsync(filePickerOpenOptions); - SelectedPaths = resFiles.Select(x => x.TryGetLocalPath()).ToArray()!; + UpdateSelectedPaths(resFiles.Select(x => x.TryGetLocalPath()).ToArray()!); break; case UsePickerTypes.SaveFile: FilePickerSaveOptions filePickerSaveOptions = new() @@ -261,9 +271,9 @@ public class PathPicker : TemplatedControl var path = (await storageProvider.SaveFilePickerAsync(filePickerSaveOptions)) ?.TryGetLocalPath(); - SelectedPaths = string.IsNullOrEmpty(path) + UpdateSelectedPaths(string.IsNullOrEmpty(path) ? Array.Empty() - : [path!]; + : [path!]); break; case UsePickerTypes.OpenFolder: FolderPickerOpenOptions folderPickerOpenOptions = new() @@ -275,18 +285,26 @@ public class PathPicker : TemplatedControl SuggestedFileName = SuggestedFileName }; var resFolder = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions); - SelectedPaths = resFolder.Select(x => x.TryGetLocalPath()).ToArray()!; + UpdateSelectedPaths(resFolder.Select(x => x.TryGetLocalPath()).ToArray()!); break; default: throw new ArgumentOutOfRangeException(); } - if (SelectedPaths.Count != 0 || IsCancelingPickerAlsoTriggers) + if (SelectedPaths.Count != 0 || IsOmitCommandOnCancel is false) Command?.Execute(SelectedPaths); } catch (Exception exception) { Logger.TryGet(LogEventLevel.Error, LogArea.Control)?.Log(this, $"{exception}"); } + + return; + } + + private void UpdateSelectedPaths(IReadOnlyList newList) + { + if (newList.Count != 0 || IsClearSelectionOnCancel && newList.Count == 0) + SelectedPaths = newList; } } \ No newline at end of file