forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultitaskingContext.cs
104 lines (91 loc) · 3.37 KB
/
MultitaskingContext.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
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.
using Files.App.UserControls.MultitaskingControl;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
using System.Collections.Specialized;
namespace Files.App.Data.Contexts
{
internal class MultitaskingContext : ObservableObject, IMultitaskingContext
{
private readonly AppModel _appModel = Ioc.Default.GetRequiredService<AppModel>();
private bool isPopupOpen = false;
private IMultitaskingControl? control;
public IMultitaskingControl? Control => control;
private ushort tabCount = 0;
public ushort TabCount => tabCount;
public TabItem CurrentTabItem => MainPageViewModel.AppInstances[currentTabIndex];
public TabItem SelectedTabItem => MainPageViewModel.AppInstances[selectedTabIndex];
private ushort currentTabIndex = 0;
public ushort CurrentTabIndex => currentTabIndex;
private ushort selectedTabIndex = 0;
public ushort SelectedTabIndex => selectedTabIndex;
public MultitaskingContext()
{
MainPageViewModel.AppInstances.CollectionChanged += AppInstances_CollectionChanged;
_appModel.PropertyChanged += AppModel_PropertyChanged;
BaseMultitaskingControl.OnLoaded += BaseMultitaskingControl_OnLoaded;
HorizontalMultitaskingControl.SelectedTabItemChanged += HorizontalMultitaskingControl_SelectedTabItemChanged;
FocusManager.GotFocus += FocusManager_GotFocus;
FocusManager.LosingFocus += FocusManager_LosingFocus;
}
private void AppInstances_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
UpdateTabCount();
}
private void AppModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(AppModel.TabStripSelectedIndex))
UpdateCurrentTabIndex();
}
private void BaseMultitaskingControl_OnLoaded(object? sender, IMultitaskingControl control)
{
SetProperty(ref this.control, control, nameof(Control));
UpdateTabCount();
UpdateCurrentTabIndex();
}
private void HorizontalMultitaskingControl_SelectedTabItemChanged(object? sender, TabItem? e)
{
isPopupOpen = e is not null;
int newSelectedIndex = e is null ? currentTabIndex : MainPageViewModel.AppInstances.IndexOf(e);
UpdateSelectedTabIndex(newSelectedIndex);
}
private void FocusManager_GotFocus(object? sender, FocusManagerGotFocusEventArgs e)
{
if (isPopupOpen)
return;
if (e.NewFocusedElement is FrameworkElement element && element.DataContext is TabItem tabItem)
{
int newSelectedIndex = MainPageViewModel.AppInstances.IndexOf(tabItem);
UpdateSelectedTabIndex(newSelectedIndex);
}
}
private void FocusManager_LosingFocus(object? sender, LosingFocusEventArgs e)
{
if (isPopupOpen)
return;
if (SetProperty(ref selectedTabIndex, currentTabIndex, nameof(SelectedTabIndex)))
{
OnPropertyChanged(nameof(selectedTabIndex));
}
}
private void UpdateTabCount()
{
SetProperty(ref tabCount, (ushort)MainPageViewModel.AppInstances.Count, nameof(TabCount));
}
private void UpdateCurrentTabIndex()
{
if (SetProperty(ref currentTabIndex, (ushort)_appModel.TabStripSelectedIndex, nameof(CurrentTabIndex)))
{
OnPropertyChanged(nameof(CurrentTabItem));
}
}
private void UpdateSelectedTabIndex(int index)
{
if (SetProperty(ref selectedTabIndex, (ushort)index, nameof(SelectedTabIndex)))
{
OnPropertyChanged(nameof(SelectedTabItem));
}
}
}
}