gtkfontchooser: Use GtkEventControllerKey

Use an event controller on GtkFontChooserDialog, a nice side effect
is that we can use gtk_event_controller_key_forward() and
gtk_search_entry_set_key_capture_widget() instead of passing events
around for dialog search.
This commit is contained in:
Carlos Garnacho 2018-06-07 00:49:04 +02:00
parent 4186a85359
commit 3eb1b22651
4 changed files with 18 additions and 28 deletions

View File

@ -39,6 +39,7 @@
#include "gtktogglebutton.h"
#include "gtkheaderbar.h"
#include "gtkactionable.h"
#include "gtkeventcontrollerkey.h"
struct _GtkFontChooserDialogPrivate
{
@ -124,21 +125,15 @@ font_activated_cb (GtkFontChooser *fontchooser,
}
static gboolean
gtk_font_chooser_dialog_event (GtkWidget *dialog,
GdkEvent *event)
dialog_forward_key (GtkEventControllerKey *controller,
guint keyval,
guint keycode,
GdkModifierType modifiers,
GtkWidget *widget)
{
GtkFontChooserDialog *fdialog = GTK_FONT_CHOOSER_DIALOG (dialog);
gboolean handled = FALSE;
GtkFontChooserDialog *fdialog = GTK_FONT_CHOOSER_DIALOG (widget);
if (gdk_event_get_event_type (event) != GDK_KEY_PRESS)
return GDK_EVENT_PROPAGATE;
handled = GTK_WIDGET_CLASS (gtk_font_chooser_dialog_parent_class)->event (dialog, event);
if (!handled)
handled = gtk_font_chooser_widget_handle_event (fdialog->priv->fontchooser, (GdkEventKey *)event);
return handled;
return gtk_event_controller_key_forward (controller, fdialog->priv->fontchooser);
}
static void
@ -208,7 +203,6 @@ gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
gobject_class->get_property = gtk_font_chooser_dialog_get_property;
gobject_class->set_property = gtk_font_chooser_dialog_set_property;
widget_class->event = gtk_font_chooser_dialog_event;
widget_class->map = gtk_font_chooser_dialog_map;
_gtk_font_chooser_install_properties (gobject_class);
@ -222,6 +216,7 @@ gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserDialog, select_button);
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserDialog, cancel_button);
gtk_widget_class_bind_template_callback (widget_class, font_activated_cb);
gtk_widget_class_bind_template_callback (widget_class, dialog_forward_key);
}
static void

View File

@ -881,6 +881,9 @@ gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
gtk_font_chooser_widget_set_cell_size (fontchooser);
gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
gtk_search_entry_set_key_capture_widget (GTK_SEARCH_ENTRY (priv->search_entry),
GTK_WIDGET (fontchooser));
}
/**
@ -2520,17 +2523,6 @@ gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
iface->get_font_map = gtk_font_chooser_widget_get_font_map;
}
gboolean
gtk_font_chooser_widget_handle_event (GtkWidget *widget,
GdkEventKey *key_event)
{
GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
GdkEvent *event = (GdkEvent *)key_event;
return gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->search_entry), event);
}
GAction *
gtk_font_chooser_widget_get_tweak_action (GtkWidget *widget)
{

View File

@ -22,9 +22,6 @@
G_BEGIN_DECLS
gboolean gtk_font_chooser_widget_handle_event (GtkWidget *widget,
GdkEventKey *event);
GAction *gtk_font_chooser_widget_get_tweak_action (GtkWidget *fontchooser);
G_END_DECLS

View File

@ -33,6 +33,12 @@
<property name="can-default">1</property>
</object>
</child>
<child>
<object class="GtkEventControllerKey">
<signal name="key-pressed" handler="dialog_forward_key" swapped="no"/>
<signal name="key-released" handler="dialog_forward_key" swapped="no"/>
</object>
</child>
<action-widgets>
<action-widget response="cancel">cancel_button</action-widget>
<action-widget response="ok" default="true">select_button</action-widget>