Files
Just Perfection 762ec75601 user-theme: Extensions review guidelines compatibility
Extensions review guidelines enforces extensions to
don't create objects in the constructor of the class
that init() returns. so creating settings object in enable()
can make the extension compatible with the ego review guidelines.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell-extensions/-/merge_requests/197>
2021-11-06 21:58:59 +00:00

62 lines
1.6 KiB
JavaScript

// -*- mode: js2; indent-tabs-mode: nil; js2-basic-offset: 4 -*-
// Load shell theme from ~/.local/share/themes/name/gnome-shell
/* exported init */
const { Gio } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Main = imports.ui.main;
const Me = ExtensionUtils.getCurrentExtension();
const Util = Me.imports.util;
const SETTINGS_KEY = 'name';
class ThemeManager {
enable() {
this._settings = ExtensionUtils.getSettings();
this._settings.connect(`changed::${SETTINGS_KEY}`, this._changeTheme.bind(this));
this._changeTheme();
}
disable() {
this._settings?.run_dispose();
this._settings = null;
Main.setThemeStylesheet(null);
Main.loadTheme();
}
_changeTheme() {
let stylesheet = null;
let themeName = this._settings.get_string(SETTINGS_KEY);
if (themeName) {
const stylesheetPaths = Util.getThemeDirs()
.map(dir => `${dir}/${themeName}/gnome-shell/gnome-shell.css`);
stylesheetPaths.push(...Util.getModeThemeDirs()
.map(dir => `${dir}/${themeName}.css`));
stylesheet = stylesheetPaths.find(path => {
let file = Gio.file_new_for_path(path);
return file.query_exists(null);
});
}
if (stylesheet)
global.log(`loading user theme: ${stylesheet}`);
else
global.log('loading default theme (Adwaita)');
Main.setThemeStylesheet(stylesheet);
Main.loadTheme();
}
}
/**
* @returns {ThemeManager} - the extension state object
*/
function init() {
return new ThemeManager();
}