Skip to content

Commit

Permalink
Focus on items after they are pasted (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsieu authored Apr 12, 2020
1 parent e02a06a commit 60693ca
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 31 deletions.
12 changes: 9 additions & 3 deletions Files/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ internal set
{
IsItemSelected = true;
}
SetSelectedItemsOnUi(value);
NotifyPropertyChanged("SelectedItems");
}
}
Expand All @@ -98,6 +99,7 @@ internal set
{
IsItemSelected = true;
}
SetSelectedItemOnUi(value);
NotifyPropertyChanged("SelectedItem");
}
}
Expand All @@ -119,6 +121,10 @@ public BaseLayout()
}
}

protected abstract void SetSelectedItemOnUi(ListedItem selectedItem);

protected abstract void SetSelectedItemsOnUi(List<ListedItem> selectedItems);

private void AppSettings_LayoutModeChangeRequested(object sender, EventArgs e)
{
if (App.CurrentInstance.ContentPage != null)
Expand All @@ -144,7 +150,7 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
{
base.OnNavigatedTo(eventArgs);
// Add item jumping handler
Expand All @@ -171,9 +177,9 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
App.CurrentInstance.NavigationToolbar.CanNavigateToParent = true;
}

App.CurrentInstance.ViewModel.AddItemsToCollectionAsync(App.CurrentInstance.ViewModel.WorkingDirectory);
App.Clipboard_ContentChanged(null, null);
await App.CurrentInstance.ViewModel.RefreshItems();

App.Clipboard_ContentChanged(null, null);
App.CurrentInstance.NavigationToolbar.PathControlDisplayText = parameters;
}

Expand Down
46 changes: 26 additions & 20 deletions Files/Interacts/Interaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ public async void CutItem_Click(object sender, RoutedEventArgs e)
Clipboard.Flush();
}
public string CopySourcePath;
public IReadOnlyList<IStorageItem> ItemsToPaste;
public IReadOnlyList<IStorageItem> itemsToPaste;
public int itemsPasted;

public async void CopyItem_ClickAsync(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -908,31 +908,36 @@ public async void CopyItem_ClickAsync(object sender, RoutedEventArgs e)

public async void PasteItem_ClickAsync(object sender, RoutedEventArgs e)
{
string DestinationPath = CurrentInstance.ViewModel.WorkingDirectory;
string destinationPath = CurrentInstance.ViewModel.WorkingDirectory;

DataPackageView packageView = Clipboard.GetContent();
ItemsToPaste = await packageView.GetStorageItemsAsync();
itemsToPaste = await packageView.GetStorageItemsAsync();
HashSet<string> pastedItemPaths = new HashSet<string>();
itemsPasted = 0;
if (ItemsToPaste.Count > 3)
if (itemsToPaste.Count > 3)
{
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, itemsPasted, ItemsToPaste.Count);
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, itemsPasted, itemsToPaste.Count);
}

foreach (IStorageItem item in ItemsToPaste)
foreach (IStorageItem item in itemsToPaste)
{

if (item.IsOfType(StorageItemTypes.Folder))
{
await CloneDirectoryAsync(item.Path, DestinationPath, item.Name, false);
StorageFolder pastedFolder = await CloneDirectoryAsync(item.Path, destinationPath, item.Name, false);
CurrentInstance.ViewModel.AddFolder(pastedFolder.Path);
pastedItemPaths.Add(pastedFolder.Path);
}
else if (item.IsOfType(StorageItemTypes.File))
{
if (ItemsToPaste.Count > 3)
if (itemsToPaste.Count > 3)
{
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++itemsPasted, ItemsToPaste.Count);
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++itemsPasted, itemsToPaste.Count);
}
StorageFile ClipboardFile = await StorageFile.GetFileFromPathAsync(item.Path);
await ClipboardFile.CopyAsync(await StorageFolder.GetFolderFromPathAsync(DestinationPath), item.Name, NameCollisionOption.GenerateUniqueName);
StorageFile clipboardFile = await StorageFile.GetFileFromPathAsync(item.Path);
StorageFile pastedFile = await clipboardFile.CopyAsync(await StorageFolder.GetFolderFromPathAsync(destinationPath), item.Name, NameCollisionOption.GenerateUniqueName);
pastedItemPaths.Add(pastedFile.Path);
CurrentInstance.ViewModel.AddFile(pastedFile.Path);
}
}

Expand All @@ -952,10 +957,11 @@ public async void PasteItem_ClickAsync(object sender, RoutedEventArgs e)
}
}
}

List<ListedItem> copiedItems = CurrentInstance.ViewModel.FilesAndFolders.Where(listedItem => pastedItemPaths.Contains(listedItem.ItemPath)).ToList();
CurrentInstance.ContentPage.SelectedItems = copiedItems;
}

