Some additional library items

UserControlRx, FilePicker control, TaskExt.FireAndForget
This commit is contained in:
Justin Swanson 2019-10-30 14:39:12 -05:00
parent 25dcb02cb5
commit 2f8977feac
6 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wabbajack
{
public static class TaskExt
{
public static async void FireAndForget(this Task task, Action<Exception> onException = null)
{
try
{
await task.ConfigureAwait(false);
}
catch (Exception ex)
when (onException != null)
{
onException(ex);
}
}
}
}

View File

@ -120,6 +120,7 @@
<Compile Include="Downloaders\MEGADownloader.cs" />
<Compile Include="Downloaders\ModDBDownloader.cs" />
<Compile Include="Downloaders\NexusDownloader.cs" />
<Compile Include="Extensions\TaskExt.cs" />
<Compile Include="Installer.cs" />
<Compile Include="ModListRegistry\ModListMetadata.cs" />
<Compile Include="NexusApi\Dtos.cs" />

View File

@ -0,0 +1,45 @@
<local:UserControlRx
x:Class="Wabbajack.FilePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:icon="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:local="clr-namespace:Wabbajack"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="35"
d:DesignWidth="400"
BorderBrush="{StaticResource DarkBackgroundBrush}"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox
Grid.Column="0"
VerticalContentAlignment="Center"
Margin="0,0,-4,0"
Background="{StaticResource DarkBackgroundBrush}"
Text="{Binding TargetPath, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding ShowTextBoxInput, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
<icon:PackIconMaterial
Margin="0,4,4,4"
Padding="0,3"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{StaticResource WarningBrush}"
Kind="Circle"
ToolTip="Path does not exist"
Visibility="{Binding Exists, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Converter={StaticResource bool2VisibilityConverter}, ConverterParameter=False}" />
<Button
Grid.Column="1"
Command="{Binding SetTargetPathCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
ToolTip="Set target path">
<icon:PackIconMaterial
Width="16"
Margin="4"
Height="12"
Kind="DotsHorizontal" />
</Button>
</Grid>
</local:UserControlRx>

View File

