Skip to content

Commit

Permalink
Default callback for buttons is None.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Feb 20, 2024
1 parent 930bfd7 commit ffeb5a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
19 changes: 10 additions & 9 deletions src/batgrl/gadgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class Button(Themable, ButtonBehavior, Gadget):
----------
label : str, default: ""
Button label.
callback : Callable[[], None], default: lambda: None
callback : Callable[[], None] | None, default: None
Called when button is released.
always_release : bool, default: False
Whether a mouse up event outside the button will trigger it.
alpha : float, default: 1.0
Transparency of gadget.
always_release : bool, default: False
Whether a mouse up event outside the button will trigger it.
size : Size, default: Size(10, 10)
Size of gadget.
pos : Point, default: Point(0, 0)
Expand All @@ -62,14 +62,14 @@ class Button(Themable, ButtonBehavior, Gadget):
----------
label : str
Button label.
callback : Callable[[], None]
callback : Callable[[], None] | None
Called when button is released.
alpha : float
Transparency of gadget.
always_release : bool
Whether a mouse up event outside the button will trigger it.
state : ButtonState
Current button state. One of `NORMAL`, `HOVER`, `DOWN`.
alpha : float
Transparency of gadget.
size : Size
Size of gadget.
height : int
Expand Down Expand Up @@ -181,11 +181,11 @@ def __init__(
self,
*,
label: str = "",
callback: Callable[[], None] = lambda: None,
callback: Callable[[], None] | None = None,
always_release: bool = False,
alpha: float = 1.0,
size=Size(10, 10),
pos=Point(0, 0),
alpha: float = 1.0,
size_hint: SizeHint | SizeHintDict | None = None,
pos_hint: PosHint | PosHintDict | None = None,
is_transparent: bool = False,
Expand Down Expand Up @@ -264,4 +264,5 @@ def update_down(self):

def on_release(self):
"""Triggered when button is released."""
self.callback()
if self.callback is not None:
self.callback()
25 changes: 14 additions & 11 deletions src/batgrl/gadgets/toggle_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget):
----------
label : str, default: ""
Toggle button label.
callback : Callable[[ToggleState], None], default: lambda: None
callback : Callable[[ToggleState], None] | None, default: None
Called when toggle state changes. The new state is provided as first argument.
alpha : float, default: 1.0
Transparency of gadget.
group : None | Hashable, default: None
If a group is provided, only one button in a group can be in the "on" state.
allow_no_selection : bool, default: False
Expand All @@ -59,8 +61,6 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget):
Initial toggle state of button.
always_release : bool, default: False
Whether a mouse up event outside the button will trigger it.
alpha : float, default: 1.0
Transparency of gadget.
size : Size, default: Size(10, 10)
Size of gadget.
pos : Point, default: Point(0, 0)
Expand All @@ -82,8 +82,10 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget):
----------
label : str
Toggle button label.
callback : Callable[[ToggleState], None]
Button callback when toggled.
callback : Callable[[ToggleState], None] | None
Called when toggle state changes.
alpha : float
Transparency of gadget.
group : None | Hashable
If a group is provided, only one button in a group can be in the "on" state.
allow_no_selection : bool
Expand All @@ -94,8 +96,6 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget):
Whether a mouse up event outside the button will trigger it.
state : ButtonState
Current button state. One of `NORMAL`, `HOVER`, `DOWN`.
alpha : float
Transparency of gadget.
size : Size
Size of gadget.
height : int
Expand Down Expand Up @@ -213,12 +213,12 @@ def __init__(
self,
*,
label: str = "",
callback: Callable[[ToggleState], None] = lambda state: None,
callback: Callable[[ToggleState], None] | None = None,
alpha: float = 1.0,
group: None | Hashable = None,
allow_no_selection: bool = False,
toggle_state: ToggleState = ToggleState.OFF,
always_release: bool = False,
alpha: float = 1.0,
size=Size(10, 10),
pos=Point(0, 0),
size_hint: SizeHint | SizeHintDict | None = None,
Expand Down Expand Up @@ -312,6 +312,9 @@ def update_down(self):

def on_toggle(self):
"""Call callback on toggle state change."""
if self.root:
self.label = self.label # Update radio button/checkbox
if self.root is None:
return

self.label = self.label # Update radio button/checkbox
if self.callback is not None:
self.callback(self.toggle_state)

0 comments on commit ffeb5a9

Please sign in to comment.