[core] make widget name an attribute

first, this fixes #607

also, i think it slightly simplifies code to make "simple" stuff like
names, etc. attributes instead of methods all the time.

so, expect this to be extended to other components, as well.
This commit is contained in:
tobi-wan-kenobi 2020-04-30 12:42:34 +02:00
parent 2cc463eb1a
commit 9cd9ff626d
7 changed files with 16 additions and 18 deletions

View file

@ -81,7 +81,7 @@ class Module(core.input.Object):
if not name: return self.widgets()[0] if not name: return self.widgets()[0]
for w in self.widgets(): for w in self.widgets():
if w.name() == name: return w if w.name == name: return w
return None return None
def state(self, widget): def state(self, widget):

View file

@ -7,10 +7,7 @@ class Widget(util.store.Store, core.input.Object):
super(Widget, self).__init__() super(Widget, self).__init__()
self.__full_text = full_text self.__full_text = full_text
self.__module = module self.__module = module
self.__name = name self.name = name
def name(self):
return self.__name
def full_text(self, value=None): def full_text(self, value=None):
if value: if value:

View file

@ -25,3 +25,4 @@
- bumblebee-ctrl - bumblebee-ctrl
- theme.exclude (battery) - theme.exclude (battery)
- help output - help output
- configuration files

View file

@ -38,9 +38,9 @@ class Module(core.module.Module):
self.__load_song() self.__load_song()
def state(self, widget): def state(self, widget):
if widget.name() == 'gpmdp.prev': if widget.name == 'gpmdp.prev':
return 'prev' return 'prev'
if widget.name() == 'gpmdp.next': if widget.name == 'gpmdp.next':
return 'next' return 'next'
return self.__status return self.__status

View file

@ -51,9 +51,9 @@ class Module(core.module.Module):
self._update_widgets(widgets) self._update_widgets(widgets)
def state(self, widget): def state(self, widget):
if 'traffic.rx' in widget.name(): if 'traffic.rx' in widget.name:
return 'rx' return 'rx'
if 'traffic.tx' in widget.name(): if 'traffic.tx' in widget.name:
return 'tx' return 'tx'
return self._status return self._status

View file

@ -124,28 +124,28 @@ class Module(core.module.Module):
widget.set('theme.exclude', 'suffix') widget.set('theme.exclude', 'suffix')
def capacity(self, widget): def capacity(self, widget):
if widget.name() == 'all-batteries': if widget.name == 'all-batteries':
capacity = self.__manager.capacity_all(self._batteries) capacity = self.__manager.capacity_all(self._batteries)
else: else:
capacity = self.__manager.capacity(widget.name()) capacity = self.__manager.capacity(widget.name)
widget.set('capacity', capacity) widget.set('capacity', capacity)
widget.set('ac', self.__manager.isac_any(self._batteries)) widget.set('ac', self.__manager.isac_any(self._batteries))
widget.set('theme.minwidth', '100%') widget.set('theme.minwidth', '100%')
# Read power conumption # Read power conumption
if util.format.asbool(self.parameter('showpowerconsumption', False)): if util.format.asbool(self.parameter('showpowerconsumption', False)):
output = '{}% ({})'.format(capacity, self.__manager.consumption(widget.name())) output = '{}% ({})'.format(capacity, self.__manager.consumption(widget.name))
else: else:
output = '{}%'.format(capacity) output = '{}%'.format(capacity)
if util.format.asbool(self.parameter('showremaining', True))\ if util.format.asbool(self.parameter('showremaining', True))\
and self.__manager.charge(widget.name()) == 'Discharging': and self.__manager.charge(widget.name) == 'Discharging':
remaining = self.__manager.remaining() remaining = self.__manager.remaining()
if remaining >= 0: if remaining >= 0:
output = '{} {}'.format(output, util.format.duration(remaining, compact=True, unit=True)) output = '{} {}'.format(output, util.format.duration(remaining, compact=True, unit=True))
if util.format.asbool(self.parameter('showdevice', False)): if util.format.asbool(self.parameter('showdevice', False)):
output = '{} ({})'.format(output, widget.name()) output = '{} ({})'.format(output, widget.name)
return output return output
@ -165,10 +165,10 @@ class Module(core.module.Module):
if widget.get('ac'): if widget.get('ac'):
state.append('AC') state.append('AC')
else: else:
if widget.name() == 'all-batteries': if widget.name == 'all-batteries':
charge = self.__manager.charge_any(self._batteries) charge = self.__manager.charge_any(self._batteries)
else: else:
charge = self.__manager.charge(widget.name()) charge = self.__manager.charge(widget.name)
if charge == 'Discharging': if charge == 'Discharging':
state.append('discharging-{}'.format(min([10, 25, 50, 80, 100], key=lambda i: abs(i-capacity)))) state.append('discharging-{}'.format(min([10, 25, 50, 80, 100], key=lambda i: abs(i-capacity))))
elif charge == 'Unknown': elif charge == 'Unknown':

View file

@ -97,8 +97,8 @@ class module(unittest.TestCase):
cfg = core.config.Config([]) cfg = core.config.Config([])
module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget]) module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget])
self.assertEqual(self.someWidget, module.widget(self.someWidget.name())) self.assertEqual(self.someWidget, module.widget(self.someWidget.name))
self.assertEqual(self.anotherWidget, module.widget(self.anotherWidget.name())) self.assertEqual(self.anotherWidget, module.widget(self.anotherWidget.name))
self.assertEqual(None, module.widget(self.unusedWidgetName)) self.assertEqual(None, module.widget(self.unusedWidgetName))
self.assertEqual(self.someWidget, module.widget()) self.assertEqual(self.someWidget, module.widget())