From 3b67c85dfc945802f8d62eaf163e084f420a966c Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Tue, 14 Feb 2023 22:21:04 -0500 Subject: [PATCH] Added styled radio buttons and checkboxes --- toot/tui/poll.py | 8 ++++---- toot/tui/widgets.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/toot/tui/poll.py b/toot/tui/poll.py index fc6f68a..dc24046 100644 --- a/toot/tui/poll.py +++ b/toot/tui/poll.py @@ -4,7 +4,7 @@ from toot import api from toot.exceptions import ApiError from toot.utils import format_content from .utils import highlight_hashtags, parse_datetime -from .widgets import Button +from .widgets import Button, CheckBox, RadioButton class Poll(urwid.ListBox): @@ -33,7 +33,7 @@ class Poll(urwid.ListBox): poll = self.status.data.get("poll") choices = [] for idx, b in enumerate(self.button_group): - if b.state: + if b.get_state(): choices.append(idx) if len(choices): @@ -62,11 +62,11 @@ class Poll(urwid.ListBox): yield urwid.Text(("gray", prefix + f'{option["title"]}')) else: if poll["multiple"]: - cb = urwid.CheckBox(f'{option["title"]}') + cb = CheckBox(f'{option["title"]}') self.button_group.append(cb) yield cb else: - yield urwid.RadioButton(self.button_group, f'{option["title"]}') + yield RadioButton(self.button_group, f'{option["title"]}') yield urwid.Divider() diff --git a/toot/tui/widgets.py b/toot/tui/widgets.py index a311d52..6f46fb3 100644 --- a/toot/tui/widgets.py +++ b/toot/tui/widgets.py @@ -46,3 +46,23 @@ class Button(urwid.AttrWrap): def set_label(self, *args, **kwargs): self.original_widget.original_widget.set_label(*args, **kwargs) self.original_widget.width = len(args[0]) + 4 + + +class CheckBox(urwid.AttrWrap): + """Styled checkbox.""" + def __init__(self, *args, **kwargs): + self.button = urwid.CheckBox(*args, **kwargs) + padding = urwid.Padding(self.button, width=len(args[0]) + 4) + return super().__init__(padding, "button", "button_focused") + + def get_state(self): + """Return the state of the checkbox.""" + return self.button._state + + +class RadioButton(urwid.AttrWrap): + """Styled radiobutton.""" + def __init__(self, *args, **kwargs): + button = urwid.RadioButton(*args, **kwargs) + padding = urwid.Padding(button, width=len(args[1]) + 4) + return super().__init__(padding, "button", "button_focused")