more docs and diagrams

This commit is contained in:
Will McGugan
2023-02-03 19:10:03 +01:00
parent 2ff278874b
commit a5808db8b8
7 changed files with 111 additions and 42 deletions

View File

@@ -24,12 +24,6 @@ class CheckerBoard(Widget):
}
"""
def get_content_width(self, container: Size, viewport: Size) -> int:
return 64
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return 32
def render_line(self, y: int) -> Strip:
"""Render a line of the widget. y is relative to the top of the widget."""

View File

@@ -23,42 +23,40 @@ class CheckerBoard(ScrollView):
}
"""
def get_content_width(self, container: Size, viewport: Size) -> int:
return 64
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return 32
def on_mount(self) -> None:
self.virtual_size = Size(64, 32)
def __init__(self, board_size: int) -> None:
super().__init__()
self.board_size = board_size
# Each square is 4 rows and 8 columns
self.virtual_size = Size(board_size * 8, board_size * 4)
def render_line(self, y: int) -> Strip:
"""Render a line of the widget. y is relative to the top of the widget."""
scroll_x, scroll_y = self.scroll_offset
y += scroll_y
scroll_x, scroll_y = self.scroll_offset # The current scroll position
y += scroll_y # The line at the top of the widget is now `scroll_y`, not zero!
row_index = y // 4 # four lines per row
white = self.get_component_rich_style("checkerboard--white-square")
black = self.get_component_rich_style("checkerboard--black-square")
if row_index >= 8:
if row_index >= self.board_size:
return Strip.blank(self.size.width)
is_odd = row_index % 2
segments = [
Segment(" " * 8, black if (column + is_odd) % 2 else white)
for column in range(8)
for column in range(self.board_size)
]
strip = Strip(segments, 8 * 8)
strip = Strip(segments, self.board_size * 8)
# Crop the strip so that is covers the visible area
strip = strip.crop(scroll_x, scroll_x + self.size.width)
return strip
class BoardApp(App):
def compose(self) -> ComposeResult:
yield CheckerBoard()
yield CheckerBoard(100)
if __name__ == "__main__":