Skip to content

Commit

Permalink
disabled actions
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed May 17, 2024
1 parent 2907736 commit 78ee843
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/textual/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Reactive(Generic[ReactiveType]):
always_update: Call watchers even when the new value equals the old value.
compute: Run compute methods when attribute is changed.
recompose: Compose the widget again when the attribute changes.
bindings: Refresh bindings when the reactive changes.
"""

_reactives: ClassVar[dict[str, object]] = {}
Expand All @@ -119,6 +120,7 @@ def __init__(
always_update: bool = False,
compute: bool = True,
recompose: bool = False,
bindings: bool = False,
) -> None:
self._default = default
self._layout = layout
Expand All @@ -127,6 +129,7 @@ def __init__(
self._always_update = always_update
self._run_compute = compute
self._recompose = recompose
self._bindings = bindings
self._owner: Type[MessageTarget] | None = None

def __rich_repr__(self) -> rich.repr.Result:
Expand Down Expand Up @@ -289,6 +292,9 @@ def __set__(self, obj: Reactable, value: ReactiveType) -> None:
if self._run_compute:
self._compute(obj)

if self._bindings:
obj.refresh_bindings()

# Refresh according to descriptor flags
if self._layout or self._repaint or self._recompose:
obj.refresh(
Expand Down Expand Up @@ -367,6 +373,7 @@ class reactive(Reactive[ReactiveType]):
repaint: Perform a repaint on change.
init: Call watchers on initialize (post mount).
always_update: Call watchers even when the new value equals the old value.
bindings: Refresh bindings when the reactive changes.
"""

def __init__(
Expand All @@ -378,6 +385,7 @@ def __init__(
init: bool = True,
always_update: bool = False,
recompose: bool = False,
bindings: bool = False,
) -> None:
super().__init__(
default,
Expand All @@ -386,6 +394,7 @@ def __init__(
init=init,
always_update=always_update,
recompose=recompose,
bindings=bindings,
)


Expand All @@ -396,20 +405,23 @@ class var(Reactive[ReactiveType]):
default: A default value or callable that returns a default.
init: Call watchers on initialize (post mount).
always_update: Call watchers even when the new value equals the old value.
bindings: Refresh bindings when the reactive changes.
"""

def __init__(
self,
default: ReactiveType | Callable[[], ReactiveType],
init: bool = True,
always_update: bool = False,
bindings: bool = False,
) -> None:
super().__init__(
default,
layout=False,
repaint=False,
init=init,
always_update=always_update,
bindings=bindings,
)


Expand Down
31 changes: 31 additions & 0 deletions tests/test_dynamic_bindings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from textual.app import App


async def test_dynamic_disabled():
"""Check we can dynamically disable bindings."""
actions = []

class DynamicApp(App):
BINDINGS = [
("a", "register('a')", "A"),
("b", "register('b')", "B"),
("c", "register('c')", "B"),
]

def action_register(self, key: str) -> None:
actions.append(key)

def check_action(
self, action: str, parameters: tuple[object, ...]
) -> bool | None:
if action == "register":
if parameters == ("b",):
return False
if parameters == ("c",):
return None
return True

app = DynamicApp()
async with app.run_test() as pilot:
await pilot.press("a", "b", "c")
assert actions == ["a"]

0 comments on commit 78ee843

Please sign in to comment.