-
Notifications
You must be signed in to change notification settings - Fork 27
/
Shell.xaml.cs
297 lines (255 loc) · 9.14 KB
/
Shell.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
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
#if !IS_WINUI || HAS_UNO
#define SYS_NAV_MGR_SUPPORTED
#endif
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Core;
using Uno.Extensions;
using Uno.Toolkit.Samples.Content;
using Uno.Toolkit.Samples.Content.Controls;
using Uno.Toolkit.Samples.Content.NestedSamples;
using Uno.Toolkit.Samples.Helpers;
using Uno.Toolkit.UI;
#if __IOS__
using Foundation;
#endif
#if IS_WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using XamlLaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs;
using XamlWindow = Microsoft.UI.Xaml.Window;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using XamlLaunchActivatedEventArgs = Windows.ApplicationModel.Activation.LaunchActivatedEventArgs;
using XamlWindow = Windows.UI.Xaml.Window;
#endif
using MUXC = Microsoft.UI.Xaml.Controls;
namespace Uno.Toolkit.Samples
{
public sealed partial class Shell : UserControl
{
public Shell()
{
this.InitializeComponent();
InitializeSafeArea();
this.Loaded += OnLoaded;
this.SizeChanged += (s, e) => InitializeSafeArea();
NestedSampleFrame.RegisterPropertyChangedCallback(ContentControl.ContentProperty, OnNestedSampleFrameChanged);
#if SYS_NAV_MGR_SUPPORTED
SystemNavigationManager.GetForCurrentView().BackRequested += (s, e) => e.Handled = BackNavigateFromNestedSample();
#endif
}
public static Shell GetForCurrentView() => (Shell)(App.Instance.Window.Content as ExtendedSplashScreen)!.Content;
public MUXC.NavigationView NavigationView => NavigationViewControl;
private void OnLoaded(object sender, RoutedEventArgs e)
{
#if DEBUG
this.FindName("DebugPanel"); // materialized x:Load=false element
#endif
SetDarkLightToggleInitialState();
}
private void SetDarkLightToggleInitialState()
{
#if !IS_WINUI
// Initialize the toggle to the current theme.
DarkModeToggle.IsChecked = SystemThemeHelper.IsAppInDarkMode();
#endif
}
/// <summary>
/// This method handles the top padding for phones like iPhone X.
/// </summary>
private void InitializeSafeArea()
{
#if !IS_WINUI
var full = XamlWindow.Current.Bounds;
var bounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;
var topPadding = Math.Abs(full.Top - bounds.Top);
if (topPadding > 0)
{
TopPaddingRow.Height = new GridLength(topPadding);
}
#endif
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
// Set theme for window root.
if (DarkModeToggle.IsChecked is { } value)
{
SystemThemeHelper.SetApplicationTheme(darkMode: value);
}
else
{
SystemThemeHelper.ToggleApplicationTheme();
DarkModeToggle.IsChecked = SystemThemeHelper.IsAppInDarkMode();
}
if (NavigationViewControl.PaneDisplayMode == MUXC.NavigationViewPaneDisplayMode.LeftMinimal)
{
// Close navigation view when changing the theme
// to allow the user to see the difference between the themes.
NavigationViewControl.IsPaneOpen = false;
}
}
private void OnNestedSampleFrameChanged(DependencyObject sender, DependencyProperty dp)
{
var isInsideNestedSample = NestedSampleFrame.Content != null;
NavViewToggleButton.Visibility = isInsideNestedSample
? Visibility.Collapsed
: Visibility.Visible;
// prevent empty frame from blocking the content (nav-view) behind it
NestedSampleFrame.Visibility = isInsideNestedSample
? Visibility.Visible
: Visibility.Collapsed;
#if SYS_NAV_MGR_SUPPORTED
// toggle built-in back button for wasm (from browser) and uwp (on title bar)
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = isInsideNestedSample
? AppViewBackButtonVisibility.Visible
: AppViewBackButtonVisibility.Collapsed;
#endif
}
public void ShowNestedSample(Type pageType, bool? clearStack = null)
{
var wasFrameEmpty = NestedSampleFrame.Content == null;
if (NestedSampleFrame.Navigate(pageType) && (clearStack ?? wasFrameEmpty))
{
NestedSampleFrame.BackStack.Clear();
}
}
public void ShowNestedSample<TPage>(bool? clearStack = null) where TPage : Page
{
ShowNestedSample(typeof(TPage), clearStack);
}
public bool BackNavigateFromNestedSample()
{
if (NestedSampleFrame.Content == null)
{
return false;
}
if (NestedSampleFrame.CanGoBack)
{
//Let the NavigationBar within the nested page handle the back nav logic
return false;
}
else
{
ExitNestedSample();
}
return true;
}
public void ExitNestedSample()
{
var sender = NestedSampleFrame.Content;
NestedSampleFrame.Content = null;
#if __IOS__
// This will force reset the UINavigationController, to prevent the back button from appearing when the stack is supposely empty.
// note: Merely setting the Frame.Content to null, doesnt fully reset the stack.
// When revisiting the page1 again, the previous page1 is still in the UINavigationController stack
// causing a back button to appear that takes us back to the previous page1
NestedSampleFrame.BackStack.Add(default);
NestedSampleFrame.BackStack.Clear();
#endif
#if __ANDROID__
NestedSampleFrame.BackStack.Clear();
#endif
if (NavigationView.Content is IExitNestedSampleHandler handler)
{
// Allows the page to be able to handle back navigation that exited nested sample.
// There is no other ways of knowing this, since this "navigation" effectively
// just changes the visibility of the nested frame that overlays everything.
handler.OnExitedFromNestedSample(sender);
}
}
private void NavViewToggleButton_Click(object sender, RoutedEventArgs e)
{
if (NavigationViewControl.PaneDisplayMode == MUXC.NavigationViewPaneDisplayMode.LeftMinimal)
{
NavigationViewControl.IsPaneOpen = !NavigationViewControl.IsPaneOpen;
}
else if (NavigationViewControl.PaneDisplayMode == MUXC.NavigationViewPaneDisplayMode.Left)
{
NavigationViewControl.IsPaneVisible = !NavigationViewControl.IsPaneVisible;
NavigationViewControl.IsPaneOpen = !NavigationViewControl.IsPaneVisible;
}
}
private void NavigationViewControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
// This could be done using VisualState with Adaptive triggers, but an issue prevents this currently - https://github.com/unoplatform/uno/issues/5168
var desktopWidth = (double)Application.Current.Resources["DesktopAdaptiveThresholdWidth"];
if (e.NewSize.Width >= desktopWidth && NavigationViewControl.PaneDisplayMode != MUXC.NavigationViewPaneDisplayMode.Left)
{
NavigationViewControl.PaneDisplayMode = MUXC.NavigationViewPaneDisplayMode.Left;
NavigationViewControl.IsPaneOpen = true;
}
else if (e.NewSize.Width < desktopWidth && NavigationViewControl.PaneDisplayMode != MUXC.NavigationViewPaneDisplayMode.LeftMinimal)
{
NavigationViewControl.IsPaneVisible = true;
NavigationViewControl.PaneDisplayMode = MUXC.NavigationViewPaneDisplayMode.LeftMinimal;
}
}
private void DebugVT(object sender, RoutedEventArgs e)
{
object FindViewOfInterest()
{
// the view of interest is:
// - in RuntimeTestRunner, the "UnitTestsUIContentHelper.Content"
// - for any page, the active section of SamplePageLayout if present OR the content of that page
// ...
// todo: add support for [RequiresFullWindow], flyout, nested navigation sample
var content = NavigationViewControl.Content;
if (content is RuntimeTestRunner runner)
{
return runner.GetFirstDescendant<ContentControl>(x => x.Name == "unitTestContentRoot")?.Content;
}
else if (content is Page page)
{
if (page.GetFirstDescendant<SamplePageLayout>() is { } layout &&
layout.GetActivePresenter() is { } presenter)
{
// presenter.Content is the optional [SampleAttribute].DataType instance
return presenter.GetChildren().FirstOrDefault();
}
else
{
return page.Content;
}
}
return null;
}
// for the best viewing experience, paste the tree in VSCode (you can collapse node) with `ini` syntax highlighting
var tree = this.TreeGraph();
var target = FindViewOfInterest();
var targetTree = (target as DependencyObject)?.TreeGraph();
// note: you can also tag element with unique x:Name to inspect here
//var sut = this.GetFirstDescendant<Chip>(x => x.Name == "SUT");
//var tree = sut?.TreeGraph();
#if WINDOWS || WINDOWS_UWP
var data = new DataPackage();
data.SetText(targetTree ?? tree);
Clipboard.SetContent(data);
#elif __WASM__
Console.WriteLine(targetTree ?? tree);
#endif
// note: insert a breakpoint around here or uncomment the next line to debug.
//if (Debugger.IsAttached) Debugger.Break();
}
private async void DebugVTAsync(object sender, RoutedEventArgs e)
{
// leave some time to perform action like: opening combo/flyout or navigation
await Task.Delay(5000);
DebugVT(sender, e);
}
}
}