[gnome-builder] greeter: add helper truncation model
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] greeter: add helper truncation model
- Date: Tue, 22 Jan 2019 06:49:13 +0000 (UTC)
commit bd5cd1622190be7cdcfd289e6105fd2824a16886
Author: Christian Hergert <chergert redhat com>
Date: Mon Jan 21 22:36:43 2019 -0800
greeter: add helper truncation model
This model is convenient for adding view-more rows to GtkListBox. It can
filter to a fixed number of rows, and then expand them when the expanded
mode is set.
src/libide/greeter/ide-truncate-model.c | 333 ++++++++++++++++++++++++++++++++
src/libide/greeter/ide-truncate-model.h | 40 ++++
src/libide/greeter/meson.build | 2 +
3 files changed, 375 insertions(+)
---
diff --git a/src/libide/greeter/ide-truncate-model.c b/src/libide/greeter/ide-truncate-model.c
new file mode 100644
index 000000000..5d6c0f18a
--- /dev/null
+++ b/src/libide/greeter/ide-truncate-model.c
@@ -0,0 +1,333 @@
+/* ide-truncate-model.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-truncate-model"
+
+#include "config.h"
+
+#include "ide-truncate-model.h"
+
+#define DEFAULT_MAX_ITEMS 4
+
+struct _IdeTruncateModel
+{
+ GObject parent_instance;
+ GListModel *child_model;
+ guint max_items;
+ guint prev_n_items;
+ guint expanded : 1;
+};
+
+static gpointer
+ide_truncate_model_get_item (GListModel *model,
+ guint position)
+{
+ return g_list_model_get_item (IDE_TRUNCATE_MODEL (model)->child_model, position);
+}
+
+static guint
+ide_truncate_model_get_n_items (GListModel *model)
+{
+ IdeTruncateModel *self = (IdeTruncateModel *)model;
+ guint n_items = g_list_model_get_n_items (IDE_TRUNCATE_MODEL (model)->child_model);
+ return self->expanded ? n_items : MIN (n_items, self->max_items);
+}
+
+static GType
+ide_truncate_model_get_item_type (GListModel *model)
+{
+ return g_list_model_get_item_type (IDE_TRUNCATE_MODEL (model)->child_model);
+}
+
+static void
+list_model_iface_init (GListModelInterface *iface)
+{
+ iface->get_item = ide_truncate_model_get_item;
+ iface->get_n_items = ide_truncate_model_get_n_items;
+ iface->get_item_type = ide_truncate_model_get_item_type;
+}
+
+G_DEFINE_TYPE_WITH_CODE (IdeTruncateModel, ide_truncate_model, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
+
+enum {
+ PROP_0,
+ PROP_CHILD_MODEL,
+ PROP_EXPANDED,
+ PROP_MAX_ITEMS,
+ N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+/**
+ * ide_truncate_model_new:
+ * @child_model: a #GListModel
+ *
+ * Create a new #IdeTruncateModel that wraps @child_model. Only
+ * #IdeTruncateModel:max-items will be displayed until
+ * #IdeTrunicateModel:expanded is set.
+ *
+ * Returns: (transfer full): a newly created #IdeTruncateModel
+ */
+IdeTruncateModel *
+ide_truncate_model_new (GListModel *child_model)
+{
+ g_return_val_if_fail (G_IS_LIST_MODEL (child_model), NULL);
+
+ return g_object_new (IDE_TYPE_TRUNCATE_MODEL,
+ "child-model", child_model,
+ NULL);
+}
+
+static void
+ide_truncate_model_items_changed_cb (IdeTruncateModel *self,
+ guint position,
+ guint removed,
+ guint added,
+ GListModel *model)
+{
+ guint n_items;
+
+ g_assert (IDE_IS_TRUNCATE_MODEL (self));
+ g_assert (G_IS_LIST_MODEL (model));
+
+ n_items = g_list_model_get_n_items (model);
+
+ if (self->expanded)
+ {
+ g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
+ }
+ else
+ {
+ if (position < (self->max_items - 1))
+ {
+ g_list_model_items_changed (G_LIST_MODEL (self),
+ 0,
+ self->prev_n_items,
+ MIN (n_items, self->max_items));
+ }
+ }
+
+ self->prev_n_items = n_items;
+}
+
+static void
+ide_truncate_model_finalize (GObject *object)
+{
+ IdeTruncateModel *self = (IdeTruncateModel *)object;
+
+ g_clear_object (&self->child_model);
+
+ G_OBJECT_CLASS (ide_truncate_model_parent_class)->finalize (object);
+}
+
+static void
+ide_truncate_model_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ IdeTruncateModel *self = IDE_TRUNCATE_MODEL (object);
+
+ switch (prop_id)
+ {
+ case PROP_CHILD_MODEL:
+ g_value_set_object (value, ide_truncate_model_get_child_model (self));
+ break;
+
+ case PROP_MAX_ITEMS:
+ g_value_set_uint (value, ide_truncate_model_get_max_items (self));
+ break;
+
+ case PROP_EXPANDED:
+ g_value_set_boolean (value, ide_truncate_model_get_expanded (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_truncate_model_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ IdeTruncateModel *self = IDE_TRUNCATE_MODEL (object);
+
+ switch (prop_id)
+ {
+ case PROP_CHILD_MODEL:
+ self->child_model = g_value_dup_object (value);
+ self->prev_n_items = g_list_model_get_n_items (self->child_model);
+ g_signal_connect_object (self->child_model,
+ "items-changed",
+ G_CALLBACK (ide_truncate_model_items_changed_cb),
+ self,
+ G_CONNECT_SWAPPED);
+ break;
+
+ case PROP_MAX_ITEMS:
+ ide_truncate_model_set_max_items (self, g_value_get_uint (value));
+ break;
+
+ case PROP_EXPANDED:
+ ide_truncate_model_set_expanded (self, g_value_get_boolean (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_truncate_model_class_init (IdeTruncateModelClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = ide_truncate_model_finalize;
+ object_class->get_property = ide_truncate_model_get_property;
+ object_class->set_property = ide_truncate_model_set_property;
+
+ /**
+ * IdeTruncateModel:child-model:
+ *
+ * The "child-model" property is the model to be trunicated.
+ */
+ properties [PROP_CHILD_MODEL] =
+ g_param_spec_object ("child-model",
+ "Child Model",
+ "Child GListModel",
+ G_TYPE_LIST_MODEL,
+ (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+ properties [PROP_MAX_ITEMS] =
+ g_param_spec_uint ("max-items",
+ "Max Items",
+ "Max items to display when not expanded",
+ 0, G_MAXUINT, DEFAULT_MAX_ITEMS,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ properties [PROP_EXPANDED] =
+ g_param_spec_boolean ("expanded",
+ "Expanded",
+ "If all the items should be displayed",
+ FALSE,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_truncate_model_init (IdeTruncateModel *self)
+{
+ self->max_items = DEFAULT_MAX_ITEMS;
+}
+
+gboolean
+ide_truncate_model_get_expanded (IdeTruncateModel *self)
+{
+ g_return_val_if_fail (IDE_IS_TRUNCATE_MODEL (self), FALSE);
+
+ return self->expanded;
+}
+
+void
+ide_truncate_model_set_expanded (IdeTruncateModel *self,
+ gboolean expanded)
+{
+ g_return_if_fail (IDE_IS_TRUNCATE_MODEL (self));
+
+ expanded = !!expanded;
+
+ if (expanded != self->expanded)
+ {
+ guint n_items = g_list_model_get_n_items (self->child_model);
+ guint old_n_items = self->expanded ? n_items : MIN (n_items, self->max_items);
+ guint new_n_items = expanded ? n_items : MIN (n_items, self->max_items);
+
+ self->expanded = expanded;
+
+ if (new_n_items > old_n_items)
+ g_list_model_items_changed (G_LIST_MODEL (self),
+ old_n_items,
+ 0,
+ new_n_items - old_n_items);
+ else
+ g_list_model_items_changed (G_LIST_MODEL (self),
+ 0, old_n_items, new_n_items);
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_EXPANDED]);
+ }
+}
+
+guint
+ide_truncate_model_get_max_items (IdeTruncateModel *self)
+{
+ g_return_val_if_fail (IDE_IS_TRUNCATE_MODEL (self), 0);
+
+ return self->max_items;
+}
+
+void
+ide_truncate_model_set_max_items (IdeTruncateModel *self,
+ guint max_items)
+{
+ g_return_if_fail (IDE_IS_TRUNCATE_MODEL (self));
+
+ if (max_items == 0)
+ max_items = DEFAULT_MAX_ITEMS;
+
+ if (max_items != self->max_items)
+ {
+ guint old_max_items = self->max_items;
+
+ self->max_items = max_items;
+
+ if (!self->expanded)
+ {
+ guint n_items = g_list_model_get_n_items (self->child_model);
+ guint old_n_items = MIN (old_max_items, n_items);
+ guint new_n_items = MIN (max_items, n_items);
+
+ if (old_n_items != new_n_items)
+ g_list_model_items_changed (G_LIST_MODEL (self), 0, old_n_items, new_n_items);
+ }
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_MAX_ITEMS]);
+ }
+}
+
+/**
+ * ide_truncate_model_get_child_model:
+ *
+ * Gets the #IdeTruncateModel:child-model property.
+ *
+ * Returns: (transfer none): a #GListModel
+ */
+GListModel *
+ide_truncate_model_get_child_model (IdeTruncateModel *self)
+{
+ g_return_val_if_fail (IDE_IS_TRUNCATE_MODEL (self), NULL);
+
+ return self->child_model;
+}
diff --git a/src/libide/greeter/ide-truncate-model.h b/src/libide/greeter/ide-truncate-model.h
new file mode 100644
index 000000000..dcd8af27c
--- /dev/null
+++ b/src/libide/greeter/ide-truncate-model.h
@@ -0,0 +1,40 @@
+/* ide-truncate-model.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TRUNCATE_MODEL (ide_truncate_model_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeTruncateModel, ide_truncate_model, IDE, TRUNCATE_MODEL, GObject)
+
+IdeTruncateModel *ide_truncate_model_new (GListModel *child_model);
+GListModel *ide_truncate_model_get_child_model (IdeTruncateModel *self);
+guint ide_truncate_model_get_max_items (IdeTruncateModel *self);
+void ide_truncate_model_set_max_items (IdeTruncateModel *self,
+ guint max_items);
+gboolean ide_truncate_model_get_expanded (IdeTruncateModel *self);
+void ide_truncate_model_set_expanded (IdeTruncateModel *self,
+ gboolean expanded);
+
+G_END_DECLS
diff --git a/src/libide/greeter/meson.build b/src/libide/greeter/meson.build
index 033137758..43dd2df5d 100644
--- a/src/libide/greeter/meson.build
+++ b/src/libide/greeter/meson.build
@@ -17,6 +17,7 @@ libide_greeter_public_headers = [
libide_greeter_private_headers = [
'ide-greeter-private.h',
+ 'ide-truncate-model.h',
]
install_headers(libide_greeter_public_headers, subdir: libide_greeter_header_subdir)
@@ -36,6 +37,7 @@ libide_greeter_private_sources = [
'ide-greeter-workspace-actions.c',
'ide-greeter-workspace-shortcuts.c',
'ide-greeter-buttons-section.c',
+ 'ide-truncate-model.c',
]
#
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]