[gnome-software/1392-unify-lozenge-styling: 76/85] Introduce GsLozengeLayout




commit ceb18e3e17832f475ae7d761b760f520ca472cdc
Author: Milan Crha <mcrha redhat com>
Date:   Tue May 3 16:56:28 2022 +0200

    Introduce GsLozengeLayout
    
    To be used in the following commit, to layout lozenge widget.

 src/gs-lozenge-layout.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/gs-lozenge-layout.h |  28 ++++++++
 src/meson.build         |   1 +
 3 files changed, 208 insertions(+)
---
diff --git a/src/gs-lozenge-layout.c b/src/gs-lozenge-layout.c
new file mode 100644
index 000000000..465498801
--- /dev/null
+++ b/src/gs-lozenge-layout.c
@@ -0,0 +1,179 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2022 Red Hat (www.redhat.com)
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "config.h"
+
+#include "gs-lozenge-layout.h"
+
+struct _GsLozengeLayout
+{
+       GsLayoutManager          parent_instance;
+
+       gboolean                 circular;
+};
+
+G_DEFINE_TYPE (GsLozengeLayout, gs_lozenge_layout, GS_TYPE_LAYOUT_MANAGER)
+
+typedef enum {
+       PROP_CIRCULAR = 1,
+} GsLozengeLayoutProperty;
+
+static GParamSpec *obj_props[PROP_CIRCULAR + 1] = { NULL, };
+
+static void
+gs_lozenge_layout_measure (GtkLayoutManager *layout_manager,
+                          GtkWidget        *widget,
+                          GtkOrientation    orientation,
+                          gint              for_size,
+                          gint             *minimum,
+                          gint             *natural,
+                          gint             *minimum_baseline,
+                          gint             *natural_baseline)
+{
+       GsLozengeLayout *self = GS_LOZENGE_LAYOUT (layout_manager);
+
+       GS_LAYOUT_MANAGER_CLASS (gs_lozenge_layout_parent_class)->measure (layout_manager,
+               widget, orientation, for_size, minimum, natural, minimum_baseline, natural_baseline);
+
+       if (self->circular) {
+               *minimum = MAX (for_size, *minimum);
+               *natural = *minimum;
+               *natural_baseline = *minimum_baseline;
+       }
+
+       if (*natural_baseline > *natural)
+               *natural_baseline = *natural;
+       if (*minimum_baseline > *minimum)
+               *minimum_baseline = *minimum;
+}
+
+static void
+gs_lozenge_layout_get_property (GObject    *object,
+                               guint       prop_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+       GsLozengeLayout *self = GS_LOZENGE_LAYOUT (object);
+
+       switch ((GsLozengeLayoutProperty) prop_id) {
+       case PROP_CIRCULAR:
+               g_value_set_boolean (value, gs_lozenge_layout_get_circular (self));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+gs_lozenge_layout_set_property (GObject      *object,
+                               guint         prop_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+       GsLozengeLayout *self = GS_LOZENGE_LAYOUT (object);
+
+       switch ((GsLozengeLayoutProperty) prop_id) {
+       case PROP_CIRCULAR:
+               gs_lozenge_layout_set_circular (self, g_value_get_boolean (value));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+gs_lozenge_layout_class_init (GsLozengeLayoutClass *klass)
+{
+       GsLayoutManagerClass *layour_manager_class = GS_LAYOUT_MANAGER_CLASS (klass);
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       layour_manager_class->measure = gs_lozenge_layout_measure;
+
+       object_class->get_property = gs_lozenge_layout_get_property;
+       object_class->set_property = gs_lozenge_layout_set_property;
+
+       /**
+        * GsLozengeLayout:circular:
+        *
+        * Whether the lozenge should be circular, thus its size square.
+        *
+        * The default is %FALSE.
+        *
+        * Since: 43
+        */
+       obj_props[PROP_CIRCULAR] =
+               g_param_spec_boolean ("circular", NULL, NULL,
+                                     FALSE,
+                                     G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
+       g_object_class_install_properties (object_class, G_N_ELEMENTS (obj_props), obj_props);
+}
+
+static void
+gs_lozenge_layout_init (GsLozengeLayout *self)
+{
+}
+
+/**
+ * gs_lozenge_layout_new:
+ *
+ * Create a new #GsLozengeLayout.
+ *
+ * Returns: (transfer full): a new #GsLozengeLayout
+ *
+ * Since: 43
+ **/
+GsLozengeLayout *
+gs_lozenge_layout_new (void)
+{
+       return g_object_new (GS_TYPE_LOZENGE_LAYOUT, NULL);
+}
+
+/**
+ * gs_lozenge_layout_get_circular:
+ * @self: a #GsLozengeLayout
+ *
+ * Get the value of #GsLozengeLayout:circular.
+ *
+ * Returns: %TRUE if the lozenge has to be circular, %FALSE otherwise
+ * Since: 43
+ */
+gboolean
+gs_lozenge_layout_get_circular (GsLozengeLayout *self)
+{
+       g_return_val_if_fail (GS_IS_LOZENGE_LAYOUT (self), FALSE);
+
+       return self->circular;
+}
+
+/**
+ * gs_lozenge_layout_set_circular:
+ * @self: a #GsLozengeLayout
+ * @value: %TRUE if the lozenge should be circular, %FALSE otherwise
+ *
+ * Set the value of #GsLozengeLayout:circular to @value.
+ *
+ * Since: 43
+ */
+void
+gs_lozenge_layout_set_circular (GsLozengeLayout *self,
+                               gboolean value)
+{
+       g_return_if_fail (GS_IS_LOZENGE_LAYOUT (self));
+
+       if ((!self->circular) == (!value))
+               return;
+
+       self->circular = value;
+
+       gtk_layout_manager_layout_changed (GTK_LAYOUT_MANAGER (self));
+
+       g_object_notify_by_pspec (G_OBJECT (self), obj_props[PROP_CIRCULAR]);
+}
diff --git a/src/gs-lozenge-layout.h b/src/gs-lozenge-layout.h
new file mode 100644
index 000000000..e731819bd
--- /dev/null
+++ b/src/gs-lozenge-layout.h
@@ -0,0 +1,28 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2022 Red Hat (www.redhat.com)
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#pragma once
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+#include "gs-layout-manager.h"
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_LOZENGE_LAYOUT (gs_lozenge_layout_get_type ())
+G_DECLARE_FINAL_TYPE (GsLozengeLayout, gs_lozenge_layout, GS, LOZENGE_LAYOUT, GsLayoutManager)
+
+GsLozengeLayout *
+               gs_lozenge_layout_new           (void);
+gboolean       gs_lozenge_layout_get_circular  (GsLozengeLayout *self);
+void           gs_lozenge_layout_set_circular  (GsLozengeLayout *self,
+                                                gboolean value);
+
+G_END_DECLS
diff --git a/src/meson.build b/src/meson.build
index 4b40abdb8..e4e03c004 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -53,6 +53,7 @@ gnome_software_sources = [
   'gs-layout-manager.c',
   'gs-license-tile.c',
   'gs-loading-page.c',
+  'gs-lozenge-layout.c',
   'gs-main.c',
   'gs-metered-data-dialog.c',
   'gs-moderate-page.c',


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