[gnome-software/1392-unify-lozenge-styling] Introduce GsLayoutManager



commit 567763c76b96c79d427807f9cc3286411aa8c696
Author: Milan Crha <mcrha redhat com>
Date:   Tue May 3 15:27:21 2022 +0200

    Introduce GsLayoutManager
    
    Create a derivable class providing GsLayoutManager, which also
    provides singlas for 'measure' and 'allocate' virtual functions.

 src/gs-layout-manager.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/gs-layout-manager.h |  43 ++++++++++++++++
 src/meson.build         |   1 +
 3 files changed, 176 insertions(+)
---
diff --git a/src/gs-layout-manager.c b/src/gs-layout-manager.c
new file mode 100644
index 000000000..93b7baf04
--- /dev/null
+++ b/src/gs-layout-manager.c
@@ -0,0 +1,132 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2021 Endless OS Foundation LLC
+ *
+ * Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "config.h"
+
+#include "gs-layout-manager.h"
+
+G_DEFINE_TYPE (GsLayoutManager, gs_layout_manager, GTK_TYPE_LAYOUT_MANAGER)
+
+enum {
+       SIGNAL_MEASURE,
+       SIGNAL_ALLOCATE,
+       SIGNAL_LAST
+};
+
+static guint signals [SIGNAL_LAST] = { 0 };
+
+static void
+gs_layout_manager_measure (GtkLayoutManager *layout_manager,
+                          GtkWidget        *widget,
+                          GtkOrientation    orientation,
+                          gint              for_size,
+                          gint             *minimum,
+                          gint             *natural,
+                          gint             *minimum_baseline,
+                          gint             *natural_baseline)
+{
+       GtkWidget *child;
+       gint min = 0;
+       gint nat = 0;
+
+       for (child = gtk_widget_get_first_child (widget);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               gint child_min_baseline = -1;
+               gint child_nat_baseline = -1;
+               gint child_min = 0;
+               gint child_nat = 0;
+
+               if (!gtk_widget_should_layout (child))
+                       continue;
+
+               gtk_widget_measure (child, orientation,
+                                   for_size,
+                                   &child_min, &child_nat,
+                                   &child_min_baseline,
+                                   &child_nat_baseline);
+
+               min = MAX (min, child_min);
+               nat = MAX (nat, child_nat);
+
+               if (child_min_baseline > -1)
+                       *minimum_baseline = MAX (*minimum_baseline, child_min_baseline);
+               if (child_nat_baseline > -1)
+                   *natural_baseline = MAX (*natural_baseline, child_nat_baseline);
+       }
+
+       *minimum = min;
+       *natural = nat;
+
+       g_signal_emit (layout_manager, signals[SIGNAL_MEASURE], 0,
+               widget, orientation, for_size, minimum, natural, minimum_baseline, natural_baseline);
+}
+
+static void
+gs_layout_manager_allocate (GtkLayoutManager *layout_manager,
+                           GtkWidget        *widget,
+                           gint              width,
+                           gint              height,
+                           gint              baseline)
+{
+       GtkWidget *child;
+
+       for (child = gtk_widget_get_first_child (widget);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               if (child && gtk_widget_should_layout (child))
+                       gtk_widget_allocate (child, width, height, baseline, NULL);
+       }
+
+       g_signal_emit (layout_manager, signals[SIGNAL_ALLOCATE], 0,
+               widget, width, height, baseline);
+}
+
+static void
+gs_layout_manager_class_init (GsLayoutManagerClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GtkLayoutManagerClass *layout_manager_class = GTK_LAYOUT_MANAGER_CLASS (klass);
+
+       layout_manager_class->measure = gs_layout_manager_measure;
+       layout_manager_class->allocate = gs_layout_manager_allocate;
+
+       signals [SIGNAL_MEASURE] =
+               g_signal_new ("measure",
+                             G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (GsLayoutManagerClass, measure),
+                             NULL, NULL, g_cclosure_marshal_generic,
+                             G_TYPE_NONE, 7, GTK_TYPE_WIDGET, GTK_TYPE_ORIENTATION, G_TYPE_INT, 
G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
+
+       signals [SIGNAL_ALLOCATE] =
+               g_signal_new ("allocate",
+                             G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (GsLayoutManagerClass, allocate),
+                             NULL, NULL, g_cclosure_marshal_generic,
+                             G_TYPE_NONE, 4, GTK_TYPE_WIDGET, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT);
+}
+
+static void
+gs_layout_manager_init (GsLayoutManager *self)
+{
+}
+
+/**
+ * gs_layout_manager_new:
+ *
+ * Returns: (transfer full): a new #GsLayoutManager
+ *
+ * Since: 43
+ **/
+GtkLayoutManager *
+gs_layout_manager_new (void)
+{
+       return g_object_new (GS_TYPE_LAYOUT_MANAGER, NULL);
+}
diff --git a/src/gs-layout-manager.h b/src/gs-layout-manager.h
new file mode 100644
index 000000000..27301dbab
--- /dev/null
+++ b/src/gs-layout-manager.h
@@ -0,0 +1,43 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2021 Endless OS Foundation LLC
+ *
+ * Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#pragma once
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_LAYOUT_MANAGER (gs_layout_manager_get_type ())
+G_DECLARE_DERIVABLE_TYPE (GsLayoutManager, gs_layout_manager, GS, LAYOUT_MANAGER, GtkLayoutManager)
+
+struct _GsLayoutManagerClass {
+       GtkLayoutManagerClass parent_class;
+
+       void    (* measure)     (GtkLayoutManager *layout_manager,
+                                GtkWidget        *widget,
+                                GtkOrientation    orientation,
+                                gint              for_size,
+                                gint             *minimum,
+                                gint             *natural,
+                                gint             *minimum_baseline,
+                                gint             *natural_baseline);
+       void    (* allocate)    (GtkLayoutManager *layout_manager,
+                                GtkWidget        *widget,
+                                gint              width,
+                                gint              height,
+                                gint              baseline);
+};
+
+GtkLayoutManager *
+               gs_layout_manager_new           (void);
+
+G_END_DECLS
diff --git a/src/meson.build b/src/meson.build
index e1ef51afb..d945ba83c 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -50,6 +50,7 @@ gnome_software_sources = [
   'gs-info-window.c',
   'gs-installed-page.c',
   'gs-language.c',
+  'gs-layout-manager.c',
   'gs-license-tile.c',
   'gs-loading-page.c',
   'gs-main.c',


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