Extension: Port to ESM

This commit is contained in:
Romain Vigier
2023-08-19 19:23:10 +02:00
parent b480820b37
commit 8c7724607e
23 changed files with 587 additions and 549 deletions
+46 -38
View File
@@ -1,47 +1,55 @@
// SPDX-FileCopyrightText: 2021, 2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Adw, Gdk, GdkPixbuf, Gio, GLib, GObject, Gtk } = imports.gi;
const { extensionUtils } = imports.misc;
import Adw from 'gi://Adw';
import Gdk from 'gi://Gdk';
import GdkPixbuf from 'gi://GdkPixbuf';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
const Me = extensionUtils.getCurrentExtension();
const _ = extensionUtils.gettext;
import { gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
var BackgroundButton = GObject.registerClass({
GTypeName: 'BackgroundButton',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/BackgroundButton.ui',
InternalChildren: ['filechooser', 'thumbnail'],
Properties: {
uri: GObject.ParamSpec.string(
'uri',
'URI',
'URI to the background file',
GObject.ParamFlags.READWRITE,
null
),
thumbWidth: GObject.ParamSpec.int(
'thumb-width',
'Thumbnail width',
'Width of the displayed thumbnail',
GObject.ParamFlags.READWRITE,
0, 600,
180
),
thumbHeight: GObject.ParamSpec.int(
'thumb-height',
'Thumbnail height',
'Height of the displayed thumbnail',
GObject.ParamFlags.READWRITE,
0, 600,
180
),
},
}, class BackgroundButton extends Gtk.Button {
export class BackgroundButton extends Gtk.Button {
#uri;
constructor(props = {}) {
super(props);
static {
GObject.registerClass({
GTypeName: 'BackgroundButton',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/BackgroundButton.ui',
InternalChildren: ['filechooser', 'thumbnail'],
Properties: {
uri: GObject.ParamSpec.string(
'uri',
'URI',
'URI to the background file',
GObject.ParamFlags.READWRITE,
null
),
thumbWidth: GObject.ParamSpec.int(
'thumb-width',
'Thumbnail width',
'Width of the displayed thumbnail',
GObject.ParamFlags.READWRITE,
0, 600,
180
),
thumbHeight: GObject.ParamSpec.int(
'thumb-height',
'Thumbnail height',
'Height of the displayed thumbnail',
GObject.ParamFlags.READWRITE,
0, 600,
180
),
},
}, this);
}
constructor({ ...params } = {}) {
super(params);
this.#setupSize();
this.#setupDropTarget();
this.#setupFileChooserFilter();
@@ -151,7 +159,7 @@ var BackgroundButton = GObject.registerClass({
if (!this.#isContentTypeSupported(Gio.content_type_guess(path, null)[0]))
throw new Error();
} catch (e) {
console.error(`[${Me.metadata.name}] No suitable background file found in ${file.get_path()}.\n${e}`);
console.error(`No suitable background file found in ${file.get_path()}.\n${e}`);
return;
}
} else {
@@ -172,4 +180,4 @@ var BackgroundButton = GObject.registerClass({
this._thumbnail.paintable = Gdk.Texture.new_for_pixbuf(thumbPixbuf);
}
});
}
+18 -15
View File
@@ -1,25 +1,28 @@
// SPDX-FileCopyrightText: 2020-2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Adw, Gio, GLib, GObject } = imports.gi;
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
var BackgroundsPage = GObject.registerClass({
GTypeName: 'BackgroundsPage',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/BackgroundsPage.ui',
InternalChildren: [
'day_button',
'night_button',
],
}, class BackgroundsPage extends Adw.PreferencesPage {
constructor(props = {}) {
super(props);
export class BackgroundsPage extends Adw.PreferencesPage {
static {
GObject.registerClass({
GTypeName: 'BackgroundsPage',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/BackgroundsPage.ui',
InternalChildren: [
'day_button',
'night_button',
],
}, this);
}
constructor({ ...params } = {}) {
super(params);
const settings = new Gio.Settings({ schema: 'org.gnome.desktop.background' });
settings.bind('picture-uri', this._day_button, 'uri', Gio.SettingsBindFlags.DEFAULT);
settings.bind('picture-uri-dark', this._night_button, 'uri', Gio.SettingsBindFlags.DEFAULT);
}
});
}
+11 -9
View File
@@ -1,18 +1,20 @@
// SPDX-FileCopyrightText: 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Gio, GLib, GObject, Gtk } = imports.gi;
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
var ClearableEntry = GObject.registerClass({
GTypeName: 'ClearableEntry',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/ClearableEntry.ui',
}, class ClearableEntry extends Gtk.Entry {
export class ClearableEntry extends Gtk.Entry {
static {
GObject.registerClass({
GTypeName: 'ClearableEntry',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/ClearableEntry.ui',
}, this);
}
onIconReleased(entry, position) {
if (position === Gtk.EntryIconPosition.SECONDARY)
entry.text = '';
}
});
}
+19 -17
View File
@@ -1,27 +1,29 @@
// SPDX-FileCopyrightText: 2020-2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Adw, Gio, GLib, GObject } = imports.gi;
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
var CommandsPage = GObject.registerClass({
GTypeName: 'CommandsPage',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/CommandsPage.ui',
InternalChildren: [
'enabled_switch',
'sunrise_entry',
'sunset_entry',
],
}, class CommandsPage extends Adw.PreferencesPage {
constructor(props = {}) {
super(props);
const settings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.commands`);
export class CommandsPage extends Adw.PreferencesPage {
static {
GObject.registerClass({
GTypeName: 'CommandsPage',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/CommandsPage.ui',
InternalChildren: [
'enabled_switch',
'sunrise_entry',
'sunset_entry',
],
}, this);
}
constructor({ settings, ...params } = {}) {
super(params);
settings.bind('enabled', this._enabled_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind('sunrise', this._sunrise_entry, 'text', Gio.SettingsBindFlags.DEFAULT);
settings.bind('sunset', this._sunset_entry, 'text', Gio.SettingsBindFlags.DEFAULT);
}
});
}
+9 -12
View File
@@ -1,18 +1,15 @@
// SPDX-FileCopyrightText: 2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Adw, GLib, GObject } = imports.gi;
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
const _ = extensionUtils.gettext;
import Adw from 'gi://Adw';
import GObject from 'gi://GObject';
var ContributePage = GObject.registerClass({
GTypeName: 'ContributePage',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/ContributePage.ui',
}, class ContributePage extends Adw.PreferencesPage {
getVersionString(_page) {
return _('Version %d').format(Me.metadata.version);
export class ContributePage extends Adw.PreferencesPage {
static {
GObject.registerClass({
GTypeName: 'ContributePage',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/ContributePage.ui',
}, this);
}
});
}
+31 -27
View File
@@ -1,32 +1,36 @@
// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { GObject } = imports.gi;
import GObject from 'gi://GObject';
var DropDownChoice = GObject.registerClass({
GTypeName: 'DropDownChoice',
Properties: {
id: GObject.ParamSpec.string(
'id',
'ID',
'Identifier',
GObject.ParamFlags.READWRITE,
null
),
title: GObject.ParamSpec.string(
'title',
'Title',
'Displayed title',
GObject.ParamFlags.READWRITE,
null
),
enabled: GObject.ParamSpec.boolean(
'enabled',
'Enabled',
'If the choice is enabled',
GObject.ParamFlags.READWRITE,
true
),
},
}, class DropDownChoice extends GObject.Object {});
export class DropDownChoice extends GObject.Object {
static {
GObject.registerClass({
GTypeName: 'DropDownChoice',
Properties: {
id: GObject.ParamSpec.string(
'id',
'ID',
'Identifier',
GObject.ParamFlags.READWRITE,
null
),
title: GObject.ParamSpec.string(
'title',
'Title',
'Displayed title',
GObject.ParamFlags.READWRITE,
null
),
enabled: GObject.ParamSpec.boolean(
'enabled',
'Enabled',
'If the choice is enabled',
GObject.ParamFlags.READWRITE,
true
),
},
}, this);
}
}
+20 -18
View File
@@ -1,25 +1,27 @@
// SPDX-FileCopyrightText: 2020-2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Adw, Gio, GLib, GObject, Gtk } = imports.gi;
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
var SchedulePage = GObject.registerClass({
GTypeName: 'SchedulePage',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/SchedulePage.ui',
InternalChildren: [
'manual_schedule_switch',
'keyboard_shortcut_button',
'schedule_sunrise_time_chooser',
'schedule_sunset_time_chooser',
],
}, class SchedulePage extends Adw.PreferencesPage {
constructor(props = {}) {
super(props);
const settings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.time`);
export class SchedulePage extends Adw.PreferencesPage {
static {
GObject.registerClass({
GTypeName: 'SchedulePage',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/SchedulePage.ui',
InternalChildren: [
'manual_schedule_switch',
'keyboard_shortcut_button',
'schedule_sunrise_time_chooser',
'schedule_sunset_time_chooser',
],
}, this);
}
constructor({ settings, ...params } = {}) {
super(params);
settings.bind('manual-schedule', this._manual_schedule_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
@@ -34,4 +36,4 @@ var SchedulePage = GObject.registerClass({
});
this._keyboard_shortcut_button.keybinding = settings.get_strv('nightthemeswitcher-ondemand-keybinding')[0];
}
});
}
+88 -23
View File
@@ -1,28 +1,29 @@
// SPDX-FileCopyrightText: 2021, 2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Gdk, GLib, GObject, Gtk } = imports.gi;
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
const utils = Me.imports.utils;
import Gdk from 'gi://Gdk';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
var ShortcutButton = GObject.registerClass({
GTypeName: 'ShortcutButton',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/ShortcutButton.ui',
InternalChildren: ['choose_button', 'change_button', 'clear_button', 'dialog'],
Properties: {
keybinding: GObject.ParamSpec.string(
'keybinding',
'Keybinding',
'Key sequence',
GObject.ParamFlags.READWRITE,
null
),
},
}, class ShortcutButton extends Gtk.Stack {
export class ShortcutButton extends Gtk.Stack {
static {
GObject.registerClass({
GTypeName: 'ShortcutButton',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/ShortcutButton.ui',
InternalChildren: ['choose_button', 'change_button', 'clear_button', 'dialog'],
Properties: {
keybinding: GObject.ParamSpec.string(
'keybinding',
'Keybinding',
'Key sequence',
GObject.ParamFlags.READWRITE,
null
),
},
}, this);
}
vfunc_mnemonic_activate() {
this.activate();
}
@@ -65,8 +66,8 @@ var ShortcutButton = GObject.registerClass({
}
if (
!utils.isBindingValid({ mask, keycode, keyval }) ||
!utils.isAccelValid({ mask, keyval })
!isBindingValid({ mask, keycode, keyval }) ||
!isAccelValid({ mask, keyval })
)
return Gdk.EVENT_STOP;
@@ -79,4 +80,68 @@ var ShortcutButton = GObject.registerClass({
this._dialog.close();
return Gdk.EVENT_STOP;
}
});
}
/**
* Check if the given keyval is forbidden.
*
* @param {number} keyval The keyval number.
* @returns {boolean} `true` if the keyval is forbidden.
*/
function isKeyvalForbidden(keyval) {
const forbiddenKeyvals = [
Gdk.KEY_Home,
Gdk.KEY_Left,
Gdk.KEY_Up,
Gdk.KEY_Right,
Gdk.KEY_Down,
Gdk.KEY_Page_Up,
Gdk.KEY_Page_Down,
Gdk.KEY_End,
Gdk.KEY_Tab,
Gdk.KEY_KP_Enter,
Gdk.KEY_Return,
Gdk.KEY_Mode_switch,
];
return forbiddenKeyvals.includes(keyval);
}
/**
* Check if the given key combo is a valid binding
*
* @param {{mask: number, keycode: number, keyval:number}} combo An object
* representing the key combo.
* @returns {boolean} `true` if the key combo is a valid binding.
*/
function isBindingValid({ mask, keycode, keyval }) {
if ((mask === 0 || mask === Gdk.SHIFT_MASK) && keycode !== 0) {
if (
(keyval >= Gdk.KEY_a && keyval <= Gdk.KEY_z) ||
(keyval >= Gdk.KEY_A && keyval <= Gdk.KEY_Z) ||
(keyval >= Gdk.KEY_0 && keyval <= Gdk.KEY_9) ||
(keyval >= Gdk.KEY_kana_fullstop && keyval <= Gdk.KEY_semivoicedsound) ||
(keyval >= Gdk.KEY_Arabic_comma && keyval <= Gdk.KEY_Arabic_sukun) ||
(keyval >= Gdk.KEY_Serbian_dje && keyval <= Gdk.KEY_Cyrillic_HARDSIGN) ||
(keyval >= Gdk.KEY_Greek_ALPHAaccent && keyval <= Gdk.KEY_Greek_omega) ||
(keyval >= Gdk.KEY_hebrew_doublelowline && keyval <= Gdk.KEY_hebrew_taf) ||
(keyval >= Gdk.KEY_Thai_kokai && keyval <= Gdk.KEY_Thai_lekkao) ||
(keyval >= Gdk.KEY_Hangul_Kiyeog && keyval <= Gdk.KEY_Hangul_J_YeorinHieuh) ||
(keyval === Gdk.KEY_space && mask === 0) ||
isKeyvalForbidden(keyval)
)
return false;
}
return true;
}
/**
* Check if the given key combo is a valid accelerator.
*
* @param {{mask: number, keyval:number}} combo An object representing the key
* combo.
* @returns {boolean} `true` if the key combo is a valid accelerator.
*/
function isAccelValid({ mask, keyval }) {
return Gtk.accelerator_valid(keyval, mask) || (keyval === Gdk.KEY_Tab && mask !== 0);
}
+33 -31
View File
@@ -1,41 +1,42 @@
// SPDX-FileCopyrightText: 2020-2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Adw, Gio, GLib, GObject, Gtk } = imports.gi;
const { extensionUtils } = imports.misc;
import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
const Me = extensionUtils.getCurrentExtension();
const _ = extensionUtils.gettext;
import { gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
const utils = Me.imports.utils;
import * as utils from '../utils.js';
const { DropDownChoice } = Me.imports.preferences.DropDownChoice;
import { DropDownChoice } from './DropDownChoice.js';
var ThemesPage = GObject.registerClass({
GTypeName: 'ThemesPage',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/ThemesPage.ui',
InternalChildren: [
'gtk_row',
'gtk_day_variant_combo_row',
'gtk_night_variant_combo_row',
'shell_row',
'shell_day_variant_combo_row',
'shell_night_variant_combo_row',
'icon_row',
'icon_day_variant_combo_row',
'icon_night_variant_combo_row',
'cursor_row',
'cursor_day_variant_combo_row',
'cursor_night_variant_combo_row',
],
}, class ThemesPage extends Adw.PreferencesPage {
constructor(props = {}) {
super(props);
const gtkSettings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.gtk-variants`);
const shellSettings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.shell-variants`);
const iconSettings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.icon-variants`);
const cursorSettings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.cursor-variants`);
export class ThemesPage extends Adw.PreferencesPage {
static {
GObject.registerClass({
GTypeName: 'ThemesPage',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/ThemesPage.ui',
InternalChildren: [
'gtk_row',
'gtk_day_variant_combo_row',
'gtk_night_variant_combo_row',
'shell_row',
'shell_day_variant_combo_row',
'shell_night_variant_combo_row',
'icon_row',
'icon_day_variant_combo_row',
'icon_night_variant_combo_row',
'cursor_row',
'cursor_day_variant_combo_row',
'cursor_night_variant_combo_row',
],
}, this);
}
constructor({ gtkSettings, shellSettings, iconSettings, cursorSettings, ...params } = {}) {
super(params);
gtkSettings.bind('enabled', this._gtk_row, 'enable-expansion', Gio.SettingsBindFlags.DEFAULT);
@@ -65,7 +66,8 @@ var ThemesPage = GObject.registerClass({
_setupComboRow(this._cursor_day_variant_combo_row, cursorThemesStore, cursorSettings, 'day');
_setupComboRow(this._cursor_night_variant_combo_row, cursorThemesStore, cursorSettings, 'night');
}
});
}
/**
* Set up the model of a combo row.
+23 -21
View File
@@ -1,28 +1,30 @@
// SPDX-FileCopyrightText: 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { GLib, GObject, Gtk } = imports.gi;
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
var TimeChooser = GObject.registerClass({
GTypeName: 'TimeChooser',
Template: 'resource:///org/gnome/shell/extensions/nightthemeswitcher/preferences/ui/TimeChooser.ui',
InternalChildren: ['hours', 'minutes'],
Properties: {
time: GObject.ParamSpec.double(
'time',
'Time',
'The time of the chooser',
GObject.ParamFlags.READWRITE,
0,
24,
0
),
},
}, class TimeChooser extends Gtk.Box {
export class TimeChooser extends Gtk.Box {
static {
GObject.registerClass({
GTypeName: 'TimeChooser',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/TimeChooser.ui',
InternalChildren: ['hours', 'minutes'],
Properties: {
time: GObject.ParamSpec.double(
'time',
'Time',
'The time of the chooser',
GObject.ParamFlags.READWRITE,
0,
24,
0
),
},
}, this);
}
onTimeChanged(chooser) {
const hours = Math.trunc(chooser.time);
const minutes = Math.round((chooser.time - hours) * 60);
@@ -40,4 +42,4 @@ var TimeChooser = GObject.registerClass({
spin.text = spin.value.toString().padStart(2, '0');
return true;
}
});
}