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
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported Indicator */
const { Clutter, Gio, GObject, St, UPowerGlib: UPower } = imports.gi;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const { loadInterfaceXML } = imports.misc.fileUtils;
const BUS_NAME = 'org.freedesktop.UPower';
const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
const DisplayDeviceInterface = loadInterfaceXML('org.freedesktop.UPower.Device');
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
const SHOW_BATTERY_PERCENTAGE = 'show-battery-percentage';
var Indicator = GObject.registerClass(
class Indicator extends PanelMenu.SystemIndicator {
_init() {
super._init();
this._desktopSettings = new Gio.Settings({
schema_id: 'org.gnome.desktop.interface',
});
this._desktopSettings.connect(
'changed::%s'.format(SHOW_BATTERY_PERCENTAGE), this._sync.bind(this));
this._indicator = this._addIndicator();
this._percentageLabel = new St.Label({
y_expand: true,
y_align: Clutter.ActorAlign.CENTER,
});
this.add_child(this._percentageLabel);
this.add_style_class_name('power-status');
this._proxy = new PowerManagerProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
(proxy, error) => {
if (error) {
log(error.message);
} else {
this._proxy.connect('g-properties-changed',
this._sync.bind(this));
}
this._sync();
});
this._item = new PopupMenu.PopupSubMenuMenuItem('', true);
this._item.menu.addSettingsAction(_('Power Settings'),
'gnome-power-panel.desktop');
this.menu.addMenuItem(this._item);
Main.sessionMode.connect('updated', this._sessionUpdated.bind(this));
this._sessionUpdated();
}
_sessionUpdated() {
let sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
this.menu.setSensitive(sensitive);
}
_getStatus() {
let seconds = 0;
if (this._proxy.State === UPower.DeviceState.FULLY_CHARGED)
return _('Fully Charged');
else if (this._proxy.State === UPower.DeviceState.CHARGING)
seconds = this._proxy.TimeToFull;
else if (this._proxy.State === UPower.DeviceState.DISCHARGING)
seconds = this._proxy.TimeToEmpty;
else if (this._proxy.State === UPower.DeviceState.PENDING_CHARGE)
return _('Not Charging');
// state is PENDING_DISCHARGE
else
return _('Estimating…');
let time = Math.round(seconds / 60);
if (time === 0) {
// 0 is reported when UPower does not have enough data
// to estimate battery life
return _('Estimating…');
}
let minutes = time % 60;
let hours = Math.floor(time / 60);
if (this._proxy.State === UPower.DeviceState.DISCHARGING) {
// Translators: this is <hours>:<minutes> Remaining (<percentage>)
return _('%d\u2236%02d Remaining (%d\u2009%%)').format(
hours, minutes, this._proxy.Percentage);
}
if (this._proxy.State === UPower.DeviceState.CHARGING) {
// Translators: this is <hours>:<minutes> Until Full (<percentage>)
return _('%d\u2236%02d Until Full (%d\u2009%%)').format(
hours, minutes, this._proxy.Percentage);
}
return null;
}
_sync() {
// Do we have batteries or a UPS?
let visible = this._proxy.IsPresent;
if (visible) {
this._item.show();
this._percentageLabel.visible =
this._desktopSettings.get_boolean(SHOW_BATTERY_PERCENTAGE);
} else {
// If there's no battery, then we use the power icon.
this._item.hide();
this._indicator.icon_name = 'system-shutdown-symbolic';
this._percentageLabel.hide();
return;
}
// The icons
let chargingState = this._proxy.State === UPower.DeviceState.CHARGING
? '-charging' : '';
let fillLevel = 10 * Math.floor(this._proxy.Percentage / 10);
const charged =
this._proxy.State === UPower.DeviceState.FULLY_CHARGED ||
(this._proxy.State === UPower.DeviceState.CHARGING && fillLevel === 100);
const icon = charged
? 'battery-level-100-charged-symbolic'
: 'battery-level-%d%s-symbolic'.format(fillLevel, chargingState);
// Make sure we fall back to fallback-icon-name and not GThemedIcon's
// default fallbacks
let gicon = new Gio.ThemedIcon({
name: icon,
use_default_fallbacks: false,
});
this._indicator.gicon = gicon;
this._item.icon.gicon = gicon;
let fallbackIcon = this._proxy.IconName;
this._indicator.fallback_icon_name = fallbackIcon;
this._item.icon.fallback_icon_name = fallbackIcon;
// The icon label
let label;
if (this._proxy.State == UPower.DeviceState.FULLY_CHARGED)
label = _("%d\u2009%%").format(100);
else
label = _("%d\u2009%%").format(this._proxy.Percentage);
this._percentageLabel.text = label;
// The status label
this._item.label.text = this._getStatus();
}
});
|