Skip to content

Computed reactives don't refresh bindings #5150

Open
@Klavionik

Description

@Klavionik

Seems like the compute methods don't refresh bindings when refresh_bindings is set to True. I assume it should work the same as the usual reactive, but maybe I'm wrong?

Here's an MRE. After entering "Open Sesame" into the input, the bindings won't be refreshed until you change the focus (like clicking ouside of the terminal and then clicking again inside the terminal).

from textual import on
from textual.app import App, ComposeResult
from textual.reactive import reactive
from textual.widgets import Input, Footer


class SesameApp(App):
    BINDINGS = [("ctrl+s", "open", "Open")]
    CSS = """
        Screen {
          align: center middle;
        }
        
        Input {
          width: 50;
        }
    """

    text = reactive("")
    is_text_magical = reactive(False, bindings=True)

    def compose(self) -> ComposeResult:
        yield Input(placeholder="Enter the magical words", id="text")
        yield Footer()

    @on(Input.Changed, "#text")
    def update_text(self, event: Input.Changed) -> None:
        self.text = event.value

    def compute_is_text_magical(self) -> bool:
        return self.text == "Open Sesame"

    def action_open(self) -> None:
        self.notify("Look, the treasure!")

    def check_action(self, action: str, parameters: tuple[object, ...]) -> bool | None:
        return not (action == "open" and not self.is_text_magical)


if __name__ == '__main__':
    app = SesameApp()
    app.run()

Textual Diagnostics

Versions

Name Value
Textual 0.83.0
Rich 13.9.2

Python

Name Value
Version 3.12.3
Implementation CPython
Compiler GCC 11.4.0
Executable /home/jediroman/Desktop/bindings-mre/.venv/bin/python

Operating System

Name Value
System Linux
Release 5.15.0-122-generic
Version #132-Ubuntu SMP Thu Aug 29 13:45:52 UTC 2024

Terminal

Name Value
Terminal Application WezTerm (20230712-072601-f4abf8fd)
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=154, height=35
legacy_windows False
min_width 1
max_width 154
is_terminal True
encoding utf-8
max_height 35
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions