You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bingo-cli/BingoField.py

64 lines
1.9 KiB
Python

from textual.widgets import Static
from textual.message import Message
from textual.reactive import reactive
class BingoField(Static):
'''
A Bingo field widget.
Attributes
----------
text : str
The text displayed in the field
num : int
The running number of this field (0-24)
selected : bool
Wether the bingo field has been checked or not
highlighted : bool
Wether the cursor is currently on this field or not
Methods
-------
set_highlighted(new_highlight):
Sets the highlighted state (cursor) of this field
'''
text = reactive('')
class Selected(Message):
'''Send message to the board containing clicked field info'''
def __init__(self, num: int, selected: bool) -> None:
self.num = num
self.selected = selected
super().__init__()
def __init__(self, num, text: str) -> None:
self.num = num
self.selected = False
self.highlighted = False
super().__init__()
self.text = text
def on_click(self) -> None:
'''Add CSS class field_selected to self and POST to BingoBoard'''
self.selected = not self.selected
if self.selected:
self.add_class('field_selected')
else:
self.remove_class('field_selected')
# The post_message method sends an event to be handled in the DOM
self.post_message(self.Selected(self.num, self.selected))
def set_highlighted(self, new_highlight: bool) -> None:
'''Add or remove the field_highlighted CSS class from self'''
self.highlighted = new_highlight
if self.highlighted:
self.add_class('field_highlighted')
else:
self.remove_class('field_highlighted')
def render(self) -> str:
'''Return the fields text'''
return str(self.text)