[libhandy] header-group: Replace 'focus' by 'decorate-all'



commit c07c0a62c463d7177f53e5133a560ae485a63e47
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed Jun 24 09:34:56 2020 +0200

    header-group: Replace 'focus' by 'decorate-all'
    
    This better works with our main use case: header bars in leaflets.
    
    Fixes https://gitlab.gnome.org/GNOME/libhandy/-/issues/150

 debian/libhandy-1-0.symbols |   4 +-
 src/hdy-header-group.c      | 153 ++++++++++++++++++++++----------------------
 src/hdy-header-group.h      |  12 ++--
 tests/test-header-group.c   |  35 +++++-----
 4 files changed, 101 insertions(+), 103 deletions(-)
---
diff --git a/debian/libhandy-1-0.symbols b/debian/libhandy-1-0.symbols
index 707fdb20..9be8eb8f 100644
--- a/debian/libhandy-1-0.symbols
+++ b/debian/libhandy-1-0.symbols
@@ -165,12 +165,12 @@ 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_header_bar@LIBHANDY_1_0 0.0.3
- hdy_header_group_get_focus@LIBHANDY_1_0 0.0.3
+ hdy_header_group_get_decorate_all@LIBHANDY_1_0 0.83.0
  hdy_header_group_get_header_bars@LIBHANDY_1_0 0.0.4~
  hdy_header_group_get_type@LIBHANDY_1_0 0.0.3
  hdy_header_group_new@LIBHANDY_1_0 0.0.3
  hdy_header_group_remove_header_bar@LIBHANDY_1_0 0.0.4~
- hdy_header_group_set_focus@LIBHANDY_1_0 0.0.3
+ 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
  hdy_keypad_button_get_digit@LIBHANDY_1_0 0.0.12
diff --git a/src/hdy-header-group.c b/src/hdy-header-group.c
index 68dd9851..79b5cd5b 100644
--- a/src/hdy-header-group.c
+++ b/src/hdy-header-group.c
@@ -12,7 +12,7 @@
 typedef struct
 {
   GSList *header_bars;
-  GtkHeaderBar *focus;
+  gboolean decorate_all;
 
 } HdyHeaderGroupPrivate;
 
@@ -36,7 +36,7 @@ G_DEFINE_TYPE_WITH_CODE (HdyHeaderGroup, hdy_header_group, G_TYPE_OBJECT,
 
 enum {
   PROP_0,
-  PROP_FOCUS,
+  PROP_DECORATE_ALL,
   N_PROPS
 };
 
@@ -84,13 +84,9 @@ update_decoration_layouts (HdyHeaderGroup *self)
   if (layout == NULL)
     layout = g_strdup (":");
 
-  if (priv->focus != NULL) {
+  if (priv->decorate_all) {
     for (; header_bars != NULL; header_bars = header_bars->next)
-      if (header_bars->data == priv->focus &&
-          gtk_widget_get_mapped (header_bars->data))
-        gtk_header_bar_set_decoration_layout (header_bars->data, layout);
-      else
-        gtk_header_bar_set_decoration_layout (header_bars->data, ":");
+      gtk_header_bar_set_decoration_layout (header_bars->data, layout);
 
     return;
   }
@@ -156,8 +152,8 @@ hdy_header_group_new (void)
  * widgets will be edited depending on their position in the composite header
  * bar, the start widget displaying only the start of the user's decoration
  * layout and the end widget displaying only its end while widgets in the middle
- * won't display anything. A header bar can be set as having the focus to
- * display all the decorations. See gtk_header_bar_set_decoration_layout().
+ * won't display anything.
+ * See gtk_header_bar_set_decoration_layout().
  *
  * When the widget is destroyed or no longer referenced elsewhere, it will
  * be removed from the header group.
@@ -205,9 +201,6 @@ hdy_header_group_remove_header_bar (HdyHeaderGroup *self,
   priv = hdy_header_group_get_instance_private (self);
   priv->header_bars = g_slist_remove (priv->header_bars, header_bar);
 
-  if (priv->focus == header_bar)
-    hdy_header_group_set_focus (self, NULL);
-
   g_signal_handlers_disconnect_by_data (header_bar, self);
 
   g_object_unref (self);
@@ -233,54 +226,6 @@ hdy_header_group_get_header_bars (HdyHeaderGroup *self)
   return priv->header_bars;
 }
 
-
-/**
- * hdy_header_group_set_focus:
- * @self: a #HdyHeaderGroup
- * @header_bar: (nullable): a #GtkHeaderBar of @self, or %NULL
- *
- * Sets the the currently focused header bar. If @header_bar is %NULL, the
- * decoration will be spread as if the header bars of the group were only one,
- * otherwise @header_bar will be the only one to receive the decoration.
- */
-void
-hdy_header_group_set_focus (HdyHeaderGroup *self,
-                            GtkHeaderBar   *header_bar)
-{
-  HdyHeaderGroupPrivate *priv;
-
-  g_return_if_fail (HDY_IS_HEADER_GROUP (self));
-  g_return_if_fail (header_bar == NULL || GTK_IS_HEADER_BAR (header_bar));
-  g_return_if_fail (header_bar == NULL || contains (self, header_bar));
-
-  priv = hdy_header_group_get_instance_private (self);
-
-  priv->focus = header_bar;
-
-  update_decoration_layouts (self);
-
-  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_FOCUS]);
-}
-
-/**
- * hdy_header_group_get_focus:
- * @self: a #HdyHeaderGroup
- *
- * Returns: (nullable) (transfer none): The currently focused header bar
- */
-GtkHeaderBar *
-hdy_header_group_get_focus (HdyHeaderGroup *self)
-{
-  HdyHeaderGroupPrivate *priv;
-
-  g_return_val_if_fail (HDY_IS_HEADER_GROUP (self), FALSE);
-
-  priv = hdy_header_group_get_instance_private (self);
-
-  return priv->focus;
-}
-
-
 typedef struct {
   gchar *name;
   gint line;
@@ -310,7 +255,6 @@ hdy_header_group_dispose (GObject *object)
 
   g_slist_free_full (priv->header_bars, (GDestroyNotify) g_object_unref);
   priv->header_bars = NULL;
-  priv->focus = NULL;
 
   G_OBJECT_CLASS (hdy_header_group_parent_class)->dispose (object);
 }
