browser export
This commit is contained in:
1 parent
1d974b42a1
commit
0966c6de17
13 files changed
+640
-207
No files matched your search
+60
-13
@@ -178,6 +178,10 @@ struct View {
|
||||
ComPtr<ICoreWebView2> webview;
|
||||
std::string html;
|
||||
std::wstring base_url;
|
||||
// Browser view (created with a url): loaded from the network, links that
|
||||
// open a window stay in the view.
|
||||
bool browser = false;
|
||||
std::wstring url;
|
||||
RECT bounds{}; // physical pixels, in host parent's client coordinates
|
||||
bool visible = false;
|
||||
// Run once WebView2 is ready; called with false if the view is disposed
|
||||
@@ -276,7 +280,7 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
// --- create ---------------------------------------------------------------
|
||||
|
||||
void Create(int64_t id, const std::string& html, const std::string& base,
|
||||
MethodResultPtr result) {
|
||||
const std::string* url, MethodResultPtr result) {
|
||||
HWND parent = HostParent();
|
||||
if (parent == nullptr) {
|
||||
result->Error("no_window", "Flutter view has no native window");
|
||||
@@ -292,6 +296,10 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
auto view = std::make_unique<View>();
|
||||
view->html = html;
|
||||
view->base_url = Utf8ToWide(base);
|
||||
if (url != nullptr) {
|
||||
view->browser = true;
|
||||
view->url = Utf8ToWide(*url);
|
||||
}
|
||||
view->host = CreateWindowExW(
|
||||
0, kHostClass, L"", WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0,
|
||||
0, 0, parent, nullptr, GetModuleHandleW(nullptr), nullptr);
|
||||
@@ -348,7 +356,8 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
ICoreWebView2* wv = v->webview.Get();
|
||||
|
||||
ComPtr<ICoreWebView2Controller2> controller2;
|
||||
if (SUCCEEDED(controller->QueryInterface(IID_PPV_ARGS(&controller2)))) {
|
||||
if (!v->browser &&
|
||||
SUCCEEDED(controller->QueryInterface(IID_PPV_ARGS(&controller2)))) {
|
||||
controller2->put_DefaultBackgroundColor({255, 0, 0, 0}); // opaque black
|
||||
}
|
||||
ComPtr<ICoreWebView2Settings> settings;
|
||||
@@ -376,10 +385,12 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
&token);
|
||||
|
||||
// Serve the page from the virtual origin, like load_html on Linux.
|
||||
std::wstring filter = v->base_url + L"*";
|
||||
wv->AddWebResourceRequestedFilter(filter.c_str(),
|
||||
COREWEBVIEW2_WEB_RESOURCE_CONTEXT_DOCUMENT);
|
||||
ComPtr<ICoreWebView2Environment> environment(env);
|
||||
if (!v->browser) {
|
||||
std::wstring filter = v->base_url + L"*";
|
||||
wv->AddWebResourceRequestedFilter(
|
||||
filter.c_str(), COREWEBVIEW2_WEB_RESOURCE_CONTEXT_DOCUMENT);
|
||||
}
|
||||
wv->add_WebResourceRequested(
|
||||
Callback<ICoreWebView2WebResourceRequestedEventHandler>(
|
||||
[this, id, environment](
|
||||
@@ -401,14 +412,21 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
.Get(),
|
||||
&token);
|
||||
|
||||
// Links opening a new window (Cesium credits...) go to the browser.
|
||||
// Links opening a new window: Cesium pages (credits...) send them to the
|
||||
// user's browser, browser views load them in place.
|
||||
wv->add_NewWindowRequested(
|
||||
Callback<ICoreWebView2NewWindowRequestedEventHandler>(
|
||||
[](ICoreWebView2*, ICoreWebView2NewWindowRequestedEventArgs* args) {
|
||||
[this, id](ICoreWebView2* sender,
|
||||
ICoreWebView2NewWindowRequestedEventArgs* args) {
|
||||
CoStr uri;
|
||||
if (SUCCEEDED(args->get_Uri(&uri.p)) && uri.p) {
|
||||
ShellExecuteW(nullptr, L"open", uri.p, nullptr, nullptr,
|
||||
SW_SHOWNORMAL);
|
||||
View* target = Find(id);
|
||||
if (target != nullptr && target->browser) {
|
||||
sender->Navigate(uri.p);
|
||||
} else {
|
||||
ShellExecuteW(nullptr, L"open", uri.p, nullptr, nullptr,
|
||||
SW_SHOWNORMAL);
|
||||
}
|
||||
}
|
||||
args->put_Handled(TRUE);
|
||||
return S_OK;
|
||||
@@ -450,7 +468,7 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
&token);
|
||||
|
||||
ApplyBounds(v);
|
||||
wv->Navigate(v->base_url.c_str());
|
||||
wv->Navigate(v->browser ? v->url.c_str() : v->base_url.c_str());
|
||||
|
||||
auto pending = std::move(v->pending);
|
||||
for (auto& fn : pending) fn(true);
|
||||
@@ -512,6 +530,16 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
// Runs [fn] now if WebView2 is ready, else once it is (or with false if the
|
||||
// view is disposed first).
|
||||
static void WhenReady(View* v, std::function<void(bool)> fn) {
|
||||
if (v->webview) {
|
||||
fn(true);
|
||||
} else {
|
||||
v->pending.push_back(std::move(fn));
|
||||
}
|
||||
}
|
||||
|
||||
void Dispose(int64_t id) {
|
||||
auto it = views_.find(id);
|
||||
if (it == views_.end()) return;
|
||||
@@ -545,19 +573,22 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
}
|
||||
if (method == "create") {
|
||||
const std::string* html = GetString(args, "html");
|
||||
if (!id || html == nullptr) {
|
||||
result->Error("bad_args", "create requires id and html");
|
||||
const std::string* url = GetString(args, "url");
|
||||
if (!id || (html == nullptr && url == nullptr)) {
|
||||
result->Error("bad_args", "create requires id and html or url");
|
||||
} else if (Find(*id)) {
|
||||
result->Error("exists", "a view with this id already exists");
|
||||
} else {
|
||||
const std::string* base = GetString(args, "baseUrl");
|
||||
Create(*id, *html, base ? *base : kDefaultBaseUrl, std::move(result));
|
||||
Create(*id, html ? *html : std::string(),
|
||||
base ? *base : kDefaultBaseUrl, url, std::move(result));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
View* v = id ? Find(*id) : nullptr;
|
||||
if (method != "setRect" && method != "runJavaScript" &&
|
||||
method != "loadUrl" && method != "reload" &&
|
||||
method != "openInspector") {
|
||||
result->NotImplemented();
|
||||
return;
|
||||
@@ -576,6 +607,22 @@ class CesiumjsPlugin : public flutter::Plugin {
|
||||
} else {
|
||||
RunJavaScript(v, *script, std::move(result));
|
||||
}
|
||||
} else if (method == "loadUrl") {
|
||||
const std::string* url = GetString(args, "url");
|
||||
if (url == nullptr) {
|
||||
result->Error("bad_args", "url missing");
|
||||
} else {
|
||||
std::wstring wurl = Utf8ToWide(*url);
|
||||
WhenReady(v, [v, wurl](bool ready) {
|
||||
if (ready) v->webview->Navigate(wurl.c_str());
|
||||
});
|
||||
result->Success();
|
||||
}
|
||||
} else if (method == "reload") {
|
||||
WhenReady(v, [v](bool ready) {
|
||||
if (ready) v->webview->Reload();
|
||||
});
|
||||
result->Success();
|
||||
} else { // openInspector
|
||||
if (v->webview) v->webview->OpenDevToolsWindow();
|
||||
result->Success();
|
||||
|
||||
Reference in new issue
Block a user