[libhandy] header-group: Support HdyHeaderBar as a child



commit 3cb2e0ba69e461ba4676b405a5541d02f859bb56
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Mon Jun 29 12:54:33 2020 +0200

    header-group: Support HdyHeaderBar as a child
    
    Fixes https://gitlab.gnome.org/GNOME/libhandy/-/issues/105

 debian/libhandy-1-0.symbols |   3 ++
 src/hdy-header-group.c      | 105 +++++++++++++++++++++++++++++++++++++++++++-
 src/hdy-header-group.h      |   7 +++
 tests/test-header-group.c   |   6 +--
 4 files changed, 117 insertions(+), 4 deletions(-)
---
diff --git a/debian/libhandy-1-0.symbols b/debian/libhandy-1-0.symbols
index 55e76aae..dba95929 100644
--- a/debian/libhandy-1-0.symbols
+++ b/debian/libhandy-1-0.symbols
@@ -165,8 +165,10 @@ libhandy-1.so.0 libhandy-1-0 #MINVER#
  hdy_header_bar_set_title@LIBHANDY_1_0 0.0.10
  hdy_header_bar_set_transition_duration@LIBHANDY_1_0 0.0.10
  hdy_header_group_add_gtk_header_bar@LIBHANDY_1_0 0.83.0
+ hdy_header_group_add_header_bar@LIBHANDY_1_0 0.83.0
  hdy_header_group_child_get_child_type@LIBHANDY_1_0 0.83.0
  hdy_header_group_child_get_gtk_header_bar@LIBHANDY_1_0 0.83.0
+ hdy_header_group_child_get_header_bar@LIBHANDY_1_0 0.83.0
  hdy_header_group_child_get_type@LIBHANDY_1_0 0.83.0
  hdy_header_group_child_type_get_type@LIBHANDY_1_0 0.83.0
  hdy_header_group_get_children@LIBHANDY_1_0 0.83.0
@@ -175,6 +177,7 @@ libhandy-1.so.0 libhandy-1-0 #MINVER#
  hdy_header_group_new@LIBHANDY_1_0 0.0.3
  hdy_header_group_remove_child@LIBHANDY_1_0 0.83.0
  hdy_header_group_remove_gtk_header_bar@LIBHANDY_1_0 0.83.0
+ hdy_header_group_remove_header_bar@LIBHANDY_1_0 0.83.0
  hdy_header_group_set_decorate_all@LIBHANDY_1_0 0.83.0
  hdy_init@LIBHANDY_1_0 0.82.0
  hdy_init_public_types@LIBHANDY_1_0 0.0.5
