-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider_windows.cpp
279 lines (239 loc) · 7.35 KB
/
provider_windows.cpp
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
// +build ignore
// To build the windows provider, you will need to have the building env for c++/winrt.
// Since CGO uses gcc for windows and does not support the VC++ compiler 'cl.exe'.
// That's why there is a 'ignore' tag above.
// The easiest way to build a GoUI windows app is through [GoUI-cli](https://github.com/FIPress/GoUI-cli)
// You will find instructions to manually build the provider in the readme.
#define UNICODE
#include <iostream>
#include <direct.h>
#include <functional>
#include <hstring.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Web.UI.Interop.h>
#include "provider_windows.h"
#pragma comment(lib, "user32")
#pragma comment(lib, "windowsapp")
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::Web;
using namespace Windows::Web::UI;
using namespace Windows::Web::UI::Interop;
HWND hWnd = nullptr;
fnHandleClientReq handleClientReq = nullptr;
WebViewControl webView = nullptr;
fnGoUILog fnLog = nullptr;
static void goLog(const char* log) {
if(fnLog != nullptr) {
fnLog(log);
}
}
namespace {
class UriToStreamResolver : public winrt::implements<UriToStreamResolver, IUriToStreamResolver>
{
private:
static hstring webBase;
public:
UriToStreamResolver()
{
}
static void InitWebPath(const char* dir) {
wchar_t cwd[MAX_PATH];
//_getcwd(cwd, MAX_PATH);
GetModuleFileName(NULL, cwd, MAX_PATH);
webBase = Uri(winrt::to_hstring(cwd), winrt::to_hstring("ui")).AbsoluteUri();
}
IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri) const
{
Uri localUri = Uri(webBase+uri.Path());
auto fOp = StorageFile::GetFileFromPathAsync(localUri.AbsoluteUri());
StorageFile f = fOp.get();
auto sOp = f.OpenAsync(FileAccessMode::Read);
IRandomAccessStream stream = sOp.get();
co_return stream.GetInputStreamAt(0);
}
};
}
hstring UriToStreamResolver::webBase ;
Rect getRect() {
if (hWnd == nullptr ) {
return Rect();
}
RECT r;
GetClientRect(hWnd, &r);
return Rect(r.left, r.top, r.right - r.left, r.bottom - r.top);
}
void resize() {
if (webView == nullptr) {
return;
}
Rect rect = getRect();
webView.Bounds(rect);
}
void invokeScript(const char* js) {
goUILog("invokeScript :%s",js);
webView.InvokeScriptAsync(
L"eval", single_threaded_vector<winrt::hstring>({ winrt::to_hstring(js) }));
}
void createWebView(const char* dir, const char* index) {
goUILog("create webview:%s",index);
init_apartment(winrt::apartment_type::single_threaded);
UriToStreamResolver::InitWebPath(dir);
WebViewControlProcess wvProcess = WebViewControlProcess();
auto op = wvProcess.CreateWebViewControlAsync(
reinterpret_cast<int64_t>(hWnd), getRect());
op.Completed([index](IAsyncOperation<WebViewControl> const& sender, AsyncStatus args) {
webView = sender.GetResults();
webView.Settings().IsScriptNotifyAllowed(true);
webView.IsVisible(true);
webView.ScriptNotify([](auto const& sender, auto const& args) {
if(handleClientReq != 0) {
goUILog("handle Client Req");
handleClientReq(to_string(args.Value()).c_str());
} else {
goUILog("no handler");
}
});
/*
webView.NavigationStarting([](auto const& sender, auto const& args) {
});
*/
auto uri = webView.BuildLocalStreamUri(L"GoUI", winrt::to_hstring(index));
auto resolver = winrt::make_self<UriToStreamResolver>();
IUriToStreamResolver r = resolver.as<IUriToStreamResolver>();
webView.NavigateToLocalStreamUri(uri, r);
//Windows::Foundation::Uri uri{ L"ms-appx-web:///web/index.html" };
//webView.Navigate(uri);
});
}
//The Microsoft Edge WebView2 control enables you to host web content in your application using Microsoft Edge(Chromium) as the rendering engine.
/*
void createWebView2(HWND hWnd) {
ComPtr<IWebView2WebView> webviewWindow;
// Known issue - app needs to run on PerMonitorV2 DPI awareness for WebView to look properly
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
// Locate the browser and set up the environment for WebView
// Use CreateWebView2EnvironmentWithDetails if you need to specify browser location, user folder, etc.
CreateWebView2Environment(
Callback<IWebView2CreateWebView2EnvironmentCompletedHandler>(
[&webviewWindow, hWnd](HRESULT result, IWebView2Environment * env) -> HRESULT {
// Create a WebView, whose parent is the main window hWnd
env->CreateWebView(hWnd, Callback<IWebView2CreateWebViewCompletedHandler>
([&webviewWindow, hWnd](HRESULT result, IWebView2WebView * webview) -> HRESULT {
if (webview != nullptr) {
webviewWindow = webview;
}
// Add a few settings for the webview
// this is a redundant demo step as they are the default settings values
IWebView2Settings* Settings;
webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Settings->put_IsWebMessageEnabled(TRUE);
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewWindow->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
webviewWindow->Navigate(L"https://www.bing.com/");
// Step 4 - Navigation events
// Step 5 - Scripting
// Step 6 - Communication between host and web content
return S_OK;
}).Get());
return S_OK;
}).Get());
}*/
void closeWindow() {
PostQuitMessage(0);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SIZE:
resize();
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
closeWindow();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
void createWindow(WindowSettings settings) {
HINSTANCE hInst = winrt::check_pointer(GetModuleHandle(nullptr));
const auto className = L"GoUIWindow";
WNDCLASSEX wce;
ZeroMemory(&wce, sizeof(WNDCLASSEX));
wce.cbSize = sizeof(WNDCLASSEX);
wce.style = CS_HREDRAW | CS_VREDRAW;
wce.lpfnWndProc = WndProc;
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hInstance = hInst;
wce.lpszClassName = className;
check_bool(::RegisterClassExW(&wce));
hWnd = check_pointer(CreateWindow(
className,
to_hstring(settings.title).c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
settings.width,
settings.height,
NULL,
NULL,
hInst,
NULL
));
ShowWindow(hWnd,
SW_SHOW);
UpdateWindow(hWnd);
SetFocus(hWnd);
}
using dispatch_fn_t = std::function<void()>;
void run() {
MSG msg;
BOOL res;
while ((res = GetMessage(&msg, nullptr, 0, 0)) != -1) {
if (msg.hwnd) {
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
if (msg.message == WM_APP) {
auto f = (dispatch_fn_t*)(msg.lParam);
(*f)();
delete f;
}
else if (msg.message == WM_QUIT) {
return;
}
}
}
void __cdecl seLogger(fnGoUILog fn) {
fnLog = fn;
}
void __cdecl setHandleClientReq(fnHandleClientReq fn) {
handleClientReq = fn;
}
void __cdecl create(WindowSettings settings, MenuDef* menuDefs, int menuCount) {
createWindow(settings);
createWebView(settings.webDir, settings.index);
run();
}
void __cdecl invokeJS(const char* js) {
invokeScript(js);
}
void __cdecl exitWebview() {
closeWindow();
}