browser export
This commit is contained in:
1 parent
1d974b42a1
commit
0966c6de17
13 files changed
+640
-207
No files matched your search
+48
-11
@@ -10,6 +10,7 @@ namespace {
|
||||
constexpr char kChannelName[] = "cesiumjs/webview";
|
||||
constexpr char kMessageHandlerName[] = "cesium";
|
||||
constexpr char kRectKey[] = "cesium-rect";
|
||||
constexpr char kBrowserKey[] = "cesium-browser";
|
||||
|
||||
struct Plugin {
|
||||
GtkOverlay* overlay = nullptr;
|
||||
@@ -112,9 +113,10 @@ void on_web_process_terminated(WebKitWebView* view,
|
||||
send_event("onError", view_id(view), msg);
|
||||
}
|
||||
|
||||
// Keep the web view from opening a new window when CesiumJS links are clicked
|
||||
// (credits, help...): open them in the user's browser instead.
|
||||
gboolean on_decide_policy(WebKitWebView*, WebKitPolicyDecision* decision,
|
||||
// Keep the web view from opening a new window. Cesium pages (credits,
|
||||
// help...) send the link to the user's browser; browser views (created with a
|
||||
// url) load it in place.
|
||||
gboolean on_decide_policy(WebKitWebView* view, WebKitPolicyDecision* decision,
|
||||
WebKitPolicyDecisionType type, gpointer) {
|
||||
if (type != WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION) return FALSE;
|
||||
auto* nav = WEBKIT_NAVIGATION_POLICY_DECISION(decision);
|
||||
@@ -123,7 +125,11 @@ gboolean on_decide_policy(WebKitWebView*, WebKitPolicyDecision* decision,
|
||||
WebKitURIRequest* request = webkit_navigation_action_get_request(action);
|
||||
const gchar* uri = webkit_uri_request_get_uri(request);
|
||||
if (uri != nullptr) {
|
||||
g_app_info_launch_default_for_uri(uri, nullptr, nullptr);
|
||||
if (g_object_get_data(G_OBJECT(view), kBrowserKey) != nullptr) {
|
||||
webkit_web_view_load_uri(view, uri);
|
||||
} else {
|
||||
g_app_info_launch_default_for_uri(uri, nullptr, nullptr);
|
||||
}
|
||||
}
|
||||
webkit_policy_decision_ignore(decision);
|
||||
return TRUE;
|
||||
@@ -145,9 +151,12 @@ FlMethodResponse* create_view(FlValue* args) {
|
||||
int64_t id = get_int(args, "id");
|
||||
const gchar* html = get_string(args, "html");
|
||||
const gchar* base_url = get_string(args, "baseUrl");
|
||||
if (id < 0 || html == nullptr) {
|
||||
// A url makes a browser view: the page is loaded from the network, links
|
||||
// opening a window stay in the view.
|
||||
const gchar* url = get_string(args, "url");
|
||||
if (id < 0 || (html == nullptr && url == nullptr)) {
|
||||
return FL_METHOD_RESPONSE(fl_method_error_response_new(
|
||||
"bad_args", "create requires id and html", nullptr));
|
||||
"bad_args", "create requires id and html or url", nullptr));
|
||||
}
|
||||
if (g_plugin->views.count(id)) {
|
||||
return FL_METHOD_RESPONSE(fl_method_error_response_new(
|
||||
@@ -183,17 +192,25 @@ FlMethodResponse* create_view(FlValue* args) {
|
||||
nullptr);
|
||||
g_signal_connect(view, "realize", G_CALLBACK(on_view_realize), nullptr);
|
||||
|
||||
// Black background so there is no white flash before CesiumJS draws.
|
||||
GdkRGBA black = {0, 0, 0, 1};
|
||||
webkit_web_view_set_background_color(view, &black);
|
||||
if (url != nullptr) {
|
||||
g_object_set_data(G_OBJECT(view), kBrowserKey, GINT_TO_POINTER(1));
|
||||
} else {
|
||||
// Black background so there is no white flash before CesiumJS draws.
|
||||
GdkRGBA black = {0, 0, 0, 1};
|
||||
webkit_web_view_set_background_color(view, &black);
|
||||
}
|
||||
|
||||
gtk_overlay_add_overlay(g_plugin->overlay, GTK_WIDGET(view));
|
||||
// Hidden until Dart reports a non-empty rectangle.
|
||||
gtk_widget_set_no_show_all(GTK_WIDGET(view), TRUE);
|
||||
gtk_widget_hide(GTK_WIDGET(view));
|
||||
|
||||
webkit_web_view_load_html(view, html,
|
||||
base_url ? base_url : "https://cesium.localhost/");
|
||||
if (url != nullptr) {
|
||||
webkit_web_view_load_uri(view, url);
|
||||
} else {
|
||||
webkit_web_view_load_html(
|
||||
view, html, base_url ? base_url : "https://cesium.localhost/");
|
||||
}
|
||||
g_plugin->views[id] = view;
|
||||
return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
|
||||
}
|
||||
@@ -288,6 +305,26 @@ void method_call_cb(FlMethodChannel*, FlMethodCall* call, gpointer) {
|
||||
g_object_ref(call));
|
||||
return; // answered asynchronously
|
||||
}
|
||||
} else if (strcmp(method, "loadUrl") == 0) {
|
||||
WebKitWebView* view = find_view(args);
|
||||
const gchar* url = get_string(args, "url");
|
||||
if (view == nullptr) {
|
||||
response = not_found();
|
||||
} else if (url == nullptr) {
|
||||
response = FL_METHOD_RESPONSE(
|
||||
fl_method_error_response_new("bad_args", "url missing", nullptr));
|
||||
} else {
|
||||
webkit_web_view_load_uri(view, url);
|
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
|
||||
}
|
||||
} else if (strcmp(method, "reload") == 0) {
|
||||
WebKitWebView* view = find_view(args);
|
||||
if (view == nullptr) {
|
||||
response = not_found();
|
||||
} else {
|
||||
webkit_web_view_reload(view);
|
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
|
||||
}
|
||||
} else if (strcmp(method, "openInspector") == 0) {
|
||||
WebKitWebView* view = find_view(args);
|
||||
if (view == nullptr) {
|
||||
|
||||
Reference in new issue
Block a user