Repository navigation
Replies: 2 comments
|
Dynamic actions can handle this if you use two mutually exclusive actions on the same key, each with its own description. You don't need to mutate the description of one binding: from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.reactive import reactive
from textual.widgets import Footer, Static
class Demo(App):
BINDINGS = [
Binding("d", "show_details", "Show details"),
Binding("d", "hide_details", "Hide details"),
]
details_visible = reactive(False, bindings=True)
def compose(self) -> ComposeResult:
yield Static("Details", id="details")
yield Footer()
def on_mount(self) -> None:
self.query_one("#details", Static).display = self.details_visible
def watch_details_visible(self, visible: bool) -> None:
if self.is_mounted:
self.query_one("#details", Static).display = visible
def check_action(self, action: str, parameters: tuple[object, ...]):
if action == "show_details":
return not self.details_visible
if action == "hide_details":
return self.details_visible
return True
def action_show_details(self) -> None:
self.details_visible = True
def action_hide_details(self) -> None:
self.details_visible = False
if __name__ == "__main__":
Demo().run()Return The rebinding result also makes sense from the implementation: |
|
Yes, if you swap the from dataclasses import replace
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.widgets import Footer, Static
class Demo(App):
BINDINGS = [Binding("d", "toggle_details", "Show details")]
def compose(self) -> ComposeResult:
yield Static("Details", id="details")
yield Footer()
def on_mount(self) -> None:
self.query_one("#details").display = False
def action_toggle_details(self) -> None:
details = self.query_one("#details")
details.display = not details.display
self.set_binding_description("d", "Hide details" if details.display else "Show details")
def set_binding_description(self, key: str, description: str) -> None:
bindings = self._bindings.key_to_bindings
bindings[key] = [replace(b, description=description) for b in bindings[key]]
self.refresh_bindings()With textual 8.2.8 the footer toggles "Show details" / "Hide details" on each press of Assign a new list instead of editing in place: |
Uh oh!
There was an error while loading. Please reload this page.
Hey.
I have for example a binding which toggles a state... like between hide/show, and it would be nice if the description indicates this by telling what it currently does.
AFAIU this can't be done via dynamic actions, which merely allow do show/hide/grey-out an action.
I tried everything I can think of... re-binding the same binding via
self.bin(), replacing the object inBINDINGS.... but the footer gets never updated, not even when I callself.refresh_bindings()orfooter.refresh()Is there any trick I don't see?
Thanks,
Chris.
All reactions