csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
3.1 KiB
85 lines
3.1 KiB
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Avalonia.Controls.Platform;
|
|
using Avalonia.Utilities;
|
|
|
|
|
|
namespace Avalonia.Dialogs.Internal
|
|
{
|
|
internal class ManagedFileChooserSources
|
|
{
|
|
public Func<ManagedFileChooserNavigationItem[]> GetUserDirectories { get; set; }
|
|
= DefaultGetUserDirectories;
|
|
|
|
public Func<ManagedFileChooserNavigationItem[]> GetFileSystemRoots { get; set; }
|
|
= DefaultGetFileSystemRoots;
|
|
|
|
public Func<ManagedFileChooserSources, ManagedFileChooserNavigationItem[]> GetAllItemsDelegate { get; set; }
|
|
= DefaultGetAllItems;
|
|
|
|
public ManagedFileChooserNavigationItem[] GetAllItems() => GetAllItemsDelegate(this);
|
|
public static readonly ObservableCollection<MountedVolumeInfo> MountedVolumes = new ObservableCollection<MountedVolumeInfo>();
|
|
|
|
public static ManagedFileChooserNavigationItem[] DefaultGetAllItems(ManagedFileChooserSources sources)
|
|
{
|
|
return sources.GetUserDirectories().Concat(sources.GetFileSystemRoots()).ToArray();
|
|
}
|
|
|
|
private static readonly Environment.SpecialFolder[] s_folders = new[]
|
|
{
|
|
Environment.SpecialFolder.Desktop,
|
|
Environment.SpecialFolder.UserProfile,
|
|
Environment.SpecialFolder.MyDocuments,
|
|
Environment.SpecialFolder.MyMusic,
|
|
Environment.SpecialFolder.MyPictures,
|
|
Environment.SpecialFolder.MyVideos
|
|
};
|
|
|
|
public static ManagedFileChooserNavigationItem[] DefaultGetUserDirectories()
|
|
{
|
|
return s_folders.Select(Environment.GetFolderPath).Distinct()
|
|
.Where(d => !string.IsNullOrWhiteSpace(d))
|
|
.Where(Directory.Exists)
|
|
.Select(d => new ManagedFileChooserNavigationItem
|
|
{
|
|
ItemType = ManagedFileChooserItemType.Folder,
|
|
Path = d,
|
|
DisplayName = Path.GetFileName(d)
|
|
}).ToArray();
|
|
}
|
|
|
|
public static ManagedFileChooserNavigationItem[] DefaultGetFileSystemRoots()
|
|
{
|
|
return MountedVolumes
|
|
.Select(x =>
|
|
{
|
|
var displayName = x.VolumeLabel;
|
|
|
|
if (displayName == null & x.VolumeSizeBytes > 0)
|
|
{
|
|
displayName = $"{ByteSizeHelper.ToString(x.VolumeSizeBytes, true)} Volume";
|
|
};
|
|
|
|
try
|
|
{
|
|
Directory.GetFiles(x.VolumePath!);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new ManagedFileChooserNavigationItem
|
|
{
|
|
ItemType = ManagedFileChooserItemType.Volume,
|
|
DisplayName = displayName,
|
|
Path = x.VolumePath
|
|
};
|
|
})
|
|
.Where(x => x != null)
|
|
.ToArray()!;
|
|
}
|
|
}
|
|
}
|
|
|