wabbajack/Wabbajack.App/Controls/FileSelectionBox.axaml.cs

86 lines
2.5 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
2021-11-08 23:17:33 +00:00
using System.Collections.Generic;
2021-11-09 02:36:07 +00:00
using System.ComponentModel;
2021-09-27 12:42:46 +00:00
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
2021-11-08 23:17:33 +00:00
using System.Threading.Tasks;
2021-09-27 12:42:46 +00:00
using Avalonia;
2021-11-08 23:17:33 +00:00
using Avalonia.Controls;
2021-09-27 12:42:46 +00:00
using Avalonia.ReactiveUI;
2021-11-09 02:36:07 +00:00
using Avalonia.Threading;
2021-09-27 12:42:46 +00:00
using Microsoft.Extensions.DependencyInjection;
using ReactiveUI;
2021-11-08 23:17:33 +00:00
using ReactiveUI.Fody.Helpers;
2021-09-27 12:42:46 +00:00
using Wabbajack.Paths;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.App.Controls;
2021-11-09 02:36:07 +00:00
public partial class FileSelectionBox : ReactiveUserControl<FileSelectionBoxViewModel>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public static readonly StyledProperty<string> AllowedExtensionsProperty =
AvaloniaProperty.Register<FileSelectionBox, string>(nameof(AllowedExtensions));
public static readonly StyledProperty<bool> SelectFolderProperty =
AvaloniaProperty.Register<FileSelectionBox, bool>(nameof(SelectFolder));
public FileSelectionBox()
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
InitializeComponent();
2021-11-08 23:17:33 +00:00
SelectButton.Command = ReactiveCommand.CreateFromTask(ShowDialog);
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-11-08 23:17:33 +00:00
private async Task ShowDialog()
2021-10-23 16:51:17 +00:00
{
2021-11-08 23:17:33 +00:00
if (SelectFolder)
{
var dialog = new OpenFolderDialog
{
Title = "Select a folder"
};
var result = await dialog.ShowAsync(App.MainWindow);
if (result != null)
Load(result.ToAbsolutePath());
}
else
{
var extensions = AllowedExtensions.Split(",").Select(e => e.ToString()[1..]).ToList();
var dialog = new OpenFileDialog
{
AllowMultiple = false,
Title = "Select a file",
Filters = new List<FileDialogFilter>
{
new FileDialogFilter {Extensions = extensions, Name = "*"}
}
};
var results = await dialog.ShowAsync(App.MainWindow);
if (results != null)
Load(results!.First().ToAbsolutePath());
}
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-11-08 23:17:33 +00:00
[Reactive]
2021-11-09 02:36:07 +00:00
public AbsolutePath SelectedPath { get; set; }
2021-11-08 23:17:33 +00:00
2021-10-23 16:51:17 +00:00
public string AllowedExtensions
{
get => GetValue(AllowedExtensionsProperty);
set => SetValue(AllowedExtensionsProperty, value);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public bool SelectFolder
{
get => GetValue(SelectFolderProperty);
set => SetValue(SelectFolderProperty, value);
2021-09-27 12:42:46 +00:00
}
2021-11-04 05:13:25 +00:00
public void Load(AbsolutePath path)
{
2021-11-09 02:36:07 +00:00
Dispatcher.UIThread.Post(() => {
TextBox.Text = path.ToString();
SelectedPath = path;
});
2021-11-04 05:13:25 +00:00
}
2021-09-27 12:42:46 +00:00
}