Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window-less Managed File Dialog #13683

Merged
merged 9 commits into from
Nov 22, 2023
4 changes: 2 additions & 2 deletions samples/ControlCatalog/Pages/DialogsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ List<FileDialogFilter> GetFilters()
SuggestedStartLocation = lastSelectedDirectory,
SuggestedFileName = "FileName",
DefaultExtension = fileTypes?.Any() == true ? "txt" : null,
ShowOverwritePrompt = false
ShowOverwritePrompt = true
});

if (file is not null)
Expand Down Expand Up @@ -436,7 +436,7 @@ private IStorageProvider GetStorageProvider()
{
var forceManaged = this.Get<CheckBox>("ForceManaged").IsChecked ?? false;
return forceManaged
? new ManagedStorageProvider<Window>(GetWindow(), null)
? new ManagedStorageProvider(GetWindow())
: GetTopLevel().StorageProvider;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#nullable enable
using Avalonia.Metadata;
using Avalonia.Platform.Storage;

namespace Avalonia.Controls.Platform;

/// <summary>
/// Factory allows to register custom storage provider instead of native implementation.
/// </summary>
[Unstable]
public interface IStorageProviderFactory
{
IStorageProvider CreateProvider(TopLevel topLevel);
Expand Down
2 changes: 0 additions & 2 deletions src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Threading.Tasks;
using Avalonia.Platform.Storage;

#nullable enable

namespace Avalonia.Controls.Platform
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/Primitives/OverlayLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class OverlayLayer : Canvas
if(v is VisualLayerManager vlm)
if (vlm.OverlayLayer != null)
return vlm.OverlayLayer;
if (visual is TopLevel tl)
if (TopLevel.GetTopLevel(visual) is {} tl)
{
var layers = tl.GetVisualDescendants().OfType<VisualLayerManager>().FirstOrDefault();
return layers?.OverlayLayer;
Expand Down
6 changes: 3 additions & 3 deletions src/Avalonia.Dialogs/AboutAvaloniaDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Avalonia.Dialogs
{
public class AboutAvaloniaDialog : Window
{
private static readonly Version s_version = typeof(AboutAvaloniaDialog).Assembly.GetName().Version;
private static readonly Version s_version = typeof(AboutAvaloniaDialog).Assembly.GetName().Version!;

public static string Version { get; } = $@"v{s_version.ToString(2)}";

Expand Down Expand Up @@ -45,7 +45,7 @@ private static void ShellExec(string cmd, bool waitForExit = true)
{
if (waitForExit)
{
process.WaitForExit();
process?.WaitForExit();
}
}
}
Expand All @@ -61,7 +61,7 @@ private void Button_OnClick(object sender, RoutedEventArgs e)
}
else
{
using Process process = Process.Start(new ProcessStartInfo
using Process? process = Process.Start(new ProcessStartInfo
{
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? url : "open",
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"{url}" : "",
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Dialogs/Avalonia.Dialogs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@

<Import Project="..\..\build\DevAnalyzers.props" />
<Import Project="..\..\build\TrimmingEnable.props" />
<Import Project="..\..\build\NullableEnable.props" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Metadata;

namespace Avalonia.Dialogs.Internal
{
public class AvaloniaDialogsInternalViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;

internal protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
internal protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
Expand All @@ -20,7 +21,7 @@ internal protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMem
return false;
}

internal protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
internal protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Expand Down
40 changes: 40 additions & 0 deletions src/Avalonia.Dialogs/Internal/BclMountedVolumeInfoProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using Avalonia.Controls.Platform;
using Avalonia.Reactive;

namespace Avalonia.Dialogs.Internal;

internal class BclMountedVolumeInfoProvider : IMountedVolumeInfoProvider
{
public IDisposable Listen(ObservableCollection<MountedVolumeInfo> mountedDrives)
{
foreach (var drive in DriveInfo.GetDrives())
{
string directory;
ulong totalSize;
try
{
if (!drive.IsReady)
continue;
totalSize = (ulong)drive.TotalSize;
directory = drive.RootDirectory.FullName;

_ = new DirectoryInfo(directory).EnumerateFileSystemInfos();
}
catch
{
continue;
}

mountedDrives.Add(new MountedVolumeInfo
{
VolumeLabel = string.IsNullOrEmpty(drive.VolumeLabel.Trim()) ? directory : drive.VolumeLabel,
VolumePath = directory,
VolumeSizeBytes = totalSize
});
}
return Disposable.Empty;
}
}
2 changes: 1 addition & 1 deletion src/Avalonia.Dialogs/Internal/ChildFitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected override Size MeasureOverride(Size availableSize)

protected override Size ArrangeOverride(Size finalSize)
{
Child.Measure(finalSize);
Child?.Measure(finalSize);
base.ArrangeOverride(finalSize);
return finalSize;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.Dialogs/Internal/FileSizeStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Avalonia.Dialogs.Internal
{
public class FileSizeStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is long size && size > 0)
{
Expand All @@ -16,7 +16,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
return "";
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Avalonia.Dialogs.Internal
{
public class ManagedFileChooserFilterViewModel : AvaloniaDialogsInternalViewModelBase
{
private readonly Regex[] _patterns;
private readonly Regex[]? _patterns;
public string Name { get; }

public ManagedFileChooserFilterViewModel(FilePickerFileType filter)
Expand Down
12 changes: 6 additions & 6 deletions src/Avalonia.Dialogs/Internal/ManagedFileChooserItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ namespace Avalonia.Dialogs.Internal
{
public class ManagedFileChooserItemViewModel : AvaloniaDialogsInternalViewModelBase
{
private string _displayName;
private string _path;
private string? _displayName;
private string? _path;
private DateTime _modified;
private string _type;
private string? _type;
private long _size;
private ManagedFileChooserItemType _itemType;

public string DisplayName
public string? DisplayName
{
get => _displayName;
set => this.RaiseAndSetIfChanged(ref _displayName, value);
}

public string Path
public string? Path
{
get => _path;
set => this.RaiseAndSetIfChanged(ref _path, value);
Expand All @@ -29,7 +29,7 @@ public DateTime Modified
set => this.RaiseAndSetIfChanged(ref _modified, value);
}

public string Type
public string? Type
{
get => _type;
set => this.RaiseAndSetIfChanged(ref _type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
public class ManagedFileChooserNavigationItem
{
public string DisplayName { get; set; }
public string Path { get; set; }
public string? DisplayName { get; set; }
public string? Path { get; set; }
public ManagedFileChooserItemType ItemType { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Avalonia.Dialogs/Internal/ManagedFileChooserSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static ManagedFileChooserNavigationItem[] DefaultGetFileSystemRoots()

try
{
Directory.GetFiles(x.VolumePath);
Directory.GetFiles(x.VolumePath!);
}
catch (Exception)
{
Expand All @@ -79,7 +79,7 @@ public static ManagedFileChooserNavigationItem[] DefaultGetFileSystemRoots()
};
})
.Where(x => x != null)
.ToArray();
.ToArray()!;
}
}
}
Loading