docs examples and diagrams

This commit is contained in:
Will McGugan
2023-02-03 11:23:14 +01:00
parent 806c80b8fe
commit 2ff278874b
33 changed files with 1381 additions and 1190 deletions

View File

@@ -0,0 +1,43 @@
from rich.segment import Segment
from rich.style import Style
from textual.app import App, ComposeResult
from textual.strip import Strip
from textual.widget import Widget
class CheckerBoard(Widget):
"""Render an 8x8 checkerboard."""
def render_line(self, y: int) -> Strip:
"""Render a line of the widget. y is relative to the top of the widget."""
row_index = y // 4 # A checkerboard square consists of 4 rows
if row_index >= 8: # Generate blank lines when we reach the end
return Strip.blank(self.size.width)
is_odd = row_index % 2 # Used to alternate the starting square on each row
white = Style.parse("on white") # Get a style object for a white background
black = Style.parse("on black") # Get a style object for a black background
# Generate a list of segments with alternating black and white space characters
segments = [
Segment(" " * 8, black if (column + is_odd) % 2 else white)
for column in range(8)
]
strip = Strip(segments, 8 * 8)
return strip
class BoardApp(App):
"""A simple app to show our widget."""
def compose(self) -> ComposeResult:
yield CheckerBoard()
if __name__ == "__main__":
app = BoardApp()
app.run()

View File

@@ -0,0 +1,63 @@
from rich.segment import Segment
from rich.style import Style
from textual.app import App, ComposeResult
from textual.geometry import Size
from textual.strip import Strip
from textual.widget import Widget
class CheckerBoard(Widget):
"""Render an 8x8 checkerboard."""
COMPONENT_CLASSES = {
"checkerboard--white-square",
"checkerboard--black-square",
}
DEFAULT_CSS = """
CheckerBoard .checkerboard--white-square {
background: #A5BAC9;
}
CheckerBoard .checkerboard--black-square {
background: #004578;
}
"""
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."""
row_index = y // 4 # four lines per row
if row_index >= 8:
return Strip.blank(self.size.width)
is_odd = row_index % 2
white = self.get_component_rich_style("checkerboard--white-square")
black = self.get_component_rich_style("checkerboard--black-square")
segments = [
Segment(" " * 8, black if (column + is_odd) % 2 else white)
for column in range(8)
]
strip = Strip(segments, 8 * 8)
return strip
class BoardApp(App):
"""A simple app to show our widget."""
def compose(self) -> ComposeResult:
yield CheckerBoard()
if __name__ == "__main__":
app = BoardApp()
app.run()

View File

@@ -15,17 +15,20 @@ class CheckerBoard(ScrollView):
}
DEFAULT_CSS = """
CheckerBoard {
background: $primary;
}
CheckerBoard .checkerboard--white-square {
background: $foreground 70%;
background: #A5BAC9;
}
CheckerBoard .checkerboard--black-square {
background: $primary;
background: #004578;
}
"""
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)