Debian Codegraph @

extensions/user-theme/prefs.js

raw
1 // SPDX-FileCopyrightText: 2020 Florian Müllner <[email protected]>
2 //
3 // SPDX-License-Identifier: GPL-2.0-or-later
4
5 // we use async/await here to not block the mainloop, not to parallelize
6 /* eslint-disable no-await-in-loop */
7
8 import Adw from 'gi://Adw';
9 import Gio from 'gi://Gio';
10 import GLib from 'gi://GLib';
11 import GObject from 'gi://GObject';
12 import Gtk from 'gi://Gtk';
13
14 import {ExtensionPreferences} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
15
16 import {getThemeDirs, getModeThemeDirs} from './util.js';
17
18 Gio._promisify(Gio.File.prototype, 'enumerate_children_async');
19 Gio._promisify(Gio.File.prototype, 'query_info_async');
20 Gio._promisify(Gio.FileEnumerator.prototype, 'next_files_async');
21
22 class UserThemePrefsWidget extends Adw.PreferencesGroup {
23 static {
24 GObject.registerClass(this);
25 }
26
27 constructor(settings) {
28 super({title: 'Themes'});
29
30 this._actionGroup = new Gio.SimpleActionGroup();
31 this.insert_action_group('theme', this._actionGroup);
32
33 this._settings = settings;
34 this._actionGroup.add_action(
35 this._settings.create_action('name'));
36
37 this.connect('destroy', () => (this._settings = null));
38
39 this._rows = new Map();
40 this._addTheme(''); // default
41
42 this._collectThemes();
43 }
44
45 async _collectThemes() {
46 for (const dirName of getThemeDirs()) {
47 const dir = Gio.File.new_for_path(dirName);
48 for (const name of await this._enumerateDir(dir)) {
49 if (this._rows.has(name))
50 continue;
51
52 const file = dir.resolve_relative_path(
53 `${name}/gnome-shell/gnome-shell.css`);
54 try {
55 await file.query_info_async(
56 Gio.FILE_ATTRIBUTE_STANDARD_NAME,
57 Gio.FileQueryInfoFlags.NONE,
58 GLib.PRIORITY_DEFAULT, null);
59 this._addTheme(name);
60 } catch (e) {
61 if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
62 logError(e);
63 }
64 }
65 }
66
67 for (const dirName of getModeThemeDirs()) {
68 const dir = Gio.File.new_for_path(dirName);
69 for (const filename of await this._enumerateDir(dir)) {
70 if (!filename.endsWith('.css'))
71 continue;
72
73 const name = filename.slice(0, -4);
74 if (!this._rows.has(name))
75 this._addTheme(name);
76 }
77 }
78 }
79
80 _addTheme(name) {
81 const row = new ThemeRow(name);
82 this._rows.set(name, row);
83
84 this.add(row);
85 }
86
87 async _enumerateDir(dir) {
88 const fileInfos = [];
89 let fileEnum;
90
91 try {
92 fileEnum = await dir.enumerate_children_async(
93 Gio.FILE_ATTRIBUTE_STANDARD_NAME,
94 Gio.FileQueryInfoFlags.NONE,
95 GLib.PRIORITY_DEFAULT, null);
96 } catch (e) {
97 if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
98 logError(e);
99 return [];
100 }
101
102 let infos;
103 do {
104 infos = await fileEnum.next_files_async(100,
105 GLib.PRIORITY_DEFAULT, null);
106 fileInfos.push(...infos);
107 } while (infos.length > 0);
108
109 return fileInfos.map(info => info.get_name());
110 }
111 }
112
113 class ThemeRow extends Adw.ActionRow {
114 static {
115 GObject.registerClass(this);
116 }
117
118 constructor(name) {
119 const check = new Gtk.CheckButton({
120 action_name: 'theme.name',
121 action_target: new GLib.Variant('s', name),
122 });
123
124 super({
125 title: name || 'Default',
126 activatable_widget: check,
127 });
128 this.add_prefix(check);
129 }
130 }
131
132 export default class UserThemePrefs extends ExtensionPreferences {
133 getPreferencesWidget() {
134 return new UserThemePrefsWidget(this.getSettings());
135 }
136 }