| 1 |
"""Which keys a binding has to claim ahead of the focused widget. |
| 2 |
|
| 3 |
A key that reaches Harlequin carrying a printable character is inserted by a |
| 4 |
focused Input or TextArea before any binding on it fires, so `bind()` derives |
| 5 |
priority from the key. What is asserted here is the split: alt+<character> is |
| 6 |
claimed, and neither an unmodified key nor a ctrl+ key is. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import pytest |
| 12 |
from textual.widget import Widget |
| 13 |
|
| 14 |
from harlequin.bindings import bind, key_needs_priority |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize( |
| 18 |
"key", |
| 19 |
[ |
| 20 |
"alt+e", |
| 21 |
"alt+1", |
| 22 |
"alt+shift+e", |
| 23 |
], |
| 24 |
) |
| 25 |
def test_keys_that_arrive_as_text(key: str) -> None: |
| 26 |
assert key_needs_priority(key) is True |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
"key", |
| 31 |
[ |
| 32 |
# typing looks exactly like this, so these keep Textual's behavior |
| 33 |
"e", |
| 34 |
"space", |
| 35 |
"full_stop", |
| 36 |
# ctrl replaces the character with a control code |
| 37 |
"ctrl+e", |
| 38 |
"ctrl+alt+e", |
| 39 |
# a terminal never prefixes a named key with alt+ |
| 40 |
"alt+left", |
| 41 |
"alt+enter", |
| 42 |
# no character at all |
| 43 |
"f4", |
| 44 |
"shift+tab", |
| 45 |
# degenerate spellings must not raise |
| 46 |
"", |
| 47 |
"+", |
| 48 |
], |
| 49 |
) |
| 50 |
def test_keys_that_do_not_arrive_as_text(key: str) -> None: |
| 51 |
assert key_needs_priority(key) is False |
| 52 |
|
| 53 |
|
| 54 |
def test_priority_is_derived_per_key() -> None: |
| 55 |
"""One binding over two keys splits, so ctrl+e keeps bubbling.""" |
| 56 |
widget = Widget() |
| 57 |
bind(widget, "alt+e,ctrl+e", "launch_external_editor") |
| 58 |
|
| 59 |
(alt_binding,) = widget._bindings.key_to_bindings["alt+e"] |
| 60 |
(ctrl_binding,) = widget._bindings.key_to_bindings["ctrl+e"] |
| 61 |
|
| 62 |
assert alt_binding.priority is True |
| 63 |
assert ctrl_binding.priority is False |
| 64 |
assert alt_binding.action == ctrl_binding.action == "launch_external_editor" |
| 65 |
assert ( |
| 66 |
alt_binding.description == ctrl_binding.description == "Launch External Editor" |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
def test_explicit_priority_applies_to_every_key() -> None: |
| 71 |
widget = Widget() |
| 72 |
bind(widget, "ctrl+q,f10", "quit", priority=True) |
| 73 |
|
| 74 |
assert all( |
| 75 |
binding.priority is True |
| 76 |
for key in ("ctrl+q", "f10") |
| 77 |
for binding in widget._bindings.key_to_bindings[key] |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
def test_key_display_and_show_survive_the_split() -> None: |
| 82 |
widget = Widget() |
| 83 |
bind( |
| 84 |
widget, |
| 85 |
"alt+e,ctrl+e", |
| 86 |
"launch_external_editor", |
| 87 |
description="Launch External Editor", |
| 88 |
key_display="⌥e or ^e", |
| 89 |
) |
| 90 |
|
| 91 |
for key in ("alt+e", "ctrl+e"): |
| 92 |
(binding,) = widget._bindings.key_to_bindings[key] |
| 93 |
assert binding.key_display == "⌥e or ^e" |
| 94 |
assert binding.show is True |