[libadwaita/wip/exalm/inspector: 3/3] Add a GtkInspector extension




commit 1c43be000b8f277f14da3caaf94a46d718fb9307
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Sep 15 02:27:01 2021 +0500

    Add a GtkInspector extension
    
    Add a page, currently listing the CSS colors that can be changed.

 examples/org.gnome.Adwaita.Demo.json              |   3 +-
 src/inspector/adw-inspector-module.c              |  43 ++++++
 src/inspector/adw-inspector-module.gresources.xml |   6 +
 src/inspector/adw-inspector-page-private.h        |  23 ++++
 src/inspector/adw-inspector-page.c                | 161 ++++++++++++++++++++++
 src/inspector/adw-inspector-page.ui               |  16 +++
 src/inspector/meson.build                         |  22 +++
 src/meson.build                                   |   4 +
 8 files changed, 277 insertions(+), 1 deletion(-)
---
diff --git a/examples/org.gnome.Adwaita.Demo.json b/examples/org.gnome.Adwaita.Demo.json
index 5e2c035a..3fa9c416 100644
--- a/examples/org.gnome.Adwaita.Demo.json
+++ b/examples/org.gnome.Adwaita.Demo.json
@@ -9,7 +9,8 @@
     "--share=ipc",
     "--socket=wayland",
     "--socket=x11",
