|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from typing import Protocol, runtime_checkable |
| 3 | +from rich.text import Text |
| 4 | +from textual.app import ComposeResult |
| 5 | +from textual.binding import Binding |
| 6 | +from textual.containers import Vertical, VerticalScroll |
| 7 | +from textual.screen import ModalScreen |
| 8 | +from textual.widget import Widget |
| 9 | +from textual.widgets import Label, Markdown |
| 10 | + |
| 11 | +from posting.widgets.datatable import PostingDataTable |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class HelpData: |
| 16 | + """Data relating to the widget to be displayed in the HelpScreen""" |
| 17 | + |
| 18 | + title: str = field(default="") |
| 19 | + """Title of the widget""" |
| 20 | + description: str = field(default="") |
| 21 | + """Markdown description to be displayed in the HelpScreen""" |
| 22 | + |
| 23 | + |
| 24 | +@runtime_checkable |
| 25 | +class Helpable(Protocol): |
| 26 | + """Widgets which contain information to be displayed in the HelpScreen |
| 27 | + should implement this protocol.""" |
| 28 | + |
| 29 | + help: HelpData |
| 30 | + |
| 31 | + |
| 32 | +class HelpModalHeader(Label): |
| 33 | + """The top help bar""" |
| 34 | + |
| 35 | + DEFAULT_CSS = """ |
| 36 | + HelpModalHeader { |
| 37 | + background: $background-lighten-1; |
| 38 | + color: $text-muted; |
| 39 | + } |
| 40 | + """ |
| 41 | + |
| 42 | + |
| 43 | +class HelpModalFooter(Label): |
| 44 | + """The bottom help bar""" |
| 45 | + |
| 46 | + DEFAULT_CSS = """ |
| 47 | + HelpModalFooter { |
| 48 | + background: $background-lighten-1; |
| 49 | + color: $text-muted; |
| 50 | + } |
| 51 | + """ |
| 52 | + |
| 53 | + |
| 54 | +class HelpModalFocusNote(Label): |
| 55 | + """A note below the help screen.""" |
| 56 | + |
| 57 | + |
| 58 | +class HelpScreen(ModalScreen[None]): |
| 59 | + DEFAULT_CSS = """ |
| 60 | + HelpScreen { |
| 61 | + align: center middle; |
| 62 | + & > VerticalScroll { |
| 63 | + background: $background; |
| 64 | + padding: 1 2; |
| 65 | + width: 65%; |
| 66 | + height: 80%; |
| 67 | + border: wide $background-lighten-2; |
| 68 | + border-title-color: $text; |
| 69 | + border-title-background: $background; |
| 70 | + border-title-style: bold; |
| 71 | + } |
| 72 | +
|
| 73 | + & DataTable#bindings-table { |
| 74 | + width: 1fr; |
| 75 | + height: 1fr; |
| 76 | + } |
| 77 | +
|
| 78 | + & HelpModalHeader { |
| 79 | + dock: top; |
| 80 | + width: 1fr; |
| 81 | + content-align: center middle; |
| 82 | + } |
| 83 | +
|
| 84 | + #footer-area { |
| 85 | + dock: bottom; |
| 86 | + height: auto; |
| 87 | + margin-top: 1; |
| 88 | + & HelpModalFocusNote { |
| 89 | + width: 1fr; |
| 90 | + content-align: center middle; |
| 91 | + color: $text-muted 40%; |
| 92 | + } |
| 93 | +
|
| 94 | + & HelpModalFooter { |
| 95 | + width: 1fr; |
| 96 | + content-align: center middle; |
| 97 | + } |
| 98 | + } |
| 99 | +
|
| 100 | +
|
| 101 | + & #bindings-title { |
| 102 | + width: 1fr; |
| 103 | + content-align: center middle; |
| 104 | + background: $background-lighten-1; |
| 105 | + color: $text-muted; |
| 106 | + } |
| 107 | +
|
| 108 | + & #help-description-wrapper { |
| 109 | + dock: top; |
| 110 | + max-height: 50%; |
| 111 | + margin-top: 1; |
| 112 | + height: auto; |
| 113 | + width: 1fr; |
| 114 | + & #help-description { |
| 115 | + margin: 0; |
| 116 | + width: 1fr; |
| 117 | + height: auto; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + """ |
| 122 | + |
| 123 | + BINDINGS = [ |
| 124 | + Binding("escape", "dismiss('')", "Close Help"), |
| 125 | + ] |
| 126 | + |
| 127 | + def __init__( |
| 128 | + self, |
| 129 | + widget: Widget, |
| 130 | + name: str | None = None, |
| 131 | + id: str | None = None, |
| 132 | + classes: str | None = None, |
| 133 | + ) -> None: |
| 134 | + super().__init__(name, id, classes) |
| 135 | + self.widget = widget |
| 136 | + |
| 137 | + def compose(self) -> ComposeResult: |
| 138 | + with VerticalScroll() as vs: |
| 139 | + vs.can_focus = False |
| 140 | + widget = self.widget |
| 141 | + # If the widget has help text, render it. |
| 142 | + if isinstance(widget, Helpable): |
| 143 | + help = widget.help |
| 144 | + help_title = help.title |
| 145 | + vs.border_title = f"[not bold]Focused Widget Help ([b]{help_title}[/])" |
| 146 | + if help_title: |
| 147 | + yield HelpModalHeader(f"[b]{help_title}[/]") |
| 148 | + help_markdown = help.description |
| 149 | + |
| 150 | + if help_markdown: |
| 151 | + help_markdown = help_markdown.strip() |
| 152 | + with VerticalScroll(id="help-description-wrapper") as vs: |
| 153 | + yield Markdown(help_markdown, id="help-description") |
| 154 | + else: |
| 155 | + yield Label( |
| 156 | + f"No help available for {help.title}", |
| 157 | + id="help-description", |
| 158 | + ) |
| 159 | + else: |
| 160 | + name = widget.__class__.__name__ |
| 161 | + vs.border_title = f"Focused Widget Help ([b]{name}[/])" |
| 162 | + yield HelpModalHeader(f"[b]{name}[/] Help") |
| 163 | + |
| 164 | + bindings = widget._bindings |
| 165 | + keys: list[tuple[str, Binding]] = [ |
| 166 | + binding for binding in bindings.keys.items() |
| 167 | + ] |
| 168 | + |
| 169 | + if keys: |
| 170 | + yield Label(" [b]All Keybindings[/]", id="bindings-title") |
| 171 | + table = PostingDataTable( |
| 172 | + id="bindings-table", |
| 173 | + cursor_type="row", |
| 174 | + zebra_stripes=True, |
| 175 | + ) |
| 176 | + table.cursor_vertical_escape = False |
| 177 | + table.add_columns("Key", "Description") |
| 178 | + for key, binding in keys: |
| 179 | + table.add_row( |
| 180 | + Text( |
| 181 | + binding.key_display or self.app.get_key_display(key), |
| 182 | + style="bold", |
| 183 | + no_wrap=True, |
| 184 | + end="", |
| 185 | + ), |
| 186 | + binding.description.lower(), |
| 187 | + ) |
| 188 | + yield table |
| 189 | + |
| 190 | + with Vertical(id="footer-area"): |
| 191 | + yield HelpModalFooter("Press [b]ESC[/] to dismiss.") |
| 192 | + yield HelpModalFocusNote( |
| 193 | + "[b]Note:[/] This page relates to the widget that is currently focused." |
| 194 | + ) |
0 commit comments