-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathMainPageViewModel.cs
298 lines (254 loc) · 10.3 KB
/
MainPageViewModel.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
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
// Copyright (c) Files Community
// Licensed under the MIT License.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Navigation;
using System.Windows.Input;
using Windows.System;
using Microsoft.UI.Xaml.Controls;
namespace Files.App.ViewModels
{
/// <summary>
/// Represents ViewModel of <see cref="MainPage"/>.
/// </summary>
public sealed class MainPageViewModel : ObservableObject
{
// Dependency injections
private IAppearanceSettingsService AppearanceSettingsService { get; } = Ioc.Default.GetRequiredService<IAppearanceSettingsService>();
private IGeneralSettingsService GeneralSettingsService { get; } = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
private INetworkService NetworkService { get; } = Ioc.Default.GetRequiredService<INetworkService>();
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
private IResourcesService ResourcesService { get; } = Ioc.Default.GetRequiredService<IResourcesService>();
private DrivesViewModel DrivesViewModel { get; } = Ioc.Default.GetRequiredService<DrivesViewModel>();
// Properties
public static ObservableCollection<TabBarItem> AppInstances { get; private set; } = [];
public List<ITabBar> MultitaskingControls { get; } = [];
public ITabBar? MultitaskingControl { get; set; }
private bool _IsSidebarPaneOpen;
public bool IsSidebarPaneOpen
{
get => _IsSidebarPaneOpen;
set => SetProperty(ref _IsSidebarPaneOpen, value);
}
private bool _IsSidebarPaneOpenToggleButtonVisible;
public bool IsSidebarPaneOpenToggleButtonVisible
{
get => _IsSidebarPaneOpenToggleButtonVisible;
set => SetProperty(ref _IsSidebarPaneOpenToggleButtonVisible, value);
}
private TabBarItem? selectedTabItem;
public TabBarItem? SelectedTabItem
{
get => selectedTabItem;
set => SetProperty(ref selectedTabItem, value);
}
private bool shouldViewControlBeDisplayed;
public bool ShouldViewControlBeDisplayed
{
get => shouldViewControlBeDisplayed;
set => SetProperty(ref shouldViewControlBeDisplayed, value);
}
private bool shouldPreviewPaneBeActive;
public bool ShouldPreviewPaneBeActive
{
get => shouldPreviewPaneBeActive;
set => SetProperty(ref shouldPreviewPaneBeActive, value);
}
private bool shouldPreviewPaneBeDisplayed;
public bool ShouldPreviewPaneBeDisplayed
{
get => shouldPreviewPaneBeDisplayed;
set => SetProperty(ref shouldPreviewPaneBeDisplayed, value);
}
public bool ShowShelfPane
=> GeneralSettingsService.ShowShelfPane && AppLifecycleHelper.AppEnvironment is AppEnvironment.Dev;
public Stretch AppThemeBackgroundImageFit
=> AppearanceSettingsService.AppThemeBackgroundImageFit;
public float AppThemeBackgroundImageOpacity
=> AppearanceSettingsService.AppThemeBackgroundImageOpacity;
public ImageSource? AppThemeBackgroundImageSource =>
string.IsNullOrEmpty(AppearanceSettingsService.AppThemeBackgroundImageSource)
? null
: new BitmapImage(new Uri(AppearanceSettingsService.AppThemeBackgroundImageSource, UriKind.RelativeOrAbsolute));
public VerticalAlignment AppThemeBackgroundImageVerticalAlignment
=> AppearanceSettingsService.AppThemeBackgroundImageVerticalAlignment;
public HorizontalAlignment AppThemeBackgroundImageHorizontalAlignment
=> AppearanceSettingsService.AppThemeBackgroundImageHorizontalAlignment;
public bool ShowToolbar
=> AppearanceSettingsService.ShowToolbar;
// Commands
public ICommand NavigateToNumberedTabKeyboardAcceleratorCommand { get; }
// Constructor
public MainPageViewModel()
{
NavigateToNumberedTabKeyboardAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(ExecuteNavigateToNumberedTabKeyboardAcceleratorCommand);
AppearanceSettingsService.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case nameof(AppearanceSettingsService.AppThemeBackgroundImageSource):
OnPropertyChanged(nameof(AppThemeBackgroundImageSource));
break;
case nameof(AppearanceSettingsService.AppThemeBackgroundImageOpacity):
OnPropertyChanged(nameof(AppThemeBackgroundImageOpacity));
break;
case nameof(AppearanceSettingsService.AppThemeBackgroundImageFit):
OnPropertyChanged(nameof(AppThemeBackgroundImageFit));
break;
case nameof(AppearanceSettingsService.AppThemeBackgroundImageVerticalAlignment):
OnPropertyChanged(nameof(AppThemeBackgroundImageVerticalAlignment));
break;
case nameof(AppearanceSettingsService.AppThemeBackgroundImageHorizontalAlignment):
OnPropertyChanged(nameof(AppThemeBackgroundImageHorizontalAlignment));
break;
case nameof(AppearanceSettingsService.ShowToolbar):
OnPropertyChanged(nameof(ShowToolbar));
break;
}
};
GeneralSettingsService.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case nameof(GeneralSettingsService.ShowShelfPane):
OnPropertyChanged(nameof(ShowShelfPane));
break;
}
};
}
// Methods
public async Task OnNavigatedToAsync(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
return;
var parameter = e.Parameter;
var ignoreStartupSettings = false;
if (parameter is MainPageNavigationArguments mainPageNavigationArguments)
{
parameter = mainPageNavigationArguments.Parameter;
ignoreStartupSettings = mainPageNavigationArguments.IgnoreStartupSettings;
}
if (parameter is null || (parameter is string eventStr && string.IsNullOrEmpty(eventStr)))
{
try
{
// add last session tabs to closed tabs stack if those tabs are not about to be opened
if (!UserSettingsService.AppSettingsService.RestoreTabsOnStartup && !UserSettingsService.GeneralSettingsService.ContinueLastSessionOnStartUp && UserSettingsService.GeneralSettingsService.LastSessionTabList != null)
{
var items = UserSettingsService.GeneralSettingsService.LastSessionTabList
.Where(tab => !string.IsNullOrEmpty(tab))
.Select(tab => TabBarItemParameter.Deserialize(tab)).ToArray();
BaseTabBar.PushRecentTab(items);
}
if (UserSettingsService.AppSettingsService.RestoreTabsOnStartup)
{
UserSettingsService.AppSettingsService.RestoreTabsOnStartup = false;
if (UserSettingsService.GeneralSettingsService.LastSessionTabList is not null)
{
foreach (string tabArgsString in UserSettingsService.GeneralSettingsService.LastSessionTabList)
{
var tabArgs = TabBarItemParameter.Deserialize(tabArgsString);
await NavigationHelpers.AddNewTabByParamAsync(tabArgs.InitialPageType, tabArgs.NavigationParameter);
}
if (!UserSettingsService.GeneralSettingsService.ContinueLastSessionOnStartUp)
UserSettingsService.GeneralSettingsService.LastSessionTabList = null;
}
}
else if (UserSettingsService.GeneralSettingsService.OpenSpecificPageOnStartup &&
UserSettingsService.GeneralSettingsService.TabsOnStartupList is not null)
{
foreach (string path in UserSettingsService.GeneralSettingsService.TabsOnStartupList)
await NavigationHelpers.AddNewTabByPathAsync(typeof(ShellPanesPage), path, true);
}
else if (UserSettingsService.GeneralSettingsService.ContinueLastSessionOnStartUp &&
UserSettingsService.GeneralSettingsService.LastSessionTabList is not null)
{
if (AppInstances.Count == 0)
{
foreach (string tabArgsString in UserSettingsService.GeneralSettingsService.LastSessionTabList)
{
var tabArgs = TabBarItemParameter.Deserialize(tabArgsString);
await NavigationHelpers.AddNewTabByParamAsync(tabArgs.InitialPageType, tabArgs.NavigationParameter);
}
}
}
else
{
await NavigationHelpers.AddNewTabAsync();
}
}
catch
{
await NavigationHelpers.AddNewTabAsync();
}
}
else
{
if (!ignoreStartupSettings)
{
try
{
if (UserSettingsService.GeneralSettingsService.OpenSpecificPageOnStartup &&
UserSettingsService.GeneralSettingsService.TabsOnStartupList is not null)
{
foreach (string path in UserSettingsService.GeneralSettingsService.TabsOnStartupList)
await NavigationHelpers.AddNewTabByPathAsync(typeof(ShellPanesPage), path, true);
}
else if (UserSettingsService.GeneralSettingsService.ContinueLastSessionOnStartUp &&
UserSettingsService.GeneralSettingsService.LastSessionTabList is not null &&
AppInstances.Count == 0)
{
foreach (string tabArgsString in UserSettingsService.GeneralSettingsService.LastSessionTabList)
{
var tabArgs = TabBarItemParameter.Deserialize(tabArgsString);
await NavigationHelpers.AddNewTabByParamAsync(tabArgs.InitialPageType, tabArgs.NavigationParameter);
}
}
}
catch { }
}
if (parameter is string navArgs)
await NavigationHelpers.AddNewTabByPathAsync(typeof(ShellPanesPage), navArgs, true);
else if (parameter is PaneNavigationArguments paneArgs)
await NavigationHelpers.AddNewTabByParamAsync(typeof(ShellPanesPage), paneArgs);
else if (parameter is TabBarItemParameter tabArgs)
await NavigationHelpers.AddNewTabByParamAsync(tabArgs.InitialPageType, tabArgs.NavigationParameter);
}
// Load the app theme resources
ResourcesService.LoadAppResources(AppearanceSettingsService);
await Task.WhenAll(
DrivesViewModel.UpdateDrivesAsync(),
NetworkService.UpdateComputersAsync(),
NetworkService.UpdateShortcutsAsync());
}
// Command methods
private async void ExecuteNavigateToNumberedTabKeyboardAcceleratorCommand(KeyboardAcceleratorInvokedEventArgs? e)
{
var indexToSelect = e!.KeyboardAccelerator.Key switch
{
VirtualKey.Number1 => 0,
VirtualKey.Number2 => 1,
VirtualKey.Number3 => 2,
VirtualKey.Number4 => 3,
VirtualKey.Number5 => 4,
VirtualKey.Number6 => 5,
VirtualKey.Number7 => 6,
VirtualKey.Number8 => 7,
VirtualKey.Number9 => AppInstances.Count - 1,
_ => AppInstances.Count - 1,
};
// Only select the tab if it is in the list
if (indexToSelect < AppInstances.Count)
{
App.AppModel.TabStripSelectedIndex = indexToSelect;
// Small delay for the UI to load
await Task.Delay(500);
// Refocus on the file list
(SelectedTabItem?.TabItemContent as Control)?.Focus(FocusState.Programmatic);
}
e.Handled = true;
}
}
}