some code issues
DELETE
WalletWasabi.Gui/ManagedDialogs/InternalViewModelBase.cs
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Runtime.CompilerServices;
-using JetBrains.Annotations;
-
-namespace WalletWasabi.Gui.ManagedDialogs
-{
- class InternalViewModelBase : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
-
- [NotifyPropertyChangedInvocator]
- protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
- {
- if (!EqualityComparer<T>.Default.Equals(field, value))
- {
- field = value;
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- return true;
- }
-
- return false;
- }
-
- [NotifyPropertyChangedInvocator]
- protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
-}

MODIFY
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooser.xaml.cs
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooser.xaml.cs
@@ -28,16 +28,24 @@ namespace WalletWasabi.Gui.ManagedDialogs
private void OnPointerPressed(object sender, PointerPressedEventArgs e)
{
var model = (e.Source as StyledElement)?.DataContext as ManagedFileChooserItemViewModel;
+
if(model == null)
+ {
return;
+ }
var isQuickLink = _quickLinksRoot.IsLogicalParentOf(e.Source as Control);
if (e.ClickCount == 2 || isQuickLink)
{
if (model.IsDirectory)
+ {
Model?.Navigate(model.Path);
+ }
else
+ {
Model?.SelectSingleFile(model);
+ }
+
e.Handled = true;
}
}
@@ -45,22 +53,36 @@ namespace WalletWasabi.Gui.ManagedDialogs
protected override async void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
+
var model = (DataContext as ManagedFileChooserViewModel);
+
if (model == null)
+ {
return;
+ }
+
var preselected = model.SelectedItems.FirstOrDefault();
+
if(preselected == null)
+ {
return;
+ }
//Let everything to settle down and scroll to selected item
await Task.Delay(100);
+
if (preselected != model.SelectedItems.FirstOrDefault())
+ {
return;
+ }
// Workaround for ListBox bug, scroll to the previous file
var indexOfPreselected = model.Items.IndexOf(preselected);
+
if (indexOfPreselected > 1)
+ {
_filesView.ScrollIntoView(model.Items[indexOfPreselected - 1]);
}
}
}
+}

