Skip to content

Commit

Permalink
change:IsCancelingPickerAlsoTriggers ->OmitCommandOnCancel
Browse files Browse the repository at this point in the history
feat:add property ClearSelectionOnCancel. support for not clearing the selection if the file picker is canceled.
  • Loading branch information
WCKYWCKF committed Jan 15, 2025
1 parent c5824a3 commit ead8591
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
15 changes: 11 additions & 4 deletions demo/Ursa.Demo/Pages/PathPickerDemo.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
<ToggleButton Name="AllowMultiple" Content="AllowMultiple" u:FormItem.NoLabel="True"
HorizontalAlignment="Stretch">
</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"
HorizontalAlignment="Stretch">
</ToggleButton>
Expand All @@ -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}">
</u:PathPicker>
</HeaderedContentControl>
<HeaderedContentControl Header="PathPickerOnlyButton">
Expand All @@ -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}">
</u:PathPicker>
</HeaderedContentControl>
<HeaderedContentControl Header="PathPickerForListView">
Expand All @@ -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}">
</u:PathPicker>
</HeaderedContentControl>
</StackPanel>
Expand Down
38 changes: 28 additions & 10 deletions src/Ursa/Controls/PathPicker/PathPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ public class PathPicker : TemplatedControl
AvaloniaProperty.Register<PathPicker, string?>(
nameof(SelectedPathsText), defaultBindingMode: BindingMode.TwoWay);

public static readonly StyledProperty<bool> IsCancelingPickerAlsoTriggersProperty =
public static readonly StyledProperty<bool> IsOmitCommandOnCancelProperty =
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(IsClearSelectionOnCancelProperty);
set => SetValue(IsClearSelectionOnCancelProperty, value);
}

public bool IsOmitCommandOnCancel
{
get => GetValue(IsCancelingPickerAlsoTriggersProperty);
set => SetValue(IsCancelingPickerAlsoTriggersProperty, value);
get => GetValue(IsOmitCommandOnCancelProperty);
set => SetValue(IsOmitCommandOnCancelProperty, value);
}

public string? SelectedPathsText
Expand Down Expand Up @@ -246,7 +256,7 @@ await storageProvider.TryGetFolderFromPathAsync(SuggestedStartPath),
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()
Expand All @@ -261,9 +271,9 @@ await storageProvider.TryGetFolderFromPathAsync(SuggestedStartPath),

var path = (await storageProvider.SaveFilePickerAsync(filePickerSaveOptions))
?.TryGetLocalPath();
SelectedPaths = string.IsNullOrEmpty(path)
UpdateSelectedPaths(string.IsNullOrEmpty(path)
? Array.Empty<string>()
: [path!];
: [path!]);
break;
case UsePickerTypes.OpenFolder:
FolderPickerOpenOptions folderPickerOpenOptions = new()
Expand All @@ -275,18 +285,26 @@ await storageProvider.TryGetFolderFromPathAsync(SuggestedStartPath),
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<string> newList)
{
if (newList.Count != 0 || IsClearSelectionOnCancel && newList.Count == 0)
SelectedPaths = newList;
}
}

0 comments on commit ead8591

Please sign in to comment.