forked from lovasoa/gnome-keyboard-backlight-menu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
169 lines (147 loc) · 5.62 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* 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";
const { Gio, GLib, GObject, St } = imports.gi;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Slider = imports.ui.slider;
const Main = imports.ui.main;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Gettext = imports.gettext;
const Domain = Gettext.domain(Me.metadata.uuid);
const _ = Domain.gettext;
function setTimeout(func, delay, ...args) {
return GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, () => {
func(...args);
return GLib.SOURCE_REMOVE;
});
};
function clearTimeout(timeout) { GLib.source_remove(timeout); };
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 Indicator = GObject.registerClass(
class Indicator extends PanelMenu.SystemIndicator {
_init() {
super._init();
this._proxy = new KbdBrightnessProxy((proxy, error) => {
if (error) throw error;
proxy.connectSignal('BrightnessChanged', this._sync.bind(this));
this._sync();
});
this._item = new PopupMenu.PopupBaseMenuItem({ activate: false });
this.menu.addMenuItem(this._item);
this._slider = new Slider.Slider(0);
this._sliderChangedId = this._slider.connect('notify::value',
this._sliderChanged.bind(this));
this._slider.accessible_name = _("Keyboard brightness");
let icon = new St.Icon({
icon_name: 'keyboard-brightness-symbolic',
style_class: 'popup-menu-icon'
});
this._item.add(icon);
this._item.add_child(this._slider);
this._item.connect('button-press-event', (actor, event) => {
return this._slider.startDragging(event);
});
this._item.connect('key-press-event', (actor, event) => {
return this._slider.emit('key-press-event', event);
});
this._item.connect('scroll-event', (actor, event) => {
return this._slider.emit('scroll-event', event);
});
this.lastChange = Date.now();
this.changeSliderTimeout = null;
}
_sliderChanged() {
this.lastChange = Date.now();
this._proxy.Brightness = this._slider.value;
}
_changeSlider(value) {
this._slider.block_signal_handler(this._sliderChangedId);
this._slider.value = value;
this._slider.unblock_signal_handler(this._sliderChangedId);
}
_sync() {
let visible = this._proxy.Brightness >= 0;
this._item.visible = visible;
if (visible) {
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();
}
});
var _indicator;
function init() {
log(`initializing ${Me.metadata.name}`);
ExtensionUtils.initTranslations(Me.metadata.uuid);
}
function enable() {
_indicator = new Indicator();
Main.panel.statusArea.aggregateMenu.menu.addMenuItem(this._indicator.menu, 2);
}
function disable() {
_indicator.destroy();
_indicator = null;
}