[gnome-builder/wip/gtk4-port: 1476/1774] libide/gtk: add helper to hide widget when model is empty
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/gtk4-port: 1476/1774] libide/gtk: add helper to hide widget when model is empty
- Date: Mon, 11 Jul 2022 22:31:47 +0000 (UTC)
commit ea04783b2d099dacf76e4d7bf30ba91ff05a2055
Author: Christian Hergert <chergert redhat com>
Date: Sat Jun 11 00:50:25 2022 -0700
libide/gtk: add helper to hide widget when model is empty
src/libide/gtk/ide-gtk.c | 29 +++++++++++++++++++++++++++++
src/libide/gtk/ide-gtk.h | 3 +++
2 files changed, 32 insertions(+)
---
diff --git a/src/libide/gtk/ide-gtk.c b/src/libide/gtk/ide-gtk.c
index 8649ef1d5..56e2f295f 100644
--- a/src/libide/gtk/ide-gtk.c
+++ b/src/libide/gtk/ide-gtk.c
@@ -532,3 +532,32 @@ ide_gtk_list_view_get_selected_row (GtkListView *view,
*position = gtk_bitset_get_minimum (bitset);
return TRUE;
}
+
+static void
+on_items_changed_cb (GListModel *model,
+ guint position,
+ guint removed,
+ guint added,
+ GtkWidget *widget)
+{
+ gboolean was_visible = gtk_widget_get_visible (widget);
+ gboolean is_visible = added > 0 || g_list_model_get_n_items (model) > 0;
+
+ if (was_visible != is_visible)
+ gtk_widget_set_visible (widget, is_visible);
+}
+
+void
+ide_gtk_widget_hide_when_empty (GtkWidget *widget,
+ GListModel *model)
+{
+ g_return_if_fail (GTK_IS_WIDGET (widget));
+ g_return_if_fail (G_IS_LIST_MODEL (model));
+
+ gtk_widget_set_visible (widget, g_list_model_get_n_items (model) > 0);
+ g_signal_connect_object (model,
+ "items-changed",
+ G_CALLBACK (on_items_changed_cb),
+ widget,
+ 0);
+}
diff --git a/src/libide/gtk/ide-gtk.h b/src/libide/gtk/ide-gtk.h
index f06a5b0f2..6aa5bfae0 100644
--- a/src/libide/gtk/ide-gtk.h
+++ b/src/libide/gtk/ide-gtk.h
@@ -66,5 +66,8 @@ void ide_gtk_list_view_move_previous (GtkListView *view);
IDE_AVAILABLE_IN_ALL
gboolean ide_gtk_list_view_get_selected_row (GtkListView *view,
guint *position);
+IDE_AVAILABLE_IN_ALL
+void ide_gtk_widget_hide_when_empty (GtkWidget *widget,
+ GListModel *model);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]