forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesystemOperationDialog.xaml.cs
190 lines (154 loc) · 5.52 KB
/
FilesystemOperationDialog.xaml.cs
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
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.
using CommunityToolkit.WinUI.UI;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace Files.App.Dialogs
{
public sealed partial class FilesystemOperationDialog : ContentDialog, IDialog<FileSystemDialogViewModel>, IRealTimeControl
{
private FrameworkElement RootAppElement
=> (FrameworkElement)MainWindow.Instance.Content;
public FileSystemDialogViewModel ViewModel
{
get => (FileSystemDialogViewModel)DataContext;
set
{
if (value is not null)
{
value.PrimaryButtonEnabled = true;
}
DataContext = value;
}
}
public FilesystemOperationDialog()
{
InitializeComponent();
InitializeContentLayout();
MainWindow.Instance.SizeChanged += Current_SizeChanged;
}
public new async Task<DialogResult> ShowAsync()
{
return (DialogResult)await SetContentDialogRoot(this).ShowAsync();
}
// WINUI3
private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
{
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
contentDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
return contentDialog;
}
private void Current_SizeChanged(object sender, WindowSizeChangedEventArgs e)
{
UpdateDialogLayout();
}
private void UpdateDialogLayout()
{
try
{
if (ViewModel.FileSystemDialogMode.ConflictsExist)
ContainerGrid.Width = MainWindow.Instance.Bounds.Width <= 700 ? MainWindow.Instance.Bounds.Width - 50 : 650;
}
catch (Exception ex)
{
// Handle exception in case WinUI Windows is closed
// (see https://github.com/files-community/Files/issues/15599)
App.Logger.LogWarning(ex, ex.Message);
}
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
var primaryButton = this.FindDescendant("PrimaryButton") as Button;
if (primaryButton is not null)
primaryButton.GotFocus += PrimaryButton_GotFocus;
}
private void PrimaryButton_GotFocus(object sender, RoutedEventArgs e)
{
if (sender is Button btn)
btn.GotFocus -= PrimaryButton_GotFocus;
if (chkPermanentlyDelete is not null)
chkPermanentlyDelete.IsEnabled = ViewModel.IsDeletePermanentlyEnabled;
DetailsGrid.IsEnabled = true;
}
private void RootDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
if (args.Result == ContentDialogResult.Primary)
ViewModel.SaveConflictResolveOption();
MainWindow.Instance.SizeChanged -= Current_SizeChanged;
ViewModel.CancelCts();
}
private void NameStackPanel_Tapped(object sender, Microsoft.UI.Xaml.Input.TappedRoutedEventArgs e)
{
if (sender is FrameworkElement element &&
element.DataContext is FileSystemDialogConflictItemViewModel conflictItem &&
conflictItem.ConflictResolveOption == FileNameConflictResolveOptionType.GenerateNewName)
{
StartRename(conflictItem);
}
}
private void NameEdit_LostFocus(object sender, RoutedEventArgs e)
{
if ((sender as FrameworkElement)?.DataContext is FileSystemDialogConflictItemViewModel conflictItem)
EndRename(conflictItem);
}
private void NameEdit_Loaded(object sender, RoutedEventArgs e)
{
(sender as TextBox)?.Focus(FocusState.Programmatic);
}
private void FilesystemOperationDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{
UpdateDialogLayout();
}
private void NameEdit_PreviewKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (sender is not FrameworkElement element || element.DataContext is not FileSystemDialogConflictItemViewModel currentItem)
return;
if (e.Key is Windows.System.VirtualKey.Down)
{
var index = ViewModel.Items.IndexOf(currentItem);
if (index == -1 || index == ViewModel.Items.Count - 1)
return;
var nextItem = ViewModel.Items.Skip(index + 1)
.FirstOrDefault(i => i is FileSystemDialogConflictItemViewModel { ConflictResolveOption: FileNameConflictResolveOptionType.GenerateNewName });
if (nextItem is null)
return;
EndRename(currentItem);
StartRename((FileSystemDialogConflictItemViewModel)nextItem);
e.Handled = true;
}
else if (e.Key is Windows.System.VirtualKey.Up)
{
var index = ViewModel.Items.IndexOf(currentItem);
if (index == -1 || index == 0)
return;
var prevItem = ViewModel.Items.Take(index)
.LastOrDefault(i => i is FileSystemDialogConflictItemViewModel { ConflictResolveOption: FileNameConflictResolveOptionType.GenerateNewName });
if (prevItem is null)
return;
EndRename(currentItem);
StartRename((FileSystemDialogConflictItemViewModel)prevItem);
e.Handled = true;
}
}
private void StartRename(FileSystemDialogConflictItemViewModel conflictItem)
{
conflictItem.IsTextBoxVisible = conflictItem.ConflictResolveOption == FileNameConflictResolveOptionType.GenerateNewName;
conflictItem.CustomName = conflictItem.DestinationDisplayName;
}
private void EndRename(FileSystemDialogConflictItemViewModel conflictItem)
{
conflictItem.CustomName = FilesystemHelpers.FilterRestrictedCharacters(conflictItem.CustomName);
if (ViewModel.IsNameAvailableForItem(conflictItem, conflictItem.CustomName!))
conflictItem.IsTextBoxVisible = false;
else
ViewModel.PrimaryButtonEnabled = false;
if (conflictItem.CustomName.Equals(conflictItem.DisplayName))
{
var savedName = conflictItem.DestinationDisplayName;
conflictItem.DestinationDisplayName = savedName;
}
}
}
}