first commit

This commit is contained in:
yves committed 2026-11-07 14:39:59 +01:00
commit c069b8945d
34 files changed
+2424

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
# Linux build of the cesiumjs Flutter plugin.
cmake_minimum_required(VERSION 3.10)
set(PROJECT_NAME "cesiumjs")
project(${PROJECT_NAME} LANGUAGES CXX)
# Must match the pluginClass in pubspec.yaml (CesiumjsPlugin -> cesiumjs_plugin).
set(PLUGIN_NAME "cesiumjs_plugin")
add_library(${PLUGIN_NAME} SHARED
"cesiumjs_plugin.cc"
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter)
target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::GTK)
# WebKitGTK hosts CesiumJS (GPU-accelerated WebGL2).
# Debian/Ubuntu: sudo apt install libwebkit2gtk-4.1-dev
pkg_check_modules(WEBKIT REQUIRED IMPORTED_TARGET webkit2gtk-4.1)
target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::WEBKIT)
# Libraries to bundle with the app (none: WebKitGTK is a system library).
set(cesiumjs_bundled_libraries
""
PARENT_SCOPE
)
+383
View File
@@ -0,0 +1,383 @@
#include "include/cesiumjs/cesiumjs_plugin.h"
#include <webkit2/webkit2.h>
#include <cstring>
#include <map>
namespace {
constexpr char kChannelName[] = "cesiumjs/webview";
constexpr char kMessageHandlerName[] = "cesium";
constexpr char kRectKey[] = "cesium-rect";
struct Plugin {
GtkOverlay* overlay = nullptr;
FlMethodChannel* channel = nullptr;
std::map<int64_t, WebKitWebView*> views;
};
Plugin* g_plugin = nullptr;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
FlValue* lookup(FlValue* args, const char* key) {
if (args == nullptr || fl_value_get_type(args) != FL_VALUE_TYPE_MAP) {
return nullptr;
}
return fl_value_lookup_string(args, key);
}
int64_t get_int(FlValue* args, const char* key, int64_t fallback = -1) {
FlValue* v = lookup(args, key);
if (v == nullptr) return fallback;
if (fl_value_get_type(v) == FL_VALUE_TYPE_INT) return fl_value_get_int(v);
if (fl_value_get_type(v) == FL_VALUE_TYPE_FLOAT) {
return static_cast<int64_t>(fl_value_get_float(v));
}
return fallback;
}
double get_double(FlValue* args, const char* key) {
FlValue* v = lookup(args, key);
if (v == nullptr) return 0;
if (fl_value_get_type(v) == FL_VALUE_TYPE_FLOAT) return fl_value_get_float(v);
if (fl_value_get_type(v) == FL_VALUE_TYPE_INT) return fl_value_get_int(v);
return 0;
}
const gchar* get_string(FlValue* args, const char* key) {
FlValue* v = lookup(args, key);
if (v == nullptr || fl_value_get_type(v) != FL_VALUE_TYPE_STRING) {
return nullptr;
}
return fl_value_get_string(v);
}
void send_event(const char* method, int64_t id, const gchar* message) {
if (g_plugin == nullptr) return;
g_autoptr(FlValue) args = fl_value_new_map();
fl_value_set_string_take(args, "id", fl_value_new_int(id));
fl_value_set_string_take(args, "message",
fl_value_new_string(message ? message : ""));
fl_method_channel_invoke_method(g_plugin->channel, method, args, nullptr,
nullptr, nullptr);
}
int64_t view_id(gpointer widget) {
return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "cesium-id"));
}
// ---------------------------------------------------------------------------
// Layout: place each web view at the rectangle reported by Dart.
// ---------------------------------------------------------------------------
gboolean on_get_child_position(GtkOverlay* overlay, GtkWidget* widget,
GdkRectangle* allocation, gpointer) {
auto* rect =
static_cast<GdkRectangle*>(g_object_get_data(G_OBJECT(widget), kRectKey));
if (rect == nullptr) return FALSE;
*allocation = *rect;
return TRUE;
}
// ---------------------------------------------------------------------------
// WebKit signal handlers
// ---------------------------------------------------------------------------
// JS -> Dart: window.webkit.messageHandlers.cesium.postMessage("...")
void on_script_message(WebKitUserContentManager*, WebKitJavascriptResult* result,
gpointer view) {
JSCValue* value = webkit_javascript_result_get_js_value(result);
g_autofree gchar* str = jsc_value_to_string(value);
send_event("onMessage", view_id(view), str);
}
gboolean on_load_failed(WebKitWebView* view, WebKitLoadEvent, gchar* uri,
GError* error, gpointer) {
g_autofree gchar* msg = g_strdup_printf("Load failed (%s): %s", uri,
error ? error->message : "?");
send_event("onError", view_id(view), msg);
return FALSE;
}
void on_web_process_terminated(WebKitWebView* view,
WebKitWebProcessTerminationReason reason,
gpointer) {
const char* msg = reason == WEBKIT_WEB_PROCESS_CRASHED
? "Web process crashed"
: "Web process terminated (memory limit or request)";
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,
WebKitPolicyDecisionType type, gpointer) {
if (type != WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION) return FALSE;
auto* nav = WEBKIT_NAVIGATION_POLICY_DECISION(decision);
WebKitNavigationAction* action =
webkit_navigation_policy_decision_get_navigation_action(nav);
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);
}
webkit_policy_decision_ignore(decision);
return TRUE;
}
// Give the web view its own native (X11) child window. Without this, the view
// is composited by GTK3 into the toplevel together with the FlView's GL
// surface, and WebKit frames reach the screen only sporadically.
void on_view_realize(GtkWidget* widget, gpointer) {
GdkWindow* window = gtk_widget_get_window(widget);
if (window != nullptr) gdk_window_ensure_native(window);
}
// ---------------------------------------------------------------------------
// Method implementations
// ---------------------------------------------------------------------------
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) {
return FL_METHOD_RESPONSE(fl_method_error_response_new(
"bad_args", "create requires id and html", nullptr));
}
if (g_plugin->views.count(id)) {
return FL_METHOD_RESPONSE(fl_method_error_response_new(
"exists", "a view with this id already exists", nullptr));
}
WebKitUserContentManager* ucm = webkit_user_content_manager_new();
webkit_user_content_manager_register_script_message_handler(
ucm, kMessageHandlerName);
auto* view = WEBKIT_WEB_VIEW(
g_object_new(WEBKIT_TYPE_WEB_VIEW, "user-content-manager", ucm, nullptr));
g_object_unref(ucm); // owned by the view now
g_object_ref_sink(view);
g_object_set_data(G_OBJECT(view), "cesium-id", GINT_TO_POINTER(id));
g_object_set_data_full(G_OBJECT(view), kRectKey, g_new0(GdkRectangle, 1),
g_free);
WebKitSettings* settings = webkit_web_view_get_settings(view);
webkit_settings_set_enable_webgl(settings, TRUE);
webkit_settings_set_enable_javascript(settings, TRUE);
webkit_settings_set_enable_developer_extras(settings, TRUE);
webkit_settings_set_javascript_can_open_windows_automatically(settings,
FALSE);
g_autofree gchar* signal =
g_strdup_printf("script-message-received::%s", kMessageHandlerName);
g_signal_connect(ucm, signal, G_CALLBACK(on_script_message), view);
g_signal_connect(view, "load-failed", G_CALLBACK(on_load_failed), nullptr);
g_signal_connect(view, "web-process-terminated",
G_CALLBACK(on_web_process_terminated), nullptr);
g_signal_connect(view, "decide-policy", G_CALLBACK(on_decide_policy),
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);
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/");
g_plugin->views[id] = view;
return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
}
WebKitWebView* find_view(FlValue* args) {
auto it = g_plugin->views.find(get_int(args, "id"));
return it == g_plugin->views.end() ? nullptr : it->second;
}
FlMethodResponse* not_found() {
return FL_METHOD_RESPONSE(
fl_method_error_response_new("not_found", "unknown view id", nullptr));
}
FlMethodResponse* set_rect(FlValue* args) {
WebKitWebView* view = find_view(args);
if (view == nullptr) return not_found();
auto* rect =
static_cast<GdkRectangle*>(g_object_get_data(G_OBJECT(view), kRectKey));
rect->x = static_cast<int>(get_double(args, "x") + 0.5);
rect->y = static_cast<int>(get_double(args, "y") + 0.5);
rect->width = static_cast<int>(get_double(args, "width") + 0.5);
rect->height = static_cast<int>(get_double(args, "height") + 0.5);
FlValue* visible_v = lookup(args, "visible");
bool visible = visible_v == nullptr ||
fl_value_get_type(visible_v) != FL_VALUE_TYPE_BOOL ||
fl_value_get_bool(visible_v);
visible = visible && rect->width > 0 && rect->height > 0;
if (visible) {
gtk_widget_show(GTK_WIDGET(view));
gtk_widget_queue_resize(GTK_WIDGET(view));
} else {
gtk_widget_hide(GTK_WIDGET(view));
}
return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
}
void on_js_finished(GObject* object, GAsyncResult* res, gpointer user_data) {
g_autoptr(FlMethodCall) call = FL_METHOD_CALL(user_data);
g_autoptr(GError) error = nullptr;
JSCValue* value = webkit_web_view_evaluate_javascript_finish(
WEBKIT_WEB_VIEW(object), res, &error);
if (value == nullptr) {
fl_method_call_respond_error(call, "js_error",
error ? error->message : "unknown", nullptr,
nullptr);
return;
}
g_autoptr(FlValue) result = nullptr;
if (jsc_value_is_undefined(value) || jsc_value_is_null(value)) {
result = fl_value_new_null();
} else {
g_autofree gchar* str = jsc_value_to_string(value);
result = fl_value_new_string(str);
}
g_object_unref(value);
fl_method_call_respond_success(call, result, nullptr);
}
void dispose_view(int64_t id) {
auto it = g_plugin->views.find(id);
if (it == g_plugin->views.end()) return;
WebKitWebView* view = it->second;
g_plugin->views.erase(it);
gtk_container_remove(GTK_CONTAINER(g_plugin->overlay), GTK_WIDGET(view));
gtk_widget_destroy(GTK_WIDGET(view));
g_object_unref(view);
}
void method_call_cb(FlMethodChannel*, FlMethodCall* call, gpointer) {
const gchar* method = fl_method_call_get_name(call);
FlValue* args = fl_method_call_get_args(call);
g_autoptr(FlMethodResponse) response = nullptr;
if (strcmp(method, "create") == 0) {
response = create_view(args);
} else if (strcmp(method, "setRect") == 0) {
response = set_rect(args);
} else if (strcmp(method, "runJavaScript") == 0) {
WebKitWebView* view = find_view(args);
const gchar* script = get_string(args, "script");
if (view == nullptr) {
response = not_found();
} else if (script == nullptr) {
response = FL_METHOD_RESPONSE(
fl_method_error_response_new("bad_args", "script missing", nullptr));
} else {
webkit_web_view_evaluate_javascript(view, script, -1, nullptr, nullptr,
nullptr, on_js_finished,
g_object_ref(call));
return; // answered asynchronously
}
} else if (strcmp(method, "openInspector") == 0) {
WebKitWebView* view = find_view(args);
if (view == nullptr) {
response = not_found();
} else {
webkit_web_inspector_show(webkit_web_view_get_inspector(view));
response = FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
}
} else if (strcmp(method, "dispose") == 0) {
dispose_view(get_int(args, "id"));
response = FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
} else if (strcmp(method, "disposeAll") == 0) {
// Called once per Dart isolate start, so a hot restart does not leave
// orphaned web views stacked over the new UI.
while (!g_plugin->views.empty()) {
dispose_view(g_plugin->views.begin()->first);
}
response = FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
}
fl_method_call_respond(call, response, nullptr);
}
// Returns the GtkOverlay hosting [view], creating one around it if needed.
//
// The default Flutter runner puts the FlView directly in the GtkWindow and
// realizes it (starting the engine and its GL surface) before plugins are
// registered. A plain remove/add would unrealize the FlView and break its GL
// context, so the move uses gtk_widget_reparent(), which keeps a widget
// realized when the destination is realized too:
// 1. realize a new overlay inside a temporary (never shown) window,
// 2. move the FlView into it (the GtkWindow is now empty),
// 3. move the overlay into the GtkWindow.
GtkOverlay* ensure_overlay(FlView* view) {
GtkWidget* view_widget = GTK_WIDGET(view);
GtkWidget* parent = gtk_widget_get_parent(view_widget);
if (parent != nullptr && GTK_IS_OVERLAY(parent)) return GTK_OVERLAY(parent);
GtkWidget* overlay = gtk_overlay_new();
gtk_widget_show(overlay);
if (parent == nullptr) {
// Not in a window yet: the host app will add it later, which would fail
// if we parented it here. Ask for the runner change instead.
g_warning(
"cesiumjs: the FlView has no parent at plugin registration; wrap it "
"in a GtkOverlay in the runner (see README).");
return GTK_OVERLAY(overlay);
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
if (gtk_widget_get_realized(view_widget)) {
GtkWidget* temp = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(temp), overlay);
gtk_widget_realize(overlay);
gtk_widget_reparent(view_widget, overlay);
gtk_widget_reparent(overlay, parent);
gtk_widget_destroy(temp);
} else {
g_object_ref(view_widget);
gtk_container_remove(GTK_CONTAINER(parent), view_widget);
gtk_container_add(GTK_CONTAINER(overlay), view_widget);
gtk_container_add(GTK_CONTAINER(parent), overlay);
g_object_unref(view_widget);
}
G_GNUC_END_IGNORE_DEPRECATIONS
return GTK_OVERLAY(overlay);
}
} // namespace
void cesiumjs_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
if (g_plugin != nullptr) return; // one FlView per app is supported
FlView* view = fl_plugin_registrar_get_view(registrar);
if (view == nullptr) {
g_warning("cesiumjs: no FlView available (headless?); plugin disabled.");
return;
}
g_plugin = new Plugin();
g_plugin->overlay = ensure_overlay(view);
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_plugin->channel = fl_method_channel_new(
fl_plugin_registrar_get_messenger(registrar), kChannelName,
FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(g_plugin->channel, method_call_cb,
nullptr, nullptr);
g_signal_connect(g_plugin->overlay, "get-child-position",
G_CALLBACK(on_get_child_position), nullptr);
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef FLUTTER_PLUGIN_CESIUMJS_PLUGIN_H_
#define FLUTTER_PLUGIN_CESIUMJS_PLUGIN_H_
#include <flutter_linux/flutter_linux.h>
G_BEGIN_DECLS
#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
#else
#define FLUTTER_PLUGIN_EXPORT
#endif
// Native side of the `cesiumjs/webview` method channel.
//
// Flutter on Linux has no platform-view support, so each CesiumJS web view is
// a real WebKitGTK widget stacked on top of the FlView inside a GtkOverlay.
// The Dart `CesiumViewer` widget reports its on-screen rectangle and the
// native widget is positioned to match.
//
// If the FlView is not already inside a GtkOverlay, registration wraps it in
// one, so host apps need no runner changes.
FLUTTER_PLUGIN_EXPORT void cesiumjs_plugin_register_with_registrar(
FlPluginRegistrar* registrar);
G_END_DECLS
#endif // FLUTTER_PLUGIN_CESIUMJS_PLUGIN_H_