Project: Switch to Prettier for code formatting

This commit is contained in:
Romain Vigier
2026-07-20 13:05:11 +02:00
parent b4ff33e681
commit 1f514c023f
40 changed files with 4511 additions and 4119 deletions
+171 -165
View File
@@ -1,184 +1,190 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
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';
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";
import { gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
Gio._promisify(Gtk.FileDialog.prototype, 'open', 'open_finish');
import { gettext as _ } from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";
Gio._promisify(Gtk.FileDialog.prototype, "open", "open_finish");
export class BackgroundButton extends Gtk.Button {
#uri;
#uri;
static {
GObject.registerClass({
GTypeName: 'BackgroundButton',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/BackgroundButton.ui',
InternalChildren: ['file_dialog', '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);
}
static {
GObject.registerClass(
{
GTypeName: "BackgroundButton",
Template: "resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/BackgroundButton.ui",
InternalChildren: ["file_dialog", "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.#setupFileDialog();
}
constructor({ ...params } = {}) {
super(params);
this.#setupSize();
this.#setupDropTarget();
this.#setupFileDialog();
}
get uri() {
return this.#uri || null;
}
get uri() {
return this.#uri || null;
}
set uri(uri) {
if (uri === this.#uri)
return;
this.#uri = uri;
this.notify('uri');
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
this.#updateThumbnail();
return GLib.SOURCE_REMOVE;
});
}
set uri(uri) {
if (uri === this.#uri) return;
this.#uri = uri;
this.notify("uri");
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
this.#updateThumbnail();
return GLib.SOURCE_REMOVE;
});
}
#setupSize() {
const display = Gdk.Display.get_default();
const monitor = display.get_monitors().get_item(0);
if (monitor.width_mm === 0 || monitor.height_mm === 0)
return;
if (monitor.width_mm > monitor.height_mm)
this.thumbHeight *= monitor.height_mm / monitor.width_mm;
else
this.thumbWidth *= monitor.width_mm / monitor.height_mm;
}
#setupSize() {
const display = Gdk.Display.get_default();
const monitor = display.get_monitors().get_item(0);
if (monitor.width_mm === 0 || monitor.height_mm === 0) return;
if (monitor.width_mm > monitor.height_mm) this.thumbHeight *= monitor.height_mm / monitor.width_mm;
else this.thumbWidth *= monitor.width_mm / monitor.height_mm;
}
#setupDropTarget() {
const dropTarget = Gtk.DropTarget.new(Gio.File.$gtype, Gdk.DragAction.COPY);
dropTarget.connect('drop', (_target, file, _x, _y) => {
const contentType = Gio.content_type_guess(file.get_basename(), null)[0];
if (this.#isContentTypeSupported(contentType)) {
this.uri = file.get_uri();
return true;
} else {
if (this.root instanceof Adw.PreferencesWindow) {
this.root.add_toast(new Adw.Toast({
title: _('This image format is not supported.'),
timeout: 10,
}));
}
return false;
}
});
this.add_controller(dropTarget);
}
#setupFileDialog() {
const filter = new Gtk.FileFilter();
filter.set_name(_('Image files'));
this.#getSupportedContentTypes().forEach(type => filter.add_mime_type(type));
this._file_dialog.filters = Gio.ListStore.new(Gtk.FileFilter);
this._file_dialog.filters.append(filter);
}
#getSupportedContentTypes() {
return GdkPixbuf.Pixbuf.get_formats().flatMap(format => format.get_mime_types()).concat('application/xml');
}
#isContentTypeSupported(contentType) {
for (const supportedContentType of this.#getSupportedContentTypes()) {
if (Gio.content_type_equals(contentType, supportedContentType))
return true;
#setupDropTarget() {
const dropTarget = Gtk.DropTarget.new(Gio.File.$gtype, Gdk.DragAction.COPY);
dropTarget.connect("drop", (_target, file, _x, _y) => {
const contentType = Gio.content_type_guess(file.get_basename(), null)[0];
if (this.#isContentTypeSupported(contentType)) {
this.uri = file.get_uri();
return true;
} else {
if (this.root instanceof Adw.PreferencesWindow) {
this.root.add_toast(
new Adw.Toast({
title: _("This image format is not supported."),
timeout: 10,
}),
);
}
return false;
}
});
this.add_controller(dropTarget);
}
#setupFileDialog() {
const filter = new Gtk.FileFilter();
filter.set_name(_("Image files"));
this.#getSupportedContentTypes().forEach((type) => filter.add_mime_type(type));
this._file_dialog.filters = Gio.ListStore.new(Gtk.FileFilter);
this._file_dialog.filters.append(filter);
}
#getSupportedContentTypes() {
return GdkPixbuf.Pixbuf.get_formats()
.flatMap((format) => format.get_mime_types())
.concat("application/xml");
}
#isContentTypeSupported(contentType) {
for (const supportedContentType of this.#getSupportedContentTypes()) {
if (Gio.content_type_equals(contentType, supportedContentType)) return true;
}
return false;
}
vfunc_mnemonic_activate() {
this.#setURIFromFileDialog();
}
onClicked(_button) {
this.#setURIFromFileDialog();
}
async #setURIFromFileDialog() {
try {
const file = await this._file_dialog.open(this.get_root(), null);
this.uri = file.get_uri();
} catch {}
}
#updateThumbnail() {
this._thumbnail.paintable = null;
if (!this.uri) return;
const file = Gio.File.new_for_uri(this.uri);
const contentType = Gio.content_type_guess(file.get_basename(), null)[0];
if (!this.#isContentTypeSupported(contentType)) return;
let path;
if (Gio.content_type_equals(contentType, "application/xml")) {
const decoder = new TextDecoder("utf-8");
const contents = decoder.decode(file.load_contents(null)[1]);
try {
path = contents.match(/<file>(.+)<\/file>/m)[1];
if (!this.#isContentTypeSupported(Gio.content_type_guess(path, null)[0])) throw new Error();
} catch (e) {
console.error(`No suitable background file found in ${file.get_path()}.\n${e}`);
return;
}
} else {
path = file.get_path();
}
vfunc_mnemonic_activate() {
this.#setURIFromFileDialog();
}
const pixbuf = GdkPixbuf.Pixbuf.new_from_file(path);
const scale =
pixbuf.width / pixbuf.height > this.thumbWidth / this.thumbHeight
? this.thumbHeight / pixbuf.height
: this.thumbWidth / pixbuf.width;
const thumbPixbuf = GdkPixbuf.Pixbuf.new(
pixbuf.colorspace,
pixbuf.has_alpha,
pixbuf.bits_per_sample,
this.thumbWidth,
this.thumbHeight,
);
pixbuf.scale(
thumbPixbuf,
0,
0,
this.thumbWidth,
this.thumbHeight,
-(pixbuf.width * scale - this.thumbWidth) / 2,
-(pixbuf.height * scale - this.thumbHeight) / 2,
scale,
scale,
GdkPixbuf.InterpType.TILES,
);
onClicked(_button) {
this.#setURIFromFileDialog();
}
async #setURIFromFileDialog() {
try {
const file = await this._file_dialog.open(this.get_root(), null);
this.uri = file.get_uri();
} catch {}
}
#updateThumbnail() {
this._thumbnail.paintable = null;
if (!this.uri)
return;
const file = Gio.File.new_for_uri(this.uri);
const contentType = Gio.content_type_guess(file.get_basename(), null)[0];
if (!this.#isContentTypeSupported(contentType))
return;
let path;
if (Gio.content_type_equals(contentType, 'application/xml')) {
const decoder = new TextDecoder('utf-8');
const contents = decoder.decode(file.load_contents(null)[1]);
try {
path = contents.match(/<file>(.+)<\/file>/m)[1];
if (!this.#isContentTypeSupported(Gio.content_type_guess(path, null)[0]))
throw new Error();
} catch (e) {
console.error(`No suitable background file found in ${file.get_path()}.\n${e}`);
return;
}
} else {
path = file.get_path();
}
const pixbuf = GdkPixbuf.Pixbuf.new_from_file(path);
const scale = pixbuf.width / pixbuf.height > this.thumbWidth / this.thumbHeight ? this.thumbHeight / pixbuf.height : this.thumbWidth / pixbuf.width;
const thumbPixbuf = GdkPixbuf.Pixbuf.new(pixbuf.colorspace, pixbuf.has_alpha, pixbuf.bits_per_sample, this.thumbWidth, this.thumbHeight);
pixbuf.scale(
thumbPixbuf,
0, 0,
this.thumbWidth, this.thumbHeight,
-(pixbuf.width * scale - this.thumbWidth) / 2, -(pixbuf.height * scale - this.thumbHeight) / 2,
scale, scale,
GdkPixbuf.InterpType.TILES
);
this._thumbnail.paintable = Gdk.Texture.new_for_pixbuf(thumbPixbuf);
}
this._thumbnail.paintable = Gdk.Texture.new_for_pixbuf(thumbPixbuf);
}
}
+19 -20
View File
@@ -1,28 +1,27 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import Adw from "gi://Adw";
import Gio from "gi://Gio";
import GObject from "gi://GObject";
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);
}
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' });
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);
}
settings.bind("picture-uri", this._day_button, "uri", Gio.SettingsBindFlags.DEFAULT);
settings.bind("picture-uri-dark", this._night_button, "uri", Gio.SettingsBindFlags.DEFAULT);
}
}
+19 -21
View File
@@ -1,29 +1,27 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import Adw from "gi://Adw";
import Gio from "gi://Gio";
import GObject from "gi://GObject";
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);
}
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);
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);
}
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);
}
}
+11 -9
View File
@@ -1,15 +1,17 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
import Adw from 'gi://Adw';
import GObject from 'gi://GObject';
import Adw from "gi://Adw";
import GObject from "gi://GObject";
export class ContributePage extends Adw.PreferencesPage {
static {
GObject.registerClass({
GTypeName: 'ContributePage',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/ContributePage.ui',
}, this);
}
static {
GObject.registerClass(
{
GTypeName: "ContributePage",
Template: "resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/ContributePage.ui",
},
this,
);
}
}
+35 -33
View File
@@ -1,45 +1,47 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import Adw from "gi://Adw";
import Gio from "gi://Gio";
import GObject from "gi://GObject";
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',
'schedule_offset_spin_button',
'fullscreen_transition_switch',
],
}, this);
}
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",
"schedule_offset_spin_button",
"fullscreen_transition_switch",
],
},
this,
);
}
constructor({ settings, ...params } = {}) {
super(params);
constructor({ settings, ...params } = {}) {
super(params);
settings.bind('manual-schedule', this._manual_schedule_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind("manual-schedule", this._manual_schedule_switch, "active", Gio.SettingsBindFlags.DEFAULT);
settings.bind('sunrise', this._schedule_sunrise_time_chooser, 'time', Gio.SettingsBindFlags.DEFAULT);
settings.bind('sunset', this._schedule_sunset_time_chooser, 'time', Gio.SettingsBindFlags.DEFAULT);
settings.bind("sunrise", this._schedule_sunrise_time_chooser, "time", Gio.SettingsBindFlags.DEFAULT);
settings.bind("sunset", this._schedule_sunset_time_chooser, "time", Gio.SettingsBindFlags.DEFAULT);
settings.bind('offset', this._schedule_offset_spin_button, 'value', Gio.SettingsBindFlags.DEFAULT);
settings.bind("offset", this._schedule_offset_spin_button, "value", Gio.SettingsBindFlags.DEFAULT);
settings.bind('fullscreen-transition', this._fullscreen_transition_switch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind("fullscreen-transition", this._fullscreen_transition_switch, "active", Gio.SettingsBindFlags.DEFAULT);
settings.connect('changed::nightthemeswitcher-ondemand-keybinding', () => {
this._keyboard_shortcut_button.keybinding = settings.get_strv('nightthemeswitcher-ondemand-keybinding')[0];
});
this._keyboard_shortcut_button.connect('notify::keybinding', () => {
settings.set_strv('nightthemeswitcher-ondemand-keybinding', [this._keyboard_shortcut_button.keybinding]);
});
this._keyboard_shortcut_button.keybinding = settings.get_strv('nightthemeswitcher-ondemand-keybinding')[0];
}
settings.connect("changed::nightthemeswitcher-ondemand-keybinding", () => {
this._keyboard_shortcut_button.keybinding = settings.get_strv("nightthemeswitcher-ondemand-keybinding")[0];
});
this._keyboard_shortcut_button.connect("notify::keybinding", () => {
settings.set_strv("nightthemeswitcher-ondemand-keybinding", [this._keyboard_shortcut_button.keybinding]);
});
this._keyboard_shortcut_button.keybinding = settings.get_strv("nightthemeswitcher-ondemand-keybinding")[0];
}
}
+101 -114
View File
@@ -1,147 +1,134 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
import Gdk from 'gi://Gdk';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import Gdk from "gi://Gdk";
import GObject from "gi://GObject";
import Gtk from "gi://Gtk";
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();
}
activate() {
if (this.keybinding)
return this._change_button.activate();
else
return this._choose_button.activate();
}
openDialog() {
this._dialog.transient_for = this.get_root();
this._dialog.present();
}
onKeybindingChanged(button) {
button.visible_child_name = button.keybinding ? 'edit' : 'choose';
}
onChooseButtonClicked(_button) {
this.openDialog();
}
onChangeButtonClicked(_button) {
this.openDialog();
}
onClearButtonClicked(_button) {
this.keybinding = '';
}
onKeyPressed(_widget, keyval, keycode, state) {
let mask = state & Gtk.accelerator_get_default_mod_mask();
mask &= ~Gdk.ModifierType.LOCK_MASK;
if (mask === 0 && keyval === Gdk.KEY_Escape) {
this._dialog.close();
return Gdk.EVENT_STOP;
}
if (
!isBindingValid({ mask, keycode, keyval }) ||
!isAccelValid({ mask, keyval })
)
return Gdk.EVENT_STOP;
this.keybinding = Gtk.accelerator_name_with_keycode(
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,
keyval,
keycode,
mask
);
this._dialog.close();
return Gdk.EVENT_STOP;
}
}
),
},
},
this,
);
}
vfunc_mnemonic_activate() {
this.activate();
}
activate() {
if (this.keybinding) return this._change_button.activate();
else return this._choose_button.activate();
}
openDialog() {
this._dialog.transient_for = this.get_root();
this._dialog.present();
}
onKeybindingChanged(button) {
button.visible_child_name = button.keybinding ? "edit" : "choose";
}
onChooseButtonClicked(_button) {
this.openDialog();
}
onChangeButtonClicked(_button) {
this.openDialog();
}
onClearButtonClicked(_button) {
this.keybinding = "";
}
onKeyPressed(_widget, keyval, keycode, state) {
let mask = state & Gtk.accelerator_get_default_mod_mask();
mask &= ~Gdk.ModifierType.LOCK_MASK;
if (mask === 0 && keyval === Gdk.KEY_Escape) {
this._dialog.close();
return Gdk.EVENT_STOP;
}
if (!isBindingValid({ mask, keycode, keyval }) || !isAccelValid({ mask, keyval })) return Gdk.EVENT_STOP;
this.keybinding = Gtk.accelerator_name_with_keycode(null, keyval, keycode, mask);
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);
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.ModifierType.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;
if ((mask === 0 || mask === Gdk.ModifierType.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);
return Gtk.accelerator_valid(keyval, mask) || (keyval === Gdk.KEY_Tab && mask !== 0);
}
+94 -78
View File
@@ -1,97 +1,113 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
import GDesktopEnums from 'gi://GDesktopEnums';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import GDesktopEnums from "gi://GDesktopEnums";
import Gio from "gi://Gio";
import GObject from "gi://GObject";
import Gtk from "gi://Gtk";
export class TimeChooser extends Gtk.Widget {
#clockFormat;
#interfaceSettings;
#clockFormat;
#interfaceSettings;
static {
GObject.registerClass({
GTypeName: 'TimeChooser',
Template: 'resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/TimeChooser.ui',
InternalChildren: ['clock_format_stack', 'hours_12', 'minutes_12', 'hours_24', 'minutes_24', 'am_toggle_button', 'pm_toggle_button'],
Properties: {
time: GObject.ParamSpec.double(
'time',
'Time',
'The time of the chooser',
GObject.ParamFlags.READWRITE,
0,
24,
0
),
},
}, this);
}
static {
GObject.registerClass(
{
GTypeName: "TimeChooser",
Template: "resource:///org/gnome/Shell/Extensions/nightthemeswitcher/preferences/ui/TimeChooser.ui",
InternalChildren: [
"clock_format_stack",
"hours_12",
"minutes_12",
"hours_24",
"minutes_24",
"am_toggle_button",
"pm_toggle_button",
],
Properties: {
time: GObject.ParamSpec.double(
"time",
"Time",
"The time of the chooser",
GObject.ParamFlags.READWRITE,
0,
24,
0,
),
},
},
this,
);
}
constructor({ ...params } = {}) {
super(params);
this.#interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
this.#interfaceSettings.connect('changed::clock-format', this.#onClockFormatChanged.bind(this));
this.#onClockFormatChanged();
}
constructor({ ...params } = {}) {
super(params);
this.#interfaceSettings = new Gio.Settings({ schema: "org.gnome.desktop.interface" });
this.#interfaceSettings.connect("changed::clock-format", this.#onClockFormatChanged.bind(this));
this.#onClockFormatChanged();
}
#onClockFormatChanged(_settings) {
this.#clockFormat = this.#interfaceSettings.get_enum('clock-format');
this.#syncClockFormatStack();
}
#onClockFormatChanged(_settings) {
this.#clockFormat = this.#interfaceSettings.get_enum("clock-format");
this.#syncClockFormatStack();
}
#syncClockFormatStack() {
this._clock_format_stack.set_visible_child_name(this.#clockFormat === GDesktopEnums.ClockFormat['12H'] ? '12h' : '24h');
}
#syncClockFormatStack() {
this._clock_format_stack.set_visible_child_name(
this.#clockFormat === GDesktopEnums.ClockFormat["12H"] ? "12h" : "24h",
);
}
#convertTimeTo24hFormat(time) {
const hours = Math.trunc(time);
const minutes = Math.round((time - hours) * 60);
return { hours, minutes };
}
#convertTimeTo24hFormat(time) {
const hours = Math.trunc(time);
const minutes = Math.round((time - hours) * 60);
return { hours, minutes };
}
#convertTimeTo12hFormat(time) {
const { hours: hours24, minutes } = this.#convertTimeTo24hFormat(time);
const hours = hours24 === 0 ? 12 : hours24 % 12;
const isPm = hours24 >= 12;
return { hours, minutes, isPm };
}
#convertTimeTo12hFormat(time) {
const { hours: hours24, minutes } = this.#convertTimeTo24hFormat(time);
const hours = hours24 === 0 ? 12 : hours24 % 12;
const isPm = hours24 >= 12;
return { hours, minutes, isPm };
}
#convert24hFormatToTime({ hours, minutes }) {
return hours + minutes / 60;
}
#convert24hFormatToTime({ hours, minutes }) {
return hours + minutes / 60;
}
#convert12hFormatToTime({ hours, minutes, isPm }) {
let hours24 = hours + Number(isPm) * 12;
if (hours === 12)
hours24 = isPm ? 12 : 0;
return this.#convert24hFormatToTime({ hours: hours24, minutes });
}
#convert12hFormatToTime({ hours, minutes, isPm }) {
let hours24 = hours + Number(isPm) * 12;
if (hours === 12) hours24 = isPm ? 12 : 0;
return this.#convert24hFormatToTime({ hours: hours24, minutes });
}
onTimeChanged(_chooser) {
const time = this.time;
onTimeChanged(_chooser) {
const time = this.time;
const clock12 = this.#convertTimeTo12hFormat(time);
this._hours_12.value = clock12.hours;
this._minutes_12.value = clock12.minutes;
this._am_toggle_button.active = !clock12.isPm;
this._pm_toggle_button.active = clock12.isPm;
const clock12 = this.#convertTimeTo12hFormat(time);
this._hours_12.value = clock12.hours;
this._minutes_12.value = clock12.minutes;
this._am_toggle_button.active = !clock12.isPm;
this._pm_toggle_button.active = clock12.isPm;
const clock24 = this.#convertTimeTo24hFormat(time);
this._hours_24.value = clock24.hours;
this._minutes_24.value = clock24.minutes;
}
const clock24 = this.#convertTimeTo24hFormat(time);
this._hours_24.value = clock24.hours;
this._minutes_24.value = clock24.minutes;
}
onValueChanged(_widget) {
this.time = this.#clockFormat === GDesktopEnums.ClockFormat['12H']
? this.#convert12hFormatToTime({ hours: this._hours_12.value, minutes: this._minutes_12.value, isPm: this._pm_toggle_button.active })
: this.#convert24hFormatToTime({ hours: this._hours_24.value, minutes: this._minutes_24.value });
}
onValueChanged(_widget) {
this.time =
this.#clockFormat === GDesktopEnums.ClockFormat["12H"]
? this.#convert12hFormatToTime({
hours: this._hours_12.value,
minutes: this._minutes_12.value,
isPm: this._pm_toggle_button.active,
})
: this.#convert24hFormatToTime({ hours: this._hours_24.value, minutes: this._minutes_24.value });
}
onOutputChanged(spin) {
spin.text = spin.value.toString().padStart(2, '0');
return true;
}
onOutputChanged(spin) {
spin.text = spin.value.toString().padStart(2, "0");
return true;
}
}