[PATCH] support battery-charge-state-dependent battery frame icon

Martin Dengler martin at martindengler.com
Wed Apr 16 14:25:45 EDT 2008


Support battery-charge-state-dependent battery frame icon and upgrade to consistency with battery icon design from http://wiki.laptop.org/go/Designs/Frame#06.

Controversially (or so I think), this commit includes a very naive algorithm to calculate battery time/life remaining, as a principled approach is much more complex and this approach is better than what a naive human would do (e.g., only misleading to those who should be coming up with better patches :)).  The code is very localized and self-contained so it's easy to rip out later.
---
 src/view/devices/battery.py |   51 +++++++++++++++++++++++++++++++-----------
 1 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/src/view/devices/battery.py b/src/view/devices/battery.py
index 8a4caf0..eefe631 100644
--- a/src/view/devices/battery.py
+++ b/src/view/devices/battery.py
@@ -19,9 +19,11 @@ from gettext import gettext as _
 import gtk
 
 from sugar import profile
+from sugar.graphics import style
 from sugar.graphics.icon import get_icon_state
 from sugar.graphics.tray import TrayIcon
 from sugar.graphics.palette import Palette
+from sugar.graphics.xocolor import XoColor
 
 from view.frame.frameinvoker import FrameWidgetInvoker
 
@@ -37,7 +39,7 @@ class DeviceView(TrayIcon):
                           xo_color=profile.get_color())
 
         self._model = model
-        self.palette = BatteryPalette(_('My Battery life'))
+        self.palette = BatteryPalette(_('My Battery'))
         self.set_palette(self.palette)
         self.palette.props.invoker = FrameWidgetInvoker(self)
         self.palette.set_group_id('frame')
@@ -48,21 +50,24 @@ class DeviceView(TrayIcon):
         self._update_info()
 
     def _update_info(self):
-        name = get_icon_state(_ICON_NAME, self._model.props.level)
-        self.icon.props.icon_name = name
+        name = _ICON_NAME
+        level = self._model.props.level
+        xo_color = profile.get_color()
 
-        # Update palette
         if self._model.props.charging:
             status = _STATUS_CHARGING
-            self.icon.props.badge_name = 'emblem-charging'
+            name += '-charging'
+            xo_color = XoColor('%s,%s' % (style.COLOR_WHITE.get_svg(),
+                                          style.COLOR_WHITE.get_svg()))
         elif self._model.props.discharging:
             status = _STATUS_DISCHARGING
-            self.icon.props.badge_name = None
         else:
             status = _STATUS_FULLY_CHARGED
-            self.icon.props.badge_name = None
 
-        self.palette.set_level(self._model.props.level)
+        self.icon.props.icon_name = get_icon_state(name, level)
+        self.icon.props.xo_color = xo_color
+
+        self.palette.set_level(level)
         self.palette.set_status(status)
 
     def _battery_status_changed_cb(self, pspec, param):
@@ -92,13 +97,31 @@ class BatteryPalette(Palette):
         self._progress_bar.set_fraction(fraction)
 
     def set_status(self, status):
-        percent_string = ' (%s%%)' % self._level
+        lvl = self._level
+        secondary_text = ''
+        status_text = '%s%%' % lvl
 
         if status == _STATUS_CHARGING:
-            charge_text = _('Battery charging') + percent_string
+            secondary_text = _('Charging')
         elif status == _STATUS_DISCHARGING:
-            charge_text = _('Battery discharging') + percent_string
-        elif status == _STATUS_FULLY_CHARGED:
-            charge_text = _('Battery fully charged')
+            if lvl <= 15:
+                secondary_text = _('Very little power remaining')
+            else:
+                minutes = _('m')
+                hours = _('h')
+                #TODO: make this less of an wild/educated guess
+                minutes_left = int(lvl / 0.59)
+                if minutes_left < 60:
+                    guess_text = '%s%s' % (minutes_left, minutes)
+                else:
+                    hours_left = minutes_left / 60
+                    mins_leftover = minutes_left % 60
+                    guess_text = '%s%s%s%s' % (hours_left, hours,
+                                               mins_leftover, minutes)
+                secondary_text = guess_text + _(' remaining')
+        else:
+            secondary_text = _('Charged')
+            status_text = ''
 
-        self._status_label.set_text(charge_text)
+        self.props.secondary_text = secondary_text
+        self._status_label.set_text(status_text)
-- 
1.5.4.1




More information about the Devel mailing list