|
| 1 | +from typing import NoReturn |
| 2 | + |
1 | 3 | from rich.text import Text |
2 | 4 | from textual.app import App, ComposeResult |
3 | 5 | from textual.containers import Container |
| 6 | +from textual.events import Key |
| 7 | +from textual.types import SelectType |
4 | 8 | from textual.widgets import Button, Label, Select, Static |
5 | 9 |
|
6 | 10 | from .lottery import request_lottery_obj |
@@ -30,29 +34,36 @@ def compose(self) -> ComposeResult: |
30 | 34 | id='main-container', |
31 | 35 | ) |
32 | 36 |
|
33 | | - def on_key(self, event): |
| 37 | + def on_key(self, event: Key) -> NoReturn: |
34 | 38 | """Handle key events.""" |
35 | 39 | if event.key == 'q': |
36 | 40 | self.exit() |
37 | 41 |
|
38 | | - def on_button_pressed(self, event): |
| 42 | + def on_button_pressed(self, event: Button.Pressed) -> None: |
39 | 43 | """Handle button press events.""" |
40 | 44 | if event.button.id == 'draw-button': |
41 | 45 | self._draw_button_handler() |
42 | 46 |
|
43 | | - def _draw_button_handler(self): |
| 47 | + def _draw_button_handler(self) -> None: |
44 | 48 | """Handle the draw button press.""" |
45 | | - if self.query_one('#lottery-select').is_blank(): |
| 49 | + if self._read_lottery_selection() is None: |
46 | 50 | self._update_result_label( |
47 | 51 | Text('Please select a lottery before drawing.', style='bold #ff8c42') |
48 | 52 | ) |
49 | 53 | return |
50 | 54 |
|
51 | | - lottery_obj = request_lottery_obj(self.query_one('#lottery-select').value) |
| 55 | + lottery_obj = request_lottery_obj(self._read_lottery_selection()) |
52 | 56 | result = lottery_obj.draw() |
53 | 57 | self._update_result_label(str(result)) |
54 | 58 |
|
55 | | - def _update_result_label(self, message: str): |
| 59 | + def _read_lottery_selection(self) -> SelectType | None: |
| 60 | + """Read the selected lottery from the dropdown.""" |
| 61 | + select_widget = self.query_one('#lottery-select') |
| 62 | + if select_widget.is_blank(): |
| 63 | + return None |
| 64 | + return select_widget.value |
| 65 | + |
| 66 | + def _update_result_label(self, message: str) -> None: |
56 | 67 | """Update the result label with a new message.""" |
57 | 68 | self.query_one('#result-label').update(message) |
58 | 69 |
|
|
0 commit comments