change:IsCancelingPickerAlsoTriggers ->OmitCommandOnCancel

feat:add property ClearSelectionOnCancel. support for not clearing the selection if the file picker is canceled.
This commit is contained in:
望尘空忧
2025-01-15 17:00:16 +08:00
parent c5824a3ddc
commit ead8591bfb
2 changed files with 39 additions and 14 deletions

View File

@@ -32,7 +32,11 @@
<ToggleButton Name="AllowMultiple" Content="AllowMultiple" u:FormItem.NoLabel="True" <ToggleButton Name="AllowMultiple" Content="AllowMultiple" u:FormItem.NoLabel="True"
HorizontalAlignment="Stretch"> HorizontalAlignment="Stretch">
</ToggleButton> </ToggleButton>
<ToggleButton Name="IsCancelingPickerAlsoTriggers" Content="Canceling selection also triggers the Command." <ToggleButton Name="IsOmitCommandOnCancel" Content="Do not trigger the command after unselecting."
u:FormItem.NoLabel="True"
HorizontalAlignment="Stretch">
</ToggleButton>
<ToggleButton Name="IsClearSelectionOnCancel" Content="Clear the selection when unselecting."
u:FormItem.NoLabel="True" u:FormItem.NoLabel="True"
HorizontalAlignment="Stretch"> HorizontalAlignment="Stretch">
</ToggleButton> </ToggleButton>
@@ -53,7 +57,8 @@
SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
SelectedPaths="{Binding Paths,Mode=OneWayToSource}" SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
Command="{Binding SelectedCommand}" Command="{Binding SelectedCommand}"
IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}"> IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}"
IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}">
</u:PathPicker> </u:PathPicker>
</HeaderedContentControl> </HeaderedContentControl>
<HeaderedContentControl Header="PathPickerOnlyButton"> <HeaderedContentControl Header="PathPickerOnlyButton">
@@ -68,7 +73,8 @@
SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
SelectedPaths="{Binding Paths,Mode=OneWayToSource}" SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
Command="{Binding SelectedCommand}" Command="{Binding SelectedCommand}"
IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}"> IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}"
IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}">
</u:PathPicker> </u:PathPicker>
</HeaderedContentControl> </HeaderedContentControl>
<HeaderedContentControl Header="PathPickerForListView"> <HeaderedContentControl Header="PathPickerForListView">
@@ -83,7 +89,8 @@
SelectedPathsText="{Binding Path,Mode=OneWayToSource}" SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
SelectedPaths="{Binding Paths,Mode=OneWayToSource}" SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
Command="{Binding SelectedCommand}" Command="{Binding SelectedCommand}"
IsCancelingPickerAlsoTriggers="{Binding #IsCancelingPickerAlsoTriggers.IsChecked}"> IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}"
IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}">
</u:PathPicker> </u:PathPicker>
</HeaderedContentControl> </HeaderedContentControl>
</StackPanel> </StackPanel>

View File

@@ -54,14 +54,24 @@ public class PathPicker : TemplatedControl
AvaloniaProperty.Register<PathPicker, string?>( AvaloniaProperty.Register<PathPicker, string?>(
nameof(SelectedPathsText), defaultBindingMode: BindingMode.TwoWay); nameof(SelectedPathsText), defaultBindingMode: BindingMode.TwoWay);
public static readonly StyledProperty<bool> IsCancelingPickerAlsoTriggersProperty = public static readonly StyledProperty<bool> IsOmitCommandOnCancelProperty =
AvaloniaProperty.Register<PathPicker, bool>( AvaloniaProperty.Register<PathPicker, bool>(
nameof(IsCancelingPickerAlsoTriggers)); nameof(IsOmitCommandOnCancel));
public bool IsCancelingPickerAlsoTriggers public static readonly StyledProperty<bool> IsClearSelectionOnCancelProperty =
AvaloniaProperty.Register<PathPicker, bool>(
nameof(IsClearSelectionOnCancel));
public bool IsClearSelectionOnCancel
{ {
get => GetValue(IsCancelingPickerAlsoTriggersProperty); get => GetValue(IsClearSelectionOnCancelProperty);
set => SetValue(IsCancelingPickerAlsoTriggersProperty, value); set => SetValue(IsClearSelectionOnCancelProperty, value);
}
public bool IsOmitCommandOnCancel
{
get => GetValue(IsOmitCommandOnCancelProperty);
set => SetValue(IsOmitCommandOnCancelProperty, value);
} }
public string? SelectedPathsText public string? SelectedPathsText
@@ -246,7 +256,7 @@ public class PathPicker : TemplatedControl
FileTypeFilter = ParseFileTypes(FileFilter) FileTypeFilter = ParseFileTypes(FileFilter)
}; };
var resFiles = await storageProvider.OpenFilePickerAsync(filePickerOpenOptions); var resFiles = await storageProvider.OpenFilePickerAsync(filePickerOpenOptions);
SelectedPaths = resFiles.Select(x => x.TryGetLocalPath()).ToArray()!; UpdateSelectedPaths(resFiles.Select(x => x.TryGetLocalPath()).ToArray()!);
break; break;
case UsePickerTypes.SaveFile: case UsePickerTypes.SaveFile:
FilePickerSaveOptions filePickerSaveOptions = new() FilePickerSaveOptions filePickerSaveOptions = new()
@@ -261,9 +271,9 @@ public class PathPicker : TemplatedControl
var path = (await storageProvider.SaveFilePickerAsync(filePickerSaveOptions)) var path = (await storageProvider.SaveFilePickerAsync(filePickerSaveOptions))
?.TryGetLocalPath(); ?.TryGetLocalPath();
SelectedPaths = string.IsNullOrEmpty(path) UpdateSelectedPaths(string.IsNullOrEmpty(path)
? Array.Empty<string>() ? Array.Empty<string>()
: [path!]; : [path!]);
break; break;
case UsePickerTypes.OpenFolder: case UsePickerTypes.OpenFolder:
FolderPickerOpenOptions folderPickerOpenOptions = new() FolderPickerOpenOptions folderPickerOpenOptions = new()
@@ -275,18 +285,26 @@ public class PathPicker : TemplatedControl
SuggestedFileName = SuggestedFileName SuggestedFileName = SuggestedFileName
}; };
var resFolder = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions); var resFolder = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions);
SelectedPaths = resFolder.Select(x => x.TryGetLocalPath()).ToArray()!; UpdateSelectedPaths(resFolder.Select(x => x.TryGetLocalPath()).ToArray()!);
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
if (SelectedPaths.Count != 0 || IsCancelingPickerAlsoTriggers) if (SelectedPaths.Count != 0 || IsOmitCommandOnCancel is false)
Command?.Execute(SelectedPaths); Command?.Execute(SelectedPaths);
} }
catch (Exception exception) catch (Exception exception)
{ {
Logger.TryGet(LogEventLevel.Error, LogArea.Control)?.Log(this, $"{exception}"); Logger.TryGet(LogEventLevel.Error, LogArea.Control)?.Log(this, $"{exception}");
} }
return;
}
private void UpdateSelectedPaths(IReadOnlyList<string> newList)
{
if (newList.Count != 0 || IsClearSelectionOnCancel && newList.Count == 0)
SelectedPaths = newList;
} }
} }