Skip to content

Commit ca7e4da

Browse files
authored
[Farewell, Interaction]: Step 1 of removing the Interaction class (#4016)
* rm1 * Use BaseLayoutCommandsViewModel * Fix build * Fix build 2 * Remove event handlers in favour of binding * Fix commands not being bound * Made functions in BaseLayoutCommandImplementationModel virtual * Fixed build error * Remove region directives from FileProperties * Fix build error Co-authored-by: d2dyno006 <[email protected]>
1 parent 04f415c commit ca7e4da

26 files changed

+784
-359
lines changed

Files/BaseLayout.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Files.Extensions;
66
using Files.Filesystem;
77
using Files.Helpers;
8+
using Files.Interacts;
89
using Files.UserControls;
910
using Files.ViewModels;
1011
using Files.Views;
@@ -37,9 +38,9 @@ namespace Files
3738
/// <summary>
3839
/// The base class which every layout page must derive from
3940
/// </summary>
40-
public abstract class BaseLayout : Page, INotifyPropertyChanged
41+
public abstract class BaseLayout : Page, IBaseLayout, INotifyPropertyChanged
4142
{
42-
private NamedPipeAsAppServiceConnection Connection => ParentShellPageInstance?.ServiceConnection;
43+
protected NamedPipeAsAppServiceConnection Connection => ParentShellPageInstance?.ServiceConnection;
4344

4445
public SelectedItemsPropertiesViewModel SelectedItemsPropertiesViewModel { get; }
4546

@@ -59,6 +60,8 @@ public abstract class BaseLayout : Page, INotifyPropertyChanged
5960

6061
public MenuFlyout BaseLayoutItemContextFlyout { get; set; }
6162

63+
public BaseLayoutCommandsViewModel CommandsViewModel { get; protected set; }
64+
6265
public IShellPage ParentShellPageInstance { get; private set; } = null;
6366

6467
public bool IsRenamingItem { get; set; } = false;
@@ -172,6 +175,8 @@ public BaseLayout()
172175
dragOverTimer = timerQueue.CreateTimer();
173176
}
174177

178+
protected abstract void InitializeCommandsViewModel();
179+
175180
public abstract void FocusFileList();
176181

177182
public abstract void SelectAllItems();
@@ -297,7 +302,7 @@ public virtual void SetItemOpacity(ListedItem item)
297302

298303
private void FolderSettings_LayoutModeChangeRequested(object sender, LayoutModeEventArgs e)
299304
{
300-
if (ParentShellPageInstance.ContentPage != null)
305+
if (ParentShellPageInstance.SlimContentPage != null)
301306
{
302307
var layoutType = FolderSettings.GetLayoutType(ParentShellPageInstance.FilesystemViewModel.WorkingDirectory);
303308

@@ -334,6 +339,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
334339
Window.Current.CoreWindow.CharacterReceived += Page_CharacterReceived;
335340
navigationArguments = (NavigationArguments)eventArgs.Parameter;
336341
ParentShellPageInstance = navigationArguments.AssociatedTabInstance;
342+
InitializeCommandsViewModel();
337343
IsItemSelected = false;
338344
FolderSettings.LayoutModeChangeRequested += FolderSettings_LayoutModeChangeRequested;
339345
ParentShellPageInstance.FilesystemViewModel.IsFolderEmptyTextDisplayed = false;
@@ -793,7 +799,7 @@ public void RightClickItemContextMenu_Opening(object sender, object e)
793799
}
794800

795801
//check the file extension of the selected item
796-
ParentShellPageInstance.ContentPage.SelectedItemsPropertiesViewModel.CheckFileExtension();
802+
SelectedItemsPropertiesViewModel.CheckFileExtension();
797803
}
798804

799805
protected virtual void Page_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
@@ -879,7 +885,7 @@ protected async void Item_DragStarting(object sender, DragStartingEventArgs e)
879885
{
880886
List<IStorageItem> selectedStorageItems = new List<IStorageItem>();
881887

882-
foreach (ListedItem item in ParentShellPageInstance.ContentPage.SelectedItems)
888+
foreach (ListedItem item in ParentShellPageInstance.SlimContentPage.SelectedItems)
883889
{
884890
if (item is ShortcutItem)
885891
{
@@ -943,7 +949,7 @@ protected async void Item_DragOver(object sender, DragEventArgs e)
943949
{
944950
dragOverItem = null;
945951
dragOverTimer.Stop();
946-
ParentShellPageInstance.InteractionOperations.OpenItem_Click(null, null);
952+
ParentShellPageInstance.InteractionOperations.OpenSelectedItems(false);
947953
}
948954
}, TimeSpan.FromMilliseconds(1000), false);
949955
}
@@ -1002,7 +1008,7 @@ protected async void Item_Drop(object sender, DragEventArgs e)
10021008
ListedItem rowItem = GetItemFromElement(sender);
10031009
if (rowItem != null)
10041010
{
1005-
await ParentShellPageInstance.InteractionOperations.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, (rowItem as ShortcutItem)?.TargetPath ?? rowItem.ItemPath, true);
1011+
await ParentShellPageInstance.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, (rowItem as ShortcutItem)?.TargetPath ?? rowItem.ItemPath, true);
10061012
}
10071013
deferral.Complete();
10081014
}
@@ -1076,5 +1082,7 @@ public void BaseLayout_PointerWheelChanged(object sender, PointerRoutedEventArgs
10761082
e.Handled = true;
10771083
}
10781084
}
1085+
1086+
public abstract void Dispose();
10791087
}
10801088
}

