Skip to content

Commit df45bdb

Browse files
authored
Improve layout mode switching (#2887)
1 parent 7a0413a commit df45bdb

File tree

8 files changed

+139
-87
lines changed

8 files changed

+139
-87
lines changed

Files/BaseLayout.cs

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public abstract class BaseLayout : Page, INotifyPropertyChanged
5656

5757
public IShellPage ParentShellPageInstance { get; private set; } = null;
5858

59-
private bool isSearchResultPage = false;
60-
6159
public bool IsRenamingItem { get; set; } = false;
6260

61+
private NavigationArguments navigationArguments;
62+
6363
private bool isItemSelected = false;
6464

6565
public bool IsItemSelected
@@ -286,14 +286,21 @@ private void FolderSettings_LayoutModeChangeRequested(object sender, EventArgs e
286286
{
287287
var layoutType = FolderSettings.GetLayoutType(ParentShellPageInstance.FilesystemViewModel.WorkingDirectory);
288288

289-
ParentShellPageInstance.ContentFrame.Navigate(layoutType, new NavigationArguments()
289+
if (layoutType != ParentShellPageInstance.CurrentPageType)
290290
{
291-
NavPathParam = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory,
292-
AssociatedTabInstance = ParentShellPageInstance
293-
}, null);
291+
ParentShellPageInstance.ContentFrame.Navigate(layoutType, new NavigationArguments()
292+
{
293+
NavPathParam = navigationArguments.NavPathParam,
294+
IsSearchResultPage = navigationArguments.IsSearchResultPage,
295+
SearchPathParam = navigationArguments.SearchPathParam,
296+
SearchResults = navigationArguments.SearchResults,
297+
IsLayoutSwitch = true,
298+
AssociatedTabInstance = ParentShellPageInstance
299+
}, null);
294300

295-
// Remove old layout from back stack
296-
ParentShellPageInstance.ContentFrame.BackStack.RemoveAt(ParentShellPageInstance.ContentFrame.BackStack.Count - 1);
301+
// Remove old layout from back stack
302+
ParentShellPageInstance.ContentFrame.BackStack.RemoveAt(ParentShellPageInstance.ContentFrame.BackStack.Count - 1);
303+
}
297304
}
298305
}
299306

@@ -309,24 +316,23 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
309316
base.OnNavigatedTo(eventArgs);
310317
// Add item jumping handler
311318
Window.Current.CoreWindow.CharacterReceived += Page_CharacterReceived;
312-
var parameters = (NavigationArguments)eventArgs.Parameter;
313-
isSearchResultPage = parameters.IsSearchResultPage;
314-
ParentShellPageInstance = parameters.AssociatedTabInstance;
319+
navigationArguments = (NavigationArguments)eventArgs.Parameter;
320+
ParentShellPageInstance = navigationArguments.AssociatedTabInstance;
315321
IsItemSelected = false;
316322
FolderSettings.LayoutModeChangeRequested += FolderSettings_LayoutModeChangeRequested;
317323
ParentShellPageInstance.FilesystemViewModel.IsFolderEmptyTextDisplayed = false;
318324

