[libadwaita/wip/exalm/tab-overview: 14/16] demo: Extract tab view pages into a separate class




commit 246dba016b4f5d7b522f6860df1c14e9878a3706
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue Sep 6 20:59:55 2022 +0400

    demo: Extract tab view pages into a separate class

 demo/adwaita-demo.gresources.xml               |   1 +
 demo/meson.build                               |   1 +
 demo/pages/tab-view/adw-tab-view-demo-page.c   | 187 +++++++++++++++++++++++++
 demo/pages/tab-view/adw-tab-view-demo-page.h   |  18 +++
 demo/pages/tab-view/adw-tab-view-demo-page.ui  |  14 ++
 demo/pages/tab-view/adw-tab-view-demo-window.c |  86 ++++--------
 6 files changed, 245 insertions(+), 62 deletions(-)
---
diff --git a/demo/adwaita-demo.gresources.xml b/demo/adwaita-demo.gresources.xml
index b307ecea..f470e937 100644
--- a/demo/adwaita-demo.gresources.xml
+++ b/demo/adwaita-demo.gresources.xml
@@ -59,6 +59,7 @@
     <file preprocess="xml-stripblanks">pages/styles/adw-demo-page-styles.ui</file>
     <file preprocess="xml-stripblanks">pages/styles/adw-style-demo-window.ui</file>
     <file preprocess="xml-stripblanks">pages/tab-view/adw-demo-page-tab-view.ui</file>
+    <file preprocess="xml-stripblanks">pages/tab-view/adw-tab-view-demo-page.ui</file>
     <file preprocess="xml-stripblanks">pages/tab-view/adw-tab-view-demo-window.ui</file>
     <file preprocess="xml-stripblanks">pages/toasts/adw-demo-page-toasts.ui</file>
     <file preprocess="xml-stripblanks">pages/view-switcher/adw-demo-page-view-switcher.ui</file>
