Fix directory select controls
This commit is contained in:
parent
e64a9d2adf
commit
05c454dce4
@ -5,29 +5,30 @@
|
|||||||
xmlns:controls="clr-namespace:LibationAvalonia.Controls"
|
xmlns:controls="clr-namespace:LibationAvalonia.Controls"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="LibationAvalonia.Controls.DirectoryOrCustomSelectControl">
|
x:Class="LibationAvalonia.Controls.DirectoryOrCustomSelectControl">
|
||||||
<Grid ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto">
|
|
||||||
<controls:DirectorySelectControl
|
<Grid ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto" Name="grid">
|
||||||
Grid.Column="1"
|
<controls:DirectorySelectControl
|
||||||
Grid.Row="0"
|
Grid.Column="1"
|
||||||
Name="directorySelectControl"
|
Grid.Row="0"
|
||||||
SubDirectory="{Binding $parent.SubDirectory}"
|
IsEnabled="{Binding KnownChecked}"
|
||||||
KnownDirectories="{Binding $parent.KnownDirectories}" />
|
SelectedDirectory="{Binding SelectedDirectory, Mode=TwoWay}"
|
||||||
|
SubDirectory="{Binding $parent[1].SubDirectory}"
|
||||||
|
KnownDirectories="{Binding $parent[1].KnownDirectories}" />
|
||||||
|
|
||||||
<RadioButton
|
<RadioButton
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
Name="knownDirRadio"
|
IsChecked="{Binding KnownChecked, Mode=TwoWay}"/>
|
||||||
IsChecked="{Binding KnownChecked, Mode=TwoWay}" />
|
|
||||||
|
|
||||||
<RadioButton
|
<RadioButton
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Grid.Row="1"
|
Grid.Row="1"
|
||||||
Name="customDirRadio"
|
IsChecked="{Binding CustomChecked, Mode=TwoWay}"/>
|
||||||
IsChecked="{Binding CustomChecked, Mode=TwoWay}" />
|
|
||||||
|
|
||||||
<Grid Grid.Column="1" Grid.Row="1" ColumnDefinitions="*,Auto">
|
<Grid Grid.Column="1" Grid.Row="1" ColumnDefinitions="*,Auto"
|
||||||
<TextBox IsEnabled="{Binding CustomChecked}" Name="customDirTbox" Grid.Column="0" IsReadOnly="True" Text="{Binding CustomDir, Mode=TwoWay}" />
|
IsEnabled="{Binding CustomChecked}">
|
||||||
<Button Name="customDirBrowseBtn" Grid.Column="1" Content="..." Margin="5,0,0,0" Padding="10,0,10,0" VerticalAlignment="Stretch" />
|
<TextBox Grid.Column="0" IsReadOnly="True" Text="{Binding CustomDir, Mode=TwoWay}" />
|
||||||
|
<Button Grid.Column="1" Content="..." Margin="5,0,0,0" Padding="10,0,10,0" Click="CustomDirBrowseBtn_Click" VerticalAlignment="Stretch" />
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@ -1,10 +1,16 @@
|
|||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Data;
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
using Dinah.Core;
|
using Dinah.Core;
|
||||||
using LibationFileManager;
|
using LibationFileManager;
|
||||||
using ReactiveUI;
|
using ReactiveUI;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reactive.Subjects;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace LibationAvalonia.Controls
|
namespace LibationAvalonia.Controls
|
||||||
{
|
{
|
||||||
@ -36,55 +42,49 @@ namespace LibationAvalonia.Controls
|
|||||||
get => GetValue(SubDirectoryProperty);
|
get => GetValue(SubDirectoryProperty);
|
||||||
set => SetValue(SubDirectoryProperty, value);
|
set => SetValue(SubDirectoryProperty, value);
|
||||||
}
|
}
|
||||||
CustomState customStates = new();
|
|
||||||
|
private readonly DirectoryState directoryState = new();
|
||||||
|
|
||||||
public DirectoryOrCustomSelectControl()
|
public DirectoryOrCustomSelectControl()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
customDirBrowseBtn = this.Find<Button>(nameof(customDirBrowseBtn));
|
grid.DataContext = directoryState;
|
||||||
directorySelectControl = this.Find<DirectorySelectControl>(nameof(directorySelectControl));
|
|
||||||
|
|
||||||
this.Find<TextBox>(nameof(customDirTbox)).DataContext = customStates;
|
directoryState.PropertyChanged += DirectoryState_PropertyChanged;
|
||||||
this.Find<RadioButton>(nameof(knownDirRadio)).DataContext = customStates;
|
|
||||||
this.Find<RadioButton>(nameof(customDirRadio)).DataContext = customStates;
|
|
||||||
|
|
||||||
customStates.PropertyChanged += CheckStates_PropertyChanged;
|
|
||||||
customDirBrowseBtn.Click += CustomDirBrowseBtn_Click;
|
|
||||||
PropertyChanged += DirectoryOrCustomSelectControl_PropertyChanged;
|
PropertyChanged += DirectoryOrCustomSelectControl_PropertyChanged;
|
||||||
directorySelectControl.PropertyChanged += DirectorySelectControl_PropertyChanged;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class CustomState : ViewModels.ViewModelBase
|
private void DirectoryState_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.PropertyName is nameof(DirectoryState.SelectedDirectory) or nameof(DirectoryState.KnownChecked) &&
|
||||||
|
directoryState.KnownChecked &&
|
||||||
|
directoryState.SelectedDirectory is Configuration.KnownDirectories kdir &&
|
||||||
|
kdir is not Configuration.KnownDirectories.None)
|
||||||
|
{
|
||||||
|
Directory = kdir is Configuration.KnownDirectories.AppDir ? Configuration.AppDir_Absolute : Configuration.GetKnownDirectoryPath(kdir);
|
||||||
|
}
|
||||||
|
else if (e.PropertyName is nameof(DirectoryState.CustomDir) or nameof(DirectoryState.CustomChecked) &&
|
||||||
|
directoryState.CustomChecked &&
|
||||||
|
directoryState.CustomDir is not null)
|
||||||
|
{
|
||||||
|
Directory = directoryState.CustomDir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DirectoryState : ViewModels.ViewModelBase
|
||||||
{
|
{
|
||||||
private string _customDir;
|
private string _customDir;
|
||||||
|
private string _subDirectory;
|
||||||
private bool _knownChecked;
|
private bool _knownChecked;
|
||||||
private bool _customChecked;
|
private bool _customChecked;
|
||||||
|
private Configuration.KnownDirectories? _selectedDirectory;
|
||||||
public string CustomDir { get => _customDir; set => this.RaiseAndSetIfChanged(ref _customDir, value); }
|
public string CustomDir { get => _customDir; set => this.RaiseAndSetIfChanged(ref _customDir, value); }
|
||||||
public bool KnownChecked
|
public string SubDirectory { get => _subDirectory; set => this.RaiseAndSetIfChanged(ref _subDirectory, value); }
|
||||||
{
|
public bool KnownChecked { get => _knownChecked; set => this.RaiseAndSetIfChanged(ref _knownChecked, value); }
|
||||||
get => _knownChecked;
|
public bool CustomChecked { get => _customChecked; set => this.RaiseAndSetIfChanged(ref _customChecked, value); }
|
||||||
set
|
|
||||||
{
|
public Configuration.KnownDirectories? SelectedDirectory { get => _selectedDirectory; set => this.RaiseAndSetIfChanged(ref _selectedDirectory, value); }
|
||||||
this.RaiseAndSetIfChanged(ref _knownChecked, value);
|
|
||||||
if (value)
|
|
||||||
CustomChecked = false;
|
|
||||||
else if (!CustomChecked)
|
|
||||||
CustomChecked = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public bool CustomChecked
|
|
||||||
{
|
|
||||||
get => _customChecked;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
this.RaiseAndSetIfChanged(ref _customChecked, value);
|
|
||||||
if (value)
|
|
||||||
KnownChecked = false;
|
|
||||||
else if (!KnownChecked)
|
|
||||||
KnownChecked = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void CustomDirBrowseBtn_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
private async void CustomDirBrowseBtn_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||||
@ -96,43 +96,12 @@ namespace LibationAvalonia.Controls
|
|||||||
|
|
||||||
var selectedFolders = await (VisualRoot as Window).StorageProvider.OpenFolderPickerAsync(options);
|
var selectedFolders = await (VisualRoot as Window).StorageProvider.OpenFolderPickerAsync(options);
|
||||||
|
|
||||||
customStates.CustomDir = selectedFolders.SingleOrDefault()?.Path?.LocalPath ?? customStates.CustomDir;
|
directoryState.CustomDir = selectedFolders.SingleOrDefault()?.Path?.LocalPath ?? directoryState.CustomDir;
|
||||||
}
|
|
||||||
|
|
||||||
private void CheckStates_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.PropertyName != nameof(CustomState.CustomDir))
|
|
||||||
{
|
|
||||||
directorySelectControl.IsEnabled = !customStates.CustomChecked;
|
|
||||||
customDirBrowseBtn.IsEnabled = customStates.CustomChecked;
|
|
||||||
}
|
|
||||||
|
|
||||||
setDirectory();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void DirectorySelectControl_PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.Property.Name == nameof(DirectorySelectControl.SelectedDirectory))
|
|
||||||
{
|
|
||||||
setDirectory();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setDirectory()
|
|
||||||
{
|
|
||||||
var selectedDir
|
|
||||||
= customStates.CustomChecked ? customStates.CustomDir
|
|
||||||
: directorySelectControl.SelectedDirectory is Configuration.KnownDirectories.AppDir ? Configuration.AppDir_Absolute
|
|
||||||
: Configuration.GetKnownDirectoryPath(directorySelectControl.SelectedDirectory);
|
|
||||||
selectedDir ??= string.Empty;
|
|
||||||
|
|
||||||
Directory = customStates.CustomChecked ? selectedDir : System.IO.Path.Combine(selectedDir, SubDirectory ?? "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DirectoryOrCustomSelectControl_PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
|
private void DirectoryOrCustomSelectControl_PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.Property.Name == nameof(Directory) && e.OldValue is null)
|
if (e.Property == DirectoryProperty)
|
||||||
{
|
{
|
||||||
var directory = Directory?.Trim() ?? "";
|
var directory = Directory?.Trim() ?? "";
|
||||||
|
|
||||||
@ -144,19 +113,19 @@ namespace LibationAvalonia.Controls
|
|||||||
|
|
||||||
if (known is Configuration.KnownDirectories.None)
|
if (known is Configuration.KnownDirectories.None)
|
||||||
{
|
{
|
||||||
customStates.CustomChecked = true;
|
directoryState.CustomDir = noSubDir;
|
||||||
customStates.CustomDir = directory;
|
directoryState.CustomChecked = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
customStates.KnownChecked = true;
|
directoryState.SelectedDirectory = known;
|
||||||
directorySelectControl.SelectedDirectory = known;
|
directoryState.KnownChecked = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (e.Property.Name == nameof(KnownDirectories))
|
else if (e.Property == KnownDirectoriesProperty &&
|
||||||
directorySelectControl.KnownDirectories = KnownDirectories;
|
KnownDirectories.Count > 0 &&
|
||||||
else if (e.Property.Name == nameof(SubDirectory))
|
directoryState.SelectedDirectory is null or Configuration.KnownDirectories.None)
|
||||||
directorySelectControl.SubDirectory = SubDirectory;
|
directoryState.SelectedDirectory = KnownDirectories[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
private string RemoveSubDirectoryFromPath(string path)
|
private string RemoveSubDirectoryFromPath(string path)
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<UserControl.Resources>
|
<UserControl.Resources>
|
||||||
<controls:KnownDirectoryConverter x:Key="KnownDirectoryConverter" />
|
<controls:KnownDirectoryConverter x:Key="KnownDirectoryConverter" />
|
||||||
|
<controls:KnownDirectoryPath x:Key="KnownDirectoryPath" />
|
||||||
</UserControl.Resources>
|
</UserControl.Resources>
|
||||||
|
|
||||||
|
|
||||||
@ -20,19 +21,26 @@
|
|||||||
<controls:WheelComboBox
|
<controls:WheelComboBox
|
||||||
HorizontalContentAlignment = "Stretch"
|
HorizontalContentAlignment = "Stretch"
|
||||||
HorizontalAlignment = "Stretch"
|
HorizontalAlignment = "Stretch"
|
||||||
|
Name="combo"
|
||||||
MinHeight="{Binding #displayPathTbox.MinHeight}"
|
MinHeight="{Binding #displayPathTbox.MinHeight}"
|
||||||
SelectedItem="{Binding $parent[1].SelectedDirectory, Mode=TwoWay}"
|
SelectedItem="{Binding $parent[1].SelectedDirectory, Mode=TwoWay}"
|
||||||
Items="{Binding $parent[1].KnownDirectories}">
|
Items="{Binding $parent[1].KnownDirectories}">
|
||||||
<ComboBox.ItemTemplate>
|
<ComboBox.ItemTemplate>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
|
<TextBlock Text="{Binding Converter={StaticResource KnownDirectoryConverter}}" />
|
||||||
<TextBlock
|
|
||||||
Text="{Binding Converter={StaticResource KnownDirectoryConverter}}" />
|
|
||||||
|
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</ComboBox.ItemTemplate>
|
</ComboBox.ItemTemplate>
|
||||||
</controls:WheelComboBox>
|
</controls:WheelComboBox>
|
||||||
<TextBox Margin="0,10,0,10" IsReadOnly="True" Name="displayPathTbox" />
|
<TextBox Margin="0,10,0,10" IsReadOnly="True">
|
||||||
|
<TextBox.Text>
|
||||||
|
<MultiBinding Converter="{StaticResource KnownDirectoryPath}">
|
||||||
|
<MultiBinding.Bindings>
|
||||||
|
<Binding Path="#combo.SelectedItem"/>
|
||||||
|
<Binding Path="$parent[1].SubDirectory"/>
|
||||||
|
</MultiBinding.Bindings>
|
||||||
|
</MultiBinding>
|
||||||
|
</TextBox.Text>
|
||||||
|
</TextBox>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@ -8,7 +8,6 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reactive.Subjects;
|
|
||||||
|
|
||||||
namespace LibationAvalonia.Controls
|
namespace LibationAvalonia.Controls
|
||||||
{
|
{
|
||||||
@ -26,6 +25,24 @@ namespace LibationAvalonia.Controls
|
|||||||
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
|
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public class KnownDirectoryPath : IMultiValueConverter
|
||||||
|
{
|
||||||
|
public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (values?.Count == 2 && values[0] is Configuration.KnownDirectories kdir && kdir is not Configuration.KnownDirectories.None)
|
||||||
|
{
|
||||||
|
var subdir = values[1] as string ?? "";
|
||||||
|
var path = kdir is Configuration.KnownDirectories.AppDir ? Configuration.AppDir_Absolute : Configuration.GetKnownDirectoryPath(kdir);
|
||||||
|
return Path.Combine(path, subdir);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public partial class DirectorySelectControl : UserControl
|
public partial class DirectorySelectControl : UserControl
|
||||||
{
|
{
|
||||||
@ -39,8 +56,8 @@ namespace LibationAvalonia.Controls
|
|||||||
Configuration.KnownDirectories.LibationFiles
|
Configuration.KnownDirectories.LibationFiles
|
||||||
};
|
};
|
||||||
|
|
||||||
public static readonly StyledProperty<Configuration.KnownDirectories> SelectedDirectoryProperty =
|
public static readonly StyledProperty<Configuration.KnownDirectories?> SelectedDirectoryProperty =
|
||||||
AvaloniaProperty.Register<DirectorySelectControl, Configuration.KnownDirectories>(nameof(SelectedDirectory));
|
AvaloniaProperty.Register<DirectorySelectControl, Configuration.KnownDirectories?>(nameof(SelectedDirectory));
|
||||||
|
|
||||||
public static readonly StyledProperty<List<Configuration.KnownDirectories>> KnownDirectoriesProperty =
|
public static readonly StyledProperty<List<Configuration.KnownDirectories>> KnownDirectoriesProperty =
|
||||||
AvaloniaProperty.Register<DirectorySelectControl, List<Configuration.KnownDirectories>>(nameof(KnownDirectories), DefaultKnownDirectories);
|
AvaloniaProperty.Register<DirectorySelectControl, List<Configuration.KnownDirectories>>(nameof(KnownDirectories), DefaultKnownDirectories);
|
||||||
@ -51,25 +68,6 @@ namespace LibationAvalonia.Controls
|
|||||||
public DirectorySelectControl()
|
public DirectorySelectControl()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
displayPathTbox = this.Get<TextBox>(nameof(displayPathTbox));
|
|
||||||
displayPathTbox.Bind(TextBox.TextProperty, TextboxPath);
|
|
||||||
PropertyChanged += DirectorySelectControl_PropertyChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Subject<string> TextboxPath = new Subject<string>();
|
|
||||||
|
|
||||||
private void DirectorySelectControl_PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.Property.Name == nameof(SelectedDirectory))
|
|
||||||
{
|
|
||||||
TextboxPath.OnNext(
|
|
||||||
Path.Combine(
|
|
||||||
SelectedDirectory is Configuration.KnownDirectories.None ? string.Empty
|
|
||||||
: SelectedDirectory is Configuration.KnownDirectories.AppDir ? Configuration.AppDir_Absolute
|
|
||||||
: Configuration.GetKnownDirectoryPath(SelectedDirectory)
|
|
||||||
, SubDirectory ?? string.Empty));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Configuration.KnownDirectories> KnownDirectories
|
public List<Configuration.KnownDirectories> KnownDirectories
|
||||||
@ -78,7 +76,7 @@ namespace LibationAvalonia.Controls
|
|||||||
set => SetValue(KnownDirectoriesProperty, value);
|
set => SetValue(KnownDirectoriesProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Configuration.KnownDirectories SelectedDirectory
|
public Configuration.KnownDirectories? SelectedDirectory
|
||||||
{
|
{
|
||||||
get => GetValue(SelectedDirectoryProperty);
|
get => GetValue(SelectedDirectoryProperty);
|
||||||
set => SetValue(SelectedDirectoryProperty, value);
|
set => SetValue(SelectedDirectoryProperty, value);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user