MODIFY
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooserFilterViewModel.cs
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooserFilterViewModel.cs
@@ -2,10 +2,11 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls;
+using WalletWasabi.Gui.ViewModels;
namespace WalletWasabi.Gui.ManagedDialogs
{
- class ManagedFileChooserFilterViewModel : InternalViewModelBase
+ class ManagedFileChooserFilterViewModel : ViewModelBase
{
private readonly string[] _extensions;
public string Name { get; }
@@ -13,8 +14,12 @@ namespace WalletWasabi.Gui.ManagedDialogs
public ManagedFileChooserFilterViewModel(FileDialogFilter filter)
{
Name = filter.Name;
+
if (filter.Extensions.Contains("*"))
+ {
return;
+ }
+
_extensions = filter.Extensions?.Select(e => "." + e.ToLowerInvariant()).ToArray();
}
@@ -26,10 +31,18 @@ namespace WalletWasabi.Gui.ManagedDialogs
public bool Match(string filename)
{
if (_extensions == null)
+ {
return true;
+ }
+
foreach (var ext in _extensions)
+ {
if (filename.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase))
+ {
return true;
+ }
+ }
+
return false;
}

MODIFY
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooserItemViewModel.cs
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooserItemViewModel.cs
+using ReactiveUI;
+using WalletWasabi.Gui.ViewModels;
+
namespace WalletWasabi.Gui.ManagedDialogs
{
- class ManagedFileChooserItemViewModel : InternalViewModelBase
+ class ManagedFileChooserItemViewModel : ViewModelBase
{
private string _displayName;
private string _path;
@@ -9,13 +12,13 @@ namespace WalletWasabi.Gui.ManagedDialogs
public string DisplayName
{
get => _displayName;
- set => RaiseAndSetIfChanged(ref _displayName, value);
+ set => this.RaiseAndSetIfChanged(ref _displayName, value);
}
public string Path
{
get => _path;
- set => RaiseAndSetIfChanged(ref _path, value);
+ set => this.RaiseAndSetIfChanged(ref _path, value);
}
public string IconKey => IsDirectory ? "Icon_Folder" : "Icon_File";
@@ -25,9 +28,9 @@ namespace WalletWasabi.Gui.ManagedDialogs
get => _isDirectory;
set
{
- if (RaiseAndSetIfChanged(ref _isDirectory, value))
+ if (this.RaiseAndSetIfChanged(ref _isDirectory, value))
{
- RaisePropertyChanged(nameof(IconKey));
+ this.RaisePropertyChanged(nameof(IconKey));
}
}
}

MODIFY
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooserViewModel.cs
WalletWasabi.Gui/ManagedDialogs/ManagedFileChooserViewModel.cs
@@ -7,10 +7,12 @@ using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Threading;
+using ReactiveUI;
+using WalletWasabi.Gui.ViewModels;
namespace WalletWasabi.Gui.ManagedDialogs
{
- class ManagedFileChooserViewModel : InternalViewModelBase
+ class ManagedFileChooserViewModel : ViewModelBase
{
public event Action CancelRequested;
public event Action<string[]> CompleteRequested;
@@ -36,7 +38,7 @@ namespace WalletWasabi.Gui.ManagedDialogs
public string Location
{
get => _location;
- private set => RaiseAndSetIfChanged(ref _location, value);
+ private set => this.RaiseAndSetIfChanged(ref _location, value);
}
public bool ShowFilters { get; }
@@ -50,13 +52,16 @@ namespace WalletWasabi.Gui.ManagedDialogs
for (var index = 0; index < QuickLinks.Count; index++)
{
var i = QuickLinks[index];
+
if (i.Path == Location)
+ {
return index;
}
+ }
return -1;
}
- set => RaisePropertyChanged(nameof(QuickLinksSelectedIndex));
+ set => this.RaisePropertyChanged(nameof(QuickLinksSelectedIndex));
}
public ManagedFileChooserFilterViewModel SelectedFilter
@@ -64,7 +69,7 @@ namespace WalletWasabi.Gui.ManagedDialogs
get => _selectedFilter;
set
{
- RaiseAndSetIfChanged(ref _selectedFilter, value);
+ this.RaiseAndSetIfChanged(ref _selectedFilter, value);
Refresh();
}
}
@@ -74,7 +79,7 @@ namespace WalletWasabi.Gui.ManagedDialogs
get => _showHiddenFiles;
set
{
- RaiseAndSetIfChanged(ref _showHiddenFiles, value);
+ this.RaiseAndSetIfChanged(ref _showHiddenFiles, value);
Refresh();
}
}
@@ -83,9 +88,11 @@ namespace WalletWasabi.Gui.ManagedDialogs
{
var quickSources = AvaloniaLocator.Current.GetService<ManagedFileChooserSources>()
?? new ManagedFileChooserSources();
+
QuickLinks.Clear();
QuickLinks.AddRange(quickSources.GetAllItems().Select(i => new ManagedFileChooserItemViewModel(i)));
+
Title = dialog.Title ?? (
dialog is OpenFileDialog ? "Open file"
: dialog is SaveFileDialog ? "Save file"
@@ -93,8 +100,11 @@ namespace WalletWasabi.Gui.ManagedDialogs
: throw new ArgumentException(nameof(dialog)));
var directory = dialog.InitialDirectory;
+
if (directory == null || !Directory.Exists(directory))
+ {
directory = Directory.GetCurrentDirectory();
+ }
if (dialog is FileDialog fd)
{
@@ -108,34 +118,43 @@ namespace WalletWasabi.Gui.ManagedDialogs
if (dialog is OpenFileDialog ofd)
{
if (ofd.AllowMultiple)
+ {
SelectionMode = SelectionMode.Multiple;
}
}
+ }
_selectingDirectory = dialog is OpenFolderDialog;
Navigate(directory, (dialog as FileDialog)?.InitialFileName);
- SelectedItems.CollectionChanged += OnSelectionChanged;
+ SelectedItems.CollectionChanged += OnSelectionChangedAsync;
}
- private async void OnSelectionChanged(object sender, NotifyCollectionChangedEventArgs e)
+ private async void OnSelectionChangedAsync(object sender, NotifyCollectionChangedEventArgs e)
{
if(_scheduledSelectionValidation)
+ {
return;
+ }
+
_scheduledSelectionValidation = true;
await Dispatcher.UIThread.InvokeAsync(() =>
{
try
{
if(_selectingDirectory)
+ {
SelectedItems.Clear();
+ }
else
{
var invalidItems = SelectedItems.Where(i => i.IsDirectory).ToList();
foreach (var item in invalidItems)
+ {
SelectedItems.Remove(item);
}
}
+ }
finally
{
_scheduledSelectionValidation = false;
@@ -146,17 +165,23 @@ namespace WalletWasabi.Gui.ManagedDialogs
void NavigateRoot(string initialSelectionName)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
Navigate(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System)), initialSelectionName);
+ }
else
+ {
Navigate("/", initialSelectionName);
}
+ }
public void Refresh() => Navigate(Location);
public void Navigate(string path, string initialSelectionName = null)
{
if (!Directory.Exists(path))
+ {
NavigateRoot(initialSelectionName);
+ }
else
{
Location = path;
@@ -166,13 +191,19 @@ namespace WalletWasabi.Gui.ManagedDialogs
if (!ShowHiddenFiles)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
infos = infos.Where(i => (i.Attributes & (FileAttributes.Hidden | FileAttributes.System)) != 0);
+ }
else
+ {
infos = infos.Where(i => !i.Name.StartsWith("."));
}
+ }
if (SelectedFilter != null)
+ {
infos = infos.Where(i => i is DirectoryInfo || SelectedFilter.Match(i.Name));
+ }
Items.AddRange(infos.Where(x=>
{
@@ -196,21 +227,26 @@ namespace WalletWasabi.Gui.ManagedDialogs
if (initialSelectionName != null)
{
var sel = Items.FirstOrDefault(i => !i.IsDirectory && i.DisplayName == initialSelectionName);
+
if (sel != null)
+ {
SelectedItems.Add(sel);
}
-
- RaisePropertyChanged(nameof(QuickLinksSelectedIndex));
}
-
+ this.RaisePropertyChanged(nameof(QuickLinksSelectedIndex));
+ }
}
public void GoUp()
{
var parent = Path.GetDirectoryName(Location);
+
if (string.IsNullOrWhiteSpace(parent))
+ {
return;
+ }
+
Navigate(parent);
}
@@ -222,10 +258,14 @@ namespace WalletWasabi.Gui.ManagedDialogs
public void Ok()
{
if (_selectingDirectory)
+ {
CompleteRequested?.Invoke(new[] {Location});
+ }
else
+ {
CompleteRequested?.Invoke(SelectedItems.Select(i => i.Path).ToArray());
}
+ }
public void SelectSingleFile(ManagedFileChooserItemViewModel item)
{

some code issues