[libadwaita/ebassi/tagged-entry] Use a template for AdwTagWidget
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libadwaita/ebassi/tagged-entry] Use a template for AdwTagWidget
- Date: Sun, 6 Mar 2022 19:24:39 +0000 (UTC)
commit 5abff32450636a69a8d9eb6fd566310be41d8698
Author: Emmanuele Bassi <ebassi gnome org>
Date: Sun Mar 6 18:31:32 2022 +0000
Use a template for AdwTagWidget
Instead of constructing the widget in code, we should use a UI description
file.
src/adw-tag-widget.c | 37 +++++++++++++------------------------
src/adw-tag-widget.ui | 29 +++++++++++++++++++++++++++++
src/adwaita.gresources.xml | 1 +
3 files changed, 43 insertions(+), 24 deletions(-)
---
diff --git a/src/adw-tag-widget.c b/src/adw-tag-widget.c
index d2eb5d49..74006075 100644
--- a/src/adw-tag-widget.c
+++ b/src/adw-tag-widget.c
@@ -17,8 +17,6 @@ struct _AdwTagWidget
AdwTag *tag;
- GtkGesture *gesture;
-
GtkWidget *icon;
GtkWidget *label;
GtkWidget *close_button;
@@ -106,9 +104,10 @@ static void
adw_tag_widget__click_released (AdwTagWidget *self,
guint n_press,
double x,
- double y)
+ double y,
+ GtkGesture *gesture)
{
- gtk_gesture_set_state (self->gesture, GTK_EVENT_SEQUENCE_CLAIMED);
+ gtk_gesture_set_state (gesture, GTK_EVENT_SEQUENCE_CLAIMED);
const char *action_name = adw_tag_get_action_name (self->tag);
if (action_name == NULL)
@@ -198,6 +197,15 @@ adw_tag_widget_class_init (AdwTagWidgetClass *klass)
NULL,
G_TYPE_NONE, 1, ADW_TYPE_TAG);
+ gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Adwaita/ui/adw-tag-widget.ui");
+
+ gtk_widget_class_bind_template_child (widget_class, AdwTagWidget, icon);
+ gtk_widget_class_bind_template_child (widget_class, AdwTagWidget, label);
+ gtk_widget_class_bind_template_child (widget_class, AdwTagWidget, close_button);
+
+ gtk_widget_class_bind_template_callback (widget_class, adw_tag_widget__click_released);
+ gtk_widget_class_bind_template_callback (widget_class, adw_tag_widget__close_clicked);
+
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_GROUP);
gtk_widget_class_set_css_name (widget_class, "tag");
@@ -206,26 +214,7 @@ adw_tag_widget_class_init (AdwTagWidgetClass *klass)
static void
adw_tag_widget_init (AdwTagWidget *self)
{
- self->icon = gtk_image_new ();
- gtk_widget_set_parent (self->icon, GTK_WIDGET (self));
-
- self->label = gtk_label_new (NULL);
- gtk_widget_set_parent (self->label, GTK_WIDGET (self));
-
- self->close_button = gtk_button_new_from_icon_name ("window-close-symbolic");
- gtk_widget_set_parent (self->close_button, GTK_WIDGET (self));
- gtk_widget_add_css_class (self->close_button, "flat");
- gtk_widget_add_css_class (self->close_button, "circular");
- g_signal_connect_swapped (self->close_button,
- "clicked",
- G_CALLBACK (adw_tag_widget__close_clicked),
- self);
-
- self->gesture = gtk_gesture_click_new ();
- gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (self->gesture), FALSE);
- gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (self->gesture), GDK_BUTTON_PRIMARY);
- g_signal_connect_swapped (self->gesture, "released", G_CALLBACK (adw_tag_widget__click_released), self);
- gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (self->gesture));
+ gtk_widget_init_template (GTK_WIDGET (self));
}
AdwTag *
diff --git a/src/adw-tag-widget.ui b/src/adw-tag-widget.ui
new file mode 100644
index 00000000..e2967d14
--- /dev/null
+++ b/src/adw-tag-widget.ui
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="libadwaita">
+ <requires lib="gtk" version="4.0"/>
+ <template class="AdwTagWidget">
+ <child>
+ <object class="GtkGestureClick">
+ <signal name="released" handler="adw_tag_widget__click_released" swapped="yes"/>
+ </object>
+ </child>
+ <child>
+ <object class="GtkImage" id="icon">
+ </object>
+ </child>
+ <child>
+ <object class="GtkLabel" id="label">
+ </object>
+ </child>
+ <child>
+ <object class="GtkButton" id="close_button">
+ <property name="icon-name">window-close-symbolic</property>
+ <signal name="clicked" handler="adw_tag_widget__close_clicked" swapped="yes"/>
+ <style>
+ <class name="flat"/>
+ <class name="circular"/>
+ </style>
+ </object>
+ </child>
+ </template>
+</interface>
diff --git a/src/adwaita.gresources.xml b/src/adwaita.gresources.xml
index 21524a9e..271a5cd1 100644
--- a/src/adwaita.gresources.xml
+++ b/src/adwaita.gresources.xml
@@ -18,6 +18,7 @@
<file preprocess="xml-stripblanks">adw-status-page.ui</file>
<file preprocess="xml-stripblanks">adw-tab.ui</file>
<file preprocess="xml-stripblanks">adw-tab-bar.ui</file>
+ <file preprocess="xml-stripblanks">adw-tag-widget.ui</file>
<file preprocess="xml-stripblanks">adw-toast-widget.ui</file>
<file preprocess="xml-stripblanks">adw-view-switcher-bar.ui</file>
<file preprocess="xml-stripblanks">adw-view-switcher-button.ui</file>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]