Skip to content

Commit 52580fd

Browse files
committed
move options to their service instances
add icons config option
1 parent 5d94ad9 commit 52580fd

File tree

5 files changed

+54
-38
lines changed

5 files changed

+54
-38
lines changed

src/app.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,43 @@ import Utils from './utils.js';
99
import { timeout, readFileAsync } from './utils.js';
1010
import { loadInterfaceXML } from './utils.js';
1111

12+
function deprecated(config: Config) {
13+
const warning = (from: string, to: string) => console.warn(
14+
`${from} config option has been removed: use ${to} instead`);
15+
16+
if (config.notificationPopupTimeout !== undefined)
17+
warning('notificationPopupTimeout', 'Notifications.popupTimeout');
18+
19+
if (config.notificationForceTimeout !== undefined)
20+
warning('notificationForceTimeout', 'Notifications.forceTimeout');
21+
22+
if (config.cacheNotificationActions !== undefined)
23+
warning('cacheNotificationActions', 'Notifications.cacheActions');
24+
25+
if (config.cacheCoverArt !== undefined)
26+
warning('cacheCoverArt', 'Mpris.cacheCoverArt');
27+
28+
if (config.maxStreamVolume !== undefined)
29+
warning('cacheCoverArt', 'Audio.maxStreamVolume');
30+
}
31+
1232
const AgsIFace = (bus: string) =>
1333
loadInterfaceXML('com.github.Aylur.ags')?.replace('@BUS@', bus);
1434

1535
export interface Config<W extends Gtk.Window = Gtk.Window> {
1636
windows?: W[]
1737
style?: string
38+
icons?: string
39+
onWindowToggled?: (windowName: string, visible: boolean) => void
40+
onConfigParsed?: (app: App) => void
41+
closeWindowDelay: { [key: string]: number }
42+
43+
// FIXME: deprecated
1844
notificationPopupTimeout: number
1945
notificationForceTimeout: boolean
2046
cacheNotificationActions: boolean
2147
cacheCoverArt: boolean
22-
closeWindowDelay: { [key: string]: number }
2348
maxStreamVolume: number
24-
onWindowToggled?: (windowName: string, visible: boolean) => void,
25-
onConfigParsed?: (app: App) => void,
2649
}
2750

