From 5fedb3de9e0642b580d72f47f22fe0001c86151d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Mon, 14 Aug 2023 01:08:56 +0200 Subject: [PATCH] extensions-tool/create: Optionally include prefs We currently only create boilerplate code for the actual extension. Now that libadwaita has largely standardized preference UIs, it makes sense to allow creating the prefs.js boilerplate as well. Part-of: --- po/POTFILES.skip | 1 + .../extensions-tool/man/gnome-extensions.txt | 3 ++ .../extensions-tool/src/command-create.c | 32 +++++++++++++- .../src/gnome-extensions-tool.gresource.xml | 1 + .../extensions-tool/src/templates/prefs.js | 44 +++++++++++++++++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 subprojects/extensions-tool/src/templates/prefs.js diff --git a/po/POTFILES.skip b/po/POTFILES.skip index 36fe39be5..02aa312ff 100644 --- a/po/POTFILES.skip +++ b/po/POTFILES.skip @@ -2,4 +2,5 @@ data/org.gnome.Shell@wayland.service.in data/org.gnome.Shell@x11.service.in js/ui/init.js subprojects/extensions-tool/src/templates/indicator/extension.js +subprojects/extensions-tool/src/templates/prefs.js subprojects/extensions-tool/src/templates/quick-settings/extension.js diff --git a/subprojects/extensions-tool/man/gnome-extensions.txt b/subprojects/extensions-tool/man/gnome-extensions.txt index cd55191fb..0d6b5eaff 100644 --- a/subprojects/extensions-tool/man/gnome-extensions.txt +++ b/subprojects/extensions-tool/man/gnome-extensions.txt @@ -137,6 +137,9 @@ Creates a new extension from a template. *--template*='TEMPLATE'::: Use 'TEMPLATE' as base for the new extension + *--prefs*::: + Include a prefs.js template + *-i*::: *--interactive*::: Prompt for any extension metadata that hasn't been provided diff --git a/subprojects/extensions-tool/src/command-create.c b/subprojects/extensions-tool/src/command-create.c index 879047cb2..1f65b265d 100644 --- a/subprojects/extensions-tool/src/command-create.c +++ b/subprojects/extensions-tool/src/command-create.c @@ -253,6 +253,23 @@ copy_extension_template (const char *template, GFile *target_dir, GError **error return TRUE; } +static gboolean +copy_prefs_template (GFile *target_dir, GError **error) +{ + g_autoptr (GFile) target = NULL; + g_autoptr (GFile) source = NULL; + g_autofree char *uri = NULL; + + uri = g_strdup ("resource://" TEMPLATES_PATH "/prefs.js"); + source = g_file_new_for_uri (uri); + target = g_file_get_child (target_dir, "prefs.js"); + + if (!g_file_copy (source, target, G_FILE_COPY_TARGET_DEFAULT_PERMS, NULL, NULL, NULL, error)) + return FALSE; + + return TRUE; +} + static gboolean launch_extension_source (GFile *dir, GError **error) { @@ -282,7 +299,8 @@ create_extension (const char *uuid, const char *description, const char *gettext_domain, const char *settings_schema, - const char *template) + const char *template, + gboolean prefs) { g_autoptr (GFile) dir = NULL; g_autoptr (GError) error = NULL; @@ -320,6 +338,12 @@ create_extension (const char *uuid, return FALSE; } + if (prefs && !copy_prefs_template (dir, &error)) + { + g_printerr ("%s\n", error->message); + return FALSE; + } + if (!launch_extension_source (dir, &error)) { g_printerr ("%s\n", error->message); @@ -481,6 +505,7 @@ handle_create (int argc, char *argv[], gboolean do_help) g_autofree char *gettext_domain = NULL; g_autofree char *settings_schema = NULL; g_autofree char *template = NULL; + gboolean prefs = FALSE; gboolean interactive = FALSE; gboolean list_templates = FALSE; GOptionEntry entries[] = { @@ -508,6 +533,9 @@ handle_create (int argc, char *argv[], gboolean do_help) .arg = G_OPTION_ARG_STRING, .arg_data = &template, .arg_description = _("TEMPLATE"), .description = _("The template to use for the new extension") }, + { .long_name = "prefs", + .arg = G_OPTION_ARG_NONE, .arg_data = &prefs, + .description = _("Include prefs.js template") }, { .long_name = "list-templates", .arg = G_OPTION_ARG_NONE, .arg_data = &list_templates, .flags = G_OPTION_FLAG_HIDDEN }, @@ -568,5 +596,5 @@ handle_create (int argc, char *argv[], gboolean do_help) return 1; } - return create_extension (uuid, name, description, gettext_domain, settings_schema, template) ? 0 : 2; + return create_extension (uuid, name, description, gettext_domain, settings_schema, template, prefs) ? 0 : 2; } diff --git a/subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml b/subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml index 4a64c6195..c81b4bc9a 100644 --- a/subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml +++ b/subprojects/extensions-tool/src/gnome-extensions-tool.gresource.xml @@ -7,6 +7,7 @@ templates/indicator/stylesheet.css templates/plain/extension.js templates/plain/stylesheet.css + templates/prefs.js templates/quick-settings.desktop templates/quick-settings/extension.js templates/quick-settings/stylesheet.css diff --git a/subprojects/extensions-tool/src/templates/prefs.js b/subprojects/extensions-tool/src/templates/prefs.js new file mode 100644 index 000000000..3651b6a90 --- /dev/null +++ b/subprojects/extensions-tool/src/templates/prefs.js @@ -0,0 +1,44 @@ +/* prefs.js + * + * 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 2 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 . + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +import Adw from 'gi://Adw'; +import GObject from 'gi://GObject'; + +import {ExtensionPreferences, gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js'; + +const MyPreferencesWidget = GObject.registerClass( +class MyPreferencesWidget extends Adw.PreferencesGroup { + constructor() { + super({ + title: _('Section'), + }); + + this.add(new Adw.SwitchRow({ + title: _('Title'), + subtitle: _('Subtitle'), + active: true, + })); + } +}); + +export default class MyPreferences extends ExtensionPreferences { + getPreferencesWidget() { + return new MyPreferencesWidget(); + } +} +