forked from lovasoa/gnome-keyboard-backlight-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
111 lines (95 loc) · 3.74 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
/* 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;
}
});
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;
}
}