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:
Dave Pearson
2022-12-14 20:55:41 +00:00
parent 96a7701de1
commit cb0f8d2664

View File

@@ -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)