-    "--talk-name=org.a11y.Bus"
+    "--talk-name=org.a11y.Bus",
+    "--env=GTK_PATH=/app/lib/gtk-4.0"
   ],
   "modules": [
     {
diff --git a/src/inspector/adw-inspector-module.c b/src/inspector/adw-inspector-module.c
new file mode 100644
index 00000000..7610697e
--- /dev/null
+++ b/src/inspector/adw-inspector-module.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Author: Alexander Mikhaylenko <alexander mikhaylenko puri sm>
+ */
+
+#include <adwaita.h>
+
+#include "adw-inspector-page-private.h"
+
+static gboolean
+get_force_inspector (void)
+{
+  const char *disable_portal = g_getenv ("ADW_FORCE_INSPECTOR");
+
+  return disable_portal && disable_portal[0] == '1';
+}
+
+
+void
+g_io_module_load (GIOModule *module)
+{
+  if (!adw_is_initialized ()) {
+    if (!get_force_inspector ())
+      return;
+
+    adw_init ();
+  }
+
+  g_type_module_use (G_TYPE_MODULE (module));
+
+  g_io_extension_point_implement ("gtk-inspector-page",
+                                  ADW_TYPE_INSPECTOR_PAGE,
+                                  "libadwaita",
+                                  10);
+}
+
+void
+g_io_module_unload (GIOModule *module)
+{
+}
diff --git a/src/inspector/adw-inspector-module.gresources.xml 
b/src/inspector/adw-inspector-module.gresources.xml
new file mode 100644
index 00000000..7fd848de
--- /dev/null
+++ b/src/inspector/adw-inspector-module.gresources.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/Adwaita/inspector/ui">
+    <file preprocess="xml-stripblanks">adw-inspector-page.ui</file>
+  </gresource>
+</gresources>
diff --git a/src/inspector/adw-inspector-page-private.h b/src/inspector/adw-inspector-page-private.h
new file mode 100644
index 00000000..fbd5b15e
--- /dev/null
+++ b/src/inspector/adw-inspector-page-private.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Author: Alexander Mikhaylenko <alexander mikhaylenko puri sm>
+ */
+
+#pragma once
+
+#if !defined(_ADWAITA_INSIDE) && !defined(ADWAITA_COMPILATION)
+#error "Only <adwaita.h> can be included directly."
+#endif
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define ADW_TYPE_INSPECTOR_PAGE (adw_inspector_page_get_type())
+
+G_DECLARE_FINAL_TYPE (AdwInspectorPage, adw_inspector_page, ADW, INSPECTOR_PAGE, GtkWidget)
+
+G_END_DECLS
diff --git a/src/inspector/adw-inspector-page.c b/src/inspector/adw-inspector-page.c
new file mode 100644
index 00000000..454009f2
--- /dev/null
+++ b/src/inspector/adw-inspector-page.c
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Author: Alexander Mikhaylenko <alexander mikhaylenko puri sm>
+ */
+
+#include "config.h"
+#include "adw-inspector-page-private.h"
+#include <adwaita.h>
+
+struct _AdwInspectorPage
+{
+  AdwBin parent_instance;
+
+  AdwPreferencesGroup *colors_group;
+
+  GtkCssProvider *provider;
+  GHashTable *colors;
+};
+
+G_DEFINE_TYPE (AdwInspectorPage, adw_inspector_page, ADW_TYPE_BIN)
+
+enum {
+  PROP_0,
+  PROP_TITLE,
+  LAST_PROP,
+};
+
+static GParamSpec *props[LAST_PROP];
+
+static void
+update_css (AdwInspectorPage *self)
+{
+  g_autofree char *data = NULL;
+  g_autoptr (GString) string = g_string_new (NULL);
+  const char *name;
+  GtkColorChooser *button;
+  GHashTableIter iter;
+
+  g_hash_table_iter_init (&iter, self->colors);
+
+  while (g_hash_table_iter_next (&iter, (gpointer) &name, (gpointer) &button)) {
+    g_autofree char *color = NULL;
+    GdkRGBA rgba;
+
+    gtk_color_chooser_get_rgba (button, &rgba);
+    color = gdk_rgba_to_string (&rgba);
+
+    g_string_append_printf (string, "@define-color %s %s;", name, color);
+  }
+
+  gtk_css_provider_load_from_data (self->provider, string->str, -1);
+}
+
+static void
+add_color_row (AdwInspectorPage *self,
+               const char       *title,
+               const char       *name)
+{
+  GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (self));
+  GtkWidget *row, *button;
+  GdkRGBA rgba;
+
+  if (!gtk_style_context_lookup_color (context, name, &rgba))
+    return;
+
+  button = gtk_color_button_new ();
+  gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
+  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), &rgba);
+  g_signal_connect_swapped (button, "color-set", G_CALLBACK (update_css), self);
+
+  row = adw_action_row_new ();
+  adw_preferences_row_set_title (ADW_PREFERENCES_ROW (row), title);
+  adw_action_row_add_suffix (ADW_ACTION_ROW (row), button);
+  adw_action_row_set_activatable_widget (ADW_ACTION_ROW (row), button);
+  adw_preferences_group_add (self->colors_group, row);
+
+  g_hash_table_insert (self->colors, g_strdup (name), button);
+}
+
+static void
+adw_inspector_page_get_property (GObject    *object,
+                                 guint       prop_id,
+                                 GValue     *value,
+                                 GParamSpec *pspec)
+{
+  switch (prop_id) {
+  case PROP_TITLE:
+    g_value_set_string (value, "Libadwaita");
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+  }
+}
+
+static void
+adw_inspector_page_dispose (GObject *object)
+{
+  AdwInspectorPage *self = ADW_INSPECTOR_PAGE (object);
+
+  if (self->provider) {
+    gtk_style_context_remove_provider_for_display (gdk_display_get_default (),
+                                                   GTK_STYLE_PROVIDER (self->provider));
+    g_clear_object (&self->provider);
+  }
+
+  g_clear_pointer (&self->colors, g_hash_table_unref);
+
+  G_OBJECT_CLASS (adw_inspector_page_parent_class)->dispose (object);
+}
+
+static void
+adw_inspector_page_class_init (AdwInspectorPageClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->get_property = adw_inspector_page_get_property;
+  object_class->dispose = adw_inspector_page_dispose;
+
+  props[PROP_TITLE] =
+    g_param_spec_string ("title",
+                         "Title",
+                         "title",
+                         "Libadwaita",
+                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+  g_object_class_install_properties (object_class, LAST_PROP, props);
+
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                               "/org/gnome/Adwaita/inspector/ui/adw-inspector-page.ui");
+  gtk_widget_class_bind_template_child (widget_class, AdwInspectorPage, colors_group);
+}
+
+static void
+adw_inspector_page_init (AdwInspectorPage *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  self->provider = gtk_css_provider_new ();
+
+  gtk_style_context_add_provider_for_display (gdk_display_get_default (),
+                                              GTK_STYLE_PROVIDER (self->provider),
+                                              1000);
+
+  self->colors = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+  add_color_row (self, "Accent Color (standalone)", "accent_color");
+  add_color_row (self, "Accent Color (background)", "accent_bg_color");
+  add_color_row (self, "Accent Color (foreground)", "accent_fg_color");
+  add_color_row (self, "Destructive Color (standalone)", "destructive_color");
+  add_color_row (self, "Destructive Color (background)", "destructive_bg_color");
+  add_color_row (self, "Destructive Color (foreground)", "destructive_fg_color");
+  add_color_row (self, "Success Color", "success_color");
+  add_color_row (self, "Warning Color", "warning_color");
+  add_color_row (self, "ErrorColor", "error_color");
+
+  update_css (self);
+}
diff --git a/src/inspector/adw-inspector-page.ui b/src/inspector/adw-inspector-page.ui
new file mode 100644
index 00000000..a5967067
--- /dev/null
+++ b/src/inspector/adw-inspector-page.ui
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="libadwaita">
+  <requires lib="gtk" version="4.0"/>
+  <requires lib="libadwaita" version="1.0"/>
+  <template class="AdwInspectorPage" parent="AdwBin">
+    <property name="child">
+      <object class="AdwPreferencesPage">
+        <child>
+          <object class="AdwPreferencesGroup" id="colors_group">
+            <property name="title" translatable="yes">Colors</property>
+          </object>
+        </child>
+      </object>
+    </property>
+  </template>
+</interface>
diff --git a/src/inspector/meson.build b/src/inspector/meson.build
new file mode 100644
index 00000000..ed25f8e9
--- /dev/null
+++ b/src/inspector/meson.build
@@ -0,0 +1,22 @@
+module_libdir = libdir / 'gtk-4.0' / 'inspector'
+
+libadwaita_inspector_resources = gnome.compile_resources(
+  'adw-inspector-resources',
+  'adw-inspector-module.gresources.xml',
+  c_name: 'adw',
+)
+
+libadwaita_inspector_sources = [
+  libadwaita_inspector_resources,
+  'adw-inspector-module.c',
+  'adw-inspector-page.c',
+]
+
+shared_library(
+  'adwaita-inspector-module' + apiversion,
+  libadwaita_inspector_sources,
+            soversion: soversion,
+         dependencies: libadwaita_dep,
+              install: true,
+          install_dir: module_libdir,
+)
diff --git a/src/meson.build b/src/meson.build
index 71e6a5ff..998db2f3 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -205,6 +205,7 @@ libadwaita_deps = [
   dependency('fribidi'),
   gio_dep,
   gtk_dep,
+  cc.find_library('dl', required: false),
   cc.find_library('m', required: false),
   cc.find_library('rt', required: false),
 ]
@@ -221,6 +222,7 @@ libadwaita_c_args = [
 config_h = configuration_data()
 config_h.set_quoted('GETTEXT_PACKAGE', 'libadwaita')
 config_h.set_quoted('LOCALEDIR', get_option('prefix') / get_option('localedir'))
+config_h.set('_GNU_SOURCE', '')
 
 # Symbol visibility
 if target_system == 'windows'
@@ -324,3 +326,5 @@ pkgg.generate(
      requires: 'gtk4',
   install_dir: libdir / 'pkgconfig',
 )
+
+subdir('inspector')


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