Repository navigation
Expand file tree
/
Copy pathextension.js
More file actions
128 lines (109 loc) · 4.38 KB
/
Copy pathextension.js
File metadata and controls
128 lines (109 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import {QuickMenuToggle, SystemIndicator} from 'resource:///org/gnome/shell/ui/quickSettings.js';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import {Lamp, lampConfig} from './lib/lamp.js';
import {PRESETS, findPreset} from './lib/presets.js';
import {PresetBar} from './lib/presetBar.js';
import {setKeyboardBacklight} from './lib/keyboard.js';
// Don't repeat the same error notification more often than this, in ms.
const NOTIFY_INTERVAL = 30000;
function errorHint(error) {
const message = error.message ?? String(error);
if (message.includes('Failed to register advertisement'))
return 'The Bluetooth controller refused to advertise. Turn Bluetooth off and on again. Running the old lampify tool causes this.';
return message.replace(/^GDBus\.Error:[\w.]+:\s*/, '');
}
const LampToggle = GObject.registerClass(
class LampToggle extends QuickMenuToggle {
_init(extension) {
const gicon = Gio.icon_new_for_string(`${extension.path}/icons/lampify-symbolic.svg`);
super._init({title: 'Lamp', gicon, toggleMode: false});
this._settings = extension.getSettings();
this._lamp = new Lamp(() => lampConfig(this._settings));
this._lastNotify = {};
this.menu.setHeader(gicon, 'Lamp', 'Bluetooth LE lamp');
// Gradient bar with one stop per preset (lib/presetBar.js).
const row = new PopupMenu.PopupBaseMenuItem({reactive: false, can_focus: false});
this._presetBar = new PresetBar(PRESETS);
this._presetBar.connect('activated', (_bar, id) => this._applyPreset(findPreset(id)));
row.add_child(this._presetBar);
this.menu.addMenuItem(row);
this.checked = this._settings.get_boolean('power');
this.connect('clicked', () => this._setPower(!this.checked));
this._sync();
}
_preset() {
return findPreset(this._settings.get_string('preset'));
}
_setPower(on) {
this.checked = on;
this._settings.set_boolean('power', on);
this._sync();
this._run(on ? this._lamp.on() : this._lamp.off());
// Keyboard lights up when the lamp goes dark, and the other way round.
if (this._settings.get_boolean('keyboard-backlight'))
setKeyboardBacklight(!on).catch(logError);
}
_applyPreset(preset) {
// The lamp ignores colors while it's off, so switch it on first.
if (!this.checked)
this._setPower(true);
this._settings.set_string('preset', preset.id);
this._sync();
this._run(this._lamp.setColor(preset.brightness, preset.temperature));
}
_sync(error = null) {
const preset = this._preset();
this._presetBar.setSelected(preset?.id ?? null);
if (error)
this.subtitle = 'Failed to send';
else if (!this.checked)
this.subtitle = 'Off';
else
this.subtitle = preset?.name ?? 'On';
}
_run(promise) {
promise.then(() => this._sync(), error => {
if (this._destroyed)
return;
this._sync(error);
const hint = errorHint(error);
console.warn(`Lampify: ${error.message ?? error}`);
const now = GLib.get_monotonic_time() / 1000;
if (now - (this._lastNotify[hint] ?? -Infinity) > NOTIFY_INTERVAL) {
this._lastNotify[hint] = now;
Main.notify('Lampify', hint);
}
});
}
destroy() {
this._destroyed = true;
this._lamp.destroy();
super.destroy();
}
});
const LampIndicator = GObject.registerClass(
class LampIndicator extends SystemIndicator {
_init(extension) {
super._init();
this.quickSettingsItems.push(new LampToggle(extension));
}
destroy() {
this.quickSettingsItems.forEach(item => item.destroy());
super.destroy();
}
});
export default class LampifyExtension extends Extension {
enable() {
this._indicator = new LampIndicator(this);
Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator);
}
disable() {
this._indicator.destroy();
this._indicator = null;
}
}