Files/Dialogs/ConfirmDeleteDialog.xaml.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,29 @@ public enum DialogResult
1414

1515
public sealed partial class ConfirmDeleteDialog : ContentDialog
1616
{
17-
private SelectedItemsPropertiesViewModel SelectedItemsPropertiesViewModel { get; set; } = null;
18-
1917
public bool PermanentlyDelete { get; set; }
2018

2119
public string Description { get; set; }
2220

2321
public DialogResult Result { get; set; }
2422

25-
public ConfirmDeleteDialog(bool deleteFromRecycleBin, bool permanently, SelectedItemsPropertiesViewModel propertiesViewModel)
23+
public ConfirmDeleteDialog(bool deleteFromRecycleBin, bool permanently, int itemsSelected)
2624
{
2725
this.InitializeComponent();
2826

2927
Result = DialogResult.Nothing; //clear the result in case the value is set from last time
3028
PermanentlyDelete = permanently;
31-
SelectedItemsPropertiesViewModel = propertiesViewModel;
3229

3330
// If deleting from recycle bin disable "permanently delete" option
3431
chkPermanentlyDelete.IsEnabled = !deleteFromRecycleBin;
3532

36-
if (SelectedItemsPropertiesViewModel.SelectedItemsCount == 1)
33+
if (itemsSelected == 1)
3734
{
3835
Description = "ConfirmDeleteDialogDeleteOneItem/Text".GetLocalized();
3936
}
4037
else
4138
{
42-
Description = string.Format("ConfirmDeleteDialogDeleteMultipleItems/Text".GetLocalized(), SelectedItemsPropertiesViewModel.SelectedItemsCount);
39+
Description = string.Format("ConfirmDeleteDialogDeleteMultipleItems/Text".GetLocalized(), itemsSelected);
4340
}
4441
}
4542

Files/Extensions/EnumExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace Files.Extensions
5+
{
6+
public static class EnumExtensions
7+
{
8+
public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
9+
{
10+
if (!typeof(TEnum).GetTypeInfo().IsEnum)
11+
{
12+
throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum.");
13+
}
14+
return (TEnum)Enum.Parse(typeof(TEnum), text);
15+
}
16+
}
17+
}

Files/Files.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
<Compile Include="Enums\FolderLayout.cs" />
185185
<Compile Include="Enums\FolderLayoutModes.cs" />
186186
<Compile Include="EventArguments\LayoutPreferenceEventArgs.cs" />
187+
<Compile Include="Extensions\EnumExtensions.cs" />
187188
<Compile Include="Extensions\TaskExtensions.cs" />
188189
<Compile Include="Filesystem\FolderHelpers.cs" />
189190
<Compile Include="Filesystem\LibraryManager.cs" />
@@ -228,9 +229,15 @@
228229
<Compile Include="Helpers\FileListCache\PersistentSQLiteCacheAdapter.cs" />
229230
<Compile Include="Helpers\ExtensionManager.cs" />
230231
<Compile Include="Helpers\IntervalSampler.cs" />
232+
<Compile Include="Helpers\QuickLookHelpers.cs" />
231233
<Compile Include="Helpers\RegistryHelper.cs" />
234+
<Compile Include="Helpers\SidebarHelpers.cs" />
232235
<Compile Include="Helpers\XamlHelpers\DependencyObjectHelpers.cs" />
233236
<Compile Include="Helpers\XamlHelpers\SystemTypeToXaml.cs" />
237+
<Compile Include="IBaseLayout.cs" />
238+
<Compile Include="Interacts\BaseLayoutCommandImplementationModel.cs" />
239+
<Compile Include="Interacts\BaseLayoutCommandsViewModel.cs" />
240+
<Compile Include="Interacts\IBaseLayoutCommandImplementationModel.cs" />
234241
<Compile Include="Interacts\IStatusCenterActions.cs" />
235242
<Compile Include="UserControls\FilePreviews\BasicPreview.xaml.cs">
236243
<DependentUpon>BasicPreview.xaml</DependentUpon>

Files/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ await DialogDisplayHelper.ShowDialogAsync(
294294

295295
if (copiedListedItems.Count > 0)
296296
{
297-
associatedInstance.ContentPage.AddSelectedItemsOnUi(copiedListedItems);
298-
associatedInstance.ContentPage.FocusSelectedItems();
297+
associatedInstance.SlimContentPage.AddSelectedItemsOnUi(copiedListedItems);
298+
associatedInstance.SlimContentPage.FocusSelectedItems();
299299
}
300300
}, Windows.UI.Core.CoreDispatcherPriority.Low);
301301
}
@@ -515,8 +515,8 @@ await DialogDisplayHelper.ShowDialogAsync(
515515

516516
if (movedListedItems.Count > 0)
517517
{
518-
associatedInstance.ContentPage.AddSelectedItemsOnUi(movedListedItems);
519-
associatedInstance.ContentPage.FocusSelectedItems();
518+
associatedInstance.SlimContentPage.AddSelectedItemsOnUi(movedListedItems);
519+
associatedInstance.SlimContentPage.FocusSelectedItems();
520520
}
521521
}, Windows.UI.Core.CoreDispatcherPriority.Low);
522522
}

