diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2020-10-15 15:55:31 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2020-10-16 00:34:01 +0100 |
commit | 5f1128899c5913b8ff6070ee0ef06007ca0c8944 (patch) | |
tree | 4b95dfb32a7997ab6b4ceaa0a5c6378d6f317015 | |
parent | e44f27a7ed8cd785d8bb1b85e36c409a1d63ade3 (diff) | |
download | gtk+-5f1128899c5913b8ff6070ee0ef06007ca0c8944.tar.gz |
a11y: De-duplicate some action code
The widgets with hard coded actions should share more code, instead of
copy-pasting it.
-rw-r--r-- | gtk/a11y/gtkatspiaction.c | 323 |
1 files changed, 165 insertions, 158 deletions
diff --git a/gtk/a11y/gtkatspiaction.c b/gtk/a11y/gtkatspiaction.c index bdec00659f..c25b0d2c34 100644 --- a/gtk/a11y/gtkatspiaction.c +++ b/gtk/a11y/gtkatspiaction.c @@ -35,28 +35,34 @@ #include <glib/gi18n-lib.h> -static void -button_handle_method (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *method_name, - GVariant *parameters, - GDBusMethodInvocation *invocation, - gpointer user_data) +typedef struct _Action Action; + +struct _Action { - GtkAtSpiContext *self = user_data; - GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self)); - GtkWidget *widget = GTK_WIDGET (accessible); + const char *name; + const char *localized_name; + const char *description; + const char *keybinding; +}; +static void +action_handle_method (GtkAtSpiContext *self, + const char *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + const Action *actions, + int n_actions) +{ if (g_strcmp0 (method_name, "GetName") == 0) { - int idx; + int idx = -1; g_variant_get (parameters, "(i)", &idx); - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "click")); + const Action *action = &actions[idx]; + + if (idx >= 0 && idx < n_actions) + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", action->name)); else g_dbus_method_invocation_return_error (invocation, G_IO_ERROR, @@ -66,47 +72,38 @@ button_handle_method (GDBusConnection *connection, } else if (g_strcmp0 (method_name, "GetLocalizedName") == 0) { - int idx; + int idx = -1; g_variant_get (parameters, "(i)", &idx); - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, - g_variant_new ("(s)", C_("accessibility", "Click"))); - else - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_INVALID_ARGUMENT, - "Unknown action %d", - idx); - } - else if (g_strcmp0 (method_name, "GetDescription") == 0) - { - int idx; - - g_variant_get (parameters, "(i)", &idx); + if (idx >= 0 && idx < n_actions) + { + const Action *action = &actions[idx]; + const char *s = g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->localized_name); - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, - g_variant_new ("(s)", - C_("accessibility", "Clicks the button"))); + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", s)); + } else - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_INVALID_ARGUMENT, - "Unknown action %d", - idx); + { + g_dbus_method_invocation_return_error (invocation, + G_IO_ERROR, + G_IO_ERROR_INVALID_ARGUMENT, + "Unknown action %d", + idx); + } } - else if (g_strcmp0 (method_name, "DoAction") == 0) + else if (g_strcmp0 (method_name, "GetDescription") == 0) { - int idx; + int idx = -1; g_variant_get (parameters, "(i)", &idx); - if (idx == 0) + if (idx >= 0 && idx < n_actions) { - gtk_widget_activate (widget); - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE)); + const Action *action = &actions[idx]; + const char *s = g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->description); + + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", s)); } else { @@ -119,12 +116,14 @@ button_handle_method (GDBusConnection *connection, } else if (g_strcmp0 (method_name, "GetKeyBinding") == 0) { - int idx; + int idx = -1; g_variant_get (parameters, "(i)", &idx); - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "<Space>")); + const Action *action = &actions[idx]; + + if (idx >= 0 && idx < n_actions) + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", action->keybinding)); else g_dbus_method_invocation_return_error (invocation, G_IO_ERROR, @@ -136,17 +135,92 @@ button_handle_method (GDBusConnection *connection, { GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("a(sss)")); - g_variant_builder_add (&builder, "(ssss)", - "click", - C_("accessibility", "Click"), - C_("accessibility", "Activates the button"), - "<Space>"); + for (int i = 0; i < n_actions; i++) + { + const Action *action = &actions[i]; - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(ssss))", &builder)); + g_variant_builder_add (&builder, "(sss)", + g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->localized_name), + g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->description), + action->keybinding); + } + + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(sss))", &builder)); + } + else if (g_strcmp0 (method_name, "DoAction") == 0) + { + GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self)); + GtkWidget *widget = GTK_WIDGET (accessible); + int idx = -1; + + if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget)) + { + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", FALSE)); + return; + } + + g_variant_get (parameters, "(i)", &idx); + + if (idx >= 0 && idx < n_actions) + { + gtk_widget_activate (widget); + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE)); + } + else + { + g_dbus_method_invocation_return_error (invocation, + G_IO_ERROR, + G_IO_ERROR_INVALID_ARGUMENT, + "Unknown action %d", + idx); + } } } static GVariant * +action_handle_get_property (GtkAtSpiContext *self, + const char *property_name, + GError **error, + const Action *actions, + int n_actions) +{ + if (g_strcmp0 (property_name, "NActions") == 0) + return g_variant_new_int32 (n_actions); + + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, + "Unknown property '%s'", property_name); + + return NULL; +} + +/* {{{ GtkButton */ +static Action button_actions[] = { + { + .name = "click", + .localized_name = NC_("accessibility", "Click"), + .description = NC_("accessibility", "Clicks the button"), + .keybinding = "<Space>", + }, +}; + +static void +button_handle_method (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + GtkAtSpiContext *self = user_data; + + action_handle_method (self, method_name, parameters, invocation, + button_actions, + G_N_ELEMENTS (button_actions)); +} + +static GVariant * button_handle_get_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, @@ -155,15 +229,11 @@ button_handle_get_property (GDBusConnection *connection, GError **error, gpointer user_data) { - GVariant *res = NULL; - - if (g_strcmp0 (property_name, "NActions") == 0) - res = g_variant_new_int32 (1); - else - g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, - "Unknown property '%s'", property_name); + GtkAtSpiContext *self = user_data; - return res; + return action_handle_get_property (self, property_name, error, + button_actions, + G_N_ELEMENTS (button_actions)); } static const GDBusInterfaceVTable button_action_vtable = { @@ -172,6 +242,19 @@ static const GDBusInterfaceVTable button_action_vtable = { NULL, }; +/* }}} */ + +/* {{{ GtkSwitch */ + +static const Action switch_actions[] = { + { + .name = "toggle", + .localized_name = NC_("accessibility", "Toggle"), + .description = NC_("accessibility", "Toggles the switch"), + .keybinding = "<Space>", + }, +}; + static void switch_handle_method (GDBusConnection *connection, const gchar *sender, @@ -183,112 +266,36 @@ switch_handle_method (GDBusConnection *connection, gpointer user_data) { GtkAtSpiContext *self = user_data; - GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self)); - GtkWidget *widget = GTK_WIDGET (accessible); - - if (g_strcmp0 (method_name, "GetName") == 0) - { - int idx; - - g_variant_get (parameters, "(i)", &idx); - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "toggle")); - else - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_INVALID_ARGUMENT, - "Unknown action %d", - idx); - } - else if (g_strcmp0 (method_name, "GetLocalizedName") == 0) - { - int idx; - - g_variant_get (parameters, "(i)", &idx); - - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, - g_variant_new ("(s)", C_("accessibility", "Toggle"))); - else - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_INVALID_ARGUMENT, - "Unknown action %d", - idx); - } - else if (g_strcmp0 (method_name, "GetDescription") == 0) - { - int idx; - - g_variant_get (parameters, "(i)", &idx); - - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, - g_variant_new ("(s)", - C_("accessibility", "Toggles the switch"))); - else - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_INVALID_ARGUMENT, - "Unknown action %d", - idx); - } - else if (g_strcmp0 (method_name, "DoAction") == 0) - { - int idx; - - g_variant_get (parameters, "(i)", &idx); - - if (idx == 0) - { - gtk_widget_activate (widget); - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE)); - } - else - { - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_INVALID_ARGUMENT, - "Unknown action %d", - idx); - } - } - else if (g_strcmp0 (method_name, "GetKeyBinding") == 0) - { - int idx; - - g_variant_get (parameters, "(i)", &idx); - - if (idx == 0) - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "<Space>")); - else - g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_INVALID_ARGUMENT, - "Unknown action %d", - idx); - } - else if (g_strcmp0 (method_name, "GetActions") == 0) - { - GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("a(sss)")); + action_handle_method (self, method_name, parameters, invocation, + switch_actions, + G_N_ELEMENTS (switch_actions)); +} - g_variant_builder_add (&builder, "(ssss)", - "click", - C_("accessibility", "Click"), - C_("accessibility", "Activates the button"), - "<Space>"); +static GVariant * +switch_handle_get_property (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *property_name, + GError **error, + gpointer user_data) +{ + GtkAtSpiContext *self = user_data; - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(ssss))", &builder)); - } + return action_handle_get_property (self, property_name, error, + switch_actions, + G_N_ELEMENTS (switch_actions)); } static const GDBusInterfaceVTable switch_action_vtable = { switch_handle_method, - button_handle_get_property, + switch_handle_get_property, NULL, }; +/* }}} */ + static gboolean is_valid_action (GtkActionMuxer *muxer, const char *action_name) |