[libdazzle] menu-button: use DzlBox for sort helpers
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libdazzle] menu-button: use DzlBox for sort helpers
- Date: Sun, 9 Jul 2017 03:28:08 +0000 (UTC)
commit b525fb40d2074fdef7ad577abc85429afbbd5e76
Author: Christian Hergert <chergert redhat com>
Date: Sat Jul 8 20:27:46 2017 -0700
menu-button: use DzlBox for sort helpers
This allows us to avoid doing position tracking over and over.
src/menus/dzl-menu-button-section.c | 41 ++++---------------------------
src/menus/dzl-menu-button-section.ui | 2 +-
src/menus/dzl-menu-button.c | 45 +++++----------------------------
src/menus/dzl-menu-button.ui | 2 +-
4 files changed, 14 insertions(+), 76 deletions(-)
---
diff --git a/src/menus/dzl-menu-button-section.c b/src/menus/dzl-menu-button-section.c
index ab612a3..f5bd2ca 100644
--- a/src/menus/dzl-menu-button-section.c
+++ b/src/menus/dzl-menu-button-section.c
@@ -21,6 +21,7 @@
#include "bindings/dzl-signal-group.h"
#include "menus/dzl-menu-button-section.h"
#include "menus/dzl-menu-button-item.h"
+#include "widgets/dzl-box.h"
struct _DzlMenuButtonSection
{
@@ -31,7 +32,7 @@ struct _DzlMenuButtonSection
/* Template references */
GtkLabel *label;
- GtkBox *items_box;
+ DzlBox *items_box;
};
enum {
@@ -45,34 +46,6 @@ G_DEFINE_TYPE (DzlMenuButtonSection, dzl_menu_button_section, GTK_TYPE_BOX)
static GParamSpec *properties [N_PROPS];
-static GtkWidget *
-get_nth_child (DzlMenuButtonSection *self,
- guint nth)
-{
- GList *children = gtk_container_get_children (GTK_CONTAINER (self->items_box));
- GtkWidget *ret = g_list_nth_data (children, nth);
- g_list_free (children);
- return ret;
-}
-
-static void
-update_positions (DzlMenuButtonSection *self)
-{
- GList *children = gtk_container_get_children (GTK_CONTAINER (self->items_box));
- guint i = 0;
-
- for (const GList *iter = children; iter; iter = iter->next)
- {
- GtkWidget *widget = iter->data;
-
- gtk_container_child_set (GTK_CONTAINER (self->items_box), widget,
- "position", i++,
- NULL);
- }
-
- g_list_free (children);
-}
-
static void
dzl_menu_button_section_items_changed (DzlMenuButtonSection *self,
guint position,
@@ -85,12 +58,10 @@ dzl_menu_button_section_items_changed (DzlMenuButtonSection *self,
for (guint i = 0; i < removed; i++)
{
- GtkWidget *widget = get_nth_child (self, i);
- gtk_widget_destroy (widget);
+ GtkWidget *child = dzl_box_get_nth_child (self->items_box, i);
+ gtk_widget_destroy (child);
}
- update_positions (self);
-
for (guint i = position; i < position + added; i++)
{
DzlMenuButtonItem *item;
@@ -113,9 +84,7 @@ dzl_menu_button_section_items_changed (DzlMenuButtonSection *self,
"accel", accel,
"visible", TRUE,
NULL);
- gtk_container_add_with_properties (GTK_CONTAINER (self->items_box), GTK_WIDGET (item),
- "position", i,
- NULL);
+ dzl_box_insert (self->items_box, GTK_WIDGET (item), i);
}
}
diff --git a/src/menus/dzl-menu-button-section.ui b/src/menus/dzl-menu-button-section.ui
index 74b5577..9179263 100644
--- a/src/menus/dzl-menu-button-section.ui
+++ b/src/menus/dzl-menu-button-section.ui
@@ -11,7 +11,7 @@
</object>
</child>
<child>
- <object class="GtkBox" id="items_box">
+ <object class="DzlBox" id="items_box">
<property name="orientation">vertical</property>
<property name="visible">true</property>
</object>
diff --git a/src/menus/dzl-menu-button.c b/src/menus/dzl-menu-button.c
index 01c27b6..138e864 100644
--- a/src/menus/dzl-menu-button.c
+++ b/src/menus/dzl-menu-button.c
@@ -24,6 +24,7 @@
#include "menus/dzl-menu-button-section.h"
#include "menus/dzl-menu-button-item.h"
#include "util/dzl-gtk.h"
+#include "widgets/dzl-box.h"
typedef struct
{
@@ -34,7 +35,7 @@ typedef struct
GtkPopover *popover;
GtkImage *image;
GtkImage *pan_down_image;
- GtkBox *popover_box;
+ DzlBox *popover_box;
guint show_accels : 1;
guint show_icons : 1;
@@ -91,36 +92,6 @@ update_image_and_accels (DzlMenuButton *self)
}
}
-static GtkWidget *
-get_nth_child (DzlMenuButton *self,
- guint nth)
-{
- DzlMenuButtonPrivate *priv = dzl_menu_button_get_instance_private (self);
- GList *children = gtk_container_get_children (GTK_CONTAINER (priv->popover_box));
- GtkWidget *ret = g_list_nth_data (children, nth);
- g_list_free (children);
- return ret;
-}
-
-static void
-update_positions (DzlMenuButton *self)
-{
- DzlMenuButtonPrivate *priv = dzl_menu_button_get_instance_private (self);
- GList *children = gtk_container_get_children (GTK_CONTAINER (priv->popover_box));
- guint i = 0;
-
- for (const GList *iter = children; iter; iter = iter->next)
- {
- GtkWidget *widget = iter->data;
-
- gtk_container_child_set (GTK_CONTAINER (priv->popover_box), widget,
- "position", i++,
- NULL);
- }
-
- g_list_free (children);
-}
-
static void
dzl_menu_button_add_linked_model (DzlMenuButton *self,
guint position,
@@ -138,9 +109,7 @@ dzl_menu_button_add_linked_model (DzlMenuButton *self,
"model", model,
"visible", TRUE,
NULL);
- gtk_container_add_with_properties (GTK_CONTAINER (priv->popover_box), GTK_WIDGET (section),
- "position", position,
- NULL);
+ dzl_box_insert (priv->popover_box, GTK_WIDGET (section), position);
}
static void
@@ -150,17 +119,17 @@ dzl_menu_button_items_changed (DzlMenuButton *self,
guint added,
GMenuModel *menu)
{
+ DzlMenuButtonPrivate *priv = dzl_menu_button_get_instance_private (self);
+
g_assert (DZL_IS_MENU_BUTTON (self));
g_assert (G_IS_MENU_MODEL (menu));
for (guint i = 0; i < removed; i++)
{
- GtkWidget *widget = get_nth_child (self, i);
- gtk_widget_destroy (widget);
+ GtkWidget *child = dzl_box_get_nth_child (priv->popover_box, i);
+ gtk_widget_destroy (child);
}
- update_positions (self);
-
for (guint i = position; i < position + added; i++)
{
g_autofree gchar *label = NULL;
diff --git a/src/menus/dzl-menu-button.ui b/src/menus/dzl-menu-button.ui
index 5c6ba2d..3d02010 100644
--- a/src/menus/dzl-menu-button.ui
+++ b/src/menus/dzl-menu-button.ui
@@ -2,7 +2,7 @@
<interface>
<object class="GtkPopover" id="popover">
<child>
- <object class="GtkBox" id="popover_box">
+ <object class="DzlBox" id="popover_box">
<property name="orientation">vertical</property>
<property name="visible">true</property>
</object>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]