-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathAvaloniaView.cs
67 lines (58 loc) · 2.67 KB
/
AvaloniaView.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
using System;
using System.Runtime.InteropServices.JavaScript;
using Avalonia.Browser.Interop;
using Avalonia.Controls;
using Avalonia.Controls.Embedding;
namespace Avalonia.Browser
{
public class AvaloniaView
{
private readonly EmbeddableControlRoot _topLevel;
/// <param name="divId">ID of the html element where avalonia content should be rendered.</param>
public AvaloniaView(string divId)
: this(DomHelper.GetElementById(divId, BrowserWindowingPlatform.GlobalThis) ??
throw new Exception($"Element with id '{divId}' was not found in the html document."))
{
}
/// <param name="host">JSObject holding a div element where avalonia content should be rendered.</param>
public AvaloniaView(JSObject host)
{
if (host is null)
{
throw new ArgumentNullException(nameof(host));
}
var hostContent = DomHelper.CreateAvaloniaHost(host);
if (hostContent == null)
{
throw new InvalidOperationException("Avalonia WASM host wasn't initialized.");
}
var nativeControlsContainer = hostContent.GetPropertyAsJSObject("nativeHost")
?? throw new InvalidOperationException("NativeHost cannot be null");
var inputElement = hostContent.GetPropertyAsJSObject("inputElement")
?? throw new InvalidOperationException("InputElement cannot be null");
var topLevelImpl = new BrowserTopLevelImpl(host, nativeControlsContainer, inputElement);
_topLevel = new EmbeddableControlRoot(topLevelImpl);
_topLevel.Prepare();
_topLevel.GotFocus += (_, _) => InputHelper.FocusElement(host);
_topLevel.Renderer.Start(); // TODO: use Start+StopRenderer() instead.
_topLevel.RequestAnimationFrame(_ =>
{
// Try to get local splash-screen of the specific host.
// If couldn't find - get global one by ID for compatibility.
var splash = DomHelper.GetElementsByClassName("avalonia-splash", host)
?? DomHelper.GetElementById("avalonia-splash", BrowserWindowingPlatform.GlobalThis);
if (splash is not null)
{
DomHelper.AddCssClass(splash, "splash-close");
splash.Dispose();
}
});
}
public Control? Content
{
get => (Control)_topLevel.Content!;
set => _topLevel.Content = value;
}
internal TopLevel TopLevel => _topLevel;
}
}