Skip to content

Commit

Permalink
Refactor preferences
Browse files Browse the repository at this point in the history
Move pages into separate modules and use declaration merging to declare
fields for template children.
  • Loading branch information
swsnr committed Sep 23, 2024
1 parent f568198 commit 6f614cf
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dist: compile
pnpm dist:format
gnome-extensions pack --force --out-dir dist build \
--extra-source=../metadata.json \
--extra-source=ui \
--extra-source=ui --extra-source=lib \
$(addprefix --extra-source=../,$(DIST-EXTRA-SRC)) \
$(addprefix --schema=../,$(wildcard schemas/*.gschema.xml))

Expand Down
85 changes: 85 additions & 0 deletions src/lib/prefs/about_page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright Sebastian Wiesner <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0.If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Alternatively, the contents of this file may be used under the terms
// of the GNU General Public License Version 2 or later, as described below:
//
// 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.

import GObject from "gi://GObject";
import Gtk from "gi://Gtk";
import Adw from "gi://Adw";

import type { ExtensionMetadata } from "@girs/gnome-shell/extensions/extension";

import { getTemplate } from "./template.js";

const LICENSE = `Copyright Sebastian Wiesner <[email protected]>
This programm is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
Alternatively, this program may be used under the terms
of the GNU General Public License Version 2 or later, as described below:
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.`;

interface AboutPageChildren {
_extensionName: Gtk.Label;
_extensionDescription: Gtk.Label;
_linkGithub: Gtk.LinkButton;
_linkIssues: Gtk.LinkButton;
_extensionLicense: Gtk.TextView;
}

class TypescriptTemplateAboutPage extends Adw.PreferencesPage {
constructor(metadata: ExtensionMetadata) {
super();
const children = this as unknown as AboutPageChildren;
children._extensionName.set_text(metadata.name);
children._extensionDescription.set_text(metadata.description);
if (metadata.url) {
children._linkGithub.set_uri(metadata.url);
children._linkIssues.set_uri(`${metadata.url}/issues`);
} else {
children._linkGithub.visible = false;
children._linkIssues.visible = false;
}
children._extensionLicense.buffer.set_text(LICENSE, -1);
}
}

export default GObject.registerClass(
{
GTypeName: "TypescriptTemplateAboutPage",
Template: getTemplate("AboutPage"),
InternalChildren: [
"extensionName",
"extensionDescription",
"linkGithub",
"linkIssues",
"extensionLicense",
],
},
TypescriptTemplateAboutPage,
);
52 changes: 52 additions & 0 deletions src/lib/prefs/general_page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright Sebastian Wiesner <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0.If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Alternatively, the contents of this file may be used under the terms
// of the GNU General Public License Version 2 or later, as described below:
//
// 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.

import GObject from "gi://GObject";
import Gio from "gi://Gio";
import Adw from "gi://Adw";

import { getTemplate } from "./template.js";

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
interface TypescriptTemplateGeneralPage {
_sayHello: Adw.SwitchRow;
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class TypescriptTemplateGeneralPage extends Adw.PreferencesPage {
constructor(settings: Gio.Settings) {
super();

settings.bind(
"say-hello",
this._sayHello,
"active",
Gio.SettingsBindFlags.DEFAULT,
);
}
}

export default GObject.registerClass(
{
GTypeName: "TypescriptTemplateGeneralPage",
Template: getTemplate("GeneralPage"),
InternalChildren: ["sayHello"],
},
TypescriptTemplateGeneralPage,
);
27 changes: 27 additions & 0 deletions src/lib/prefs/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Sebastian Wiesner <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0.If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Alternatively, the contents of this file may be used under the terms
// of the GNU General Public License Version 2 or later, as described below:
//
// 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.

import GLib from "gi://GLib";

export const getTemplate = (name: string): string =>
GLib.uri_resolve_relative(
import.meta.url,
`ui/${name}.ui`,
GLib.UriFlags.NONE,
);
95 changes: 2 additions & 93 deletions src/prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,104 +17,13 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import GObject from "gi://GObject";
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Gtk from "gi://Gtk";
import Adw from "gi://Adw";

import { ExtensionPreferences } from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";

import type { ExtensionMetadata } from "@girs/gnome-shell/extensions/extension";

const LICENSE = `Copyright Sebastian Wiesner <[email protected]>
This programm is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
Alternatively, this program may be used under the terms
of the GNU General Public License Version 2 or later, as described below:
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.`;

const getTemplate = (name: string): string =>
GLib.uri_resolve_relative(
import.meta.url,
`ui/${name}.ui`,
GLib.UriFlags.NONE,
);

interface GeneralPageChildren {
_sayHello: Adw.SwitchRow;
}

const GeneralPage = GObject.registerClass(
{
GTypeName: "TypescriptTemplateGeneralPage",
Template: getTemplate("GeneralPage"),
InternalChildren: ["sayHello"],
},
class TypescriptTemplateGeneralPage extends Adw.PreferencesPage {
constructor(settings: Gio.Settings) {
super();

const children = this as unknown as GeneralPageChildren;
settings.bind(
"say-hello",
children._sayHello,
"active",
Gio.SettingsBindFlags.DEFAULT,
);
}
},
);

interface AboutPageChildren {
_extensionName: Gtk.Label;
_extensionDescription: Gtk.Label;
_linkGithub: Gtk.LinkButton;
_linkIssues: Gtk.LinkButton;
_extensionLicense: Gtk.TextView;
}

const AboutPage = GObject.registerClass(
{
GTypeName: "TypescriptTemplateAboutPage",
Template: getTemplate("AboutPage"),
InternalChildren: [
"extensionName",
"extensionDescription",
"linkGithub",
"linkIssues",
"extensionLicense",
],
},
class TypescriptTemplateAboutPage extends Adw.PreferencesPage {
constructor(metadata: ExtensionMetadata) {
super();
const children = this as unknown as AboutPageChildren;
children._extensionName.set_text(metadata.name);
children._extensionDescription.set_text(metadata.description);
if (metadata.url) {
children._linkGithub.set_uri(metadata.url);
children._linkIssues.set_uri(`${metadata.url}/issues`);
} else {
children._linkGithub.visible = false;
children._linkIssues.visible = false;
}
children._extensionLicense.buffer.set_text(LICENSE, -1);
}
},
);
import AboutPage from "./lib/prefs/about_page.js";
import GeneralPage from "./lib/prefs/general_page.js";

export default class HelloWorldPreferences extends ExtensionPreferences {
override fillPreferencesWindow(
Expand Down

0 comments on commit 6f614cf

Please sign in to comment.