Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Common/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Files.Common
{
Expand Down Expand Up @@ -48,5 +50,24 @@ public static DateTime ToDateTime(this System.Runtime.InteropServices.ComTypes.F
return DateTime.FromFileTimeUtc(0xFFFFFFFF);
}
}

public static async Task WithTimeout(this Task task,
TimeSpan timeout)
{
if (task == await Task.WhenAny(task, Task.Delay(timeout)))
{
await task;
}
}

public static async Task<T> WithTimeout<T>(this Task<T> task,
TimeSpan timeout)
{
if (task == await Task.WhenAny(task, Task.Delay(timeout)))
{
return await task;
}
return default(T);
}
}
}
6 changes: 3 additions & 3 deletions Files.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static void Main(string[] args)
foreach (var drive in DriveInfo.GetDrives())
{
var recycle_path = Path.Combine(drive.Name, "$Recycle.Bin", sid);
if (!Directory.Exists(recycle_path))
if (drive.DriveType == DriveType.Network || !Directory.Exists(recycle_path))
{
continue;
}
Expand All @@ -83,7 +83,7 @@ private static void Main(string[] args)
// Preload context menu for better performace
// We query the context menu for the app's local folder
var preloadPath = ApplicationData.Current.LocalFolder.Path;
using var _ = Win32API.ContextMenu.GetContextMenuForFiles(new string[] { preloadPath }, Shell32.CMF.CMF_NORMAL);
using var _ = Win32API.ContextMenu.GetContextMenuForFiles(new string[] { preloadPath }, Shell32.CMF.CMF_NORMAL | Shell32.CMF.CMF_SYNCCASCADEMENU, FilterMenuItems(false));

// Connect to app service and wait until the connection gets closed
appServiceExit = new AutoResetEvent(false);
Expand Down Expand Up @@ -324,7 +324,7 @@ private static object HandleMenuMessage(ValueSet message, Win32API.DisposableDic
var showOpenMenu = (bool)message["ShowOpenMenu"];
var split = filePath.Split('|').Where(x => !string.IsNullOrWhiteSpace(x));
var cMenuLoad = Win32API.ContextMenu.GetContextMenuForFiles(split.ToArray(),
extendedMenu ? Shell32.CMF.CMF_EXTENDEDVERBS : Shell32.CMF.CMF_NORMAL, FilterMenuItems(showOpenMenu));
(extendedMenu ? Shell32.CMF.CMF_EXTENDEDVERBS : Shell32.CMF.CMF_NORMAL) | Shell32.CMF.CMF_SYNCCASCADEMENU, FilterMenuItems(showOpenMenu));
table.SetValue("MENU", cMenuLoad);
return cMenuLoad;

Expand Down
51 changes: 28 additions & 23 deletions Files/Filesystem/DriveItem.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using ByteSizeLib;
using Files.Common;
using Files.Helpers;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Uwp.Extensions;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml;

namespace Files.Filesystem
{
public class DriveItem : INavigationControlItem
public class DriveItem : ObservableObject, INavigationControlItem
{
public string Glyph { get; set; }
public string Text { get; set; }
Expand All @@ -18,7 +22,6 @@ public class DriveItem : INavigationControlItem
public ByteSize MaxSpace { get; set; }
public ByteSize FreeSpace { get; set; }
public ByteSize SpaceUsed { get; set; }
public string SpaceText { get; set; }
public Visibility ItemVisibility { get; set; } = Visibility.Visible;

private DriveType _type;
Expand All @@ -33,6 +36,13 @@ public DriveType Type
}
}

private string _spaceText;
public string SpaceText
{
get => _spaceText;
set => SetProperty(ref _spaceText, value);
}

public DriveItem()
{
ItemType = NavigationControlItemType.OneDrive;
Expand All @@ -44,31 +54,26 @@ public DriveItem(StorageFolder root, DriveType type)
Type = type;
Path = string.IsNullOrEmpty(root.Path) ? $"\\\\?\\{root.Name}\\" : root.Path;
Root = root;
GetDriveItemProperties();
}

var properties = Task.Run(async () =>
private async void GetDriveItemProperties()
{
try
{
return await root.Properties.RetrievePropertiesAsync(new[] { "System.FreeSpace", "System.Capacity" });
}).Result;
var properties = await Root.Properties.RetrievePropertiesAsync(new[] { "System.FreeSpace", "System.Capacity" })
.AsTask().WithTimeout(TimeSpan.FromSeconds(5));

if (properties.ContainsKey("System.Capacity") && properties.ContainsKey("System.FreeSpace"))
{
try
{
MaxSpace = ByteSize.FromBytes((ulong)properties["System.Capacity"]);
FreeSpace = ByteSize.FromBytes((ulong)properties["System.FreeSpace"]);

SpaceUsed = MaxSpace - FreeSpace;
SpaceText = string.Format(
"DriveFreeSpaceAndCapacity".GetLocalized(),
FreeSpace.ToBinaryString().ConvertSizeAbbreviation(),
MaxSpace.ToBinaryString().ConvertSizeAbbreviation());
}
catch (NullReferenceException)
{
SpaceText = "DriveCapacityUnknown".GetLocalized();
}
MaxSpace = ByteSize.FromBytes((ulong)properties["System.Capacity"]);
FreeSpace = ByteSize.FromBytes((ulong)properties["System.FreeSpace"]);

SpaceUsed = MaxSpace - FreeSpace;
SpaceText = string.Format(
"DriveFreeSpaceAndCapacity".GetLocalized(),
FreeSpace.ToBinaryString().ConvertSizeAbbreviation(),
MaxSpace.ToBinaryString().ConvertSizeAbbreviation());
}
else
catch (NullReferenceException)
{
SpaceText = "DriveCapacityUnknown".GetLocalized();
}
Expand Down
2 changes: 1 addition & 1 deletion Files/UserControls/SidebarControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
IsRightTapEnabled="True"
RightTapped="NavigationViewDriveItem_RightTapped"
Tag="{x:Bind Path}"
ToolTipService.ToolTip="{x:Bind SpaceText}"
ToolTipService.ToolTip="{x:Bind SpaceText, Mode=OneWay}"
Visibility="{x:Bind ItemVisibility}">
<muxc:NavigationViewItem.Icon>
<FontIcon
Expand Down
37 changes: 26 additions & 11 deletions Files/View Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,10 @@ public async void RapidAddItemsToCollectionAsync(string path)
}
else
{
await EnumerateItemsFromStandardFolder(path);
WatchForDirectoryChanges(path);
if (await EnumerateItemsFromStandardFolder(path))
{
WatchForDirectoryChanges(path);
}
}

if (FilesAndFolders.Count == 0)
Expand Down Expand Up @@ -783,7 +785,7 @@ public async Task EnumerateItemsFromSpecialFolder(string path)
}
}

