-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNavigationExtension.cs
More file actions
267 lines (248 loc) · 9.97 KB
/
NavigationExtension.cs
File metadata and controls
267 lines (248 loc) · 9.97 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
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Lemon.ModuleNavigation.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
namespace Lemon.ModuleNavigation.Avaloniaui;
public class NavigationExtension
{
private static readonly HashSet<AvaloniaObject> _targets = [];
#region RegionNameProperty
public static readonly AttachedProperty<string> RegionNameProperty =
AvaloniaProperty.RegisterAttached<NavigationExtension, Control, string>("RegionName",
coerce: CoerceRegionName);
private static string CoerceRegionName(AvaloniaObject targetObject, string currentValue)
{
if (targetObject is Control control)
{
if (control is ContentControl || control is ItemsControl)
{
if (!string.IsNullOrEmpty(GetModuleContainerName(control)))
{
throw new InvalidOperationException($"The {control} have a {nameof(ModuleContainerNameProperty)} already..");
}
void LoadedHandler(object? sender, RoutedEventArgs e)
{
if (control.DataContext is IServiceAware navigationProvider)
{
var serviceProvider = navigationProvider!.ServiceProvider;
var handler = serviceProvider.GetRequiredService<INavigationHandler>();
handler.RegionManager.AddRegion(currentValue, control.ToRegion(currentValue));
}
control.Loaded -= LoadedHandler;
}
control.Loaded += LoadedHandler;
}
else
{
throw new InvalidOperationException($"{nameof(RegionNameProperty)} supports {nameof(ContentControl)} and {nameof(ItemsControl)} Only");
}
}
return currentValue;
}
public static string GetRegionName(Control control)
{
return control.GetValue(RegionNameProperty);
}
public static void SetRegionName(Control control, string value)
{
control.SetValue(RegionNameProperty, value);
}
#endregion
#region ModuleContainerName
public static readonly AttachedProperty<string> ModuleContainerNameProperty =
AvaloniaProperty.RegisterAttached<NavigationExtension, Control, string>("ModuleContainerName",
validate: ValidateModuleContainerName,
coerce: CoerceModuleContainerName);
private static bool ValidateModuleContainerName(string arg)
{
return true;
}
private static string CoerceModuleContainerName(AvaloniaObject targetObject, string currentValue)
{
if (string.IsNullOrEmpty(currentValue))
{
return currentValue;
}
if (targetObject is Control control)
{
if (!string.IsNullOrEmpty(GetRegionName(control)))
{
throw new InvalidOperationException($"The {control} have a {nameof(RegionNameProperty)} already.");
}
void LoadedHandler(object? sender, RoutedEventArgs e)
{
if (control.DataContext is IServiceAware navigationContextProvider)
{
var serviceProvider = navigationContextProvider.ServiceProvider;
var handler = serviceProvider.GetRequiredService<INavigationHandler>();
SetBinding(control, handler, currentValue);
}
control.Loaded -= LoadedHandler;
}
control.Loaded += LoadedHandler;
}
return currentValue;
}
public static string GetModuleContainerName(Control control)
{
return control.GetValue(ModuleContainerNameProperty);
}
public static void SetModuleContainerName(Control control, string value)
{
control.SetValue(ModuleContainerNameProperty, value);
}
#endregion
#region CanUnloadProperty
public static readonly AttachedProperty<bool> CanUnloadProperty =
AvaloniaProperty.RegisterAttached<NavigationExtension, Button, bool>("CanUnload",
defaultValue: false,
coerce: CoerceCanUnload);
private static bool CoerceCanUnload(AvaloniaObject targetObject, bool currentValue)
{
if (targetObject is Button button)
{
button.IsVisible = currentValue;
if (currentValue)
{
if (!_targets.Contains(button))
{
_targets.Add(button);
button.Unloaded += Button_Unloaded;
button.AddHandler(Button.ClickEvent, UnloadModule, RoutingStrategies.Bubble, true);
}
}
return currentValue;
}
return false;
}
private static void Button_Unloaded(object? sender, RoutedEventArgs e)
{
if (sender is Button button)
{
if (_targets.Contains(button))
{
button.Unloaded -= Button_Unloaded;
button.RemoveHandler(Button.ClickEvent, UnloadModule);
_targets.Remove(button);
}
}
}
public static bool GetCanUnload(Control control)
{
return control.GetValue(CanUnloadProperty);
}
public static void SetCanUnload(Control control, bool value)
{
control.SetValue(CanUnloadProperty, value);
}
private static void UnloadModule(object? sender, RoutedEventArgs e)
{
if (sender is Button button)
{
var view = button.FindLogicalAncestorOfType<IView>(includeSelf: false);
if (view != null)
{
}
var tabItem = button.FindLogicalAncestorOfType<TabItem>(includeSelf: false) ?? throw new InvalidOperationException($"There is no 'TabItem' found in parents of {button}");
var tabContainer = tabItem.FindLogicalAncestorOfType<TabControl>(includeSelf: false);
if (tabContainer != null)
{
if (tabItem.DataContext is IModule item)
{
if (item.CanUnload)
{
if (tabContainer.DataContext is IServiceAware serviceAware)
{
var handler = serviceAware.ServiceProvider.GetRequiredService<INavigationHandler>();
handler.ModuleManager.ActiveModules.Remove(item);
}
}
}
}
}
}
#endregion
private static void SetBinding(Control control,
INavigationHandler navigationHandler,
string regionName)
{
if (control is TabControl tabControl)
{
tabControl.Bind(SelectingItemsControl.SelectedItemProperty,
new Binding(nameof(IModuleManager.CurrentModule))
{
Mode = BindingMode.TwoWay,
Source = navigationHandler.ModuleManager
});
tabControl.Bind(ItemsControl.ItemsSourceProperty,
new Binding(nameof(IModuleManager.ActiveModules))
{
Source = navigationHandler.ModuleManager
});
tabControl.ContentTemplate = new FuncDataTemplate<IModule>((m, np) =>
{
if (m == null)
{
return null;
}
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
});
}
else if (control is ItemsControl itemsControl)
{
if (navigationHandler.ModuleManager is INotifyPropertyChanged npc)
{
npc.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == nameof(IModuleManager.CurrentModule))
{
if (navigationHandler.ModuleManager.CurrentModule != null)
{
//https://github.com/AvaloniaUI/Avalonia/issues/17349
if (itemsControl is SelectingItemsControl selecting)
{
selecting.SelectedItem = navigationHandler.ModuleManager.CurrentModule;
}
itemsControl.ScrollIntoView(navigationHandler.ModuleManager.CurrentModule);
}
}
};
}
itemsControl.Bind(ItemsControl.ItemsSourceProperty,
new Binding(nameof(IModuleManager.ActiveModules))
{
Source = navigationHandler.ModuleManager
});
itemsControl.ItemTemplate = new FuncDataTemplate<IModule>((m, np) =>
{
if (m == null)
{
return null;
}
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
});
}
else if (control is ContentControl contentControl)
{
contentControl.Bind(ContentControl.ContentProperty,
new Binding(nameof(NavigationHandler.ModuleManager.CurrentModule))
{
Source = navigationHandler.ModuleManager
});
contentControl.ContentTemplate = new FuncDataTemplate<IModule>((m, np) =>
{
if (m == null)
{
return null;
}
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
});
}
}
}