|
|
@ -1,10 +1,22 @@ |
|
|
|
from .core import AbstractControl, WrappingControl, action |
|
|
|
|
|
|
|
import abc |
|
|
|
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__() |
|
|
|
super().__init__() |
|
|
|
|
|
|
|
self.__letter = letter |
|
|
|
self.__state = initial_state |
|
|
|
self.__state = initial_state |
|
|
|
|
|
|
|
|
|
|
|
def dump_state(self): |
|
|
|
def dump_state(self): |
|
|
@ -15,14 +27,23 @@ class ToggleAction(AbstractControl, metaclass=abc.ABCMeta): |
|
|
|
self.state = state['state'] |
|
|
|
self.state = state['state'] |
|
|
|
|
|
|
|
|
|
|
|
def toggle(self): |
|
|
|
def toggle(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Called when the button is clicked. |
|
|
|
|
|
|
|
""" |
|
|
|
self.state = not self.state |
|
|
|
self.state = not self.state |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
@property |
|
|
|
def state(self): |
|
|
|
def state(self) -> bool: |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
:return: The current toggle state of the button |
|
|
|
|
|
|
|
""" |
|
|
|
return self.__state |
|
|
|
return self.__state |
|
|
|
|
|
|
|
|
|
|
|
@state.setter |
|
|
|
@state.setter |
|
|
|
def state(self, state: bool): |
|
|
|
def state(self, state: bool): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Updates state and triggers enable() or disable() methods when state changed |
|
|
|
|
|
|
|
""" |
|
|
|
if self.__state == state: |
|
|
|
if self.__state == state: |
|
|
|
return |
|
|
|
return |
|
|
|
if state: |
|
|
|
if state: |
|
|
@ -32,26 +53,24 @@ class ToggleAction(AbstractControl, metaclass=abc.ABCMeta): |
|
|
|
self.__state = state |
|
|
|
self.__state = state |
|
|
|
|
|
|
|
|
|
|
|
def enable(self): |
|
|
|
def enable(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Called when the toggle button is enabled |
|
|
|
|
|
|
|
""" |
|
|
|
pass |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
def disable(self): |
|
|
|
def disable(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Called when the toggle button is disabled |
|
|
|
|
|
|
|
""" |
|
|
|
pass |
|
|
|
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): |
|
|
|
def respond_to(self, command): |
|
|
|
if command == ':toggle': |
|
|
|
if command == ':toggle': |
|
|
|
self.child.toggle() |
|
|
|
self.toggle() |
|
|
|
return True |
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
|
def __str__(self): |
|
|
|
return action( |
|
|
|
return action( |
|
|
|
self.create_pipe_command(':toggle'), |
|
|
|
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() |
|
|
|
) |
|
|
|
) |
|
|
|