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: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2889>
This commit is contained in:
Florian Müllner 2023-08-14 01:08:56 +02:00 committed by Marge Bot
parent 8a01141e7d
commit 5fedb3de9e
5 changed files with 79 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -7,6 +7,7 @@
<file>templates/indicator/stylesheet.css</file>
<file>templates/plain/extension.js</file>
<file>templates/plain/stylesheet.css</file>
<file>templates/prefs.js</file>
<file>templates/quick-settings.desktop</file>
<file>templates/quick-settings/extension.js</file>
<file>templates/quick-settings/stylesheet.css</file>

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* 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();
}
}