Files/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public async Task<ReturnResult> DeleteItemsAsync(IEnumerable<IStorageItemWithPat
117117
ConfirmDeleteDialog dialog = new ConfirmDeleteDialog(
118118
deleteFromRecycleBin,
119119
!deleteFromRecycleBin ? permanently : deleteFromRecycleBin,
120-
associatedInstance.ContentPage.SelectedItemsPropertiesViewModel);
120+
associatedInstance.SlimContentPage.SelectedItems.Count);
121121

122122
if (Interacts.Interaction.IsAnyContentDialogOpen())
123123
{
@@ -221,7 +221,7 @@ public async Task<ReturnResult> DeleteItemAsync(IStorageItemWithPath source, boo
221221
ConfirmDeleteDialog dialog = new ConfirmDeleteDialog(
222222
deleteFromRecycleBin,
223223
permanently,
224-
associatedInstance.ContentPage.SelectedItemsPropertiesViewModel);
224+
associatedInstance.SlimContentPage.SelectedItems.Count);
225225

226226
if (Interacts.Interaction.IsAnyContentDialogOpen())
227227
{
@@ -299,7 +299,7 @@ public async Task<ReturnResult> DeleteItemAsync(IStorageItem source, bool showDi
299299
ConfirmDeleteDialog dialog = new ConfirmDeleteDialog(
300300
deleteFromRecycleBin,
301301
permanently,
302-
associatedInstance.ContentPage.SelectedItemsPropertiesViewModel);
302+
associatedInstance.SlimContentPage.SelectedItems.Count);
303303

304304
if (Interacts.Interaction.IsAnyContentDialogOpen())
305305
{
@@ -424,7 +424,7 @@ public async Task<ReturnResult> CopyItemsAsync(IEnumerable<IStorageItemWithPath>
424424
IStorageHistory history;
425425
List<IStorageHistory> rawStorageHistory = new List<IStorageHistory>();
426426

427-
associatedInstance.ContentPage.ClearSelection();
427+
associatedInstance.SlimContentPage.ClearSelection();
428428
float progress;
429429
for (int i = 0; i < source.Count(); i++)
430430
{
@@ -483,7 +483,7 @@ public async Task<ReturnResult> CopyItemAsync(IStorageItemWithPath source, strin
483483
var sw = new Stopwatch();
484484
sw.Start();
485485

486-
associatedInstance.ContentPage.ClearSelection();
486+
associatedInstance.SlimContentPage.ClearSelection();
487487
IStorageHistory history = await filesystemOperations.CopyAsync(source, destination, banner.Progress, banner.ErrorCode, cancellationToken);
488488
((IProgress<float>)banner.Progress).Report(100.0f);
489489

@@ -596,7 +596,7 @@ public async Task<ReturnResult> MoveItemsAsync(IEnumerable<IStorageItemWithPath>
596596
IStorageHistory history;
597597
var rawStorageHistory = new List<IStorageHistory>();
598598

599-
associatedInstance.ContentPage.ClearSelection();
599+
associatedInstance.SlimContentPage.ClearSelection();
600600
float progress;
601601
for (int i = 0; i < source.Count(); i++)
602602
{
@@ -655,7 +655,7 @@ public async Task<ReturnResult> MoveItemAsync(IStorageItemWithPath source, strin
655655
var sw = new Stopwatch();
656656
sw.Start();
657657

658-
associatedInstance.ContentPage.ClearSelection();
658+
associatedInstance.SlimContentPage.ClearSelection();
659659
IStorageHistory history = await filesystemOperations.MoveAsync(source, destination, banner.Progress, banner.ErrorCode, cancellationToken);
660660
((IProgress<float>)banner.Progress).Report(100.0f);
661661

Files/Helpers/QuickLookHelpers.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using Microsoft.Toolkit.Uwp;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Threading.Tasks;
6+
using Windows.ApplicationModel.Core;
7+
using Windows.Foundation.Collections;
8+
using Windows.UI.Core;
9+
10+
namespace Files.Helpers
11+
{
12+
public static class QuickLookHelpers
13+
{
14+
public static async void ToggleQuickLook(IShellPage associatedInstance)
15+
{
16+
try
17+
{
18+
if (associatedInstance.SlimContentPage.IsItemSelected && !associatedInstance.SlimContentPage.IsRenamingItem)
19+
{
20+
Debug.WriteLine("Toggle QuickLook");
21+
if (associatedInstance.ServiceConnection != null)
22+
{
23+
await associatedInstance.ServiceConnection.SendMessageAsync(new ValueSet()
24+
{
25+
{ "path", associatedInstance.SlimContentPage.SelectedItem.ItemPath },
26+
{ "Arguments", "ToggleQuickLook" }
27+
});
28+
}
29+
}
30+
}
31+
catch (FileNotFoundException)
32+
{
33+
await DialogDisplayHelper.ShowDialogAsync("FileNotFoundDialog/Title".GetLocalized(), "FileNotFoundPreviewDialog/Text".GetLocalized());
34+
associatedInstance.NavigationToolbar.CanRefresh = false;
35+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
36+
{
37+
var ContentOwnedViewModelInstance = associatedInstance.FilesystemViewModel;
38+
ContentOwnedViewModelInstance?.RefreshItems(null);
39+
});
40+
}
41+
}
42+
}
43+
}

Files/Helpers/RecycleBinHelpers.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Files.Common;
2+
using Microsoft.Toolkit.Uwp;
23
using Newtonsoft.Json;
34
using System;
45
using System.Collections.Generic;
@@ -8,6 +9,7 @@
89
using Windows.ApplicationModel.AppService;
910
using Windows.Foundation.Collections;
1011
using Windows.Storage;
12+
using Windows.UI.Xaml.Controls;
1113

1214
namespace Files.Helpers
1315
{
@@ -80,6 +82,36 @@ public bool IsPathUnderRecycleBin(string path)
8082
return recycleBinPathRegex.IsMatch(path);
8183
}
8284

85+
public static void EmptyRecycleBin(IShellPage associatedInstance)
86+
{
87+
new RecycleBinHelpers(associatedInstance).EmptyRecycleBin();
88+
}
89+
90+
public async void EmptyRecycleBin()
91+
{
92+
var ConfirmEmptyBinDialog = new ContentDialog()
93+
{
94+
Title = "ConfirmEmptyBinDialogTitle".GetLocalized(),
95+
Content = "ConfirmEmptyBinDialogContent".GetLocalized(),
96+
PrimaryButtonText = "ConfirmEmptyBinDialog/PrimaryButtonText".GetLocalized(),
97+
SecondaryButtonText = "ConfirmEmptyBinDialog/SecondaryButtonText".GetLocalized()
98+
};
99+
100+
ContentDialogResult result = await ConfirmEmptyBinDialog.ShowAsync();
101+
102+
if (result == ContentDialogResult.Primary)
103+
{
104+
if (Connection != null)
105+
{
106+
var value = new ValueSet();
107+
value.Add("Arguments", "RecycleBin");
108+
value.Add("action", "Empty");
109+
// Send request to fulltrust process to empty recyclebin
110+
await Connection.SendMessageAsync(value);
111+
}
112+
}
113+
}
114+
83115
#region IDisposable
84116

85117
public void Dispose()

Files/Helpers/SidebarHelpers.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Files.Filesystem;
2+
using System.Collections.Generic;
3+
4+
namespace Files.Helpers
5+
{
6+
public static class SidebarHelpers
7+
{
8+
public static void UnpinItems(List<ListedItem> itemsToUnpin)
9+
{
10+
foreach (var item in itemsToUnpin)
11+
{
12+
App.SidebarPinnedController.Model.RemoveItem(item.ItemPath);
13+
}
14+
}
15+
16+
public static void PinItems(List<ListedItem> itemsToPin)
17+
{
18+
foreach (ListedItem listedItem in itemsToPin)
19+
{
20+
App.SidebarPinnedController.Model.AddItem(listedItem.ItemPath);
21+
}
22+
}
23+
}
24+
}

Files/Helpers/ThemeHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Files.Extensions;
2+
using System;
23
using Windows.Storage;
34
using Windows.UI;
45
using Windows.UI.ViewManagement;
@@ -29,7 +30,7 @@ public static ElementTheme RootTheme
2930

3031
if (!string.IsNullOrEmpty(savedTheme))
3132
{
32-
return Interacts.Interaction.GetEnum<ElementTheme>(savedTheme);
33+
return EnumExtensions.GetEnum<ElementTheme>(savedTheme);
3334
}
3435
else
3536
{

0 commit comments

Comments
 (0)