Skip to content

Commit

Permalink
Merge pull request linuxmint#588 from izacus/master
Browse files Browse the repository at this point in the history
Battery time remaining display on battery applet
  • Loading branch information
clefebvre committed Apr 25, 2012
2 parents 4f4324e + 2e2efc5 commit 4aaf04d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
9 changes: 9 additions & 0 deletions data/org.cinnamon.gschema.xml
Expand Up @@ -245,6 +245,7 @@
<child schema="org.cinnamon.theme" name="theme"/>
<child schema="org.cinnamon.recorder" name="recorder"/>
<child schema="org.cinnamon.keyboard" name="keyboard"/>
<child schema="org.cinnamon.power" name="power" />
</schema>

<schema path="/org/cinnamon/theme/" id="org.cinnamon.theme" gettext-domain="@GETTEXT_PACKAGE@">
Expand All @@ -263,6 +264,14 @@
</key>
</schema>

<schema path="/org/cinnamon/power/" id="org.cinnamon.power" gettext-domain="@GETTEXT_PACKAGE">
<key type="s" name="power-label">
<default>'percent'</default>
<summary>Display percentage in power applet</summary>
<description>Display battery remaining as percentage or time in power applet.</description>
</key>
</schema>

<schema path="/org/cinnamon/calendar/" id="org.cinnamon.calendar" gettext-domain="@GETTEXT_PACKAGE@">

<key type="b" name="show-weekdate">
Expand Down
86 changes: 78 additions & 8 deletions files/usr/share/cinnamon/applets/power@cinnamon.org/applet.js
Expand Up @@ -5,6 +5,8 @@ const Lang = imports.lang;
const St = imports.gi.St;
const PopupMenu = imports.ui.popupMenu;

const POWER_SCHEMA = "org.cinnamon.power"
const SHOW_PERCENTAGE_KEY = "power-label";
const BUS_NAME = 'org.gnome.SettingsDaemon';
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Power';

Expand Down Expand Up @@ -33,6 +35,12 @@ const UPDeviceState = {
PENDING_DISCHARGE: 6
};

const LabelDisplay = {
NONE: 'none',
PERCENT: 'percent',
TIME: 'time'
};

const PowerManagerInterface = {
name: 'org.gnome.SettingsDaemon.Power',
methods: [
Expand Down Expand Up @@ -109,6 +117,7 @@ function MyApplet(orientation) {
this._init(orientation);
}


MyApplet.prototype = {
__proto__: Applet.TextIconApplet.prototype,

Expand All @@ -122,7 +131,7 @@ MyApplet.prototype = {

this.set_applet_icon_symbolic_name('battery-missing');
this._proxy = new PowerManagerProxy(DBus.session, BUS_NAME, OBJECT_PATH);

let icon = this.actor.get_children()[0];
this.actor.remove_actor(icon);
let box = new St.BoxLayout({ name: 'batteryBox' });
Expand All @@ -136,6 +145,13 @@ MyApplet.prototype = {
this._deviceItems = [ ];
this._hasPrimary = false;
this._primaryDeviceId = null;

let settings = new Gio.Settings({ schema: POWER_SCHEMA });
this._labelDisplay = settings.get_string(SHOW_PERCENTAGE_KEY);
let applet = this;
settings.connect('changed::'+SHOW_PERCENTAGE_KEY, function() {
applet._switchLabelDisplay(settings.get_string(SHOW_PERCENTAGE_KEY));
});

this._batteryItem = new PopupMenu.PopupMenuItem('', { reactive: false });
this._primaryPercentage = new St.Label();
Expand All @@ -144,6 +160,26 @@ MyApplet.prototype = {

this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._otherDevicePosition = 2;

// Setup label display settings
this._displayItem = new PopupMenu.PopupSubMenuMenuItem(_("Display"));
this.menu.addMenuItem(this._displayItem);
this._displayPercentageItem = new PopupMenu.PopupMenuItem(_("Show percentage"));
this._displayPercentageItem.connect('activate', Lang.bind(this, function() {
settings.set_string(SHOW_PERCENTAGE_KEY, LabelDisplay.PERCENT);
}));
this._displayItem.menu.addMenuItem(this._displayPercentageItem);
this._displayTimeItem = new PopupMenu.PopupMenuItem(_("Show time remaining"));
this._displayTimeItem.connect('activate', Lang.bind(this, function() {
settings.set_string(SHOW_PERCENTAGE_KEY, LabelDisplay.TIME);
}));
this._displayItem.menu.addMenuItem(this._displayTimeItem);
this._displayNoneItem = new PopupMenu.PopupMenuItem(_("Hide label"));
this._displayNoneItem.connect('activate', Lang.bind(this, function() {
settings.set_string(SHOW_PERCENTAGE_KEY, LabelDisplay.NONE);
}));
this._displayItem.menu.addMenuItem(this._displayNoneItem);
this._switchLabelDisplay(this._labelDisplay);

this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this.menu.addSettingsAction(_("Power Settings"), 'gnome-power-panel.desktop');
Expand All @@ -159,6 +195,26 @@ MyApplet.prototype = {
on_applet_clicked: function(event) {
this.menu.toggle();
},

_switchLabelDisplay: function(display) {
this._labelDisplay = display;

this._displayPercentageItem.setShowDot(false);
this._displayNoneItem.setShowDot(false);
this._displayTimeItem.setShowDot(false);

if (this._labelDisplay == LabelDisplay.PERCENT) {
this._displayPercentageItem.setShowDot(true);
}
else if (this._labelDisplay == LabelDisplay.TIME) {
this._displayTimeItem.setShowDot(true);
}
else {
this._displayNoneItem.setShowDot(true);
}

this._updateLabel();
},

_readPrimaryDevice: function() {
this._proxy.GetPrimaryDeviceRemote(Lang.bind(this, function(device, error) {
Expand Down Expand Up @@ -251,15 +307,29 @@ MyApplet.prototype = {
this._mainLabel.set_text("");
return;
}
for (let i = 0; i < devices.length; i++) {
let [device_id, device_type, icon, percentage, state, time] = devices[i];
if (device_type == UPDeviceType.BATTERY || device_id == this._primaryDeviceId) {
let percentageText = C_("percent of battery remaining", "%d%%").format(Math.round(percentage));
this._mainLabel.set_text(percentageText);
return;

if (this._labelDisplay != LabelDisplay.NONE) {
for (let i = 0; i < devices.length; i++) {
let [device_id, device_type, icon, percentage, state, time] = devices[i];
if (device_type == UPDeviceType.BATTERY || device_id == this._primaryDeviceId) {
let labelText = "";

if (this._labelDisplay == LabelDisplay.PERCENT || time == 0) {
labelText = C_("percent of battery remaining", "%d%%").format(Math.round(percentage));
}
else if (this._labelDisplay == LabelDisplay.TIME) {
let seconds = time / 60;
let minutes = Math.floor(seconds % 60);
let hours = Math.floor(seconds / 60);
labelText = C_("time of battery remaining", "%d:%02d").format(hours,minutes);
}

this._mainLabel.set_text(labelText);
return;
}
}
}
// no battery found... hot-unplugged?
// Display disabled or no battery found... hot-unplugged?
this._mainLabel.set_text("");
}));
}
Expand Down

0 comments on commit 4aaf04d

Please sign in to comment.