From 98c03498aa38ea9af65bde258ce3d8c4239d571a Mon Sep 17 00:00:00 2001 From: Lars Vierbergen Date: Mon, 23 Jan 2017 19:44:14 +0100 Subject: [PATCH] Simplify ToggleControl Remove indirection through ToggleAction objects as everything can easily be inlined --- modules/caffeine.py | 12 ++++-------- modules/toggle.py | 47 +++++++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/modules/caffeine.py b/modules/caffeine.py index ec9a77c..cb5cca9 100644 --- a/modules/caffeine.py +++ b/modules/caffeine.py @@ -2,15 +2,15 @@ import subprocess import logging -from modules.toggle import ToggleAction, ToggleControl +from .toggle import ToggleControl from .util import process_reaper, backoff logger = logging.getLogger(__name__) -class CaffeineToggleAction(ToggleAction): - def __init__(self): - super().__init__(False) +class CaffeineControl(ToggleControl): + def __init__(self, letter: str = 'c'): + super().__init__(letter, False) def configure(self, argument_parser): argument_parser.add_argument('--caffeine-timeout', @@ -28,7 +28,3 @@ class CaffeineToggleAction(ToggleAction): return subprocess.Popen(['xscreensaver-command', '-deactivate'], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - - -def CaffeineControl(): - return ToggleControl('c', CaffeineToggleAction()) diff --git a/modules/toggle.py b/modules/toggle.py index ebfcf19..192e346 100644 --- a/modules/toggle.py +++ b/modules/toggle.py @@ -1,10 +1,22 @@ -from .core import AbstractControl, WrappingControl, action import abc +from .core import AbstractControl, action -class ToggleAction(AbstractControl, metaclass=abc.ABCMeta): - def __init__(self, initial_state: bool = False): + +class ToggleControl(AbstractControl, metaclass=abc.ABCMeta): + """ + Implements a simple toggle button + + The toggle button will show a text in upper or lower case, depending on the activation state of the button. + A click on the button toggles the button by calling toggle() + """ + def __init__(self, letter: str, initial_state: bool = False): + """ + :param letter: The text to show on the toggle control. Will be upper- or lowercased when the button is activated or deactivated. + :param initial_state: The initial state of the button (used only when there is no saved data yet) + """ super().__init__() + self.__letter = letter self.__state = initial_state def dump_state(self): @@ -15,14 +27,23 @@ class ToggleAction(AbstractControl, metaclass=abc.ABCMeta): self.state = state['state'] def toggle(self): + """ + Called when the button is clicked. + """ self.state = not self.state @property - def state(self): + def state(self) -> bool: + """ + :return: The current toggle state of the button + """ return self.__state @state.setter def state(self, state: bool): + """ + Updates state and triggers enable() or disable() methods when state changed + """ if self.__state == state: return if state: @@ -32,26 +53,24 @@ class ToggleAction(AbstractControl, metaclass=abc.ABCMeta): self.__state = state def enable(self): + """ + Called when the toggle button is enabled + """ pass def disable(self): + """ + Called when the toggle button is disabled + """ pass - -class ToggleControl(WrappingControl): - """Implements a simple toggle button""" - - def __init__(self, letter: str, toggle_action: ToggleAction): - super().__init__(toggle_action) - self.__letter = letter - def respond_to(self, command): if command == ':toggle': - self.child.toggle() + self.toggle() return True def __str__(self): return action( self.create_pipe_command(':toggle'), - self.__letter.upper() if self.child.state else self.__letter.lower() + self.__letter.upper() if self.state else self.__letter.lower() )