bowl/cheuph/attributed_lines_widget.py
2019-06-21 11:50:23 +00:00

66 lines
1.8 KiB
Python

# TODO send event on mouse click
from typing import Optional, Tuple
import urwid
from .attributed_lines import AttributedLines
from .attributed_text_widget import ATWidget
from .markup import AT
__all__ = ["AttributedLinesWidget"]
class AttributedLinesWidget(urwid.WidgetWrap):
"""
This widget draws an AttributedLines with a horizontal and a vertical
offset.
"""
def __init__(self, lines: Optional[AttributedLines] = None) -> None:
self._text = ATWidget(AT())
self._filler = urwid.Filler(self._text, valign=urwid.TOP)
super().__init__(self._filler)
self._horizontal_offset = 0
self.set_lines(lines or AttributedLines())
@property
def horizontal_offset(self) -> int:
return self._horizontal_offset
@horizontal_offset.setter
def horizontal_offset(self, offset: int) -> None:
if offset != self._horizontal_offset:
self._horizontal_offset = offset
self._invalidate()
@property
def upper_offset(self) -> int:
return self._lines.upper_offset
@upper_offset.setter
def upper_offset(self, offset: int) -> None:
self._lines.upper_offset = offset
self._invalidate()
@property
def lower_offset(self) -> int:
return self._lines.lower_offset
@lower_offset.setter
def lower_offset(self, offset: int) -> None:
self._lines.lower_offset = offset
self._invalidate()
def set_lines(self, lines: AttributedLines) -> None:
self._lines = lines
self._invalidate()
def render(self, size: Tuple[int, int], focus: bool) -> None:
width, height = size
text = self._lines.render(width, height, self.horizontal_offset)
self._text.set_attributed_text(text)
return super().render(size, focus)