public async Task CloneDirectoryAsync(string SourcePath, string DestinationPath, string sourceRootName, bool suppressProgressFlyout)
public async Task<StorageFolder> CloneDirectoryAsync(string SourcePath, string DestinationPath, string sourceRootName, bool suppressProgressFlyout)
{
StorageFolder SourceFolder = await StorageFolder.GetFolderFromPathAsync(SourcePath);
StorageFolder DestinationFolder = await StorageFolder.GetFolderFromPathAsync(DestinationPath);
Expand All @@ -964,29 +970,29 @@ public async Task CloneDirectoryAsync(string SourcePath, string DestinationPath,

foreach (StorageFile fileInSourceDir in await SourceFolder.GetFilesAsync())
{
if(ItemsToPaste != null)
if(itemsToPaste != null)
{
if (ItemsToPaste.Count > 3 && !suppressProgressFlyout)
if (itemsToPaste.Count > 3 && !suppressProgressFlyout)
{
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++itemsPasted, ItemsToPaste.Count + (await SourceFolder.GetItemsAsync()).Count);
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++itemsPasted, itemsToPaste.Count + (await SourceFolder.GetItemsAsync()).Count);
}
}

await fileInSourceDir.CopyAsync(DestinationFolder, fileInSourceDir.Name, NameCollisionOption.GenerateUniqueName);
}
foreach (StorageFolder folderinSourceDir in await SourceFolder.GetFoldersAsync())
{
if (ItemsToPaste != null)
if (itemsToPaste != null)
{
if (ItemsToPaste.Count > 3 && !suppressProgressFlyout)
if (itemsToPaste.Count > 3 && !suppressProgressFlyout)
{
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++itemsPasted, ItemsToPaste.Count + (await SourceFolder.GetItemsAsync()).Count);
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++itemsPasted, itemsToPaste.Count + (await SourceFolder.GetItemsAsync()).Count);
}
}

await CloneDirectoryAsync(folderinSourceDir.Path, DestinationFolder.Path, folderinSourceDir.Name, false);
}

return createdRoot;
}

public void NewFolder_Click(object sender, RoutedEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions Files/Navigation/NavigationActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class NavigationActions
{
public async static void Refresh_Click(object sender, RoutedEventArgs e)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
var ContentOwnedViewModelInstance = App.CurrentInstance.ViewModel;
ContentOwnedViewModelInstance.AddItemsToCollectionAsync(ContentOwnedViewModelInstance.WorkingDirectory);
await ContentOwnedViewModelInstance.RefreshItems();
});
}

Expand Down
28 changes: 26 additions & 2 deletions Files/UserControls/LayoutModes/GenericFileBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ public GenericFileBrowser()
App.CurrentInstance.ViewModel.PropertyChanged += ViewModel_PropertyChanged;
}

protected override void SetSelectedItemOnUi(ListedItem selectedItem)
{
// Required to check if sequences are equal, if not it will result in an infinite loop
// between the UI Control and the BaseLayout set function
if (AllView.SelectedItem != selectedItem)
{
AllView.SelectedItem = selectedItem;
AllView.UpdateLayout();
AllView.ScrollIntoView(AllView.SelectedItem, null);
}
}
protected override void SetSelectedItemsOnUi(List<ListedItem> selectedItems)
{
// Required to check if sequences are equal, if not it will result in an infinite loop
// between the UI Control and the BaseLayout set function
if (Enumerable.SequenceEqual<ListedItem>(AllView.SelectedItems.Cast<ListedItem>(), selectedItems))
return;
AllView.SelectedItems.Clear();
foreach (ListedItem selectedItem in selectedItems)
AllView.SelectedItems.Add(selectedItem);
AllView.UpdateLayout();
AllView.ScrollIntoView(AllView.ItemsSource.Cast<ListedItem>().Last(), null);
}

