Coverage for src/devboard/modal.py: 50.00%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-19 20:21 +0100

1"""Modal screen.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING, Any 

6 

7from rich.text import Text 

8from textual.containers import VerticalScroll 

9from textual.screen import ModalScreen 

10from textual.widgets import Static 

11 

12if TYPE_CHECKING: 

13 from textual.app import App, ComposeResult 

14 from textual.events import Key 

15 

16 

17class Modal(ModalScreen): 

18 """A modal screen.""" 

19 

20 def __init__(self, *args: Any, text: Any, **kwargs: Any) -> None: 

21 """Initialize the screen.""" 

22 super().__init__(*args, **kwargs) 

23 if isinstance(text, str): 

24 self.text = Text.from_ansi(text) 

25 else: 

26 self.text = text 

27 

28 def compose(self) -> ComposeResult: 

29 """Screen composition.""" 

30 yield VerticalScroll(Static(self.text), id="modal-contents") 

31 

32 def on_key(self, event: Key) -> None: 

33 """Dismiss on any unbound key.""" 

34 if not any(bindings.keys.get(event.key) for _, bindings in self.app._modal_binding_chain): 

35 self.dismiss() 

36 

37 

38class ModalMixin: 

39 """Mixin class to add a modal method.""" 

40 

41 app: App 

42 

43 def modal(self, text: str) -> None: 

44 """Push a modal.""" 

45 self.app.push_screen(Modal(text=text))