319-
if (!isSearchResultPage)
325+
if (!navigationArguments.IsSearchResultPage)
320326
{
321327
ParentShellPageInstance.NavigationToolbar.CanRefresh = true;
322-
ParentShellPageInstance.NavigationToolbar.CanCopyPathInPage = true;
323328
string previousDir = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory;
324-
await ParentShellPageInstance.FilesystemViewModel.SetWorkingDirectoryAsync(parameters.NavPathParam);
329+
await ParentShellPageInstance.FilesystemViewModel.SetWorkingDirectoryAsync(navigationArguments.NavPathParam);
325330

326331
// pathRoot will be empty on recycle bin path
327332
var workingDir = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory;
328333
string pathRoot = Path.GetPathRoot(workingDir);
329-
if (string.IsNullOrEmpty(pathRoot) || workingDir == pathRoot)
334+
if (string.IsNullOrEmpty(pathRoot) || workingDir == pathRoot
335+
|| workingDir.StartsWith(AppSettings.RecycleBinPath)) // Can't go up from recycle bin
330336
{
331337
ParentShellPageInstance.NavigationToolbar.CanNavigateToParent = false;
332338
}
@@ -337,19 +343,30 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
337343

338344
ParentShellPageInstance.InstanceViewModel.IsPageTypeRecycleBin = workingDir.StartsWith(App.AppSettings.RecycleBinPath);
339345
ParentShellPageInstance.InstanceViewModel.IsPageTypeMtpDevice = workingDir.StartsWith("\\\\?\\");
340-
ParentShellPageInstance.FilesystemViewModel.RefreshItems(previousDir);
341-
ParentShellPageInstance.NavigationToolbar.PathControlDisplayText = parameters.NavPathParam;
342346
ParentShellPageInstance.InstanceViewModel.IsPageTypeSearchResults = false;
347+
ParentShellPageInstance.NavigationToolbar.PathControlDisplayText = navigationArguments.NavPathParam;
348+
if (!navigationArguments.IsLayoutSwitch)
349+
{
350+
ParentShellPageInstance.FilesystemViewModel.RefreshItems(previousDir);
351+
}
352+
else
353+
{
354+
ParentShellPageInstance.NavigationToolbar.CanGoForward = false;
355+
}
343356
}
344357
else
345358
{
346359
ParentShellPageInstance.NavigationToolbar.CanRefresh = false;
347-
ParentShellPageInstance.NavigationToolbar.CanCopyPathInPage = false;
360+
ParentShellPageInstance.NavigationToolbar.CanGoForward = false;
361+
ParentShellPageInstance.NavigationToolbar.CanGoBack = true; // Impose no artificial restrictions on back navigation. Even in a search results page.
348362
ParentShellPageInstance.NavigationToolbar.CanNavigateToParent = false;
349363
ParentShellPageInstance.InstanceViewModel.IsPageTypeRecycleBin = false;
350364
ParentShellPageInstance.InstanceViewModel.IsPageTypeMtpDevice = false;
351365
ParentShellPageInstance.InstanceViewModel.IsPageTypeSearchResults = true;
352-
ParentShellPageInstance.FilesystemViewModel.AddSearchResultsToCollection(parameters.SearchResults, parameters.SearchPathParam);
366+
if (!navigationArguments.IsLayoutSwitch)
367+
{
368+
ParentShellPageInstance.FilesystemViewModel.AddSearchResultsToCollection(navigationArguments.SearchResults, navigationArguments.SearchPathParam);
369+
}
353370
}
354371

355372
ParentShellPageInstance.InstanceViewModel.IsPageTypeNotHome = true; // show controls that were hidden on the home page
@@ -364,12 +381,20 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
364381
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
365382
{
366383
base.OnNavigatingFrom(e);
367-
ParentShellPageInstance.FilesystemViewModel.CancelLoadAndClearFiles(isSearchResultPage);
368384
// Remove item jumping handler
369385
Window.Current.CoreWindow.CharacterReceived -= Page_CharacterReceived;
370386
FolderSettings.LayoutModeChangeRequested -= FolderSettings_LayoutModeChangeRequested;
371387
}
372388

389+
protected override void OnNavigatedFrom(NavigationEventArgs e)
390+
{
391+
var parameter = e.Parameter as NavigationArguments;
392+
if (!parameter.IsLayoutSwitch)
393+
{
394+
ParentShellPageInstance.FilesystemViewModel.CancelLoadAndClearFiles();
395+
}
396+
}
397+
373398
private void UnloadMenuFlyoutItemByName(string nameToUnload)
374399
{
375400
if (FindName(nameToUnload) is MenuFlyoutItemBase menuItem) // Prevent crash if the MenuFlyoutItem is missing

Files/ViewModels/FolderSettingsViewModel.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public FolderLayoutModes LayoutMode
3030
if (SetProperty(ref LayoutPreference.LayoutMode, value, nameof(LayoutMode)))
3131
{
3232
UpdateLayoutPreferencesForPath(associatedInstance.FilesystemViewModel.WorkingDirectory, LayoutPreference);
33+
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
3334
}
3435
}
3536
}
@@ -82,40 +83,30 @@ public Type GetLayoutType(string folderPath)
8283
LayoutMode = FolderLayoutModes.GridView; // Grid View
8384

8485
GridViewSize = Constants.Browser.GridViewBrowser.GridViewSizeLarge; // Size
85-
86-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
8786
});
8887

8988
public RelayCommand ToggleLayoutModeGridViewMedium => new RelayCommand(() =>
9089
{
9190
LayoutMode = FolderLayoutModes.GridView; // Grid View
9291

9392
GridViewSize = Constants.Browser.GridViewBrowser.GridViewSizeMedium; // Size
94-
95-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
9693
});
9794

