-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathextension.js
136 lines (115 loc) · 4.53 KB
/
extension.js
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
129
130
131
132
133
134
135
136
/* extension.js
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/* exported init */
"use strict";
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import * as QuickSettings from 'resource:///org/gnome/shell/ui/quickSettings.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
// This is the live instance of the Quick Settings menu
const QuickSettingsMenu = Main.panel.statusArea.quickSettings;
class KbdBrightnessProxy {
constructor(callback) {
const BrightnessProxy = Gio.DBusProxy.makeProxyWrapper(`
<node>
<interface name="org.freedesktop.UPower.KbdBacklight">
<method name="SetBrightness">
<arg name="value" type="i" direction="in"/>
</method>
<method name="GetBrightness">
<arg name="value" type="i" direction="out"/>
</method>
<method name="GetMaxBrightness">
<arg name="value" type="i" direction="out"/>
</method>
<signal name="BrightnessChanged">
<arg type="i"/>
</signal>
</interface>
</node>`);
this._proxy = new BrightnessProxy(
Gio.DBus.system,
'org.freedesktop.UPower',
'/org/freedesktop/UPower/KbdBacklight',
callback);
}
get Brightness() {
return this._proxy.GetBrightnessSync() / this.getMaxBrightness();
}
set Brightness(value) {
const brightness = Math.round(value * this.getMaxBrightness());
log(`Setting brightness to ${brightness}`);
this._proxy.SetBrightnessSync(brightness);
}
getMaxBrightness() {
return this._proxy.GetMaxBrightnessSync();
}
}
const FeatureSlider = GObject.registerClass(
class FeatureSlider extends QuickSettings.QuickSlider {
_init() {
super._init({
iconName: 'keyboard-brightness-symbolic',
});
this._sliderChangedId = this.slider.connect('notify::value',
this._onSliderChanged.bind(this));
this.slider.accessible_name = 'Keyboard Brightness';
// create instance of KbpBrightnessProxy
this._proxy = new KbdBrightnessProxy((proxy, error) => {
if (error) throw error;
proxy.connectSignal('BrightnessChanged', this._sync.bind(this));
this._sync();
});
}
_onSliderChanged() {
this._proxy.Brightness = this.slider.value;
this.lastChange = Date.now();
}
_changeSlider(value) {
this.slider.block_signal_handler(this._sliderChangedId);
this.slider.value = value;
this.slider.unblock_signal_handler(this._sliderChangedId);
}
_sync() {
if (this.changeSliderTimeout) clearTimeout(this.changeSliderTimeout);
let dt = this.lastChange + 1000 - Date.now();
if (dt < 0) dt = 0;
this.changeSliderTimeout = setTimeout(_ => {
this.changeSliderTimeout = null;
this._changeSlider(this._proxy.Brightness)
}, dt);
}
destroy() {
if (this.changeSliderTimeout) clearTimeout(this.changeSliderTimeout);
this.menu.destroy();
super.destroy();
}
});
export default class KeyboardBacklightExtension extends Extension {
enable() {
this._indicator = new QuickSettings.SystemIndicator();
this._indicator.quickSettingsItems.push(new FeatureSlider());
QuickSettingsMenu.addExternalIndicator(this._indicator, 2);
}
disable() {
this._indicator.quickSettingsItems.forEach(item => item.destroy());
this._indicator.destroy();
this._indicator = null;
}
}