[gnome-builder/editor-layout] command-bar: Use binding set for complete keybinding
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/editor-layout] command-bar: Use binding set for complete keybinding
- Date: Wed, 26 Nov 2014 03:48:10 +0000 (UTC)
commit eb8e9d4403c75754686bb7e50df6bc16b7ff9066
Author: Alexander Larsson <alexl redhat com>
Date: Mon Nov 24 21:47:48 2014 +0100
command-bar: Use binding set for complete keybinding
This seems cleaner.
https://bugzilla.gnome.org/show_bug.cgi?id=740659
src/commands/gb-command-bar.c | 231 +++++++++++++++++++++++------------------
src/commands/gb-command-bar.h | 2 +
2 files changed, 131 insertions(+), 102 deletions(-)
---
diff --git a/src/commands/gb-command-bar.c b/src/commands/gb-command-bar.c
index bfae313..bd3659a 100644
--- a/src/commands/gb-command-bar.c
+++ b/src/commands/gb-command-bar.c
@@ -38,6 +38,13 @@ struct _GbCommandBarPrivate
G_DEFINE_TYPE_WITH_PRIVATE (GbCommandBar, gb_command_bar, GTK_TYPE_REVEALER)
+enum {
+ COMPLETE,
+ LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
GtkWidget *
gb_command_bar_new (void)
{
@@ -228,124 +235,120 @@ find_longest_common_prefix (gchar **strv)
#define N_COMPLETION_COLUMS 3
#define N_UNSCROLLED_COMPLETION_ROWS 4
-static gboolean
-gb_command_bar_on_entry_key_press_event (GbCommandBar *bar,
- GdkEventKey *event,
- GtkEntry *entry)
+static void
+gb_command_bar_complete (GbCommandBar *bar)
{
- g_return_val_if_fail (GB_IS_COMMAND_BAR (bar), FALSE);
- g_return_val_if_fail (event, FALSE);
- g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
+ GtkEditable *editable = GTK_EDITABLE (bar->priv->entry);
+ GtkWidget *viewport = gtk_bin_get_child (GTK_BIN (bar->priv->completion_scroller));
+ GbWorkbench *workbench;
+ GbCommandManager *manager;
+ gchar **completions;
+ int pos, i;
+ gchar *current_prefix, *expanded_prefix;
- if (event->keyval == GDK_KEY_Escape)
+ workbench = GB_WORKBENCH (gtk_widget_get_toplevel (GTK_WIDGET (bar)));
+ if (!workbench)
+ return;
+
+ pos = gtk_editable_get_position (editable);
+ current_prefix = gtk_editable_get_chars (editable, 0, pos);
+
+ /* If we complete again with the same data we scroll the completion instead */
+ if (gtk_widget_is_visible (GTK_WIDGET (bar->priv->completion_scroller)) &&
+ bar->priv->last_completion != NULL &&
+ strcmp (bar->priv->last_completion, current_prefix) == 0)
{
- gb_command_bar_hide (bar);
- return TRUE;
- }
+ GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment (bar->priv->completion_scroller);
+ int viewport_height = gtk_widget_get_allocated_height (viewport);
+ int y = gtk_adjustment_get_value (vadj);
+ int max = gtk_adjustment_get_upper (vadj);
+
+ y += viewport_height;
+ if (y >= max)
+ y = 0;
- if (event->keyval == GDK_KEY_Tab)
+ gtk_adjustment_set_value (vadj, y);
+ }
+ else
{
- GbWorkbench *workbench = NULL;
+ g_clear_pointer (&bar->priv->last_completion, g_free);
+
+ manager = gb_workbench_get_command_manager (workbench);
+ completions = gb_command_manager_complete (manager, current_prefix);
+
+ expanded_prefix = find_longest_common_prefix (completions);
- workbench = GB_WORKBENCH (gtk_widget_get_toplevel (GTK_WIDGET (bar)));
- if (workbench)
+ if (strlen (expanded_prefix) > strlen (current_prefix))
{
- GtkEditable *editable = GTK_EDITABLE (bar->priv->entry);
- GtkWidget *viewport = gtk_bin_get_child (GTK_BIN (bar->priv->completion_scroller));
- GbCommandManager *manager;
- gchar **completions;
- int pos, i;
- gchar *current_prefix, *expanded_prefix;
-
- pos = gtk_editable_get_position (editable);
- current_prefix = gtk_editable_get_chars (editable, 0, pos);
-
- /* If we complete again with the same data we scroll the completion instead */
- if (gtk_widget_is_visible (GTK_WIDGET (bar->priv->completion_scroller)) &&
- bar->priv->last_completion != NULL &&
- strcmp (bar->priv->last_completion, current_prefix) == 0)
+ gtk_widget_hide (GTK_WIDGET (bar->priv->completion_scroller));
+ gtk_editable_insert_text (editable, expanded_prefix + strlen (current_prefix), -1, &pos);
+ gtk_editable_set_position (editable, pos);
+ }
+ else if (g_strv_length (completions) > 1)
+ {
+ gint wrapped_height = 0;
+ bar->priv->last_completion = g_strdup (current_prefix);
+
+ gtk_widget_show (GTK_WIDGET (bar->priv->completion_scroller));
+ gtk_container_foreach (GTK_CONTAINER (bar->priv->flow_box),
+ (GtkCallback)gtk_widget_destroy, NULL);
+
+ gtk_flow_box_set_min_children_per_line (bar->priv->flow_box, N_COMPLETION_COLUMS);
+ gtk_flow_box_set_max_children_per_line (bar->priv->flow_box, N_COMPLETION_COLUMS);
+
+ for (i = 0; completions[i] != NULL; i++)
{
- GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment (bar->priv->completion_scroller);
- int viewport_height = gtk_widget_get_allocated_height (viewport);
- int y = gtk_adjustment_get_value (vadj);
- int max = gtk_adjustment_get_upper (vadj);
+ GtkWidget *label;
+ char *s;
- y += viewport_height;
- if (y >= max)
- y = 0;
+ label = gtk_label_new ("");
+ s = g_strdup_printf ("<b>%s</b>%s", current_prefix, completions[i] + strlen (current_prefix));
+ gtk_label_set_markup (GTK_LABEL (label), s);
+ g_free (s);
- gtk_adjustment_set_value (vadj, y);
+ gtk_container_add (GTK_CONTAINER (bar->priv->flow_box), label);
+ gtk_widget_show (label);
+
+ if (i == N_COMPLETION_COLUMS * N_UNSCROLLED_COMPLETION_ROWS - 1)
+ gtk_widget_get_preferred_height (GTK_WIDGET (bar->priv->flow_box), &wrapped_height, NULL);
+ }
+
+ if (i < N_COMPLETION_COLUMS * N_UNSCROLLED_COMPLETION_ROWS)
+ {
+ gtk_widget_set_size_request (GTK_WIDGET (bar->priv->completion_scroller), -1, -1);
+ gtk_scrolled_window_set_policy (bar->priv->completion_scroller,
+ GTK_POLICY_NEVER, GTK_POLICY_NEVER);
}
else
{
- g_clear_pointer (&bar->priv->last_completion, g_free);
-
- manager = gb_workbench_get_command_manager (workbench);
- completions = gb_command_manager_complete (manager, current_prefix);
-
- expanded_prefix = find_longest_common_prefix (completions);
-
- if (strlen (expanded_prefix) > strlen (current_prefix))
- {
- gtk_widget_hide (GTK_WIDGET (bar->priv->completion_scroller));
- gtk_editable_insert_text (editable, expanded_prefix + strlen (current_prefix), -1, &pos);
- gtk_editable_set_position (editable, pos);
- }
- else if (g_strv_length (completions) > 1)
- {
- gint wrapped_height = 0;
- bar->priv->last_completion = g_strdup (current_prefix);
-
- gtk_widget_show (GTK_WIDGET (bar->priv->completion_scroller));
- gtk_container_foreach (GTK_CONTAINER (bar->priv->flow_box),
- (GtkCallback)gtk_widget_destroy, NULL);
-
-
- gtk_flow_box_set_min_children_per_line (bar->priv->flow_box, N_COMPLETION_COLUMS);
- gtk_flow_box_set_max_children_per_line (bar->priv->flow_box, N_COMPLETION_COLUMS);
-
- for (i = 0; completions[i] != NULL; i++)
- {
- GtkWidget *label;
- char *s;
-
- label = gtk_label_new ("");
- s = g_strdup_printf ("<b>%s</b>%s", current_prefix, completions[i] + strlen
(current_prefix));
- gtk_label_set_markup (GTK_LABEL (label), s);
- g_free (s);
-
- gtk_container_add (GTK_CONTAINER (bar->priv->flow_box), label);
- gtk_widget_show (label);
-
- if (i == N_COMPLETION_COLUMS * N_UNSCROLLED_COMPLETION_ROWS - 1)
- gtk_widget_get_preferred_height (GTK_WIDGET (bar->priv->flow_box), &wrapped_height,
NULL);
- }
-
- if (i < N_COMPLETION_COLUMS * N_UNSCROLLED_COMPLETION_ROWS)
- {
- gtk_widget_set_size_request (GTK_WIDGET (bar->priv->completion_scroller), -1, -1);
- gtk_scrolled_window_set_policy (bar->priv->completion_scroller,
- GTK_POLICY_NEVER, GTK_POLICY_NEVER);
- }
- else
- {
- gtk_widget_set_size_request (GTK_WIDGET (bar->priv->completion_scroller), -1,
wrapped_height);
- gtk_scrolled_window_set_policy (bar->priv->completion_scroller,
- GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
- }
- }
- else
- gtk_widget_hide (GTK_WIDGET (bar->priv->completion_scroller));
-
- g_free (expanded_prefix);
-
- g_strfreev (completions);
+ gtk_widget_set_size_request (GTK_WIDGET (bar->priv->completion_scroller), -1, wrapped_height);
+ gtk_scrolled_window_set_policy (bar->priv->completion_scroller,
+ GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
}
+ }
+ else
+ gtk_widget_hide (GTK_WIDGET (bar->priv->completion_scroller));
- g_free (current_prefix);
+ g_free (expanded_prefix);
+ g_strfreev (completions);
+ }
- return TRUE;
- }
+ g_free (current_prefix);
+}
+
+static gboolean
+gb_command_bar_on_entry_key_press_event (GbCommandBar *bar,
+ GdkEventKey *event,
+ GtkEntry *entry)
+{
+ g_return_val_if_fail (GB_IS_COMMAND_BAR (bar), FALSE);
+ g_return_val_if_fail (event, FALSE);
+ g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
+
+ if (event->keyval == GDK_KEY_Escape)
+ {
+ gb_command_bar_hide (bar);
+ return TRUE;
}
return GDK_EVENT_PROPAGATE;
@@ -421,12 +424,15 @@ gb_command_bar_class_init (GbCommandBarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+ GtkBindingSet *binding_set;
object_class->constructed = gb_command_bar_constructed;
object_class->finalize = gb_command_bar_finalize;
widget_class->grab_focus = gb_command_bar_grab_focus;
+ klass->complete = gb_command_bar_complete;
+
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gnome/builder/ui/gb-command-bar.ui");
@@ -436,6 +442,27 @@ gb_command_bar_class_init (GbCommandBarClass *klass)
gtk_widget_class_bind_template_child_private (widget_class, GbCommandBar, result_size_group);
gtk_widget_class_bind_template_child_private (widget_class, GbCommandBar, completion_scroller);
gtk_widget_class_bind_template_child_private (widget_class, GbCommandBar, flow_box);
+
+
+ /**
+ * GbCommandBar::complete:
+ * @bar: the object which received the signal.
+ */
+ signals[COMPLETE] =
+ g_signal_new ("complete",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+ G_STRUCT_OFFSET (GbCommandBarClass, complete),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+
+ binding_set = gtk_binding_set_by_class (klass);
+
+ gtk_binding_entry_add_signal (binding_set,
+ GDK_KEY_Tab, 0,
+ "complete", 0);
}
static void
diff --git a/src/commands/gb-command-bar.h b/src/commands/gb-command-bar.h
index 7f120b8..fa333aa 100644
--- a/src/commands/gb-command-bar.h
+++ b/src/commands/gb-command-bar.h
@@ -46,6 +46,8 @@ struct _GbCommandBar
struct _GbCommandBarClass
{
GtkRevealerClass parent;
+
+ void (*complete) (GbCommandBar *bar);
};
GType gb_command_bar_get_type (void) G_GNUC_CONST;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]