1
0
mirror of https://github.com/ihabunek/toot synced 2024-12-23 23:52:40 +01:00

Added styled radio buttons and checkboxes

This commit is contained in:
Daniel Schwarz 2023-02-14 22:21:04 -05:00
parent d399eec6f5
commit 3b67c85dfc
2 changed files with 24 additions and 4 deletions

View File

@ -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()

View File

@ -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")