@@ -324,8 +268,8 @@ hdy_header_group_get_property (GObject    *object,
   HdyHeaderGroup *self = HDY_HEADER_GROUP (object);
 
   switch (prop_id) {
-  case PROP_FOCUS:
-    g_value_set_object (value, hdy_header_group_get_focus (self));
+  case PROP_DECORATE_ALL:
+    g_value_set_boolean (value, hdy_header_group_get_decorate_all (self));
     break;
   default:
     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -341,8 +285,8 @@ hdy_header_group_set_property (GObject      *object,
   HdyHeaderGroup *self = HDY_HEADER_GROUP (object);
 
   switch (prop_id) {
-  case PROP_FOCUS:
-    hdy_header_group_set_focus (self, g_value_get_object (value));
+  case PROP_DECORATE_ALL:
+    hdy_header_group_set_decorate_all (self, g_value_get_boolean (value));
     break;
   default:
     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -579,18 +523,21 @@ hdy_header_group_class_init (HdyHeaderGroupClass *klass)
   object_class->set_property = hdy_header_group_set_property;
 
   /**
-   * HdyHeaderGroup:focus:
+   * HdyHeaderGroup:decorate-all:
    *
-   * The the currently focused header bar. If %NULL, the decoration will be
-   * spread as if the header bars of the group were only one, otherwise the
-   * focused header bar will be the only one to receive the decoration.
+   * Whether the elements of the group should all receive the full decoration.
+   * This is useful in conjunction with #HdyLeaflet:folded when the leaflet
+   * contains the header bars of the group, as you want them all to display the
+   * complete decoration when the leaflet is folded.
+   *
+   * Since: 1.0
    */
-  props[PROP_FOCUS] =
-    g_param_spec_object ("focus",
-                         _("Focus"),
-                         _("The header bar that should have the focus"),
-                         GTK_TYPE_HEADER_BAR,
-                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+  props[PROP_DECORATE_ALL] =
+    g_param_spec_boolean ("decorate-all",
+                          _("Decorate all"),
+                          _("Whether the elements of the group should all receive the full decoration"),
+                          FALSE,
+                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
   g_object_class_install_properties (object_class, N_PROPS, props);
 }
@@ -609,3 +556,57 @@ hdy_header_group_buildable_init (GtkBuildableIface *iface)
   iface->custom_tag_start = hdy_header_group_buildable_custom_tag_start;
   iface->custom_finished = hdy_header_group_buildable_custom_finished;
 }
+
+/**
+ * hdy_header_group_set_decorate_all:
+ * @self: a #HdyHeaderGroup
+ * @decorate_all: whether the elements of the group should all receive the full decoration
+ *
+ * Sets whether the elements of the group should all receive the full decoration.
+ *
+ * Since: 1.0
+ */
+void
+hdy_header_group_set_decorate_all (HdyHeaderGroup *self,
+                                   gboolean        decorate_all)
+{
+  HdyHeaderGroupPrivate *priv;
+
+  g_return_if_fail (HDY_IS_HEADER_GROUP (self));
+
+  priv = hdy_header_group_get_instance_private (self);
+
+  decorate_all = !!decorate_all;
+
+  if (priv->decorate_all == decorate_all)
+    return;
+
+  priv->decorate_all = decorate_all;
+
+  update_decoration_layouts (self);
+
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_DECORATE_ALL]);
+}
+
+/**
+ * hdy_header_group_get_decorate_all:
+ * @self: a #HdyHeaderGroup
+ *
+ * Gets whether the elements of the group should all receive the full decoration.
+ *
+ * Returns: %TRUE if the elements of the group should all receive the full
+ *   decoration, %FALSE otherwise.
+ *
+ * Since: 1.0
+ */
+gboolean
+hdy_header_group_get_decorate_all (HdyHeaderGroup *self)
+{
+  HdyHeaderGroupPrivate *priv;
+
+  g_return_val_if_fail (HDY_IS_HEADER_GROUP (self), FALSE);
+
+  priv = hdy_header_group_get_instance_private (self);
+
+  return priv->decorate_all;
+}
diff --git a/src/hdy-header-group.h b/src/hdy-header-group.h
index 373b32a0..442e1a8c 100644
--- a/src/hdy-header-group.h
+++ b/src/hdy-header-group.h
@@ -32,12 +32,12 @@ HdyHeaderGroup *hdy_header_group_new (void);
 void hdy_header_group_add_header_bar (HdyHeaderGroup *self,
                                       GtkHeaderBar   *header_bar);
 
-GtkHeaderBar *hdy_header_group_get_focus         (HdyHeaderGroup *self);
-void          hdy_header_group_set_focus         (HdyHeaderGroup *self,
-                                                  GtkHeaderBar   *header_bar);
-GSList *      hdy_header_group_get_header_bars   (HdyHeaderGroup *self);
-void          hdy_header_group_remove_header_bar (HdyHeaderGroup *self,
-                                                  GtkHeaderBar   *header_bar);
+GSList  *hdy_header_group_get_header_bars   (HdyHeaderGroup *self);
+void     hdy_header_group_remove_header_bar (HdyHeaderGroup *self,
+                                             GtkHeaderBar   *header_bar);
 
+gboolean hdy_header_group_get_decorate_all (HdyHeaderGroup *self);
+void     hdy_header_group_set_decorate_all (HdyHeaderGroup *self,
+                                            gboolean        decorate_all);
 
 G_END_DECLS
diff --git a/tests/test-header-group.c b/tests/test-header-group.c
index 07b7bb0a..ef7edba7 100644
--- a/tests/test-header-group.c
+++ b/tests/test-header-group.c
@@ -9,27 +9,26 @@
 
 
 static void
-test_hdy_header_group_focus (void)
+test_hdy_header_group_decorate_all (void)
 {
-  HdyHeaderGroup *hg;
-  GtkHeaderBar *bar1, *bar2 = NULL;
+  g_autoptr (HdyHeaderGroup) hg = HDY_HEADER_GROUP (hdy_header_group_new ());
+  gboolean decorate_all = FALSE;
 
-  hg = HDY_HEADER_GROUP (hdy_header_group_new ());
+  g_assert_false (hdy_header_group_get_decorate_all (hg));
+  g_object_get (hg, "decorate-all", &decorate_all, NULL);
+  g_assert_false (decorate_all);
 
-  bar1 = hdy_header_group_get_focus (hg);
-  g_assert_null (bar1);
-  g_object_get (hg, "focus", &bar2, NULL);
-  g_assert (bar1 == bar2);
+  hdy_header_group_set_decorate_all (hg, TRUE);
 
-  bar1 = GTK_HEADER_BAR (gtk_header_bar_new ());
-  hdy_header_group_add_header_bar (hg, GTK_HEADER_BAR (bar1));
-  hdy_header_group_set_focus (hg, GTK_HEADER_BAR (bar1));
-  bar2 = hdy_header_group_get_focus (hg);
-  g_assert (bar1 == bar2);
-  g_object_get (hg, "focus", &bar2, NULL);
-  g_assert (bar1 == bar2);
+  g_assert_true (hdy_header_group_get_decorate_all (hg));
+  g_object_get (hg, "decorate-all", &decorate_all, NULL);
+  g_assert_true (decorate_all);
 
-  g_object_unref (hg);
+  g_object_set (hg, "decorate-all", FALSE, NULL);
+
+  g_assert_false (hdy_header_group_get_decorate_all (hg));
+  g_object_get (hg, "decorate-all", &decorate_all, NULL);
+  g_assert_false (decorate_all);
 }
 
 
@@ -51,11 +50,9 @@ test_hdy_header_group_add_remove (void)
 
   hdy_header_group_add_header_bar (hg, GTK_HEADER_BAR (bar2));
   g_assert_cmpint (g_slist_length (hdy_header_group_get_header_bars (hg)), ==, 2);
-  hdy_header_group_set_focus (hg, GTK_HEADER_BAR (bar2));
 
   hdy_header_group_remove_header_bar (hg, GTK_HEADER_BAR (bar2));
   g_assert_cmpint (g_slist_length (hdy_header_group_get_header_bars (hg)), ==, 1);
-  g_assert_null (hdy_header_group_get_focus (hg));
 
   hdy_header_group_remove_header_bar (hg, GTK_HEADER_BAR (bar1));
   g_assert_cmpint (g_slist_length (hdy_header_group_get_header_bars (hg)), ==, 0);
@@ -71,7 +68,7 @@ main (gint argc,
   gtk_test_init (&argc, &argv, NULL);
   hdy_init ();
 
-  g_test_add_func("/Handy/HeaderGroup/focus", test_hdy_header_group_focus);
+  g_test_add_func("/Handy/HeaderGroup/decorate_all", test_hdy_header_group_decorate_all);
   g_test_add_func("/Handy/HeaderGroup/add_remove", test_hdy_header_group_add_remove);
   return g_test_run();
 }


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