Replace update semantics handler with signal. (#163583)

The previous code was built where there was only one view, and this view
provided the callback to the engine.

Now we have multiple views, they all should process the semantics update
and so the callback is replaced with a signal. This is similar to the
signal used for engine restarts.

---------

Co-authored-by: Harlen Batagelo <hbatagelo@gmail.com>
This commit is contained in:
Robert Ancell 2025-02-28 12:30:53 +13:00 committed by GitHub
parent bffe0ffc50
commit 407065ea20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 74 deletions

View File

@ -97,11 +97,6 @@ struct _FlEngine {
FlEnginePlatformMessageHandler platform_message_handler;
gpointer platform_message_handler_data;
GDestroyNotify platform_message_handler_destroy_notify;
// Function to call when a semantic node is received.
FlEngineUpdateSemanticsHandler update_semantics_handler;
gpointer update_semantics_handler_data;
GDestroyNotify update_semantics_handler_destroy_notify;
};
G_DEFINE_QUARK(fl_engine_error_quark, fl_engine_error)
@ -109,7 +104,7 @@ G_DEFINE_QUARK(fl_engine_error_quark, fl_engine_error)
static void fl_engine_plugin_registry_iface_init(
FlPluginRegistryInterface* iface);
enum { SIGNAL_ON_PRE_ENGINE_RESTART, LAST_SIGNAL };
enum { SIGNAL_ON_PRE_ENGINE_RESTART, SIGNAL_UPDATE_SEMANTICS, LAST_SIGNAL };
static guint fl_engine_signals[LAST_SIGNAL];
@ -389,10 +384,7 @@ static void fl_engine_update_semantics_cb(const FlutterSemanticsUpdate2* update,
void* user_data) {
FlEngine* self = FL_ENGINE(user_data);
if (self->update_semantics_handler != nullptr) {
self->update_semantics_handler(self, update,
self->update_semantics_handler_data);
}
g_signal_emit(self, fl_engine_signals[SIGNAL_UPDATE_SEMANTICS], 0, update);
}
static void setup_keyboard(FlEngine* self) {
@ -505,13 +497,6 @@ static void fl_engine_dispose(GObject* object) {
self->platform_message_handler_data = nullptr;
self->platform_message_handler_destroy_notify = nullptr;
if (self->update_semantics_handler_destroy_notify) {
self->update_semantics_handler_destroy_notify(
self->update_semantics_handler_data);
}
self->update_semantics_handler_data = nullptr;
self->update_semantics_handler_destroy_notify = nullptr;
G_OBJECT_CLASS(fl_engine_parent_class)->dispose(object);
}
@ -530,6 +515,9 @@ static void fl_engine_class_init(FlEngineClass* klass) {
fl_engine_signals[SIGNAL_ON_PRE_ENGINE_RESTART] = g_signal_new(
"on-pre-engine-restart", fl_engine_get_type(), G_SIGNAL_RUN_LAST, 0,
nullptr, nullptr, nullptr, G_TYPE_NONE, 0);
fl_engine_signals[SIGNAL_UPDATE_SEMANTICS] = g_signal_new(
"update-semantics", fl_engine_get_type(), G_SIGNAL_RUN_LAST, 0, nullptr,
nullptr, nullptr, G_TYPE_NONE, 1, G_TYPE_POINTER);
}
static void fl_engine_init(FlEngine* self) {
@ -838,23 +826,6 @@ void fl_engine_set_platform_message_handler(
self->platform_message_handler_destroy_notify = destroy_notify;
}
void fl_engine_set_update_semantics_handler(
FlEngine* self,
FlEngineUpdateSemanticsHandler handler,
gpointer user_data,
GDestroyNotify destroy_notify) {
g_return_if_fail(FL_IS_ENGINE(self));
if (self->update_semantics_handler_destroy_notify) {
self->update_semantics_handler_destroy_notify(
self->update_semantics_handler_data);
}
self->update_semantics_handler = handler;
self->update_semantics_handler_data = user_data;
self->update_semantics_handler_destroy_notify = destroy_notify;
}
// Note: This function can be called from any thread.
gboolean fl_engine_send_platform_message_response(
FlEngine* self,

View File

@ -50,19 +50,6 @@ typedef gboolean (*FlEnginePlatformMessageHandler)(
const FlutterPlatformMessageResponseHandle* response_handle,
gpointer user_data);
/**
* FlEngineUpdateSemanticsHandler:
* @engine: an #FlEngine.
* @node: semantic node information.
* @user_data: (closure): data provided when registering this handler.
*
* Function called when semantics node updates are received.
*/
typedef void (*FlEngineUpdateSemanticsHandler)(
FlEngine* engine,
const FlutterSemanticsUpdate2* update,
gpointer user_data);
/**
* fl_engine_new_with_binary_messenger:
* @binary_messenger: an #FlBinaryMessenger.
@ -231,22 +218,6 @@ void fl_engine_set_platform_message_handler(
gpointer user_data,
GDestroyNotify destroy_notify);
/**
* fl_engine_set_update_semantics_handler:
* @engine: an #FlEngine.
* @handler: function to call when a semantics update is received.
* @user_data: (closure): user data to pass to @handler.
* @destroy_notify: (allow-none): a function which gets called to free
* @user_data, or %NULL.
*
* Registers the function called when a semantics update is received.
*/
void fl_engine_set_update_semantics_handler(
FlEngine* engine,
FlEngineUpdateSemanticsHandler handler,
gpointer user_data,
GDestroyNotify destroy_notify);
/**
* fl_engine_send_window_metrics_event:
* @engine: an #FlEngine.

View File

@ -33,9 +33,12 @@ struct _FlView {
// Engine this view is showing.
FlEngine* engine;
// Signal subscription for engine restarts.
// Signal subscription for engine restart signal.
guint on_pre_engine_restart_cb_id;
// Signal subscription for updating semantics signal.
guint update_semantics_cb_id;
// ID for this view.
FlutterViewId view_id;
@ -238,11 +241,8 @@ static void view_added_cb(GObject* object,
}
// Called when the engine updates accessibility.
static void update_semantics_cb(FlEngine* engine,
const FlutterSemanticsUpdate2* update,
gpointer user_data) {
FlView* self = FL_VIEW(user_data);
static void update_semantics_cb(FlView* self,
const FlutterSemanticsUpdate2* update) {
fl_view_accessible_handle_update_semantics(self->view_accessible, update);
}
@ -565,9 +565,6 @@ static void fl_view_dispose(GObject* object) {
g_cancellable_cancel(self->cancellable);
if (self->engine != nullptr) {
fl_engine_set_update_semantics_handler(self->engine, nullptr, nullptr,
nullptr);
FlMouseCursorHandler* handler =
fl_engine_get_mouse_cursor_handler(self->engine);
if (self->cursor_changed_cb_id != 0) {
@ -589,6 +586,11 @@ static void fl_view_dispose(GObject* object) {
self->on_pre_engine_restart_cb_id = 0;
}
if (self->update_semantics_cb_id != 0) {
g_signal_handler_disconnect(self->engine, self->update_semantics_cb_id);
self->update_semantics_cb_id = 0;
}
g_clear_object(&self->engine);
g_clear_object(&self->renderer);
g_clear_pointer(&self->background_color, gdk_rgba_free);
@ -762,11 +764,11 @@ G_MODULE_EXPORT FlView* fl_view_new(FlDartProject* project) {
self->pointer_manager = fl_pointer_manager_new(self->view_id, engine);
fl_engine_set_update_semantics_handler(self->engine, update_semantics_cb,
self, nullptr);
self->on_pre_engine_restart_cb_id =
g_signal_connect_swapped(self->engine, "on-pre-engine-restart",
G_CALLBACK(on_pre_engine_restart_cb), self);
self->update_semantics_cb_id = g_signal_connect_swapped(
engine, "update-semantics", G_CALLBACK(update_semantics_cb), self);
g_signal_connect_swapped(self->gl_area, "create-context",
G_CALLBACK(create_context_cb), self);