Skip to content

Commit

Permalink
Feature: Auto refresh after adding items if no directory watcher is a…
Browse files Browse the repository at this point in the history
…vailable (#13068)
  • Loading branch information
hishitetsu authored Aug 1, 2023
1 parent b707b56 commit f4ddd25
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
18 changes: 2 additions & 16 deletions src/Files.App/Actions/FileSystem/CreateShortcutAction.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using System.IO;

namespace Files.App.Actions
{
internal class CreateShortcutAction : BaseUIAction, IAction
Expand Down Expand Up @@ -30,21 +28,9 @@ public CreateShortcutAction()
context.PropertyChanged += Context_PropertyChanged;
}

public async Task ExecuteAsync()
public Task ExecuteAsync()
{
var currentPath = context.ShellPage?.FilesystemViewModel.WorkingDirectory;

if (App.LibraryManager.TryGetLibrary(currentPath ?? string.Empty, out var library) && !library.IsEmpty)
currentPath = library.DefaultSaveFolder;

foreach (ListedItem selectedItem in context.SelectedItems)
{
var fileName = string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), selectedItem.Name) + ".lnk";
var filePath = Path.Combine(currentPath ?? string.Empty, fileName);

if (!await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath))
await UIFilesystemHelpers.HandleShortcutCannotBeCreated(fileName, selectedItem.ItemPath);
}
return UIFilesystemHelpers.CreateShortcutAsync(context.ShellPage, context.SelectedItems);
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
21 changes: 15 additions & 6 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ public bool AreDirectoriesSortedAlongsideFiles
}
}

public bool HasNoWatcher { get; private set; }

public ItemViewModel(FolderSettingsViewModel folderSettingsViewModel)
{
folderSettings = folderSettingsViewModel;
Expand Down Expand Up @@ -1381,7 +1383,8 @@ private async Task RapidAddItemsToCollection(string? path, LibraryItem? library
IsTypeCloudDrive = syncStatus != CloudDriveSyncStatus.NotSynced && syncStatus != CloudDriveSyncStatus.Unknown,
IsTypeGitRepository = GitDirectory is not null
});
WatchForDirectoryChanges(path, syncStatus);
if (!HasNoWatcher)
WatchForDirectoryChanges(path, syncStatus);
if (GitDirectory is not null)
WatchForGitChanges();
break;
Expand All @@ -1390,13 +1393,15 @@ private async Task RapidAddItemsToCollection(string? path, LibraryItem? library
case 1:
PageTypeUpdated?.Invoke(this, new PageTypeUpdatedEventArgs() { IsTypeCloudDrive = false, IsTypeRecycleBin = isRecycleBin });
currentStorageFolder ??= await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderWithPathFromPathAsync(path));
WatchForStorageFolderChanges(currentStorageFolder?.Item);
if (!HasNoWatcher)
WatchForStorageFolderChanges(currentStorageFolder?.Item);
break;

// Watch for changes using Win32 in Box Drive folder (#7428) and network drives (#5869)
case 2:
PageTypeUpdated?.Invoke(this, new PageTypeUpdatedEventArgs() { IsTypeCloudDrive = false });
WatchForWin32FolderChanges(path);
if (!HasNoWatcher)
WatchForWin32FolderChanges(path);
break;

// Enumeration failed
Expand Down Expand Up @@ -1429,14 +1434,16 @@ public void CloseWatcher()
watcherCTS = new CancellationTokenSource();
}