private async void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "DirectorySortOption")
Expand Down Expand Up @@ -133,7 +157,7 @@ private async void AllView_DropAsync(object sender, DragEventArgs e)
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
App.CurrentInstance.InteractionOperations.itemsPasted = 0;
App.CurrentInstance.InteractionOperations.ItemsToPaste = await e.DataView.GetStorageItemsAsync();
App.CurrentInstance.InteractionOperations.itemsToPaste = await e.DataView.GetStorageItemsAsync();
foreach (IStorageItem item in await e.DataView.GetStorageItemsAsync())
{
if (item.IsOfType(StorageItemTypes.Folder))
Expand All @@ -142,7 +166,7 @@ private async void AllView_DropAsync(object sender, DragEventArgs e)
}
else
{
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++App.CurrentInstance.InteractionOperations.itemsPasted, App.CurrentInstance.InteractionOperations.ItemsToPaste.Count);
(App.CurrentInstance as ModernShellPage).UpdateProgressFlyout(InteractionOperationType.PasteItems, ++App.CurrentInstance.InteractionOperations.itemsPasted, App.CurrentInstance.InteractionOperations.itemsToPaste.Count);
await (item as StorageFile).CopyAsync(await StorageFolder.GetFolderFromPathAsync(App.CurrentInstance.ViewModel.WorkingDirectory));
}
}
Expand Down
24 changes: 24 additions & 0 deletions Files/UserControls/LayoutModes/PhotoAlbum.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Files.Filesystem;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.System;
using Windows.UI.Core;
Expand All @@ -19,6 +20,29 @@ public PhotoAlbum()
this.InitializeComponent();

}
protected override void SetSelectedItemOnUi(ListedItem selectedItem)
{
// Required to check if sequences are equal, if not it will result in an infinite loop
// between the UI Control and the BaseLayout set function
if (FileList.SelectedItem != selectedItem)
{
FileList.SelectedItem = selectedItem;
FileList.UpdateLayout();
FileList.ScrollIntoView(FileList.SelectedItem);
}
}
protected override void SetSelectedItemsOnUi(List<ListedItem> selectedItems)
{
// Required to check if sequences are equal, if not it will result in an infinite loop
// between the UI Control and the BaseLayout set function
if (Enumerable.SequenceEqual<ListedItem>(FileList.SelectedItems.Cast<ListedItem>(), selectedItems))
return;
FileList.SelectedItems.Clear();
foreach (ListedItem selectedItem in selectedItems)
FileList.SelectedItems.Add(selectedItem);
FileList.UpdateLayout();
FileList.ScrollIntoView(FileList.Items.Last());
}

private void StackPanel_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
Expand Down
32 changes: 28 additions & 4 deletions Files/View Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,12 @@ public async void LoadExtendedItemProperties(ListedItem item, uint thumbnailSize
}
}

public async void RapidAddItemsToCollectionAsync(string path)
public async Task RefreshItems()
{
await AddItemsToCollectionAsync(WorkingDirectory);
}

public async Task RapidAddItemsToCollectionAsync(string path)
{
App.CurrentInstance.NavigationToolbar.CanRefresh = false;

Expand Down Expand Up @@ -752,6 +757,16 @@ public async void RapidAddItemsToCollectionAsync(string path)
IsLoadingItems = false;
}

public void AddFolder(string folderPath)
{
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_CASE_SENSITIVE;

IntPtr hFile = FindFirstFileExFromApp(folderPath, findInfoLevel, out WIN32_FIND_DATA findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
additionalFlags);
AddFolder(findData, Directory.GetParent(folderPath).FullName);
}

private void AddFolder(WIN32_FIND_DATA findData, string pathRoot)
{
if ((App.CurrentInstance.CurrentPageType) == typeof(GenericFileBrowser) || (App.CurrentInstance.CurrentPageType == typeof(PhotoAlbum)))
Expand Down Expand Up @@ -794,6 +809,16 @@ private void AddFolder(WIN32_FIND_DATA findData, string pathRoot)
}
}

public void AddFile(string filePath)
{
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_CASE_SENSITIVE;

IntPtr hFile = FindFirstFileExFromApp(filePath, findInfoLevel, out WIN32_FIND_DATA findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero,
additionalFlags);
AddFile(findData, Directory.GetParent(filePath).FullName);
}

private void AddFile(WIN32_FIND_DATA findData, string pathRoot)
{
var itemPath = Path.Combine(pathRoot, findData.cFileName);
Expand Down Expand Up @@ -858,9 +883,9 @@ private void AddFile(WIN32_FIND_DATA findData, string pathRoot)
EmptyTextState.IsVisible = Visibility.Collapsed;
}

public void AddItemsToCollectionAsync(string path)
public async Task AddItemsToCollectionAsync(string path)
{
RapidAddItemsToCollectionAsync(path);
await RapidAddItemsToCollectionAsync(path);
return;
}

Expand Down Expand Up @@ -891,7 +916,6 @@ private async Task AddFolder(StorageFolder folder)
//}

//string tooltipString = dateCreatedText + "\n" + "Folders: " + firstFoldersText + "\n" + "Files: " + firstFilesText;

_filesAndFolders.Add(new ListedItem(folder.FolderRelativeId)
{
//FolderTooltipText = tooltipString,
Expand Down

0 comments on commit 60693ca

Please sign in to comment.