forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseGroupableLayoutPage.cs
More file actions
424 lines (344 loc) · 13.5 KB
/
Copy pathBaseGroupableLayoutPage.cs
File metadata and controls
424 lines (344 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) Files Community
// Licensed under the MIT License.
using CommunityToolkit.WinUI;
using Files.App.ViewModels.Layouts;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using System.Runtime.InteropServices;
using Windows.System;
using Windows.UI.Core;
using Windows.Win32;
namespace Files.App.Views.Layouts
{
/// <summary>
/// Represents layout page that can be grouped by.
/// </summary>
public abstract class BaseGroupableLayoutPage : BaseLayoutPage
{
// Constants
private const int KEY_DOWN_MASK = 0x8000;
// Fields
protected int NextRenameIndex = 0;
// Properties
protected abstract ListViewBase ListViewBase { get; }
protected abstract SemanticZoom RootZoom { get; }
protected override ItemsControl ItemsControl => ListViewBase;
// Constructor
public BaseGroupableLayoutPage() : base()
{
}
// Abstract methods
protected abstract void ItemManipulationModel_AddSelectedItemInvoked(object? sender, ListedItem e);
protected abstract void ItemManipulationModel_RemoveSelectedItemInvoked(object? sender, ListedItem e);
protected abstract void ItemManipulationModel_FocusSelectedItemsInvoked(object? sender, EventArgs e);
protected abstract void ItemManipulationModel_ScrollIntoViewInvoked(object? sender, ListedItem e);
protected abstract void ItemManipulationModel_ScrollToTopInvoked(object? sender, EventArgs e);
protected abstract void FileList_PreviewKeyDown(object sender, KeyRoutedEventArgs e);
protected abstract void EndRename(TextBox textBox);
// Overridden methods
protected override void InitializeCommandsViewModel()
{
CommandsViewModel = new BaseLayoutViewModel(ParentShellPageInstance, ItemManipulationModel);
}
protected override void HookEvents()
{
UnhookEvents();
ItemManipulationModel.FocusFileListInvoked += ItemManipulationModel_FocusFileListInvoked;
ItemManipulationModel.SelectAllItemsInvoked += ItemManipulationModel_SelectAllItemsInvoked;
ItemManipulationModel.ClearSelectionInvoked += ItemManipulationModel_ClearSelectionInvoked;
ItemManipulationModel.InvertSelectionInvoked += ItemManipulationModel_InvertSelectionInvoked;
ItemManipulationModel.AddSelectedItemInvoked += ItemManipulationModel_AddSelectedItemInvoked;
ItemManipulationModel.RemoveSelectedItemInvoked += ItemManipulationModel_RemoveSelectedItemInvoked;
ItemManipulationModel.FocusSelectedItemsInvoked += ItemManipulationModel_FocusSelectedItemsInvoked;
ItemManipulationModel.StartRenameItemInvoked += ItemManipulationModel_StartRenameItemInvoked;
ItemManipulationModel.ScrollIntoViewInvoked += ItemManipulationModel_ScrollIntoViewInvoked;
ItemManipulationModel.ScrollToTopInvoked += ItemManipulationModel_ScrollToTopInvoked;
ItemManipulationModel.RefreshItemThumbnailInvoked += ItemManipulationModel_RefreshItemThumbnail;
ItemManipulationModel.RefreshItemsThumbnailInvoked += ItemManipulationModel_RefreshItemsThumbnail;
}
protected override void UnhookEvents()
{
if (ItemManipulationModel is null)
return;
ItemManipulationModel.FocusFileListInvoked -= ItemManipulationModel_FocusFileListInvoked;
ItemManipulationModel.SelectAllItemsInvoked -= ItemManipulationModel_SelectAllItemsInvoked;
ItemManipulationModel.ClearSelectionInvoked -= ItemManipulationModel_ClearSelectionInvoked;
ItemManipulationModel.InvertSelectionInvoked -= ItemManipulationModel_InvertSelectionInvoked;
ItemManipulationModel.AddSelectedItemInvoked -= ItemManipulationModel_AddSelectedItemInvoked;
ItemManipulationModel.RemoveSelectedItemInvoked -= ItemManipulationModel_RemoveSelectedItemInvoked;
ItemManipulationModel.FocusSelectedItemsInvoked -= ItemManipulationModel_FocusSelectedItemsInvoked;
ItemManipulationModel.StartRenameItemInvoked -= ItemManipulationModel_StartRenameItemInvoked;
ItemManipulationModel.ScrollIntoViewInvoked -= ItemManipulationModel_ScrollIntoViewInvoked;
ItemManipulationModel.ScrollToTopInvoked -= ItemManipulationModel_ScrollToTopInvoked;
ItemManipulationModel.RefreshItemThumbnailInvoked -= ItemManipulationModel_RefreshItemThumbnail;
ItemManipulationModel.RefreshItemsThumbnailInvoked -= ItemManipulationModel_RefreshItemsThumbnail;
}
protected override void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
{
if (ParentShellPageInstance is null ||
ParentShellPageInstance.CurrentPageType != this.GetType() ||
IsRenamingItem)
return;
// Don't block the various uses of enter key (key 13)
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(XamlRoot);
var isHeaderFocused = DependencyObjectHelpers.FindParent<DataGridHeader>(focusedElement) is not null;
if (InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Enter) == CoreVirtualKeyStates.Down ||
(focusedElement is Button && !isHeaderFocused) || // Allow jumpstring when header is focused
focusedElement is TextBox ||
focusedElement is PasswordBox ||
DependencyObjectHelpers.FindParent<ContentDialog>(focusedElement) is not null)
return;
base.Page_CharacterReceived(sender, args);
}
// Virtual methods
protected virtual async void ItemManipulationModel_RefreshItemsThumbnail(object? sender, EventArgs e)
{
await ReloadSelectedItemsIconAsync();
}
protected virtual async void ItemManipulationModel_RefreshItemThumbnail(object? sender, EventArgs args)
{
await ReloadSelectedItemIconAsync();
}
protected virtual async Task ReloadSelectedItemIconAsync()
{
if (ParentShellPageInstance?.SlimContentPage?.SelectedItem is null)
return;
ParentShellPageInstance.ShellViewModel.CancelExtendedPropertiesLoading();
ParentShellPageInstance.SlimContentPage.SelectedItem.ItemPropertiesInitialized = false;
await ParentShellPageInstance.ShellViewModel.LoadExtendedItemPropertiesAsync(ParentShellPageInstance.SlimContentPage.SelectedItem);
if (ParentShellPageInstance.ShellViewModel.EnabledGitProperties is not GitProperties.None &&
ParentShellPageInstance.SlimContentPage.SelectedItem is IGitItem gitItem)
{
await ParentShellPageInstance.ShellViewModel.LoadGitPropertiesAsync(gitItem);
}
}
protected virtual async Task ReloadSelectedItemsIconAsync()
{
if (ParentShellPageInstance?.SlimContentPage?.SelectedItems is null)
return;
ParentShellPageInstance.ShellViewModel.CancelExtendedPropertiesLoading();
foreach (var selectedItem in ParentShellPageInstance.SlimContentPage.SelectedItems)
{
selectedItem.ItemPropertiesInitialized = false;
await ParentShellPageInstance.ShellViewModel.LoadExtendedItemPropertiesAsync(selectedItem);
}
if (ParentShellPageInstance.ShellViewModel.EnabledGitProperties is not GitProperties.None)
{
await Task.WhenAll(ParentShellPageInstance.SlimContentPage.SelectedItems.Select(item =>
{
if (item is IGitItem gitItem)
return ParentShellPageInstance.ShellViewModel.LoadGitPropertiesAsync(gitItem);
return Task.CompletedTask;
}));
}
}
protected virtual void ItemManipulationModel_FocusFileListInvoked(object? sender, EventArgs e)
{
try
{
if (App.AppModel.IsMainWindowClosed)
return;
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
var isFileListFocused = DependencyObjectHelpers.FindParent<ListViewBase>(focusedElement) == ItemsControl;
if (!isFileListFocused)
ListViewBase.Focus(FocusState.Programmatic);
}
catch
{
// Handle exception in case the window is closed during the operation
}
}
protected virtual void ItemManipulationModel_SelectAllItemsInvoked(object? sender, EventArgs e)
{
ListViewBase.SelectAll();
}
protected virtual void ItemManipulationModel_ClearSelectionInvoked(object? sender, EventArgs e)
{
ListViewBase.SelectedItems.Clear();
}
protected virtual void ItemManipulationModel_InvertSelectionInvoked(object? sender, EventArgs e)
{
if (SelectedItems.Count < GetAllItems().Count() / 2)
{
var oldSelectedItems = SelectedItems.ToList();
ItemManipulationModel.SelectAllItems();
ItemManipulationModel.RemoveSelectedItems(oldSelectedItems);
return;
}
List<ListedItem> newSelectedItems = GetAllItems()
.Cast<ListedItem>()
.Except(SelectedItems)
.ToList();
ItemManipulationModel.SetSelectedItems(newSelectedItems);
}
protected virtual void ItemManipulationModel_StartRenameItemInvoked(object? sender, EventArgs e)
{
StartRenameItem();
}
protected override void ZoomIn()
{
RootZoom.IsZoomedInViewActive = true;
}
protected virtual void FileList_SelectionChanged(object sender, SelectionChangedEventArgs? e)
{
if (e is null && SelectedItems?.Count == 0)
return;
if (e is not null && e.AddedItems.Count == 0 && e.RemovedItems.Count == 0)
return;
var selectedItems = ListViewBase.SelectedItems.Cast<ListedItem>().Where(x => x is not null).ToList();
if (SelectedItems is not null && SelectedItems.SequenceEqual(selectedItems))
return;
SelectedItems = selectedItems;
if (e is null)
return;
OnSelectionChanged(e);
}
protected abstract void OnSelectionChanged(SelectionChangedEventArgs e);
protected virtual void SelectionRectangle_SelectionEnded(object? sender, EventArgs e)
{
ListViewBase.Focus(FocusState.Programmatic);
}
protected virtual void StartRenameItem(string itemNameTextBox)
{
RenamingItem = SelectedItem;
if (RenamingItem is null)
return;
int extensionLength = RenamingItem.FileExtension?.Length ?? 0;
ListViewItem? listViewItem = ListViewBase.ContainerFromItem(RenamingItem) as ListViewItem;
if (listViewItem is null)
return;
TextBox? textBox = null;
TextBlock? textBlock = listViewItem.FindDescendant("ItemName") as TextBlock;
textBox = listViewItem.FindDescendant(itemNameTextBox) as TextBox;
textBox!.Text = textBlock!.Text;
OldItemName = textBlock.Text;
textBlock.Visibility = Visibility.Collapsed;
textBox.Visibility = Visibility.Visible;
if (textBox.FindParent<Grid>() is null)
{
textBlock.Visibility = Visibility.Visible;
textBox.Visibility = Visibility.Collapsed;
return;
}
Grid.SetColumnSpan(textBox.FindParent<Grid>(), 8);
textBox.Focus(FocusState.Pointer);
textBox.LostFocus += RenameTextBox_LostFocus;
textBox.KeyDown += RenameTextBox_KeyDown;
int selectedTextLength = SelectedItem.Name.Length;
if (!SelectedItem.IsShortcut && UserSettingsService.FoldersSettingsService.ShowFileExtensions)
selectedTextLength -= extensionLength;
textBox.Select(0, selectedTextLength);
IsRenamingItem = true;
}
protected virtual async Task CommitRenameAsync(TextBox textBox)
{
EndRename(textBox);
string newItemName = textBox.Text.Trim().TrimEnd('.');
await UIFilesystemHelpers.RenameFileItemAsync(RenamingItem, newItemName, ParentShellPageInstance);
}
protected virtual async void RenameTextBox_LostFocus(object sender, RoutedEventArgs e)
{
try
{
// This check allows the user to use the text box context menu without ending the rename
if (!(FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot) is AppBarButton or Popup))
{
TextBox textBox = (TextBox)e.OriginalSource;
await CommitRenameAsync(textBox);
}
}
catch (COMException)
{
}
}
// Methods
protected async void RenameTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
var textBox = (TextBox)sender;
var isShiftPressed = (PInvoke.GetKeyState((int)VirtualKey.Shift) & KEY_DOWN_MASK) != 0;
switch (e.Key)
{
case VirtualKey.Escape:
textBox.LostFocus -= RenameTextBox_LostFocus;
textBox.Text = OldItemName;
EndRename(textBox);
e.Handled = true;
break;
case VirtualKey.Enter:
textBox.LostFocus -= RenameTextBox_LostFocus;
await CommitRenameAsync(textBox);
e.Handled = true;
break;
case VirtualKey.Home:
textBox.SelectionStart = 0;
textBox.SelectionLength = 0;
e.Handled = true;
break;
case VirtualKey.Up:
if (!isShiftPressed)
textBox.SelectionStart = 0;
e.Handled = true;
break;
case VirtualKey.Down:
if (!isShiftPressed)
textBox.SelectionStart = textBox.Text.Length;
e.Handled = true;
break;
case VirtualKey.Left:
e.Handled = textBox.SelectionStart == 0;
break;
case VirtualKey.Right:
e.Handled = (textBox.SelectionStart + textBox.SelectionLength) == textBox.Text.Length;
break;
case VirtualKey.Tab:
textBox.LostFocus -= RenameTextBox_LostFocus;
NextRenameIndex = isShiftPressed ? -1 : 1;
if (textBox.Text != OldItemName)
{
await CommitRenameAsync(textBox);
}
else
{
var newIndex = ListViewBase.SelectedIndex + NextRenameIndex;
NextRenameIndex = 0;
EndRename(textBox);
if (newIndex >= 0 &&
newIndex < ListViewBase.Items.Count)
{
ListViewBase.SelectedIndex = newIndex;
StartRenameItem();
}
}
e.Handled = true;
break;
}
}
protected bool TryStartRenameNextItem(ListedItem item)
{
var nextItemIndex = ListViewBase.Items.IndexOf(item) + NextRenameIndex;
NextRenameIndex = 0;
if (nextItemIndex >= 0 &&
nextItemIndex < ListViewBase.Items.Count)
{
ListViewBase.SelectedIndex = nextItemIndex;
StartRenameItem();
return true;
}
return false;
}
protected void SelectionCheckbox_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
e.Handled = true;
}
// Disposer
public override void Dispose()
{
base.Dispose();
UnhookEvents();
CommandsViewModel?.Dispose();
}
}
}