public async Task<int> EnumerateItemsFromStandardFolderAsync(string path, CancellationToken cancellationToken, LibraryItem? library = null)
private async Task<int> EnumerateItemsFromStandardFolderAsync(string path, CancellationToken cancellationToken, LibraryItem? library = null)
{
// Flag to use FindFirstFileExFromApp or StorageFolder enumeration - Use storage folder for Box Drive (#4629)
var isBoxFolder = App.CloudDrivesManager.Drives.FirstOrDefault(x => x.Text == "Box")?.Path?.TrimEnd('\\') is string boxFolder && path.StartsWith(boxFolder);
bool isWslDistro = App.WSLDistroManager.TryGetDistro(path, out _);
bool isMtp = path.StartsWith(@"\\?\", StringComparison.Ordinal);
bool isShellFolder = path.StartsWith(@"\\SHELL\", StringComparison.Ordinal);
bool isNetwork = path.StartsWith(@"\\", StringComparison.Ordinal) &&
!path.StartsWith(@"\\?\", StringComparison.Ordinal) &&
!path.StartsWith(@"\\SHELL\", StringComparison.Ordinal) &&
!isMtp &&
!isShellFolder &&
!isWslDistro;
bool isFtp = FtpHelpers.IsFtpPath(path);
bool enumFromStorageFolder = isBoxFolder || isFtp;
Expand Down Expand Up @@ -1507,6 +1514,8 @@ await DialogDisplayHelper.ShowDialogAsync(
await ContextMenu.InvokeVerb("unlock-bde", pathRoot);
}

HasNoWatcher = isFtp || isWslDistro || isMtp || currentStorageFolder?.Item is ZipStorageFolder;

if (enumFromStorageFolder)
{
var basicProps = await rootFolder?.GetBasicPropertiesAsync();
Expand Down
30 changes: 28 additions & 2 deletions src/Files.App/Helpers/UI/UIFilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public static async Task PasteItemAsync(string destinationPath, IShellPage assoc
if (packageView && packageView.Result is not null)
{
await associatedInstance.FilesystemHelpers.PerformOperationTypeAsync(packageView.Result.RequestedOperation, packageView, destinationPath, false, true);
associatedInstance?.SlimContentPage?.ItemManipulationModel?.RefreshItemsOpacity();
associatedInstance.SlimContentPage?.ItemManipulationModel?.RefreshItemsOpacity();
await associatedInstance.RefreshIfNoWatcherExists();
}
}

Expand Down Expand Up @@ -264,6 +265,7 @@ public static async Task<bool> RenameFileItemAsync(ListedItem item, string newNa
if (renamed == ReturnResult.Success)
{
associatedInstance.ToolbarViewModel.CanGoForward = false;
await associatedInstance.RefreshIfNoWatcherExists();
return true;
}

Expand All @@ -273,9 +275,10 @@ public static async Task<bool> RenameFileItemAsync(ListedItem item, string newNa
public static async Task CreateFileFromDialogResultType(AddItemDialogItemType itemType, ShellNewEntry? itemInfo, IShellPage associatedInstance)
{
await CreateFileFromDialogResultTypeForResult(itemType, itemInfo, associatedInstance);
await associatedInstance.RefreshIfNoWatcherExists();
}

public static async Task<IStorageItem?> CreateFileFromDialogResultTypeForResult(AddItemDialogItemType itemType, ShellNewEntry? itemInfo, IShellPage associatedInstance)
private static async Task<IStorageItem?> CreateFileFromDialogResultTypeForResult(AddItemDialogItemType itemType, ShellNewEntry? itemInfo, IShellPage associatedInstance)
{
string? currentPath = null;

Expand Down Expand Up @@ -346,6 +349,7 @@ public static async Task CreateFolderWithSelectionAsync(IShellPage associatedIns
return;

await associatedInstance.FilesystemHelpers.MoveItemsAsync(items, items.Select(x => PathNormalization.Combine(folder.Path, x.Name)), false, true);
await associatedInstance.RefreshIfNoWatcherExists();
}
catch (Exception ex)
{
Expand All @@ -365,6 +369,26 @@ public static void SetHiddenAttributeItem(ListedItem item, bool isHidden, ItemMa
itemManipulationModel.RefreshItemsOpacity();
}

public static async Task CreateShortcutAsync(IShellPage? associatedInstance, IReadOnlyList<ListedItem> selectedItems)
{
var currentPath = associatedInstance?.FilesystemViewModel.WorkingDirectory;

if (App.LibraryManager.TryGetLibrary(currentPath ?? string.Empty, out var library) && !library.IsEmpty)
currentPath = library.DefaultSaveFolder;

foreach (ListedItem selectedItem in selectedItems)
{
var fileName = string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), selectedItem.Name) + ".lnk";
var filePath = Path.Combine(currentPath ?? string.Empty, fileName);

if (!await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath))
await HandleShortcutCannotBeCreated(fileName, selectedItem.ItemPath);
}

if (associatedInstance is not null)
await associatedInstance.RefreshIfNoWatcherExists();
}

public static async Task CreateShortcutFromDialogAsync(IShellPage associatedInstance)
{
var currentPath = associatedInstance.FilesystemViewModel.WorkingDirectory;
Expand All @@ -382,6 +406,8 @@ public static async Task CreateShortcutFromDialogAsync(IShellPage associatedInst
return;

await HandleShortcutCannotBeCreated(viewModel.ShortcutCompleteName, viewModel.DestinationItemPath);

await associatedInstance.RefreshIfNoWatcherExists();
}

public static async Task<bool> HandleShortcutCannotBeCreated(string shortcutName, string destinationPath)
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/ViewModels/LayoutModes/BaseLayoutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public async Task Drop(DragEventArgs e)
{
await _associatedInstance.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, _associatedInstance.FilesystemViewModel.WorkingDirectory, false, true);
e.Handled = true;

await _associatedInstance.RefreshIfNoWatcherExists();
}

deferral.Complete();
Expand Down
7 changes: 6 additions & 1 deletion src/Files.App/Views/Shells/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.MultitaskingControl;
using Files.Core.Services;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -487,6 +486,12 @@ public Task TabItemDrop(object sender, DragEventArgs e)
return SlimContentPage?.CommandsViewModel.Drop(e);
}

public async Task RefreshIfNoWatcherExists()
{
if (FilesystemViewModel.HasNoWatcher)
await Refresh_Click();
}

public async Task Refresh_Click()
{
if (InstanceViewModel.IsPageTypeSearchResults)
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Views/Shells/IShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface IShellPage : ITabItemContent, IMultiPaneInfo, IDisposable, INot

bool CanNavigateForward { get; }

Task RefreshIfNoWatcherExists();

Task Refresh_Click();

void Back_Click();
Expand Down

0 comments on commit f4ddd25

Please sign in to comment.