9895
public RelayCommand ToggleLayoutModeGridViewSmall => new RelayCommand(() =>
9996
{
10097
LayoutMode = FolderLayoutModes.GridView; // Grid View
10198

10299
GridViewSize = Constants.Browser.GridViewBrowser.GridViewSizeSmall; // Size
103-
104-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
105100
});
106101

107102
public RelayCommand ToggleLayoutModeTiles => new RelayCommand(() =>
108103
{
109104
LayoutMode = FolderLayoutModes.TilesView; // Tiles View
110-
111-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
112105
});
113106

114107
public RelayCommand ToggleLayoutModeDetailsView => new RelayCommand(() =>
115108
{
116109
LayoutMode = FolderLayoutModes.DetailsView; // Details View
117-
118-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
119110
});
120111

121112
public int GridViewSize
@@ -128,12 +119,10 @@ public int GridViewSize
128119
if (LayoutMode == FolderLayoutModes.TilesView) // Size down from tiles to list
129120
{
130121
LayoutMode = 0;
131-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
132122
}
133123
else if (LayoutMode == FolderLayoutModes.GridView && value < Constants.Browser.GridViewBrowser.GridViewSizeSmall) // Size down from grid to tiles
134124
{
135125
LayoutMode = FolderLayoutModes.TilesView;
136-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
137126
}
138127
else if (LayoutMode != FolderLayoutModes.DetailsView) // Resize grid view
139128
{
@@ -143,7 +132,6 @@ public int GridViewSize
143132
if (LayoutMode != FolderLayoutModes.GridView) // Only update layout mode if it isn't already in grid view
144133
{
145134
LayoutMode = FolderLayoutModes.GridView;
146-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
147135
}
148136
else
149137
{
@@ -158,7 +146,6 @@ public int GridViewSize
158146
if (LayoutMode == 0) // Size up from list to tiles
159147
{
160148
LayoutMode = FolderLayoutModes.TilesView;
161-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
162149
}
163150
else // Size up from tiles to grid
164151
{
@@ -168,7 +155,6 @@ public int GridViewSize
168155
if (LayoutMode != FolderLayoutModes.GridView) // Only update layout mode if it isn't already in grid view
169156
{
170157
LayoutMode = FolderLayoutModes.GridView;
171-
LayoutModeChangeRequested?.Invoke(this, EventArgs.Empty);
172158
}
173159
else
174160
{

Files/ViewModels/ItemViewModel.cs

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -425,38 +425,15 @@ private void WorkingDirectoryChanged(string singleItemOverride = null)
425425
}
426426
}
427427

428-
public void CancelLoadAndClearFiles(bool isSearchResultPage = false)
428+
public void CancelLoadAndClearFiles()
429429
{
430430
Debug.WriteLine("CancelLoadAndClearFiles");
431-
if (!isSearchResultPage)
431+
CloseWatcher();
432+
if (IsLoadingItems)
432433
{
433-
CloseWatcher();
434-
435-
AssociatedInstance.NavigationToolbar.CanRefresh = true;
436-
if (IsLoadingItems == false)
437-
{
438-
return;
439-
}
440-
441434
_addFilesCTS.Cancel();
442-
AssociatedInstance.NavigationToolbar.CanGoForward = true;
443435
}
444-
else
445-
{
446-
AssociatedInstance.NavigationToolbar.CanRefresh = false;
447-
AssociatedInstance.NavigationToolbar.CanGoForward = false;
448-
AssociatedInstance.NavigationToolbar.CanNavigateToParent = false;
449-
AssociatedInstance.NavigationToolbar.CanCopyPathInPage = false;
450-
}
451-
452-
AssociatedInstance.NavigationToolbar.CanGoBack = true; // Impose no artificial restrictions on back navigation. Even in a search results page.
453436
_filesAndFolders.Clear();
454-
455-
if (!(WorkingDirectory?.StartsWith(AppSettings.RecycleBinPath) ?? false) && !isSearchResultPage)
456-
{
457-
// Can't go up from recycle bin
458-
AssociatedInstance.NavigationToolbar.CanNavigateToParent = true;
459-
}
460437
}
461438

462439
public void OrderFiles(IList<ListedItem> orderedList = null)
@@ -594,24 +571,39 @@ private IList<ListedItem> OrderFiles2(IList<ListedItem> listToSort)
594571
return orderedList;
595572
}
596573

597-
private bool _isLoadingItems = false;
574+
private bool isLoadingIndicatorActive = false;
598575

599-
public bool IsLoadingItems
576+
public bool IsLoadingIndicatorActive
600577
{
601578
get
602579
{
603-
return _isLoadingItems;
580+
return isLoadingIndicatorActive;
604581
}
605-
internal set
582+
set
606583
{
607-
if (_isLoadingItems != value)
584+
if (isLoadingIndicatorActive != value)
608585
{
609-
_isLoadingItems = value;
610-
NotifyPropertyChanged(nameof(IsLoadingItems));
586+
isLoadingIndicatorActive = value;
587+
NotifyPropertyChanged(nameof(IsLoadingIndicatorActive));
611588
}
612589
}
613590
}
614591

592+
private bool isLoadingItems = false;
593+
594+
public bool IsLoadingItems
595+
{
596+
get
597+
{
598+
return isLoadingItems;
599+
}
600+
set
601+
{
602+
isLoadingItems = value;
603+
IsLoadingIndicatorActive = value;
604+
}
605+
}
606+
615607
// This works for recycle bin as well as GetFileFromPathAsync/GetFolderFromPathAsync work
616608
// for file inside the recycle bin (but not on the recycle bin folder itself)
617609
public async void LoadExtendedItemProperties(ListedItem item, uint thumbnailSize = 20)
@@ -786,7 +778,7 @@ public async void RapidAddItemsToCollectionAsync(string path, string previousDir
786778
var orderedList = OrderFiles2(cacheEntry.FileList);
787779
OrderFiles(orderedList);
788780
Debug.WriteLine($"Loading of items from cache in {WorkingDirectory} completed in {stopwatch.ElapsedMilliseconds} milliseconds.\n");
789-
IsLoadingItems = false;
781+
IsLoadingIndicatorActive = false;
790782
}
791783

792784
if (path.StartsWith(AppSettings.RecycleBinPath))

Files/Views/LayoutModes/GenericFileBrowser.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@
636636
<muxc:ProgressBar
637637
x:Name="progBar"
638638
VerticalAlignment="Top"
639-
x:Load="{x:Bind ParentShellPageInstance.FilesystemViewModel.IsLoadingItems, Mode=OneWay}"
639+
x:Load="{x:Bind ParentShellPageInstance.FilesystemViewModel.IsLoadingIndicatorActive, Mode=OneWay}"
640640
Background="Transparent"
641641
IsIndeterminate="True" />
642642
<TextBlock

Files/Views/LayoutModes/GenericFileBrowser.xaml.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
112112
AllView.LoadingRow += AllView_LoadingRow;
113113
AppSettings.ThemeModeChanged += AppSettings_ThemeModeChanged;
114114
ViewModel_PropertyChanged(null, new PropertyChangedEventArgs("DirectorySortOption"));
115+
var parameters = (NavigationArguments)eventArgs.Parameter;
116+
if (parameters.IsLayoutSwitch)
117+
{
118+
ReloadItemIcons();
119+
}
120+
}
121+
122+
private void ReloadItemIcons()
123+
{
124+
var rows = new List<DataGridRow>();
125+
Interaction.FindChildren<DataGridRow>(rows, AllView);
126+
foreach (ListedItem listedItem in ParentShellPageInstance.FilesystemViewModel.FilesAndFolders)
127+
{
128+
listedItem.ItemPropertiesInitialized = false;
129+
if (rows.Any(x => x.DataContext == listedItem))
130+
{
131+
ParentShellPageInstance.FilesystemViewModel.LoadExtendedItemProperties(listedItem);
132+
listedItem.ItemPropertiesInitialized = true;
133+
}
134+
}
115135
}
116136

117137
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)

Files/Views/LayoutModes/GridViewBrowser.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@
889889
<muxc:ProgressBar
890890
x:Name="progBar"
891891
VerticalAlignment="Top"
892-
x:Load="{x:Bind ParentShellPageInstance.FilesystemViewModel.IsLoadingItems, Mode=OneWay}"
892+
x:Load="{x:Bind ParentShellPageInstance.FilesystemViewModel.IsLoadingIndicatorActive, Mode=OneWay}"
893893
Background="Transparent"
894894
IsIndeterminate="True" />
895895
<TextBlock

0 commit comments

Comments
 (0)