2851
export class App extends Gtk.Application {
@@ -34,19 +57,16 @@ export class App extends Gtk.Application {
3457
}
3558

3659
private _dbus!: Gio.DBusExportedObject;
37-
private _closeDelay!: { [key: string]: number };
60+
private _closeDelay!: Config['closeWindowDelay'];
3861
private _cssProviders: Gtk.CssProvider[] = [];
3962
private _objectPath!: string;
40-
4163
private _windows: Map<string, Gtk.Window> = new Map();
4264
private _configPath!: string;
4365
private _configDir!: string;
44-
private _config!: Config;
4566

46-
get windows() { return this._windows; }
67+
get windows() { return [...this._windows.values()]; }
4768
get configPath() { return this._configPath; }
4869
get configDir() { return this._configDir; }
49-
get config() { return this._config; }
5070

5171
resetCss() {
5272
const screen = Gdk.Screen.get_default();
@@ -190,36 +210,26 @@ export class App extends Gtk.Application {
190210

191211
private async _load() {
192212
try {
193-
const mod = await import(`file://${this.configPath}`);
194-
const config = mod.default as Config;
195-
config.closeWindowDelay ??= {};
196-
config.notificationPopupTimeout ??= 3000;
197-
config.notificationForceTimeout ??= false;
198-
config.maxStreamVolume ??= 1.5;
199-
config.cacheNotificationActions ??= false;
200-
config.cacheCoverArt ??= true;
201-
this._config = config;
202-
213+
const entry = await import(`file://${this.configPath}`);
214+
const config = entry.default as Config;
203215
if (!config) {
204216
console.error('Missing default export');
205-
this.emit('config-parsed');
206-
return;
217+
return this.emit('config-parsed');
207218
}
208219

209-
this._closeDelay = config.closeWindowDelay;
220+
// FIXME:
221+
deprecated(config);
222+
223+
this._closeDelay = config?.closeWindowDelay || {};
210224

211225
if (config.style) {
212226
this.applyCss(config.style.startsWith('.')
213227
? `${this.configDir}${config.style.slice(1)}`
214228
: config.style);
215229
}
216230

217-
if (config.windows && !Array.isArray(config.windows)) {
218-
console.error('windows attribute has to be an array, ' +
219-
`but it is a ${typeof config.windows}`);
220-
this.emit('config-parsed');
221-
return;
222-
}
231+
if (config.icons)
232+
Gtk.IconTheme.get_default().append_search_path(config.icons);
223233

224234
if (typeof config.onWindowToggled === 'function')
225235
this.connect('window-toggled', (_, n, v) => config.onWindowToggled!(n, v));

src/service/audio.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Service from '../service.js';
22
import GObject from 'gi://GObject';
33
import Gvc from 'gi://Gvc';
4-
import App from '../app.js';
54
import { bulkConnect, bulkDisconnect } from '../utils.js';
65

76
const _MIXER_CONTROL_STATE = {
@@ -89,8 +88,8 @@ export class Stream extends Service {
8988
}
9089

9190
set volume(value) { // 0..100
92-
if (value > (App.config.maxStreamVolume))
93-
value = (App.config.maxStreamVolume);
91+
if (value > (audio.maxStreamVolume))
92+
value = (audio.maxStreamVolume);
9493

9594
if (value < 0)
9695
value = 0;
@@ -123,6 +122,8 @@ export class Audio extends Service {
123122
});
124123
}
125124

125+
maxStreamVolume = 1.5;
126+
126127
private _control: Gvc.MixerControl;
127128
private _streams: Map<number, Stream>;
128129
private _streamBindings: Map<number, number>;

src/service/mpris.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import GLib from 'gi://GLib';
22
import Gio from 'gi://Gio';
3-
import App from '../app.js';
43
import Service from '../service.js';
54
import { ensureDirectory, idle } from '../utils.js';
65
import { CACHE_DIR } from '../utils.js';
@@ -178,7 +177,7 @@ export class MprisPlayer extends Service {
178177
}
179178

180179
private _cacheCoverArt() {
181-
if (!App.config.cacheCoverArt || this._trackCoverUrl === '')
180+
if (!mpris.cacheCoverArt || this._trackCoverUrl === '')
182181
return;
183182

184183
this._coverPath = MEDIA_CACHE_PATH + '/' +
@@ -276,6 +275,8 @@ export class Mpris extends Service {
276275
});
277276
}
278277

278+
cacheCoverArt = true;
279+
279280
private _proxy: DBusProxy;
280281
private _players: Map<string, MprisPlayer> = new Map;
281282

src/service/notifications.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Gio from 'gi://Gio';
22
import GdkPixbuf from 'gi://GdkPixbuf';
33
import GLib from 'gi://GLib';
44
import Service from '../service.js';
5-
import App from '../app.js';
65
import {
76
CACHE_DIR, ensureDirectory,
87
loadInterfaceXML, readFileAsync,
@@ -209,7 +208,7 @@ export class Notification extends Service {
209208
this.close();
210209
}
211210

212-
toJson(cacheActions = App.config.cacheNotificationActions): NotifcationJson {
211+
toJson(cacheActions = notifications.cacheActions): NotifcationJson {
213212
return {
214213
actionIcons: this._actionIcons,
215214
actions: cacheActions ? this._actions : [],
@@ -294,6 +293,10 @@ export class Notifications extends Service {
294293
});
295294
}
296295

296+
popupTimeout = 3000;
297+
forceTimeout = false;
298+
cacheActions = false;
299+
297300
private _dbus!: Gio.DBusExportedObject;
298301
private _notifications: Map<number, Notification>;
299302
private _dnd = false;
@@ -348,9 +351,9 @@ export class Notifications extends Service {
348351
const id = replacesId || this._idCount++;
349352
const n = new Notification(appName, id, appIcon, summary, body, acts, hints, !this.dnd);
350353

351-
if (App.config.notificationForceTimeout || expiration === -1) {
352-
n.timeout = App.config.notificationPopupTimeout;
353-
timeout(App.config.notificationPopupTimeout, () => this.DismissNotification(id));
354+
if (this.forceTimeout || expiration === -1) {
355+
n.timeout = this.popupTimeout;
356+
timeout(this.popupTimeout, () => this.DismissNotification(id));
354357
} else {
355358
n.timeout = expiration;
356359
if (expiration > 0)

src/utils/init.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ export async function init(configDir: string, entry: string) {
120120
conf.compilerOptions.typeRoots = [...list, './types'];
121121

122122
await writeFile(JSON.stringify(conf, null, 2), tsconfig);
123-
} finally {
123+
} catch (err) {
124+
logError(err);
124125
console.warn(tsconfigWarning);
125126
}
126127
}

0 commit comments

Comments
 (0)