[gtksourceview/wip/chergert/vim: 330/363] remove observe-key signal
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/chergert/vim: 330/363] remove observe-key signal
- Date: Mon, 8 Nov 2021 19:53:55 +0000 (UTC)
commit d707c1bc17367c60e22afb70a7f91b23af7c6226
Author: Christian Hergert <chergert redhat com>
Date: Fri Nov 5 16:57:16 2021 -0700
remove observe-key signal
this was annoying and we didnt want it long term. just need a hook for
testing the input method.
gtksourceview/gtksourcevimimcontext-private.h | 38 +++++++++++++++++
gtksourceview/gtksourcevimimcontext.c | 60 ++++++++++++++++++++-------
tests/test-vim.c | 6 ++-
3 files changed, 87 insertions(+), 17 deletions(-)
---
diff --git a/gtksourceview/gtksourcevimimcontext-private.h b/gtksourceview/gtksourcevimimcontext-private.h
new file mode 100644
index 00000000..c6f9f61f
--- /dev/null
+++ b/gtksourceview/gtksourcevimimcontext-private.h
@@ -0,0 +1,38 @@
+/*
+ * This file is part of GtkSourceView
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#pragma once
+
+#include "gtksourcevimimcontext.h"
+
+G_BEGIN_DECLS
+
+typedef void (*GtkSourceVimIMContextObserver) (GtkSourceVimIMContext *im_context,
+ const char *string,
+ gboolean reset,
+ gpointer user_data);
+
+void _gtk_source_vim_im_context_add_observer (GtkSourceVimIMContext *self,
+ GtkSourceVimIMContextObserver observer,
+ gpointer user_data,
+ GDestroyNotify notify);
+
+G_END_DECLS
diff --git a/gtksourceview/gtksourcevimimcontext.c b/gtksourceview/gtksourcevimimcontext.c
index 5b730a21..c6d7187d 100644
--- a/gtksourceview/gtksourcevimimcontext.c
+++ b/gtksourceview/gtksourcevimimcontext.c
@@ -22,7 +22,7 @@
#include "config.h"
#include "gtksourceview.h"
-#include "gtksourcevimimcontext.h"
+#include "gtksourcevimimcontext-private.h"
#include "gtksource-enumtypes.h"
#include "vim/gtk-source-vim.h"
@@ -39,9 +39,17 @@ struct _GtkSourceVimIMContext
{
GtkIMContext parent_instance;
GtkSourceVim *vim;
+ GArray *observers;
guint reset_observer : 1;
};
+typedef struct
+{
+ GtkSourceVimIMContextObserver observer;
+ gpointer data;
+ GDestroyNotify notify;
+} Observer;
+
G_DEFINE_TYPE (GtkSourceVimIMContext, gtk_source_vim_im_context, GTK_TYPE_IM_CONTEXT)
enum {
@@ -54,13 +62,21 @@ enum {
enum {
EXECUTE_COMMAND,
FORMAT_TEXT,
- OBSERVE_KEY,
N_SIGNALS
};
static GParamSpec *properties[N_PROPS];
static guint signals[N_SIGNALS];
+static void
+clear_observer (Observer *o)
+{
+ if (o->notify)
+ {
+ o->notify (o->data);
+ }
+}
+
GtkIMContext *
gtk_source_vim_im_context_new (void)
{
@@ -216,7 +232,12 @@ gtk_source_vim_im_context_filter_keypress (GtkIMContext *context,
keyval = gdk_key_event_get_keyval (event);
gtk_source_vim_state_keyval_to_string (keyval, mods, str);
- g_signal_emit (self, signals[OBSERVE_KEY], 0, str, self->reset_observer);
+ for (guint i = 0; i < self->observers->len; i++)
+ {
+ const Observer *o = &g_array_index (self->observers, Observer, i);
+
+ o->observer (self, str, self->reset_observer, o->data);
+ }
self->reset_observer = FALSE;
}
@@ -230,6 +251,7 @@ gtk_source_vim_im_context_dispose (GObject *object)
GtkSourceVimIMContext *self = (GtkSourceVimIMContext *)object;
g_clear_object (&self->vim);
+ g_clear_pointer (&self->observers, g_array_unref);
G_OBJECT_CLASS (gtk_source_vim_im_context_parent_class)->dispose (object);
}
@@ -336,23 +358,31 @@ gtk_source_vim_im_context_class_init (GtkSourceVimIMContextClass *klass)
2,
GTK_TYPE_TEXT_ITER,
GTK_TYPE_TEXT_ITER);
-
- signals[OBSERVE_KEY] =
- g_signal_new ("observe-key",
- G_TYPE_FROM_CLASS (klass),
- G_SIGNAL_RUN_LAST,
- 0,
- NULL, NULL,
- NULL,
- G_TYPE_NONE,
- 2,
- G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
- G_TYPE_BOOLEAN);
}
static void
gtk_source_vim_im_context_init (GtkSourceVimIMContext *self)
{
+ self->observers = g_array_new (FALSE, FALSE, sizeof (Observer));
+ g_array_set_clear_func (self->observers, (GDestroyNotify)clear_observer);
+}
+
+void
+_gtk_source_vim_im_context_add_observer (GtkSourceVimIMContext *self,
+ GtkSourceVimIMContextObserver observer,
+ gpointer data,
+ GDestroyNotify notify)
+{
+ Observer o;
+
+ g_return_if_fail (GTK_SOURCE_IS_VIM_IM_CONTEXT (self));
+ g_return_if_fail (observer != NULL);
+
+ o.observer = observer;
+ o.data = data;
+ o.notify = notify;
+
+ g_array_append_val (self->observers, o);
}
/**
diff --git a/tests/test-vim.c b/tests/test-vim.c
index 88c40fe0..e50dd633 100644
--- a/tests/test-vim.c
+++ b/tests/test-vim.c
@@ -22,6 +22,7 @@
#include "config.h"
#include <gtksourceview/gtksource.h>
+#include <gtksourceview/gtksourcevimimcontext-private.h>
static GMainLoop *main_loop;
static GString *sequence;
@@ -97,8 +98,9 @@ static void
observe_key (GtkSourceVimIMContext *self,
const char *str,
gboolean reset_observer,
- GtkLabel *label)
+ gpointer data)
{
+ GtkLabel *label = data;
if (reset_observer)
g_string_truncate (sequence, 0);
g_string_append (sequence, str);
@@ -197,7 +199,7 @@ main (int argc,
g_object_bind_property (im_context, "command-bar-text", command_bar, "label", G_BINDING_SYNC_CREATE);
g_object_bind_property (im_context, "command-text", command, "label", G_BINDING_SYNC_CREATE);
g_signal_connect (im_context, "execute-command", G_CALLBACK (execute_command), NULL);
- g_signal_connect (im_context, "observe-key", G_CALLBACK (observe_key), observe);
+ _gtk_source_vim_im_context_add_observer (GTK_SOURCE_VIM_IM_CONTEXT (im_context), observe_key,
observe, NULL);
gtk_im_context_set_client_widget (im_context, GTK_WIDGET (view));
key = gtk_event_controller_key_new ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]