Merge pull request #533 from WCKYWCKF/pr1
Feat: add a new control PathPicker, add PathPickerDemo
This commit is contained in:
@@ -7,27 +7,84 @@
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Sandbox.Views.MainWindow"
|
||||
x:DataType="vm:MainWindowViewModel"
|
||||
xmlns:sys="using:System"
|
||||
Icon="/Assets/avalonia-logo.ico"
|
||||
Title="Sandbox">
|
||||
|
||||
<Design.DataContext>
|
||||
<!-- This only sets the DataContext for the previewer in an IDE,
|
||||
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
|
||||
<vm:MainWindowViewModel/>
|
||||
<vm:MainWindowViewModel />
|
||||
</Design.DataContext>
|
||||
|
||||
<Grid>
|
||||
<u:Form>
|
||||
<u:FormItem Label="_Numeric">
|
||||
<u:NumericIntUpDown/>
|
||||
</u:FormItem>
|
||||
<u:FormItem Label="_AnotherNumeric">
|
||||
<u:NumericIntUpDown/>
|
||||
</u:FormItem>
|
||||
<u:FormItem Label="_TextBox">
|
||||
<TextBox/>
|
||||
</u:FormItem>
|
||||
</u:Form>
|
||||
</Grid>
|
||||
|
||||
</Window>
|
||||
<Grid ColumnDefinitions="*,*"
|
||||
RowDefinitions="7*,3*">
|
||||
<ScrollViewer Grid.Column="0" Grid.Row="0">
|
||||
<u:Form LabelAlignment="Left" LabelPosition="Left" LabelWidth="*" HorizontalAlignment="Stretch">
|
||||
<TextBox Name="Title" u:FormItem.Label="Title"></TextBox>
|
||||
<TextBox Name="SuggestedFileName" u:FormItem.Label="SuggestedFileName"></TextBox>
|
||||
<TextBox Name="SuggestedStartPath" u:FormItem.Label="SuggestedStartPath"></TextBox>
|
||||
<TextBox Name="FileFilter" u:FormItem.Label="FileFilter"></TextBox>
|
||||
<TextBox Name="DefaultFileExtension" u:FormItem.Label="DefaultFileExtension"></TextBox>
|
||||
<ToggleButton Name="AllowMultiple" Content="AllowMultiple" u:FormItem.NoLabel="True"
|
||||
HorizontalAlignment="Stretch">
|
||||
</ToggleButton>
|
||||
<u:EnumSelector Name="UsePickerType" EnumType="u:UsePickerTypes" u:FormItem.Label="UsePickerType"></u:EnumSelector>
|
||||
</u:Form>
|
||||
</ScrollViewer>
|
||||
<ScrollViewer Grid.Column="0" Grid.Row="1">
|
||||
<StackPanel Spacing="2">
|
||||
<HeaderedContentControl Header="Default">
|
||||
<u:PathPicker Name="PathPicker"
|
||||
Title="{Binding #Title.Text}"
|
||||
SuggestedFileName="{Binding #SuggestedFileName.Text}"
|
||||
SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
|
||||
FileFilter="{Binding #FileFilter.Text}"
|
||||
DefaultFileExtension="{Binding #DefaultFileExtension.Text}"
|
||||
AllowMultiple="{Binding #AllowMultiple.IsChecked}"
|
||||
UsePickerType="{Binding #UsePickerType.Value}">
|
||||
</u:PathPicker>
|
||||
</HeaderedContentControl>
|
||||
<HeaderedContentControl Header="PathPickerOnlyButton">
|
||||
<u:PathPicker Name="PathPicker1"
|
||||
Theme="{DynamicResource PathPickerOnlyButton}"
|
||||
Title="{Binding #Title.Text}"
|
||||
SuggestedFileName="{Binding #SuggestedFileName.Text}"
|
||||
SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
|
||||
FileFilter="{Binding #FileFilter.Text}"
|
||||
DefaultFileExtension="{Binding #DefaultFileExtension.Text}"
|
||||
AllowMultiple="{Binding #AllowMultiple.IsChecked}"
|
||||
UsePickerType="{Binding #UsePickerType.Value}">
|
||||
</u:PathPicker>
|
||||
</HeaderedContentControl>
|
||||
<HeaderedContentControl Header="PathPickerForList">
|
||||
<u:PathPicker Name="PathPicker2"
|
||||
Theme="{DynamicResource PathPickerForList}"
|
||||
Title="{Binding #Title.Text}"
|
||||
SuggestedFileName="{Binding #SuggestedFileName.Text}"
|
||||
SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
|
||||
FileFilter="{Binding #FileFilter.Text}"
|
||||
DefaultFileExtension="{Binding #DefaultFileExtension.Text}"
|
||||
AllowMultiple="{Binding #AllowMultiple.IsChecked}"
|
||||
UsePickerType="{Binding #UsePickerType.Value}">
|
||||
</u:PathPicker>
|
||||
</HeaderedContentControl>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
<ScrollViewer Grid.Column="1" Grid.Row="0" Grid.RowSpan="2">
|
||||
<StackPanel Spacing="1">
|
||||
<HeaderedContentControl Header="SelectedPath">
|
||||
<TextBox Name="SelectedPath" u:FormItem.Label="SelectedPath" IsReadOnly="True"
|
||||
Text="{Binding #PathPicker.SelectedPath}">
|
||||
</TextBox>
|
||||
</HeaderedContentControl>
|
||||
<HeaderedContentControl Header="SelectedPaths">
|
||||
<ListBox Name="SelectedPaths"
|
||||
ItemsSource="{Binding #PathPicker.SelectedPaths}">
|
||||
</ListBox>
|
||||
</HeaderedContentControl>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
</Window>
|
||||
114
demo/Ursa.Demo/Pages/PathPickerDemo.axaml
Normal file
114
demo/Ursa.Demo/Pages/PathPickerDemo.axaml
Normal file
@@ -0,0 +1,114 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:u="https://irihi.tech/ursa"
|
||||
xmlns:vm="using:Ursa.Demo.ViewModels"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Ursa.Demo.Pages.PathPickerDemo"
|
||||
x:DataType="vm:PathPickerDemoViewModel">
|
||||
<Grid ColumnDefinitions="*,*"
|
||||
RowDefinitions="7*,3*">
|
||||
<ScrollViewer Grid.Column="0" Grid.Row="0">
|
||||
<StackPanel>
|
||||
<HeaderedContentControl Theme="{DynamicResource GroupBox}"
|
||||
Header="Functionality and Usage"
|
||||
Content="PathPicker aggregates a file selector and provides a Command property. The Command is triggered solely after opening the file selector and selecting a file, whereupon the Command receives an IReadOnlyList<string> parameter.">
|
||||
</HeaderedContentControl>
|
||||
<u:Form LabelAlignment="Left" LabelPosition="Left" LabelWidth="*" HorizontalAlignment="Stretch">
|
||||
<TextBox Name="Title" u:FormItem.Label="Title"></TextBox>
|
||||
<TextBox Name="SuggestedFileName" u:FormItem.Label="SuggestedFileName"
|
||||
Watermark="filename(not have file extension)">
|
||||
</TextBox>
|
||||
<TextBox Name="SuggestedStartPath" u:FormItem.Label="SuggestedStartPath"
|
||||
Watermark="D:\Win7 Help\win-x64">
|
||||
</TextBox>
|
||||
<TextBox Name="FileFilter" u:FormItem.Label="FileFilter"
|
||||
Watermark="[Name,Pattern] like this [123,*.exe,*.pdb] or [All][ImageAll][11,*.txt]">
|
||||
</TextBox>
|
||||
<TextBox Name="DefaultFileExtension" u:FormItem.Label="DefaultFileExtension"
|
||||
Watermark="json">
|
||||
</TextBox>
|
||||
<ToggleButton Name="AllowMultiple" Content="AllowMultiple" u:FormItem.NoLabel="True"
|
||||
HorizontalAlignment="Stretch">
|
||||
</ToggleButton>
|
||||
<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>
|
||||
<u:EnumSelector Name="UsePickerType" EnumType="u:UsePickerTypes" u:FormItem.Label="UsePickerType"></u:EnumSelector>
|
||||
</u:Form>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
<ScrollViewer Grid.Column="0" Grid.Row="1">
|
||||
<StackPanel Spacing="2">
|
||||
<HeaderedContentControl Header="Default">
|
||||
<u:PathPicker Title="{Binding #Title.Text}"
|
||||
SuggestedFileName="{Binding #SuggestedFileName.Text}"
|
||||
SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
|
||||
FileFilter="{Binding #FileFilter.Text}"
|
||||
DefaultFileExtension="{Binding #DefaultFileExtension.Text}"
|
||||
AllowMultiple="{Binding #AllowMultiple.IsChecked}"
|
||||
UsePickerType="{Binding #UsePickerType.Value}"
|
||||
SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
|
||||
SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
|
||||
Command="{Binding SelectedCommand}"
|
||||
IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}"
|
||||
IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}">
|
||||
</u:PathPicker>
|
||||
</HeaderedContentControl>
|
||||
<HeaderedContentControl Header="PathPickerOnlyButton">
|
||||
<u:PathPicker Theme="{DynamicResource PathPickerOnlyButton}"
|
||||
Title="{Binding #Title.Text}"
|
||||
SuggestedFileName="{Binding #SuggestedFileName.Text}"
|
||||
SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
|
||||
FileFilter="{Binding #FileFilter.Text}"
|
||||
DefaultFileExtension="{Binding #DefaultFileExtension.Text}"
|
||||
AllowMultiple="{Binding #AllowMultiple.IsChecked}"
|
||||
UsePickerType="{Binding #UsePickerType.Value}"
|
||||
SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
|
||||
SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
|
||||
Command="{Binding SelectedCommand}"
|
||||
IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}"
|
||||
IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}">
|
||||
</u:PathPicker>
|
||||
</HeaderedContentControl>
|
||||
<HeaderedContentControl Header="PathPickerForListView">
|
||||
<u:PathPicker Theme="{DynamicResource PathPickerForListView}"
|
||||
Title="{Binding #Title.Text}"
|
||||
SuggestedFileName="{Binding #SuggestedFileName.Text}"
|
||||
SuggestedStartPath="{Binding #SuggestedStartPath.Text}"
|
||||
FileFilter="{Binding #FileFilter.Text}"
|
||||
DefaultFileExtension="{Binding #DefaultFileExtension.Text}"
|
||||
AllowMultiple="{Binding #AllowMultiple.IsChecked}"
|
||||
UsePickerType="{Binding #UsePickerType.Value}"
|
||||
SelectedPathsText="{Binding Path,Mode=OneWayToSource}"
|
||||
SelectedPaths="{Binding Paths,Mode=OneWayToSource}"
|
||||
Command="{Binding SelectedCommand}"
|
||||
IsOmitCommandOnCancel="{Binding #IsOmitCommandOnCancel.IsChecked}"
|
||||
IsClearSelectionOnCancel="{Binding #IsClearSelectionOnCancel.IsChecked}">
|
||||
</u:PathPicker>
|
||||
</HeaderedContentControl>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
<ScrollViewer Grid.Column="1" Grid.Row="0" Grid.RowSpan="2">
|
||||
<StackPanel Spacing="1">
|
||||
<TextBlock Text="{Binding CommandTriggerCount,StringFormat='Command Trigger Count:{0}'}"></TextBlock>
|
||||
<HeaderedContentControl Header="SelectedPathsText">
|
||||
<TextBox Name="SelectedPath" u:FormItem.Label="SelectedPath" IsReadOnly="True"
|
||||
Text="{Binding Path}">
|
||||
</TextBox>
|
||||
</HeaderedContentControl>
|
||||
<HeaderedContentControl Header="SelectedPaths">
|
||||
<ListBox Name="SelectedPaths"
|
||||
ItemsSource="{Binding Paths}">
|
||||
</ListBox>
|
||||
</HeaderedContentControl>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
13
demo/Ursa.Demo/Pages/PathPickerDemo.axaml.cs
Normal file
13
demo/Ursa.Demo/Pages/PathPickerDemo.axaml.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace Ursa.Demo.Pages;
|
||||
|
||||
public partial class PathPickerDemo : UserControl
|
||||
{
|
||||
public PathPickerDemo()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Avalonia;
|
||||
using Avalonia.Styling;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Themes.Semi;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
@@ -79,6 +80,7 @@ public partial class MainViewViewModel : ViewModelBase
|
||||
MenuKeys.MenuKeyTreeComboBox => new TreeComboBoxDemoViewModel(),
|
||||
MenuKeys.MenuKeyTwoTonePathIcon => new TwoTonePathIconDemoViewModel(),
|
||||
MenuKeys.AspectRatioLayout => new AspectRatioLayoutDemoViewModel(),
|
||||
MenuKeys.PathPicker => new PathPickerDemoViewModel(),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(s), s, null)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -59,7 +59,8 @@ public class MenuViewModel : ViewModelBase
|
||||
new() { MenuHeader = "ToolBar", Key = MenuKeys.MenuKeyToolBar },
|
||||
new() { MenuHeader = "TreeComboBox", Key = MenuKeys.MenuKeyTreeComboBox },
|
||||
new() { MenuHeader = "TwoTonePathIcon", Key = MenuKeys.MenuKeyTwoTonePathIcon },
|
||||
new() { MenuHeader = "AspectRatioLayout", Key = MenuKeys.AspectRatioLayout ,Status = "WIP"},
|
||||
new() { MenuHeader = "AspectRatioLayout", Key = MenuKeys.AspectRatioLayout, Status = "New" },
|
||||
new() { MenuHeader = "PathPicker", Key = MenuKeys.PathPicker, Status = "WIP" },
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -115,4 +116,5 @@ public static class MenuKeys
|
||||
public const string MenuKeyTreeComboBox = "TreeComboBox";
|
||||
public const string MenuKeyTwoTonePathIcon = "TwoTonePathIcon";
|
||||
public const string AspectRatioLayout = "AspectRatioLayout";
|
||||
public const string PathPicker = "PathPicker";
|
||||
}
|
||||
18
demo/Ursa.Demo/ViewModels/PathPickerDemoViewModel.cs
Normal file
18
demo/Ursa.Demo/ViewModels/PathPickerDemoViewModel.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
public partial class PathPickerDemoViewModel : ViewModelBase
|
||||
{
|
||||
[ObservableProperty] private string? _path;
|
||||
[ObservableProperty] private IReadOnlyList<string>? _paths;
|
||||
[ObservableProperty] private int _commandTriggerCount = 0;
|
||||
|
||||
[RelayCommand]
|
||||
private void Selected(IReadOnlyList<string> paths)
|
||||
{
|
||||
CommandTriggerCount++;
|
||||
}
|
||||
}
|
||||
@@ -14,14 +14,14 @@
|
||||
|
||||
这个是一个Ursa拓展包。这个包整合并互相兼容了UrsaWindow和UrsaView与Avalonia.ReactiveUI的功能。【Avalonia.ReactiveUI参见:https://docs.avaloniaui.net/docs/concepts/reactiveui/】
|
||||
</Description>
|
||||
<Version>1.0.1</Version>
|
||||
<Version>1.0.2</Version>
|
||||
<RepositoryUrl>https://github.com/irihitech/Ursa.Avalonia</RepositoryUrl>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia.ReactiveUI" Version="11.1.1"/>
|
||||
<PackageReference Include="Avalonia.ReactiveUI" Version="$(AvaloniaVersion)"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
83
src/Ursa.Themes.Semi/Controls/PathPicker.axaml
Normal file
83
src/Ursa.Themes.Semi/Controls/PathPicker.axaml
Normal file
@@ -0,0 +1,83 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:ursa="https://irihi.tech/ursa">
|
||||
<ControlTheme x:Key="{x:Type ursa:PathPicker}" TargetType="ursa:PathPicker">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate>
|
||||
<DockPanel HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalAlignment}">
|
||||
<Button Name="PART_Button"
|
||||
DockPanel.Dock="Right"
|
||||
Content="{TemplateBinding Title}"
|
||||
Margin="1,0,0,0">
|
||||
</Button>
|
||||
<TextBox Name="PART_TextBox"
|
||||
DockPanel.Dock="Left"
|
||||
AcceptsReturn="{TemplateBinding AllowMultiple}"
|
||||
Text="{TemplateBinding SelectedPathsText,Mode=TwoWay}">
|
||||
</TextBox>
|
||||
</DockPanel>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
|
||||
<Style Selector="^[AllowMultiple=False]">
|
||||
<Style Selector="^ /template/ Button#PART_Button">
|
||||
<Setter Property="DockPanel.Dock" Value="Right"></Setter>
|
||||
</Style>
|
||||
<Style Selector="^ /template/ TextBox#PART_TextBox">
|
||||
<Setter Property="DockPanel.Dock" Value="Left"></Setter>
|
||||
</Style>
|
||||
</Style>
|
||||
<Style Selector="^[AllowMultiple=True]">
|
||||
<Style Selector="^ /template/ Button#PART_Button">
|
||||
<Setter Property="DockPanel.Dock" Value="Top"></Setter>
|
||||
</Style>
|
||||
<Style Selector="^ /template/ TextBox#PART_TextBox">
|
||||
<Setter Property="DockPanel.Dock" Value="Bottom"></Setter>
|
||||
</Style>
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
|
||||
|
||||
<ControlTheme x:Key="PathPickerOnlyButton" TargetType="ursa:PathPicker">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate>
|
||||
<Button Name="PART_Button"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalAlignment}"
|
||||
Content="{TemplateBinding Title}">
|
||||
</Button>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
</ControlTheme>
|
||||
|
||||
<ControlTheme x:Key="PathPickerForListView" TargetType="ursa:PathPicker">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate>
|
||||
<Expander HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalAlignment}">
|
||||
<Expander.Header>
|
||||
<Button Name="PART_Button"
|
||||
HorizontalAlignment="Stretch"
|
||||
Content="{TemplateBinding Title}">
|
||||
<Button.Theme>
|
||||
<ControlTheme TargetType="Button">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate>
|
||||
<TextPresenter Text="{TemplateBinding Content}"
|
||||
Background="Transparent"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Stretch">
|
||||
</TextPresenter>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
</ControlTheme>
|
||||
</Button.Theme>
|
||||
</Button>
|
||||
</Expander.Header>
|
||||
<ListBox ItemsSource="{TemplateBinding SelectedPaths}"></ListBox>
|
||||
</Expander>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
</ControlTheme>
|
||||
</ResourceDictionary>
|
||||
@@ -55,5 +55,6 @@
|
||||
<ResourceInclude Source="UrsaView.axaml" />
|
||||
<ResourceInclude Source="UrsaWindow.axaml"/>
|
||||
<ResourceInclude Source="PinCode.axaml" />
|
||||
<ResourceInclude Source="PathPicker.axaml"/>
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
|
||||
310
src/Ursa/Controls/PathPicker/PathPicker.cs
Normal file
310
src/Ursa/Controls/PathPicker/PathPicker.cs
Normal file
@@ -0,0 +1,310 @@
|
||||
using System.Text;
|
||||
using System.Windows.Input;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Logging;
|
||||
using Avalonia.Platform.Storage;
|
||||
using Avalonia.Threading;
|
||||
using Irihi.Avalonia.Shared.Common;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[TemplatePart(Name = "PART_Button", Type = typeof(Button))]
|
||||
public class PathPicker : TemplatedControl
|
||||
{
|
||||
public static readonly StyledProperty<string> SuggestedStartPathProperty =
|
||||
AvaloniaProperty.Register<PathPicker, string>(
|
||||
nameof(SuggestedStartPath), string.Empty);
|
||||
|
||||
public static readonly StyledProperty<UsePickerTypes> UsePickerTypeProperty =
|
||||
AvaloniaProperty.Register<PathPicker, UsePickerTypes>(
|
||||
nameof(UsePickerType));
|
||||
|
||||
public static readonly StyledProperty<string> SuggestedFileNameProperty =
|
||||
AvaloniaProperty.Register<PathPicker, string>(
|
||||
nameof(SuggestedFileName), string.Empty);
|
||||
|
||||
public static readonly StyledProperty<string> FileFilterProperty = AvaloniaProperty.Register<PathPicker, string>(
|
||||
nameof(FileFilter), string.Empty);
|
||||
|
||||
public static readonly StyledProperty<string> TitleProperty = AvaloniaProperty.Register<PathPicker, string>(
|
||||
nameof(Title), string.Empty);
|
||||
|
||||
public static readonly StyledProperty<string> DefaultFileExtensionProperty =
|
||||
AvaloniaProperty.Register<PathPicker, string>(
|
||||
nameof(DefaultFileExtension), string.Empty);
|
||||
|
||||
public static readonly DirectProperty<PathPicker, IReadOnlyList<string>> SelectedPathsProperty =
|
||||
AvaloniaProperty.RegisterDirect<PathPicker, IReadOnlyList<string>>(
|
||||
nameof(SelectedPaths), o => o.SelectedPaths, (o, v) => o.SelectedPaths = v);
|
||||
|
||||
public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<PathPicker, ICommand?>(
|
||||
nameof(Command));
|
||||
|
||||
public static readonly StyledProperty<bool> AllowMultipleProperty = AvaloniaProperty.Register<PathPicker, bool>(
|
||||
nameof(AllowMultiple));
|
||||
|
||||
public static readonly StyledProperty<string?> SelectedPathsTextProperty =
|
||||
AvaloniaProperty.Register<PathPicker, string?>(
|
||||
nameof(SelectedPathsText), defaultBindingMode: BindingMode.TwoWay);
|
||||
|
||||
public static readonly StyledProperty<bool> IsOmitCommandOnCancelProperty =
|
||||
AvaloniaProperty.Register<PathPicker, bool>(
|
||||
nameof(IsOmitCommandOnCancel));
|
||||
|
||||
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(IsOmitCommandOnCancelProperty);
|
||||
set => SetValue(IsOmitCommandOnCancelProperty, value);
|
||||
}
|
||||
|
||||
public string? SelectedPathsText
|
||||
{
|
||||
get => GetValue(SelectedPathsTextProperty);
|
||||
set => SetValue(SelectedPathsTextProperty, value);
|
||||
}
|
||||
|
||||
private Button? _button;
|
||||
|
||||
private IReadOnlyList<string> _selectedPaths = [];
|
||||
|
||||
public bool AllowMultiple
|
||||
{
|
||||
get => GetValue(AllowMultipleProperty);
|
||||
set => SetValue(AllowMultipleProperty, value);
|
||||
}
|
||||
|
||||
public ICommand? Command
|
||||
{
|
||||
get => GetValue(CommandProperty);
|
||||
set => SetValue(CommandProperty, value);
|
||||
}
|
||||
|
||||
public IReadOnlyList<string> SelectedPaths
|
||||
{
|
||||
get => _selectedPaths;
|
||||
private set => SetAndRaise(SelectedPathsProperty, ref _selectedPaths, value);
|
||||
}
|
||||
|
||||
public string SuggestedFileName
|
||||
{
|
||||
get => GetValue(SuggestedFileNameProperty);
|
||||
set => SetValue(SuggestedFileNameProperty, value);
|
||||
}
|
||||
|
||||
public string DefaultFileExtension
|
||||
{
|
||||
get => GetValue(DefaultFileExtensionProperty);
|
||||
set => SetValue(DefaultFileExtensionProperty, value);
|
||||
}
|
||||
|
||||
|
||||
public string Title
|
||||
{
|
||||
get => GetValue(TitleProperty);
|
||||
set => SetValue(TitleProperty, value);
|
||||
}
|
||||
|
||||
public string FileFilter
|
||||
{
|
||||
get => GetValue(FileFilterProperty);
|
||||
set => SetValue(FileFilterProperty, value);
|
||||
}
|
||||
|
||||
public UsePickerTypes UsePickerType
|
||||
{
|
||||
get => GetValue(UsePickerTypeProperty);
|
||||
set => SetValue(UsePickerTypeProperty, value);
|
||||
}
|
||||
|
||||
public string SuggestedStartPath
|
||||
{
|
||||
get => GetValue(SuggestedStartPathProperty);
|
||||
set => SetValue(SuggestedStartPathProperty, value);
|
||||
}
|
||||
|
||||
private bool _twoConvertLock;
|
||||
|
||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
||||
{
|
||||
base.OnPropertyChanged(change);
|
||||
if (_twoConvertLock) return;
|
||||
if (change.Property == SelectedPathsProperty)
|
||||
{
|
||||
_twoConvertLock = true;
|
||||
var stringBuilder = new StringBuilder();
|
||||
if (SelectedPaths.Count != 0)
|
||||
{
|
||||
stringBuilder.Append(SelectedPaths[0]);
|
||||
foreach (var item in SelectedPaths.Skip(1))
|
||||
{
|
||||
stringBuilder.AppendLine(item);
|
||||
}
|
||||
}
|
||||
|
||||
SelectedPathsText = stringBuilder.ToString();
|
||||
_twoConvertLock = false;
|
||||
}
|
||||
|
||||
if (change.Property == SelectedPathsTextProperty)
|
||||
{
|
||||
_twoConvertLock = true;
|
||||
string[] separatedStrings = ["\r", "\n", "\r\n"];
|
||||
// var list = SelectedPathsText?.Split(separatedStrings, StringSplitOptions.RemoveEmptyEntries)
|
||||
// .Select(RemoveNewLine).ToArray()
|
||||
// ?? [];
|
||||
// if (list.Length == SelectedPaths.Count)
|
||||
// {
|
||||
// if (SelectedPaths.Select(x => list.Any(y => x == y)).All(x => x is false))
|
||||
// }
|
||||
|
||||
SelectedPaths = SelectedPathsText?.Split(separatedStrings, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(RemoveNewLine).ToArray()
|
||||
?? [];
|
||||
_twoConvertLock = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
Button.ClickEvent.RemoveHandler(LaunchPicker, _button);
|
||||
_button = e.NameScope.Find<Button>("PART_Button");
|
||||
Button.ClickEvent.AddHandler(LaunchPicker, _button);
|
||||
}
|
||||
|
||||
|
||||
private static string RemoveNewLine(string str)
|
||||
{
|
||||
return str.Replace("\r", "")
|
||||
.Replace("\n", "")
|
||||
.Replace("\r\n", "")
|
||||
.Replace(Environment.NewLine, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* FilePickerFileTypeName,Pattern,Pattern,Pattern...
|
||||
*/
|
||||
private static FilePickerFileType ParseFilePickerType(string str)
|
||||
{
|
||||
return str switch
|
||||
{
|
||||
nameof(FilePickerFileTypes.All) => FilePickerFileTypes.All,
|
||||
nameof(FilePickerFileTypes.Pdf) => FilePickerFileTypes.Pdf,
|
||||
nameof(FilePickerFileTypes.ImageAll) => FilePickerFileTypes.ImageAll,
|
||||
nameof(FilePickerFileTypes.ImageJpg) => FilePickerFileTypes.ImageJpg,
|
||||
nameof(FilePickerFileTypes.ImagePng) => FilePickerFileTypes.ImagePng,
|
||||
nameof(FilePickerFileTypes.ImageWebp) => FilePickerFileTypes.ImageWebp,
|
||||
nameof(FilePickerFileTypes.TextPlain) => FilePickerFileTypes.TextPlain,
|
||||
_ => parse()
|
||||
};
|
||||
|
||||
FilePickerFileType parse()
|
||||
{
|
||||
var list = str.Split(',');
|
||||
return new FilePickerFileType(list.First())
|
||||
{
|
||||
Patterns = list.Skip(1).Select(x => x.Trim()).ToArray()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [ParseFilePickerTypeStr][ParseFilePickerTypeStr]...
|
||||
*/
|
||||
private static IReadOnlyList<FilePickerFileType>? ParseFileTypes(string str)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str)) return null;
|
||||
string[] separatedStrings = ["[", "][", "]"];
|
||||
var list = RemoveNewLine(str)
|
||||
.Replace(" ", string.Empty)
|
||||
.Split(separatedStrings, StringSplitOptions.RemoveEmptyEntries);
|
||||
return list.Select(ParseFilePickerType).ToArray();
|
||||
}
|
||||
|
||||
private async void LaunchPicker(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (TopLevel.GetTopLevel(this)?.StorageProvider is not { } storageProvider) return;
|
||||
|
||||
switch (UsePickerType)
|
||||
{
|
||||
case UsePickerTypes.OpenFile:
|
||||
FilePickerOpenOptions filePickerOpenOptions = new()
|
||||
{
|
||||
Title = Title,
|
||||
AllowMultiple = AllowMultiple,
|
||||
SuggestedStartLocation =
|
||||
await storageProvider.TryGetFolderFromPathAsync(SuggestedStartPath),
|
||||
FileTypeFilter = ParseFileTypes(FileFilter)
|
||||
};
|
||||
var resFiles = await storageProvider.OpenFilePickerAsync(filePickerOpenOptions);
|
||||
UpdateSelectedPaths(resFiles.Select(x => x.TryGetLocalPath()).ToArray()!);
|
||||
break;
|
||||
case UsePickerTypes.SaveFile:
|
||||
FilePickerSaveOptions filePickerSaveOptions = new()
|
||||
{
|
||||
Title = Title,
|
||||
SuggestedStartLocation =
|
||||
await storageProvider.TryGetFolderFromPathAsync(SuggestedStartPath),
|
||||
SuggestedFileName = SuggestedFileName,
|
||||
FileTypeChoices = ParseFileTypes(FileFilter),
|
||||
DefaultExtension = DefaultFileExtension
|
||||
};
|
||||
|
||||
var path = (await storageProvider.SaveFilePickerAsync(filePickerSaveOptions))
|
||||
?.TryGetLocalPath();
|
||||
UpdateSelectedPaths(string.IsNullOrEmpty(path)
|
||||
? Array.Empty<string>()
|
||||
: [path!]);
|
||||
break;
|
||||
case UsePickerTypes.OpenFolder:
|
||||
FolderPickerOpenOptions folderPickerOpenOptions = new()
|
||||
{
|
||||
Title = Title,
|
||||
AllowMultiple = AllowMultiple,
|
||||
SuggestedStartLocation =
|
||||
await storageProvider.TryGetFolderFromPathAsync(SuggestedStartPath),
|
||||
SuggestedFileName = SuggestedFileName
|
||||
};
|
||||
var resFolder = await storageProvider.OpenFolderPickerAsync(folderPickerOpenOptions);
|
||||
UpdateSelectedPaths(resFolder.Select(x => x.TryGetLocalPath()).ToArray()!);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
8
src/Ursa/Controls/PathPicker/UsePickerTypes.cs
Normal file
8
src/Ursa/Controls/PathPicker/UsePickerTypes.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public enum UsePickerTypes
|
||||
{
|
||||
OpenFile,
|
||||
SaveFile,
|
||||
OpenFolder
|
||||
}
|
||||
Reference in New Issue
Block a user