-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindowViewModel.cs
81 lines (68 loc) · 2.75 KB
/
MainWindowViewModel.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
////////////////////////////////////////////////////////////////////////////
//
// DupeNukem - WebView attachable full-duplex asynchronous interoperable
// independent messaging library between .NET and JavaScript.
//
// Copyright (c) Kouji Matsui (@kozy_kekyo, @kekyo@mastodon.cloud)
//
// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0
//
////////////////////////////////////////////////////////////////////////////
using CefSharp;
using CefSharp.Wpf;
using Epoxy;
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace DupeNukem.ViewModels
{
[ViewModel] // PropChanged injection by Epoxy
internal sealed partial class MainWindowViewModel
{
public Command Loaded { get; }
public string? Url { get; private set; }
public Pile<ChromiumWebBrowser> CefSharpPile { get; } =
Pile.Factory.Create<ChromiumWebBrowser>();
public MainWindowViewModel()
{
// Step 1: Construct DupeNukem Messenger.
var messenger = new WebViewMessenger();
HookWithMessengerTestCode(messenger); // FOR TEST
// ----
// MainWindow.Loaded:
this.Loaded = Command.Factory.Create<EventArgs>(async _ =>
{
await this.CefSharpPile.RentAsync(cefSharp =>
{
// Startup sequence.
// Bound between CefSharp and DupeNukem Messenger.
// Step 2: Hook up .NET --> JavaScript message handler.
messenger.SendRequest += (s, e) =>
cefSharp.BrowserCore.MainFrame.ExecuteJavaScriptAsync(
e.ToJavaScript());
// Step 3: Attached JavaScript --> .NET message handler.
cefSharp.JavascriptMessageReceived += (s, e) =>
messenger.ReceivedRequest(e.Message.ToString());
// Step 4: Injected Messenger script.
var script = messenger.GetInjectionScript(true);
AddJavaScriptTestCode(script); // FOR TEST
cefSharp.FrameLoadEnd += (s, e) =>
{
if (e.Frame.IsMain)
{
cefSharp.BrowserCore.MainFrame.ExecuteJavaScriptAsync(
script.ToString());
}
};
// Enable dev tools.
cefSharp.ShowDevTools();
// Register test objects.
this.RegisterTestObjects(messenger);
return default;
});
this.Url = "https://www.google.com/";
});
}
}
}