diff --git a/src/hdy-header-group.c b/src/hdy-header-group.c
index 6304abf2..0a0c5aeb 100644
--- a/src/hdy-header-group.c
+++ b/src/hdy-header-group.c
@@ -11,6 +11,7 @@
 
 /**
  * HdyHeaderGroupChildType:
+ * @HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR: The child is a #HdyHeaderBar
  * @HDY_HEADER_GROUP_CHILD_TYPE_GTK_HEADER_BAR: The child is a #GtkHeaderBar
  *
  * This enumeration value describes the child types handled by #HdyHeaderGroup.
@@ -98,6 +99,7 @@ hdy_header_group_child_dispose (GObject *object)
   if (self->object) {
 
     switch (self->type) {
+    case HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR:
     case HDY_HEADER_GROUP_CHILD_TYPE_GTK_HEADER_BAR:
       g_signal_handlers_disconnect_by_func (self->object, G_CALLBACK (object_destroyed_cb), self);
       g_signal_handlers_disconnect_by_func (self->object, G_CALLBACK (forward_update_decoration_layouts), 
self);
@@ -112,6 +114,30 @@ hdy_header_group_child_dispose (GObject *object)
   G_OBJECT_CLASS (hdy_header_group_child_parent_class)->dispose (object);
 }
 
+static HdyHeaderGroupChild *
+hdy_header_group_child_new_for_header_bar (HdyHeaderBar *header_bar)
+{
+  HdyHeaderGroupChild *self;
+  gpointer header_group;
+
+  g_return_val_if_fail (HDY_IS_HEADER_BAR (header_bar), NULL);
+
+  header_group = g_object_get_data (G_OBJECT (header_bar), "header-group");
+
+  g_return_val_if_fail (header_group == NULL, NULL);
+
+  self = g_object_new (HDY_TYPE_HEADER_GROUP_CHILD, NULL);
+  self->type = HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR;
+  self->object = G_OBJECT (header_bar);
+
+  g_signal_connect_swapped (header_bar, "destroy", G_CALLBACK (object_destroyed_cb), self);
+
+  g_signal_connect_swapped (header_bar, "map", G_CALLBACK (forward_update_decoration_layouts), self);
+  g_signal_connect_swapped (header_bar, "unmap", G_CALLBACK (forward_update_decoration_layouts), self);
+
+  return self;
+}
+
 static HdyHeaderGroupChild *
 hdy_header_group_child_new_for_gtk_header_bar (GtkHeaderBar *header_bar)
 {
@@ -156,6 +182,9 @@ hdy_header_group_child_set_decoration_layout (HdyHeaderGroupChild *self,
   g_assert (HDY_IS_HEADER_GROUP_CHILD (self));
 
   switch (self->type) {
+  case HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR:
+    hdy_header_bar_set_decoration_layout (HDY_HEADER_BAR (self->object), layout);
+    break;
   case HDY_HEADER_GROUP_CHILD_TYPE_GTK_HEADER_BAR:
     gtk_header_bar_set_decoration_layout (GTK_HEADER_BAR (self->object), layout);
     break;
@@ -170,6 +199,7 @@ hdy_header_group_child_get_mapped (HdyHeaderGroupChild *self)
   g_assert (HDY_IS_HEADER_GROUP_CHILD (self));
 
   switch (self->type) {
+  case HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR:
   case HDY_HEADER_GROUP_CHILD_TYPE_GTK_HEADER_BAR:
     return gtk_widget_get_mapped (GTK_WIDGET (self->object));
   default:
@@ -296,6 +326,31 @@ hdy_header_group_add_child (HdyHeaderGroup      *self,
   g_object_set_data (G_OBJECT (child), "header-group", self);
 }
 
+/**
+ * hdy_header_group_add_header_bar:
+ * @self: a #HdyHeaderGroup
+ * @header_bar: the #HdyHeaderBar to add
+ *
+ * Adds @header_bar to @self.
+ * When the widget is destroyed or no longer referenced elsewhere, it will
+ * be removed from the header group.
+ *
+ * Since: 1.0
+ */
+void
+hdy_header_group_add_header_bar (HdyHeaderGroup *self,
+                                 HdyHeaderBar   *header_bar)
+{
+  HdyHeaderGroupChild *child;
+
+  g_return_if_fail (HDY_IS_HEADER_GROUP (self));
+  g_return_if_fail (HDY_IS_HEADER_BAR (header_bar));
+  g_return_if_fail (get_child_for_object (self, header_bar) == NULL);
+
+  child = hdy_header_group_child_new_for_header_bar (header_bar);
+  hdy_header_group_add_child (self, child);
+}
+
 /**
  * hdy_header_group_add_gtk_header_bar:
  * @self: a #HdyHeaderGroup
@@ -604,6 +659,9 @@ hdy_header_group_buildable_custom_finished (GtkBuildable *buildable,
     if (GTK_IS_HEADER_BAR (object))
       hdy_header_group_add_gtk_header_bar (HDY_HEADER_GROUP (data->object),
                                            GTK_HEADER_BAR (object));
+    else if (HDY_IS_HEADER_BAR (object))
+      hdy_header_group_add_header_bar (HDY_HEADER_GROUP (data->object),
+                                       HDY_HEADER_BAR (object));
   }
 
   g_slist_free_full (data->items, item_data_free);
@@ -654,6 +712,26 @@ hdy_header_group_buildable_init (GtkBuildableIface *iface)
   iface->custom_finished = hdy_header_group_buildable_custom_finished;
 }
 
+/**
+ * hdy_header_group_child_get_header_bar:
+ * @self: a #HdyHeaderGroupChild
+ *
+ * Gets the child #HdyHeaderBar.
+ * Use hdy_header_group_child_get_child_type() to check the child type.
+ *
+ * Returns: (transfer none): the child #HdyHeaderBar, or %NULL in case of error.
+ *
+ * Since: 1.0
+ */
+HdyHeaderBar *
+hdy_header_group_child_get_header_bar (HdyHeaderGroupChild *self)
+{
+  g_return_val_if_fail (HDY_IS_HEADER_GROUP_CHILD (self), NULL);
+  g_return_val_if_fail (self->type == HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR, NULL);
+
+  return HDY_HEADER_BAR (self->object);
+}
+
 /**
  * hdy_header_group_child_get_gtk_header_bar:
  * @self: a #HdyHeaderGroupChild
@@ -687,7 +765,7 @@ hdy_header_group_child_get_gtk_header_bar (HdyHeaderGroupChild *self)
 HdyHeaderGroupChildType
 hdy_header_group_child_get_child_type (HdyHeaderGroupChild *self)
 {
-  g_return_val_if_fail (HDY_IS_HEADER_GROUP_CHILD (self), HDY_HEADER_GROUP_CHILD_TYPE_GTK_HEADER_BAR);
+  g_return_val_if_fail (HDY_IS_HEADER_GROUP_CHILD (self), HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR);
 
   return self->type;
 }
@@ -723,6 +801,31 @@ remove_child (HdyHeaderGroup      *self,
   g_object_unref (child);
 }
 
+/**
+ * hdy_header_group_remove_header_bar:
+ * @self: a #HdyHeaderGroup
+ * @header_bar: the #HdyHeaderBar to remove
+ *
+ * Removes @header_bar from @self.
+ *
+ * Since: 1.0
+ */
+void
+hdy_header_group_remove_header_bar (HdyHeaderGroup *self,
+                                    HdyHeaderBar   *header_bar)
+{
+  HdyHeaderGroupChild *child;
+
+  g_return_if_fail (HDY_IS_HEADER_GROUP (self));
+  g_return_if_fail (HDY_IS_HEADER_BAR (header_bar));
+
+  child = get_child_for_object (self, header_bar);
+
+  g_return_if_fail (child != NULL);
+
+  remove_child (self, child);
+}
+
 /**
  * hdy_header_group_remove_gtk_header_bar:
  * @self: a #HdyHeaderGroup
diff --git a/src/hdy-header-group.h b/src/hdy-header-group.h
index 50a3127a..ed997ca0 100644
--- a/src/hdy-header-group.h
+++ b/src/hdy-header-group.h
@@ -11,6 +11,7 @@
 #endif
 
 #include <gtk/gtk.h>
+#include "hdy-header-bar.h"
 
 G_BEGIN_DECLS
 
@@ -23,20 +24,26 @@ G_DECLARE_FINAL_TYPE (HdyHeaderGroupChild, hdy_header_group_child, HDY, HEADER_G
 G_DECLARE_FINAL_TYPE (HdyHeaderGroup, hdy_header_group, HDY, HEADER_GROUP, GObject)
 
 typedef enum {
+  HDY_HEADER_GROUP_CHILD_TYPE_HEADER_BAR,
   HDY_HEADER_GROUP_CHILD_TYPE_GTK_HEADER_BAR,
 } HdyHeaderGroupChildType;
 
+HdyHeaderBar   *hdy_header_group_child_get_header_bar     (HdyHeaderGroupChild *self);
 GtkHeaderBar   *hdy_header_group_child_get_gtk_header_bar (HdyHeaderGroupChild *self);
 
 HdyHeaderGroupChildType hdy_header_group_child_get_child_type (HdyHeaderGroupChild *self);
 
 HdyHeaderGroup *hdy_header_group_new (void);
 
+void hdy_header_group_add_header_bar     (HdyHeaderGroup *self,
+                                          HdyHeaderBar   *header_bar);
 void hdy_header_group_add_gtk_header_bar (HdyHeaderGroup *self,
                                           GtkHeaderBar   *header_bar);
 
 GSList *hdy_header_group_get_children (HdyHeaderGroup *self);
 
+void hdy_header_group_remove_header_bar     (HdyHeaderGroup *self,
+                                             HdyHeaderBar   *header_bar);
 void hdy_header_group_remove_gtk_header_bar (HdyHeaderGroup *self,
                                              GtkHeaderBar   *header_bar);
 void hdy_header_group_remove_child          (HdyHeaderGroup      *self,
diff --git a/tests/test-header-group.c b/tests/test-header-group.c
index 2a902a9f..3fca0be9 100644
--- a/tests/test-header-group.c
+++ b/tests/test-header-group.c
@@ -36,12 +36,12 @@ static void
 test_hdy_header_group_add_remove (void)
 {
   g_autoptr (HdyHeaderGroup) hg = HDY_HEADER_GROUP (hdy_header_group_new ());
-  g_autoptr (GtkHeaderBar) bar1 = GTK_HEADER_BAR (g_object_ref_sink (gtk_header_bar_new ()));
+  g_autoptr (HdyHeaderBar) bar1 = HDY_HEADER_BAR (g_object_ref_sink (hdy_header_bar_new ()));
   g_autoptr (GtkHeaderBar) bar2 = GTK_HEADER_BAR (g_object_ref_sink (gtk_header_bar_new ()));
 
   g_assert_cmpint (g_slist_length (hdy_header_group_get_children (hg)), ==, 0);
 
-  hdy_header_group_add_gtk_header_bar (hg, bar1);
+  hdy_header_group_add_header_bar (hg, bar1);
   g_assert_cmpint (g_slist_length (hdy_header_group_get_children (hg)), ==, 1);
 
   hdy_header_group_add_gtk_header_bar (hg, bar2);
@@ -50,7 +50,7 @@ test_hdy_header_group_add_remove (void)
   hdy_header_group_remove_gtk_header_bar (hg, bar2);
   g_assert_cmpint (g_slist_length (hdy_header_group_get_children (hg)), ==, 1);
 
-  hdy_header_group_remove_gtk_header_bar (hg, bar1);
+  hdy_header_group_remove_header_bar (hg, bar1);
 
   g_assert_cmpint (g_slist_length (hdy_header_group_get_children (hg)), ==, 0);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]