-
Notifications
You must be signed in to change notification settings - Fork 47
/
ReactorApplication.cs
367 lines (301 loc) · 10.9 KB
/
ReactorApplication.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using MauiReactor.HotReload;
using MauiReactor.Internals;
using Microsoft.Maui.Dispatching;
using System.Reflection;
namespace MauiReactor
{
internal abstract class ReactorApplicationHost : VisualNode, IHostElement, IVisualNode
{
protected readonly ReactorApplication _application;
protected ReactorApplicationHost(ReactorApplication application, bool enableHotReload)
{
Instance = this;
_application = application ?? throw new ArgumentNullException(nameof(application));
if (enableHotReload)
{
ComponentLoader = new RemoteComponentLoader();
ComponentLoader.AssemblyChanged += (s, e) => OnComponentAssemblyChanged();
}
else
{
ComponentLoader = new LocalComponentLoader();
}
}
public static ReactorApplicationHost? Instance { get; private set; }
internal IComponentLoader ComponentLoader { get; }
public Action<UnhandledExceptionEventArgs>? UnhandledException { get; set; }
internal void FireUnhandledExceptionEvent(Exception ex)
{
UnhandledException?.Invoke(new UnhandledExceptionEventArgs(ex, false));
System.Diagnostics.Debug.WriteLine(ex);
}
public Microsoft.Maui.Controls.Window? MainWindow { get; protected set; }
public abstract IHostElement Run();
public abstract void Stop();
protected virtual void OnComponentAssemblyChanged()
{ }
public ReactorApplicationHost OnUnhandledException(Action<UnhandledExceptionEventArgs> action)
{
UnhandledException = action;
return this;
}
public abstract void RequestAnimationFrame(VisualNode visualNode);
//public INavigation? Navigation => _application.MainPage?.Navigation;
public Microsoft.Maui.Controls.Page? ContainerPage => _application?.MainPage;
IHostElement? IVisualNode.GetPageHost()
{
return this;
}
Microsoft.Maui.Controls.Page? IVisualNode.GetContainerPage()
{
return ContainerPage;
}
}
internal class ReactorApplicationHost<T> : ReactorApplicationHost where T : Component, new()
{
private Component? _rootComponent;
private bool _sleeping = false;
private bool _started = false;
private readonly LinkedList<VisualNode> _listOfVisualsToAnimate = new();
internal ReactorApplicationHost(ReactorApplication<T> application, bool enableHotReload)
:base(application, enableHotReload)
{
}
protected sealed override void OnAddChild(VisualNode widget, BindableObject nativeControl)
{
if (nativeControl is Microsoft.Maui.Controls.Page page)
{
_application.MainPage = page;
if (page.Parent is Microsoft.Maui.Controls.Window mainWindow)
{
MainWindow = mainWindow;
}
}
else if (nativeControl is Microsoft.Maui.Controls.Window mainWindow)
{
MainWindow = mainWindow;
}
else
{
throw new NotSupportedException($"Invalid root component ({nativeControl.GetType()}): must be a page (i.e. RxContentPage, RxShell etc)");
}
}
protected sealed override void OnRemoveChild(VisualNode widget, BindableObject nativeControl)
{
//MainPage can't be set to null!
//_application.MainPage = null;
}
public void Pause()
{
_sleeping = true;
}
public void Resume()
{
_sleeping = false;
}
public override IHostElement Run()
{
if (!_started)
{
_started = true;
_rootComponent ??= new T();
OnLayout();
ComponentLoader.Run();
}
return this;
}
protected override void OnComponentAssemblyChanged()
{
try
{
var newComponent = ComponentLoader.LoadComponent<T>();
if (newComponent != null &&
_rootComponent != newComponent)
{
_rootComponent = newComponent;
Invalidate();
}
else
{
System.Diagnostics.Debug.WriteLine($"Unable to hot relead component {typeof(T).FullName}: type not found in received assembly");
}
}
catch (Exception ex)
{
FireUnhandledExceptionEvent(ex);
}
}
public override void Stop()
{
if (_started)
{
_started = false;
ComponentLoader.Stop();
}
}
protected internal override void OnLayoutCycleRequested()
{
if (_started && !_sleeping)
{
ContainerPage?.Dispatcher.Dispatch(OnLayout);
}
base.OnLayoutCycleRequested();
}
private void OnLayout()
{
try
{
Layout();
SetupAnimationTimer();
}
catch (Exception ex)
{
FireUnhandledExceptionEvent(ex);
}
}
protected override IEnumerable<VisualNode?> RenderChildren()
{
yield return _rootComponent;
}
public override void RequestAnimationFrame(VisualNode visualNode)
{
_listOfVisualsToAnimate.AddFirst(visualNode);
}
private void SetupAnimationTimer()
{
if (_listOfVisualsToAnimate.Count > 0 && Application.Current != null)
{
Application.Current.Dispatcher.Dispatch(AnimationCallback);
}
}
private void AnimationCallback()
{
if (!_started || _sleeping)
{
return;
}
DateTime now = DateTime.Now;
if (Application.Current != null && AnimateVisuals())
{
//System.Diagnostics.Debug.WriteLine($"{(DateTime.Now - now).TotalMilliseconds}");
var elapsedMilliseconds = (DateTime.Now - now).TotalMilliseconds;
if (elapsedMilliseconds > 16)
{
System.Diagnostics.Debug.WriteLine("FPS WARNING");
Application.Current.Dispatcher.Dispatch(AnimationCallback);
}
else
{
Application.Current.Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(16 - elapsedMilliseconds), AnimationCallback);
}
}
}
private bool AnimateVisuals()
{
if (_listOfVisualsToAnimate.Count == 0)
return false;
bool animated = false;
LinkedListNode<VisualNode>? nodeToAnimate = _listOfVisualsToAnimate.First;
while (nodeToAnimate != null)
{
var nextNode = nodeToAnimate.Next;
if (nodeToAnimate.Value.Animate())
{
animated = true;
}
else
{
_listOfVisualsToAnimate.Remove(nodeToAnimate);
}
nodeToAnimate = nextNode;
}
return animated;
}
}
public abstract class ReactorApplication : Application
{
protected ReactorApplication()
{
}
internal static bool HotReloadEnabled { get; set; }
}
public class ReactorApplication<T> : ReactorApplication where T : Component, new()
{
private ReactorApplicationHost<T>? _host;
public ReactorApplication()
{ }
protected override Microsoft.Maui.Controls.Window CreateWindow(IActivationState? activationState)
{
_host ??= new ReactorApplicationHost<T>(this, HotReloadEnabled);
_host.Run();
if (_host.MainWindow != null)
{
return _host.MainWindow;
}
return base.CreateWindow(activationState);
}
public override void CloseWindow(Microsoft.Maui.Controls.Window window)
{
base.CloseWindow(window);
}
protected override void OnStart()
{
_host?.Run();
base.OnStart();
}
protected override void OnResume()
{
//https://github.com/adospace/reactorui-maui/issues/26
//seems like some devices (Android 9.0?) do not send (or Maui app doesn't receive) the resume event
//so for now do not suspend the event loop (actually it's even not required at all to suspend it as the app itself is suspended by the os)
//_host?.Resume();
base.OnResume();
}
protected override void OnSleep()
{
//do not pause the event loop: see OnResume() above
//_host?.Pause();
base.OnSleep();
}
protected override void CleanUp()
{
_host?.Stop();
base.CleanUp();
}
}
public static class MauiAppBuilderExtensions
{
public static MauiAppBuilder UseMauiReactorApp<TComponent>(this MauiAppBuilder appBuilder, Action<Application>? configureApplication = null) where TComponent : Component, new()
=> appBuilder.UseMauiApp(sp =>
{
ServiceCollectionProvider.ServiceProvider = sp;
var app = new ReactorApplication<TComponent>();
configureApplication?.Invoke(app);
return app;
});
public static MauiAppBuilder EnableMauiReactorHotReload(this MauiAppBuilder appBuilder)
{
ReactorApplication.HotReloadEnabled = true;
return appBuilder;
}
}
public static class ApplicationExtensions
{
public static Application AddResource(this Application application, string resourceName, Assembly? containerAssembly = null)
{
var resourceDictionary = new ResourceDictionary();
resourceDictionary.SetAndLoadSource(
new Uri(resourceName, UriKind.Relative),
resourceName,
containerAssembly ?? Assembly.GetCallingAssembly(),
null);
application.Resources.MergedDictionaries.Add(resourceDictionary);
return application;
}
public static Application SetWindowsSpecificAssectDirectory(this Application application, string directoryName)
{
Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific.Application.SetImageDirectory(application, directoryName);
return application;
}
}
}