[themes] Add state-dependent themeing

Module themes (only!) can now contain state-specific theme information -
for example, the "battery" module has different states for charging and
discharging, and those can have different prefix and postfix
configurations to indicate what is going on.
This commit is contained in:
Tobias Witek 2016-10-31 07:34:43 +01:00
parent 3ca53dd0fa
commit 97273b5a41
3 changed files with 22 additions and 3 deletions

View file

@ -11,9 +11,14 @@ class Module(bumblebee.module.Module):
def data(self): def data(self):
with open("/sys/class/power_supply/%s/capacity" % self._battery) as f: with open("/sys/class/power_supply/%s/capacity" % self._battery) as f:
self._capacity = int(f.read()) self._capacity = int(f.read())
with open("/sys/class/power_supply/%s/status" % self._battery) as f:
self._status = f.read().strip()
return "%02d%%" % self._capacity return "%02d%%" % self._capacity
def state(self):
with open("/sys/class/power_supply/%s/status" % self._battery) as f:
self._status = f.read().strip()
if self._status == "Discharging":
return "discharging"
else:
return "charging"
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -17,6 +17,12 @@ class Theme:
value = self._defaults.get(key, value) value = self._defaults.get(key, value)
value = module_theme.get(key, value) value = module_theme.get(key, value)
if hasattr(obj, "state"):
state = getattr(obj, "state")()
state_theme = module_theme.get("states", {}).get(state, {})
value = state_theme.get(key, value)
return value return value
def prefix(self, obj): def prefix(self, obj):

View file

@ -4,7 +4,15 @@
"suffix" : " " "suffix" : " "
}, },
"battery": { "battery": {
"prefix": "bat " "prefix": "bat ",
"states": {
"charging": {
"suffix": "+"
},
"discharging": {
"suffix": "-"
}
}
}, },
"time": { "time": {
} }