Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed reactives don't refresh bindings #5150

Open
Klavionik opened this issue Oct 21, 2024 · 1 comment
Open

Computed reactives don't refresh bindings #5150

Klavionik opened this issue Oct 21, 2024 · 1 comment

Comments

@Klavionik
Copy link

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
Copy link

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant