This commit is contained in:
2024-07-08 22:46:35 +02:00
parent 02f44c49d2
commit 27254d817a
56249 changed files with 808097 additions and 1 deletions

View File

@ -0,0 +1,183 @@
// 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 { gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
export class BackgroundButton extends Gtk.Button {
#uri;
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();
}
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;
});
}
#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);
}
#setupFileChooserFilter() {
this._filechooser.filter = new Gtk.FileFilter();
this._filechooser.filter.add_pixbuf_formats();
this._filechooser.filter.add_mime_type('application/xml');
}
#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.openFileChooser();
}
openFileChooser() {
this._filechooser.transient_for = this.get_root();
this._filechooser.show();
}
onFileChooserResponse(fileChooser, responseId) {
if (responseId !== Gtk.ResponseType.ACCEPT)
return;
this.uri = fileChooser.get_file().get_uri();
}
onClicked(_button) {
this.openFileChooser();
}
#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);
}
}

View File

@ -0,0 +1,28 @@
// 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';
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);
}
}

View File

@ -0,0 +1,29 @@
// 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';
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);
}
}

View File

@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
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);
}
}

View File

@ -0,0 +1,42 @@
// 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';
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',
'fullscreen_transition_switch',
],
}, this);
}
constructor({ settings, ...params } = {}) {
super(params);
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('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];
}
}

View File

@ -0,0 +1,147 @@
// 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';
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(
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);
}
/**
* 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);
}

View File

@ -0,0 +1,97 @@
// 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';
export class TimeChooser extends Gtk.Widget {
#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);
}
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();
}
#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 };
}
#convertTimeTo12hFormat(time) {
const { hours: hours24, minutes } = this.#convertTimeTo24hFormat(time);
const hours = hours24 % 12;
const isPm = hours24 > 12;
return { hours, minutes, isPm };
}
#convert24hFormatToTime({ hours, minutes }) {
return hours + minutes / 60;
}
#convert12hFormatToTime({ hours, minutes, isPm }) {
return this.#convert24hFormatToTime({
hours: hours + Number(isPm) * 12,
minutes,
});
}
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 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 });
}
onOutputChanged(spin) {
spin.text = spin.value.toString().padStart(2, '0');
return true;
}
}