public async Task EnumerateItemsFromStandardFolder(string path)
public async Task<bool> EnumerateItemsFromStandardFolder(string path)
{
// Flag to use FindFirstFileExFromApp or StorageFolder enumeration
bool enumFromStorageFolder = false;
Expand All @@ -796,15 +798,15 @@ public async Task EnumerateItemsFromStandardFolder(string path)
{
var consentDialogDisplay = new ConsentDialog();
await consentDialogDisplay.ShowAsync(ContentDialogPlacement.Popup);
return;
return false;
}
catch (FileNotFoundException)
{
await DialogDisplayHelper.ShowDialog(
"FolderNotFoundDialog/Title".GetLocalized(),
"FolderNotFoundDialog/Text".GetLocalized());
IsLoadingItems = false;
return;
return false;
}
catch (Exception e)
{
Expand All @@ -817,7 +819,7 @@ await DialogDisplayHelper.ShowDialog(
{
await DialogDisplayHelper.ShowDialog("DriveUnpluggedDialog/Title".GetLocalized(), e.Message);
IsLoadingItems = false;
return;
return false;
}
}

Expand Down Expand Up @@ -873,13 +875,18 @@ await DialogDisplayHelper.ShowDialog(
FileSizeBytes = 0
};
await EnumFromStorageFolder();
return true;
}
else
{
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
IntPtr hFile = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
additionalFlags);
(IntPtr hFile, WIN32_FIND_DATA findData) = await Task.Run(() =>
{
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
IntPtr hFileTsk = FindFirstFileExFromApp(path + "\\*.*", findInfoLevel, out WIN32_FIND_DATA findDataTsk, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
additionalFlags);
return (hFileTsk, findDataTsk);
}).WithTimeout(TimeSpan.FromSeconds(5));

DateTime itemDate = DateTime.UtcNow;
try
Expand Down Expand Up @@ -909,9 +916,16 @@ await DialogDisplayHelper.ShowDialog(
};

var count = 0;
if (hFile.ToInt64() == -1)
if (hFile == IntPtr.Zero)
{
await DialogDisplayHelper.ShowDialog("DriveUnpluggedDialog/Title".GetLocalized(), "");
IsLoadingItems = false;
return false;
}
else if (hFile.ToInt64() == -1)
{
await EnumFromStorageFolder();
return false;
}
else
{
Expand Down Expand Up @@ -945,6 +959,7 @@ await DialogDisplayHelper.ShowDialog(
} while (FindNextFile(hFile, out findData));

FindClose(hFile);
return true;
}
}
}
Expand Down