2019-08-24 11:20:31 +02:00
|
|
|
import urwid
|
2023-03-14 01:50:23 +01:00
|
|
|
from wcwidth import wcswidth
|
2019-08-24 11:20:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Clickable:
|
|
|
|
"""
|
|
|
|
Add a `click` signal which is sent when the item is activated or clicked.
|
|
|
|
|
|
|
|
TODO: make it work on widgets which have other signals.
|
|
|
|
"""
|
|
|
|
signals = ["click"]
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
if self._command_map[key] == urwid.ACTIVATE:
|
|
|
|
self._emit('click')
|
|
|
|
return
|
|
|
|
|
|
|
|
return key
|
|
|
|
|
|
|
|
def mouse_event(self, size, event, button, x, y, focus):
|
|
|
|
if button == 1:
|
|
|
|
self._emit('click')
|
|
|
|
|
|
|
|
|
|
|
|
class SelectableText(Clickable, urwid.Text):
|
|
|
|
_selectable = True
|
|
|
|
|
|
|
|
|
|
|
|
class SelectableColumns(Clickable, urwid.Columns):
|
|
|
|
_selectable = True
|
2019-08-30 12:28:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditBox(urwid.AttrWrap):
|
|
|
|
"""Styled edit box."""
|
2019-08-30 15:31:57 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2020-01-03 09:15:11 +01:00
|
|
|
self.edit = urwid.Edit(*args, **kwargs)
|
|
|
|
return super().__init__(self.edit, "editbox", "editbox_focused")
|
2019-08-30 12:28:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Button(urwid.AttrWrap):
|
|
|
|
"""Styled button."""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
button = urwid.Button(*args, **kwargs)
|
2023-03-14 01:50:23 +01:00
|
|
|
padding = urwid.Padding(button, width=wcswidth(args[0]) + 4)
|
2019-08-30 12:28:03 +02:00
|
|
|
return super().__init__(padding, "button", "button_focused")
|
|
|
|
|
|
|
|
def set_label(self, *args, **kwargs):
|
|
|
|
self.original_widget.original_widget.set_label(*args, **kwargs)
|
2023-03-14 01:50:23 +01:00
|
|
|
self.original_widget.width = wcswidth(args[0]) + 4
|
2023-02-15 04:21:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
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")
|