[libadwaita/wip/exalm/inspector: 7/7] Add a GtkInspector extension
- From: Alexander Mikhaylenko <alexm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libadwaita/wip/exalm/inspector: 7/7] Add a GtkInspector extension
- Date: Sun, 26 Sep 2021 21:58:25 +0000 (UTC)
commit 295e50e93f5ae61374ba91418736013b5337e17d
Author: Alexander Mikhaylenko <alexm gnome org>
Date: Wed Sep 15 02:27:01 2021 +0500
Add a GtkInspector extension
Add a page, currently allowing to override AdwSettings properties.
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 | 162 ++++++++++++++++++++++
src/inspector/adw-inspector-page.ui | 58 ++++++++
src/inspector/meson.build | 22 +++
src/meson.build | 4 +
8 files changed, 320 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..c4941d47
--- /dev/null
+++ b/src/inspector/adw-inspector-page.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * Author: Alexander Mikhaylenko <alexander mikhaylenko puri sm>
+ */
+
+#include "config.h"
+#include <glib/gi18n.h>
+
+#include "adw-inspector-page-private.h"
+
+#include <adwaita.h>
+#include "adw-settings-private.h"
+
+struct _AdwInspectorPage
+{
+ AdwBin parent_instance;
+
+ AdwSettings *settings;
+
+ GtkSwitch *support_color_schemes_switch;
+ AdwComboRow *color_scheme_row;
+ GtkSwitch *high_contrast_switch;
+};
+
+G_DEFINE_TYPE (AdwInspectorPage, adw_inspector_page, ADW_TYPE_BIN)
+
+enum {
+ PROP_0,
+ PROP_TITLE,
+ LAST_PROP,
+};
+
+static GParamSpec *props[LAST_PROP];
+
+static void
+color_scheme_changed_cb (AdwInspectorPage *self)
+{
+ AdwEnumListItem *item = adw_combo_row_get_selected_item (self->color_scheme_row);
+ AdwSystemColorScheme color_scheme = adw_enum_list_item_get_value (item);
+
+ adw_settings_override_color_scheme (self->settings, color_scheme);
+}
+
+static void
+support_color_schemes_changed_cb (AdwInspectorPage *self)
+{
+ gboolean supports = gtk_switch_get_active (self->support_color_schemes_switch);
+
+ adw_settings_override_system_supports_color_schemes (self->settings, supports);
+
+ if (supports)
+ color_scheme_changed_cb (self);
+}
+
+static void
+high_contrast_changed_cb (AdwInspectorPage *self)
+{
+ gboolean hc = gtk_switch_get_active (self->high_contrast_switch);
+
+ adw_settings_override_high_contrast (self->settings, hc);
+}
+
+static char *
+get_system_color_scheme_name (AdwEnumListItem *item,
+ gpointer user_data)
+{
+ switch (adw_enum_list_item_get_value (item)) {
+ case ADW_SYSTEM_COLOR_SCHEME_DEFAULT:
+ return g_strdup (_("No Preference"));
+ case ADW_SYSTEM_COLOR_SCHEME_PREFER_DARK:
+ return g_strdup (_("Prefer Dark"));
+ case ADW_SYSTEM_COLOR_SCHEME_PREFER_LIGHT:
+ return g_strdup (_("Prefer Light"));
+ default:
+ return NULL;
+ }
+ return "";
+}
+
+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->settings) {
+ adw_settings_end_override (self->settings);
+ self->settings = NULL;
+ }
+
+ 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, support_color_schemes_switch);
+ gtk_widget_class_bind_template_child (widget_class, AdwInspectorPage, color_scheme_row);
+ gtk_widget_class_bind_template_child (widget_class, AdwInspectorPage, high_contrast_switch);
+
+ gtk_widget_class_bind_template_callback (widget_class, get_system_color_scheme_name);
+ gtk_widget_class_bind_template_callback (widget_class, support_color_schemes_changed_cb);
+ gtk_widget_class_bind_template_callback (widget_class, color_scheme_changed_cb);
+ gtk_widget_class_bind_template_callback (widget_class, high_contrast_changed_cb);
+}
+
+static void
+adw_inspector_page_init (AdwInspectorPage *self)
+{
+ AdwSystemColorScheme color_scheme;
+ gboolean supports, hc;
+
+ self->settings = adw_settings_get_default ();
+
+ gtk_widget_init_template (GTK_WIDGET (self));
+
+ adw_settings_start_override (self->settings);
+
+ supports = adw_settings_get_system_supports_color_schemes (self->settings);
+ gtk_switch_set_active (self->support_color_schemes_switch, supports);
+
+ color_scheme = adw_settings_get_color_scheme (self->settings);
+ adw_combo_row_set_selected (self->color_scheme_row, color_scheme);
+
+ hc = adw_settings_get_high_contrast (self->settings);
+ gtk_switch_set_active (self->high_contrast_switch, hc);
+}
diff --git a/src/inspector/adw-inspector-page.ui b/src/inspector/adw-inspector-page.ui
new file mode 100644
index 00000000..ca151c5d
--- /dev/null
+++ b/src/inspector/adw-inspector-page.ui
@@ -0,0 +1,58 @@
+<?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">
+ <property name="title" translatable="yes">System Appearance</property>
+ <property name="description" translatable="yes">Override settings for this application. They
will be reset upon closing the inspector.</property>
+ <child>
+ <object class="AdwActionRow">
+ <property name="title" translatable="yes">System Supports Color Schemes</property>
+ <property name="activatable-widget">support_color_schemes_switch</property>
+ <child>
+ <object class="GtkSwitch" id="support_color_schemes_switch">
+ <property name="valign">center</property>
+ <signal name="notify::active" handler="support_color_schemes_changed_cb" swapped="yes"/>
+ </object>
+ </child>
+ </object>
+ </child>
+ <child>
+ <object class="AdwComboRow" id="color_scheme_row">
+ <property name="title" translatable="yes">Preferred Color Scheme</property>
+ <property name="model">
+ <object class="AdwEnumListModel">
+ <property name="enum-type">AdwSystemColorScheme</property>
+ </object>
+ </property>
+ <property name="expression">
+ <closure type="gchararray" function="get_system_color_scheme_name"/>
+ </property>
+ <binding name="sensitive">
+ <lookup name="active">support_color_schemes_switch</lookup>
+ </binding>
+ <signal name="notify::selected" handler="color_scheme_changed_cb" swapped="yes"/>
+ </object>
+ </child>
+ <child>
+ <object class="AdwActionRow">
+ <property name="title" translatable="yes">High Contrast</property>
+ <property name="activatable-widget">high_contrast_switch</property>
+ <child>
+ <object class="GtkSwitch" id="high_contrast_switch">
+ <property name="valign">center</property>
+ <signal name="notify::active" handler="high_contrast_changed_cb" swapped="yes"/>
+ </object>
+ </child>
+ </object>
+ </child>
+ </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]