Add support for Materia-compact and Arc themes, watch theme changes
This commit is contained in:
+58
-33
@@ -20,44 +20,13 @@ this program. If not, see <http s ://www.gnu.org/licenses/>.
|
||||
|
||||
const Gio = imports.gi.Gio;
|
||||
|
||||
let interface_settings;
|
||||
let interface_settings, interface_settings_connect_id;
|
||||
let nightlight_active;
|
||||
let original_user_theme, user_theme_day, user_theme_night;
|
||||
let conn, proxy, proxy_connect_id;
|
||||
|
||||
|
||||
function _get_theme() {
|
||||
return interface_settings.get_string('gtk-theme');
|
||||
}
|
||||
|
||||
function _set_theme(theme) {
|
||||
interface_settings.set_string('gtk-theme', theme);
|
||||
}
|
||||
|
||||
function _apply_theme_variant() {
|
||||
const variant = proxy.get_cached_property('NightLightActive');
|
||||
if ( variant.get_boolean() === nightlight_active ) return;
|
||||
nightlight_active = variant.get_boolean();
|
||||
_set_theme(nightlight_active ? user_theme_night : user_theme_day);
|
||||
}
|
||||
|
||||
function init() {}
|
||||
|
||||
function enable() {
|
||||
// Store the current user theme and build day and night variants
|
||||
interface_settings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
|
||||
original_user_theme = _get_theme();
|
||||
if ( original_user_theme.includes('HighContrast') ) {
|
||||
user_theme_day = 'HighContrast';
|
||||
user_theme_night = 'HighContrastInverse';
|
||||
}
|
||||
else {
|
||||
user_theme_day = original_user_theme.replace(/(-dark)?/gi, '');
|
||||
user_theme_night = user_theme_day.replace(/(-light)?(-darker)?/gi, '') + '-dark';
|
||||
}
|
||||
|
||||
// Connect to session bus, listen to Color changes and change theme variant
|
||||
try {
|
||||
function _connect_dbus_proxy() {
|
||||
conn = Gio.bus_get_sync(Gio.BusType.SESSION, null);
|
||||
if ( conn === null ) {
|
||||
throw new Error('Unable to connect to the session bus');
|
||||
@@ -74,22 +43,78 @@ function enable() {
|
||||
if ( proxy === null ) {
|
||||
throw new Error('Unable to create proxy to the session bus');
|
||||
}
|
||||
}
|
||||
|
||||
function _get_theme() {
|
||||
return interface_settings.get_string('gtk-theme');
|
||||
}
|
||||
|
||||
function _set_theme(theme) {
|
||||
interface_settings.set_string('gtk-theme', theme);
|
||||
}
|
||||
|
||||
function _build_theme_variants() {
|
||||
if ( original_user_theme.includes('HighContrast') ) {
|
||||
user_theme_day = 'HighContrast';
|
||||
user_theme_night = 'HighContrastInverse';
|
||||
}
|
||||
else if ( original_user_theme.match(/Materia.*-compact/g) ) {
|
||||
user_theme_day = original_user_theme.replace('-dark', '');
|
||||
user_theme_night = user_theme_day.replace(/(-light)?-compact/g, '-dark-compact');
|
||||
}
|
||||
else if ( original_user_theme.includes('Arc') ) {
|
||||
user_theme_day = original_user_theme.replace('-Dark', '');
|
||||
user_theme_night = user_theme_day.replace('-Darker', '') + '-Dark';
|
||||
}
|
||||
else {
|
||||
user_theme_day = original_user_theme.replace('-dark', '');
|
||||
user_theme_night = user_theme_day.replace(/(-light)?(-darker)?/g, '') + '-dark';
|
||||
}
|
||||
}
|
||||
|
||||
function _apply_theme_variant() {
|
||||
const variant = proxy.get_cached_property('NightLightActive');
|
||||
if ( variant.get_boolean() === nightlight_active ) return;
|
||||
nightlight_active = variant.get_boolean();
|
||||
_set_theme(nightlight_active ? user_theme_night : user_theme_day);
|
||||
}
|
||||
|
||||
function init() {}
|
||||
|
||||
function enable() {
|
||||
// Store the current user theme and build day and night variants
|
||||
interface_settings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
|
||||
original_user_theme = _get_theme();
|
||||
_build_theme_variants();
|
||||
|
||||
// Connect to session bus, listen to Color changes and change theme variant
|
||||
try {
|
||||
_connect_dbus_proxy();
|
||||
}
|
||||
catch(e) {
|
||||
logError(e);
|
||||
}
|
||||
proxy_connect_id = proxy.connect('g-properties-changed', _apply_theme_variant);
|
||||
_apply_theme_variant();
|
||||
|
||||
// Listen to theme change
|
||||
interface_settings_connect_id = interface_settings.connect('changed::gtk-theme', () => {
|
||||
original_user_theme = _get_theme();
|
||||
_build_theme_variants();
|
||||
_apply_theme_variant();
|
||||
});
|
||||
}
|
||||
|
||||
function disable() {
|
||||
if ( proxy && proxy_connect_id ) {
|
||||
proxy.disconnect(proxy_connect_id);
|
||||
}
|
||||
interface_settings.disconnect(interface_settings_connect_id);
|
||||
|
||||
_set_theme(original_user_theme);
|
||||
|
||||
interface_settings = null;
|
||||
interface_settings_connect_id = null;
|
||||
nightlight_active = null;
|
||||
original_user_theme = null;
|
||||
user_theme_day = null;
|
||||
|
||||
Reference in New Issue
Block a user