@ -0,0 +1,164 @@
using Microsoft.WindowsAPICodePack.Dialogs;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Wabbajack
{
/// <summary>
/// Interaction logic for FilePicker.xaml
/// </summary>
public partial class FilePicker : UserControlRx
{
public enum PathTypeOptions
{
Off,
Either,
File,
Folder
}
public ICommand SetTargetPathCommand
{
get => (ICommand)GetValue(SetTargetPathCommandProperty);
set => SetValue(SetTargetPathCommandProperty, value);
}
public static readonly DependencyProperty SetTargetPathCommandProperty = DependencyProperty.Register(nameof(SetTargetPathCommand), typeof(ICommand), typeof(FilePicker),
new FrameworkPropertyMetadata(default(ICommand)));
public string TargetPath
{
get { return (string)GetValue(TargetPathProperty); }
set { SetValue(TargetPathProperty, value); }
}
public static readonly DependencyProperty TargetPathProperty = DependencyProperty.Register(nameof(TargetPath), typeof(string), typeof(FilePicker),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, WireNotifyPropertyChanged));
public bool ShowTextBoxInput
{
get => (bool)GetValue(ShowTextBoxInputProperty);
set => SetValue(ShowTextBoxInputProperty, value);
}
public static readonly DependencyProperty ShowTextBoxInputProperty = DependencyProperty.Register(nameof(ShowTextBoxInput), typeof(bool), typeof(FilePicker),
new FrameworkPropertyMetadata(true));
public PathTypeOptions PathType
{
get => (PathTypeOptions)GetValue(PathTypeProperty);
set => SetValue(PathTypeProperty, value);
}
public static readonly DependencyProperty PathTypeProperty = DependencyProperty.Register(nameof(PathType), typeof(PathTypeOptions), typeof(FilePicker),
new FrameworkPropertyMetadata(PathTypeOptions.Off, WireNotifyPropertyChanged));
public bool Exists
{
get => (bool)GetValue(ExistsProperty);
set => SetValue(ExistsProperty, value);
}
public static readonly DependencyProperty ExistsProperty = DependencyProperty.Register(nameof(Exists), typeof(bool), typeof(FilePicker),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, WireNotifyPropertyChanged));
public bool DoExistsCheck
{
get => (bool)GetValue(DoExistsCheckProperty);
set => SetValue(DoExistsCheckProperty, value);
}
public static readonly DependencyProperty DoExistsCheckProperty = DependencyProperty.Register(nameof(DoExistsCheck), typeof(bool), typeof(FilePicker),
new FrameworkPropertyMetadata(true, WireNotifyPropertyChanged));
public string PromptTitle
{
get => (string)GetValue(PromptTitleProperty);
set => SetValue(PromptTitleProperty, value);
}
public static readonly DependencyProperty PromptTitleProperty = DependencyProperty.Register(nameof(PromptTitle), typeof(string), typeof(FilePicker),
new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public FilePicker()
{
InitializeComponent();
this.SetTargetPathCommand = ReactiveCommand.Create(
execute: () =>
{
string dirPath;
if (File.Exists(this.TargetPath))
{
dirPath = System.IO.Path.GetDirectoryName(this.TargetPath);
}
else
{
dirPath = this.TargetPath;
}
var dlg = new CommonOpenFileDialog();
dlg.Title = this.PromptTitle;
dlg.IsFolderPicker = this.PathType == PathTypeOptions.Folder;
dlg.InitialDirectory = this.TargetPath;
dlg.AddToMostRecentlyUsedList = false;
dlg.AllowNonFileSystemItems = false;
dlg.DefaultDirectory = this.TargetPath;
dlg.EnsureFileExists = true;
dlg.EnsurePathExists = true;
dlg.EnsureReadOnly = false;
dlg.EnsureValidNames = true;
dlg.Multiselect = false;
dlg.ShowPlacesList = true;
if (dlg.ShowDialog() != CommonFileDialogResult.Ok) return;
this.TargetPath = dlg.FileName;
});
// Check that file exists
Observable.Interval(TimeSpan.FromSeconds(3))
.FilterSwitch(
Observable.CombineLatest(
this.WhenAny(x => x.PathType),
this.WhenAny(x => x.DoExistsCheck),
resultSelector: (type, doExists) => type != PathTypeOptions.Off && doExists))
.Unit()
// Also do it when fields change
.Merge(this.WhenAny(x => x.PathType).Unit())
.Merge(this.WhenAny(x => x.DoExistsCheck).Unit())
.CombineLatest(
this.WhenAny(x => x.DoExistsCheck),
this.WhenAny(x => x.PathType),
this.WhenAny(x => x.TargetPath)
.Throttle(TimeSpan.FromMilliseconds(200)),
resultSelector: (_, DoExists, Type, Path) => (DoExists, Type, Path))
// Refresh exists
.Select(t =>
{
if (!t.DoExists) return true;
switch (t.Type)
{
case PathTypeOptions.Either:
return File.Exists(t.Path) || Directory.Exists(t.Path);
case PathTypeOptions.File:
return File.Exists(t.Path);
case PathTypeOptions.Folder:
return Directory.Exists(t.Path);
default:
return true;
}
})
.DistinctUntilChanged()
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(exists => this.Exists = exists)
.DisposeWith(this.CompositeDisposable);
}
}
}

View File

@ -0,0 +1,48 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Disposables;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Wabbajack
{
public class UserControlRx : UserControl, IDisposable, IReactiveObject
{
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
public void RaisePropertyChanging(PropertyChangingEventArgs args)
{
PropertyChanging?.Invoke(this, args);
}
public void RaisePropertyChanged(PropertyChangedEventArgs args)
{
PropertyChanged?.Invoke(this, args);
}
private readonly Lazy<CompositeDisposable> _CompositeDisposable = new Lazy<CompositeDisposable>();
public CompositeDisposable CompositeDisposable => _CompositeDisposable.Value;
public virtual void Dispose()
{
if (_CompositeDisposable.IsValueCreated)
{
_CompositeDisposable.Value.Dispose();
}
}
protected static void WireNotifyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is UserControlRx control)) return;
if (object.Equals(e.OldValue, e.NewValue)) return;
control.RaisePropertyChanged(e.Property.Name);
}
}
}

View File

@ -31,6 +31,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<XamlDebuggingInformation>True</XamlDebuggingInformation>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
@ -135,6 +136,13 @@
<Compile Include="Converters\IsNotNullVisibilityConverter.cs" />
<Compile Include="Enums\RunMode.cs" />
<Compile Include="Extensions\ReactiveUIExt.cs" />
<Page Include="Views\FilePicker.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="Views\FilePicker.xaml.cs">
<DependentUpon>FilePicker.xaml</DependentUpon>
</Compile>
<Compile Include="Views\InstallationView.xaml.cs">
<DependentUpon>InstallationView.xaml</DependentUpon>
</Compile>
@ -153,6 +161,7 @@
<Compile Include="Views\TextViewer.xaml.cs">
<DependentUpon>TextViewer.xaml</DependentUpon>
</Compile>
<Compile Include="Views\UserControlRx.cs" />
<Page Include="Views\CompilerView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>