diff --git a/demo/meson.build b/demo/meson.build
index d41d19e9..ee805e78 100644
--- a/demo/meson.build
+++ b/demo/meson.build
@@ -37,6 +37,7 @@ adwaita_demo_sources = [
   'pages/styles/adw-demo-page-styles.c',
   'pages/styles/adw-style-demo-window.c',
   'pages/tab-view/adw-demo-page-tab-view.c',
+  'pages/tab-view/adw-tab-view-demo-page.c',
   'pages/tab-view/adw-tab-view-demo-window.c',
   'pages/toasts/adw-demo-page-toasts.c',
   'pages/view-switcher/adw-demo-page-view-switcher.c',
diff --git a/demo/pages/tab-view/adw-tab-view-demo-page.c b/demo/pages/tab-view/adw-tab-view-demo-page.c
new file mode 100644
index 00000000..e9d143c3
--- /dev/null
+++ b/demo/pages/tab-view/adw-tab-view-demo-page.c
@@ -0,0 +1,187 @@
+#include "adw-tab-view-demo-page.h"
+
+struct _AdwTabViewDemoPage
+{
+  AdwBin parent_instance;
+
+  GtkEditable *title_entry;
+
+  char *title;
+  GIcon *icon;
+  GIcon *last_icon;
+};
+
+G_DEFINE_TYPE (AdwTabViewDemoPage, adw_tab_view_demo_page, ADW_TYPE_BIN)
+
+enum {
+  PROP_0,
+  PROP_TITLE,
+  PROP_ICON,
+  LAST_PROP,
+};
+
+static GParamSpec *props[LAST_PROP];
+
+char **icon_names = NULL;
+gsize n_icon_names = 0;
+
+static void
+init_icon_names (GtkIconTheme *theme)
+{
+  if (icon_names)
+    return;
+
+  icon_names = gtk_icon_theme_get_icon_names (theme);
+  n_icon_names = g_strv_length (icon_names);
+}
+
+static GIcon *
+get_random_icon (void)
+{
+  GdkDisplay *display = gdk_display_get_default ();
+  GtkIconTheme *theme = gtk_icon_theme_get_for_display (display);
+  int index;
+
+  init_icon_names (theme);
+
+  index = g_random_int_range (0, n_icon_names);
+
+  return g_themed_icon_new (icon_names[index]);
+}
+
+static void
+adw_tab_view_demo_page_finalize (GObject *object)
+{
+  AdwTabViewDemoPage *self = ADW_TAB_VIEW_DEMO_PAGE (object);
+
+  g_clear_pointer (&self->title, g_free);
+  g_clear_object (&self->icon);
+  g_clear_object (&self->last_icon);
+
+  G_OBJECT_CLASS (adw_tab_view_demo_page_parent_class)->finalize (object);
+}
+
+static void
+adw_tab_view_demo_page_get_property (GObject    *object,
+                                     guint       prop_id,
+                                     GValue     *value,
+                                     GParamSpec *pspec)
+{
+  AdwTabViewDemoPage *self = ADW_TAB_VIEW_DEMO_PAGE (object);
+
+  switch (prop_id) {
+  case PROP_TITLE:
+    g_value_set_string (value, self->title);
+    break;
+  case PROP_ICON:
+    g_value_set_object (value, self->icon);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  }
+}
+
+static void
+adw_tab_view_demo_page_set_property (GObject      *object,
+                                     guint         prop_id,
+                                     const GValue *value,
+                                     GParamSpec   *pspec)
+{
+  AdwTabViewDemoPage *self = ADW_TAB_VIEW_DEMO_PAGE (object);
+
+  switch (prop_id) {
+  case PROP_TITLE:
+    g_clear_pointer (&self->title, g_free);
+    self->title = g_value_dup_string (value);
+    break;
+  case PROP_ICON:
+    g_set_object (&self->icon, g_value_get_object (value));
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  }
+}
+
+static void
+adw_tab_view_demo_page_class_init (AdwTabViewDemoPageClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->finalize = adw_tab_view_demo_page_finalize;
+  object_class->get_property = adw_tab_view_demo_page_get_property;
+  object_class->set_property = adw_tab_view_demo_page_set_property;
+
+  props[PROP_TITLE] =
+    g_param_spec_string ("title", NULL, NULL,
+                         NULL,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  props[PROP_ICON] =
+    g_param_spec_object ("icon", NULL, NULL,
+                         G_TYPE_ICON,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (object_class, LAST_PROP, props);
+
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Adwaita1/Demo/ui/pages/tab-view/adw-tab-view-demo-page.ui");
+  gtk_widget_class_bind_template_child (widget_class, AdwTabViewDemoPage, title_entry);
+}
+
+static void
+adw_tab_view_demo_page_init (AdwTabViewDemoPage *self)
+{
+  self->icon = get_random_icon ();
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+AdwTabViewDemoPage *
+adw_tab_view_demo_page_new (const char *title)
+{
+  return g_object_new (ADW_TYPE_TAB_VIEW_DEMO_PAGE,
+                       "title", title,
+                       NULL);
+}
+
+AdwTabViewDemoPage *
+adw_tab_view_demo_page_new_duplicate (AdwTabViewDemoPage *self)
+{
+  g_return_val_if_fail (ADW_IS_TAB_VIEW_DEMO_PAGE (self), NULL);
+
+  return g_object_new (ADW_TYPE_TAB_VIEW_DEMO_PAGE,
+                       "title", self->title,
+                       "icon", self->icon,
+                       NULL);
+}
+
+void
+adw_tab_view_demo_page_refresh_icon (AdwTabViewDemoPage *self)
+{
+  GIcon *icon;
+
+  g_return_if_fail (ADW_IS_TAB_VIEW_DEMO_PAGE (self));
+
+  icon = get_random_icon ();
+
+  g_object_set (self, "icon", icon, NULL);
+
+  g_object_unref (icon);
+}
+
+void
+adw_tab_view_demo_page_set_enable_icon (AdwTabViewDemoPage *self,
+                                        gboolean            enable_icon)
+{
+  g_return_if_fail (ADW_IS_TAB_VIEW_DEMO_PAGE (self));
+
+  enable_icon = !!enable_icon;
+
+  if (enable_icon) {
+    g_object_set (self, "icon", self->last_icon, NULL);
+    g_clear_object (&self->last_icon);
+  } else {
+    self->last_icon = g_object_ref (self->icon);
+    g_object_set (self, "icon", NULL, NULL);
+  }
+}
diff --git a/demo/pages/tab-view/adw-tab-view-demo-page.h b/demo/pages/tab-view/adw-tab-view-demo-page.h
new file mode 100644
index 00000000..dab3bf9d
--- /dev/null
+++ b/demo/pages/tab-view/adw-tab-view-demo-page.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <adwaita.h>
+
+G_BEGIN_DECLS
+
+#define ADW_TYPE_TAB_VIEW_DEMO_PAGE (adw_tab_view_demo_page_get_type())
+
+G_DECLARE_FINAL_TYPE (AdwTabViewDemoPage, adw_tab_view_demo_page, ADW, TAB_VIEW_DEMO_PAGE, AdwBin)
+
+AdwTabViewDemoPage *adw_tab_view_demo_page_new           (const char         *title);
+AdwTabViewDemoPage *adw_tab_view_demo_page_new_duplicate (AdwTabViewDemoPage *self);
+
+void adw_tab_view_demo_page_refresh_icon    (AdwTabViewDemoPage *self);
+void adw_tab_view_demo_page_set_enable_icon (AdwTabViewDemoPage *self,
+                                             gboolean            enable_icon);
+
+G_END_DECLS
diff --git a/demo/pages/tab-view/adw-tab-view-demo-page.ui b/demo/pages/tab-view/adw-tab-view-demo-page.ui
new file mode 100644
index 00000000..9814b990
--- /dev/null
+++ b/demo/pages/tab-view/adw-tab-view-demo-page.ui
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk" version="4.0"/>
+  <requires lib="libadwaita" version="1.0"/>
+  <template class="AdwTabViewDemoPage" parent="AdwBin">
+    <property name="child">
+      <object class="GtkEntry" id="title_entry">
+        <property name="halign">center</property>
+        <property name="valign">center</property>
+        <property name="text" bind-source="AdwTabViewDemoPage" bind-property="title" 
bind-flags="bidirectional"/>
+      </object>
+    </property>
+  </template>
+</interface>
diff --git a/demo/pages/tab-view/adw-tab-view-demo-window.c b/demo/pages/tab-view/adw-tab-view-demo-window.c
index 65d15861..01a54132 100644
--- a/demo/pages/tab-view/adw-tab-view-demo-window.c
+++ b/demo/pages/tab-view/adw-tab-view-demo-window.c
@@ -2,6 +2,8 @@
 
 #include <glib/gi18n.h>
 
+#include "adw-tab-view-demo-page.h"
+
 struct _AdwTabViewDemoWindow
 {
   AdwWindow parent_instance;
@@ -24,33 +26,6 @@ enum {
 
 static GParamSpec *props[LAST_PROP];
 
-char **icon_names = NULL;
-gsize n_icon_names = 0;
-
-static void
-init_icon_names (GtkIconTheme *theme)
-{
-  if (icon_names)
-    return;
-
-  icon_names = gtk_icon_theme_get_icon_names (theme);
-  n_icon_names = g_strv_length (icon_names);
-}
-
-static GIcon *
-get_random_icon (AdwTabViewDemoWindow *self)
-{
-  GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (self));
-  GtkIconTheme *theme = gtk_icon_theme_get_for_display (display);
-  int index;
-
-  init_icon_names (theme);
-
-  index = g_random_int_range (0, n_icon_names);
-
-  return g_themed_icon_new (icon_names[index]);
-}
-
 static void
 window_new (GSimpleAction *action,
             GVariant      *parameter,
@@ -80,30 +55,24 @@ text_to_tooltip (GBinding     *binding,
 static AdwTabPage *
 add_page (AdwTabViewDemoWindow *self,
           AdwTabPage           *parent,
-          const char           *title,
-          GIcon                *icon)
+          AdwTabViewDemoPage   *content)
 {
-  GtkWidget *content;
   AdwTabPage *page;
 
-  content = g_object_new (GTK_TYPE_ENTRY,
-                          "text", title,
-                          "halign", GTK_ALIGN_CENTER,
-                          "valign", GTK_ALIGN_CENTER,
-                          NULL);
-
   page = adw_tab_view_add_page (self->view, GTK_WIDGET (content), parent);
 
-  g_object_bind_property (content, "text",
+  g_object_bind_property (content, "title",
                           page, "title",
-                          G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
-  g_object_bind_property_full (content, "text",
+                          G_BINDING_SYNC_CREATE);
+  g_object_bind_property_full (content, "title",
                                page, "tooltip",
                                G_BINDING_SYNC_CREATE,
                                text_to_tooltip, NULL,
                                NULL, NULL);
+  g_object_bind_property (content, "icon",
+                          page, "icon",
+                          G_BINDING_SYNC_CREATE);
 
-  adw_tab_page_set_icon (page, icon);
   adw_tab_page_set_indicator_activatable (page, TRUE);
 
   return page;
@@ -117,19 +86,17 @@ tab_new (GSimpleAction *action,
   AdwTabViewDemoWindow *self = ADW_TAB_VIEW_DEMO_WINDOW (user_data);
   char *title;
   AdwTabPage *page;
-  GtkWidget *content;
-  GIcon *icon;
+  AdwTabViewDemoPage *content;
   static int next_page = 1;
 
   title = g_strdup_printf (_("Tab %d"), next_page);
-  icon = get_random_icon (self);
 
-  page = add_page (self, NULL, title, icon);
-  content = adw_tab_page_get_child (page);
+  content = adw_tab_view_demo_page_new (title);
+  page = add_page (self, NULL, content);
 
   adw_tab_view_set_selected_page (self->view, page);
 
-  gtk_widget_grab_focus (content);
+  gtk_widget_grab_focus (GTK_WIDGET (content));
 
   next_page++;
 
@@ -306,16 +273,11 @@ tab_change_icon (GSimpleAction *action,
 {
   AdwTabViewDemoWindow *self = ADW_TAB_VIEW_DEMO_WINDOW (user_data);
   gboolean enable_icon = g_variant_get_boolean (parameter);
+  AdwTabPage *page = get_current_page (self);
+  GtkWidget *child = adw_tab_page_get_child (page);
 
-  if (enable_icon) {
-    GIcon *icon = get_random_icon (self);
-
-    adw_tab_page_set_icon (get_current_page (self), icon);
-
-    g_object_unref (icon);
-  } else {
-    adw_tab_page_set_icon (get_current_page (self), NULL);
-  }
+  adw_tab_view_demo_page_set_enable_icon (ADW_TAB_VIEW_DEMO_PAGE (child),
+                                          enable_icon);
 
   g_simple_action_set_state (action, g_variant_new_boolean (enable_icon));
 }
@@ -326,11 +288,10 @@ tab_refresh_icon (GSimpleAction *action,
                   gpointer       user_data)
 {
   AdwTabViewDemoWindow *self = ADW_TAB_VIEW_DEMO_WINDOW (user_data);
-  GIcon *icon = get_random_icon (self);
+  AdwTabPage *page = get_current_page (self);
+  GtkWidget *child = adw_tab_page_get_child (page);
 
-  adw_tab_page_set_icon (get_current_page (self), icon);
-
-  g_object_unref (icon);
+  adw_tab_view_demo_page_refresh_icon (ADW_TAB_VIEW_DEMO_PAGE (child));
 }
 
 static void
@@ -340,11 +301,12 @@ tab_duplicate (GSimpleAction *action,
 {
   AdwTabViewDemoWindow *self = ADW_TAB_VIEW_DEMO_WINDOW (user_data);
   AdwTabPage *parent = get_current_page (self);
+  GtkWidget *parent_content = adw_tab_page_get_child (parent);
+  AdwTabViewDemoPage *content;
   AdwTabPage *page;
 
-  page = add_page (self, parent,
-                   adw_tab_page_get_title (parent),
-                   adw_tab_page_get_icon (parent));
+  content = adw_tab_view_demo_page_new_duplicate (ADW_TAB_VIEW_DEMO_PAGE (parent_content));
+  page = add_page (self, parent, content);
 
   adw_tab_page_set_indicator_icon (page, adw_tab_page_get_indicator_icon (parent));
   adw_tab_page_set_indicator_tooltip (page, adw_tab_page_get_indicator_tooltip (parent));


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