mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Add support for a PRIORITY_BINDINGS classvar
This works in conjunction with BINDINGS. If a widget has BINDINGS, and if any of those bindings have a priority that isn't True or False, the value of PRIORITY_BINDINGS will be used (or the value from one of the parent classes, if there are any, will be used if it isn't set on the current class). See #1343.
This commit is contained in:
@@ -92,6 +92,9 @@ class DOMNode(MessagePump):
|
||||
# Virtual DOM nodes
|
||||
COMPONENT_CLASSES: ClassVar[set[str]] = set()
|
||||
|
||||
# Should the content of BINDINGS be treated as priority bindings?
|
||||
PRIORITY_BINDINGS: ClassVar[bool] = False
|
||||
|
||||
# Mapping of key bindings
|
||||
BINDINGS: ClassVar[list[BindingType]] = []
|
||||
|
||||
@@ -225,11 +228,18 @@ class DOMNode(MessagePump):
|
||||
"""
|
||||
bindings: list[Bindings] = []
|
||||
|
||||
# To start with, assume that bindings won't be priority bindings.
|
||||
priority = False
|
||||
|
||||
for base in reversed(cls.__mro__):
|
||||
if issubclass(base, DOMNode):
|
||||
# See if the current class wants to set the bindings as
|
||||
# priority bindings. If it doesn't have that property on the
|
||||
# class, go with what we saw last.
|
||||
priority = base.__dict__.get("PRIORITY_BINDINGS", priority)
|
||||
if not base._inherit_bindings:
|
||||
bindings.clear()
|
||||
bindings.append(Bindings(base.__dict__.get("BINDINGS", [])))
|
||||
bindings.append(Bindings(base.__dict__.get("BINDINGS", []), priority))
|
||||
keys = {}
|
||||
for bindings_ in bindings:
|
||||
keys.update(bindings_.keys)
|
||||
|
||||
Reference in New Issue
Block a user