diff --git a/src/textual/_activation.py b/src/textual/_activation.py index 7ec5b635d4..bfa4366d3e 100644 --- a/src/textual/_activation.py +++ b/src/textual/_activation.py @@ -6,6 +6,8 @@ from itertools import islice from typing import TYPE_CHECKING, Callable +from formbridge.activation import ContextEffect, PrimaryEffect + from textual import events from textual._callback import invoke from functools import partial @@ -127,27 +129,20 @@ async def activate(self) -> None: await widget.app.dispatch_style_action(self.handler, event, widget) -class PrimaryActivation: - """A widget declares its existing primary effect; discovery is shared.""" +class PrimaryActivation(PrimaryEffect): + """A widget's primary effect is found at its published click cell; discovery is shared.""" def primary_activation_targets(self, region): return (CallbackTarget(self, region, self.primary_activation),) - @property - @abstractmethod - def primary_activation(self) -> Callable: ... - -class ContextActivation: - """A widget declares its contextual effect at the published click offset.""" +class ContextActivation(ContextEffect): + """A widget's contextual effect is found at the published click offset.""" def context_activation_targets(self, region): return (CallbackTarget(self, region, self.context_activation, arguments=(region.offset,)),) - @abstractmethod - def context_activation(self, offset) -> None: ... - class FocusActivation(PrimaryActivation): """Text entry is reached by focus, never by submitting or editing its value.""" diff --git a/src/textual/hint_labels.py b/src/textual/hint_labels.py deleted file mode 100644 index db5dd1be19..0000000000 --- a/src/textual/hint_labels.py +++ /dev/null @@ -1,28 +0,0 @@ -"""How a click hint shows its stable assigned key.""" -from abc import ABC, abstractmethod - -from textual.content import Content - - -class HintLabelStyle(ABC): - """Presentation never changes the key or its original activation.""" - - @classmethod - @abstractmethod - def caption(cls, key: str, prefix: str) -> Content: ... - - -class NextLetterHints(HintLabelStyle): - """Show the next untyped letter at the target.""" - - @classmethod - def caption(cls, key: str, prefix: str) -> Content: - return Content(key[len(prefix):len(prefix) + 1]) - - -class FullKeyHints(HintLabelStyle): - """Show the complete assigned sequence, including the typed prefix.""" - - @classmethod - def caption(cls, key: str, prefix: str) -> Content: - return Content(key).stylize("not reverse", 0, len(prefix)) diff --git a/src/textual/widgets/_hints.py b/src/textual/widgets/_hints.py index bf554a504c..2ab29840c9 100644 --- a/src/textual/widgets/_hints.py +++ b/src/textual/widgets/_hints.py @@ -3,13 +3,14 @@ from enum import IntEnum +from formbridge.hint_keys import HintKeyAssignment, HintLabelStyle, NextLetterHints + from textual import events from textual._activation import Activation from textual._compositor import ActivationScene, FrameOverlay from textual._on import on from textual.content import Content from textual.geometry import Region -from textual.hint_labels import HintLabelStyle, NextLetterHints from textual.widget import Widget @@ -84,11 +85,17 @@ def __init__(self, session, text: str, target: Activation): self.text = text self.target = target + @staticmethod + def caption(style: type[HintLabelStyle], key: str, prefix: str) -> Content: + """The shown part of the key, its typed characters out of the hint's reverse style.""" + text, typed = style.shown(key, prefix) + return Content(text).stylize("not reverse", 0, typed) + def render(self) -> Content: - return self.session.label_style.caption(self.text, self.session.prefix) + return self.caption(self.session.label_style, self.text, self.session.prefix) -class HintSession(HintOverlay, FrameOverlay, can_focus=True): +class HintSession(HintKeyAssignment, HintOverlay, FrameOverlay, can_focus=True): """Own stable keys, current-frame positions, input, focus and retirement.""" DEFAULT_CSS = """ @@ -101,29 +108,12 @@ class HintSession(HintOverlay, FrameOverlay, can_focus=True): scrollbar-size-horizontal: 0; } """ - alphabet = "asdfghjklqwertyuiopzxcvbnm" - @classmethod def for_scene(cls, screen, scene: ActivationScene, click: HintClick): targets = sorted(scene.targets.values(), key=lambda target: ( target.region.y, target.region.x)) return cls(screen, targets, click, screen.hint_label_style) - @classmethod - def label_width(cls, count: int) -> int: - width = 1 - while len(cls.alphabet) ** width < count: - width += 1 - return width - - @classmethod - def label(cls, index, width): - characters = [] - for _ in range(width): - index, digit = divmod(index, len(cls.alphabet)) - characters.append(cls.alphabet[digit]) - return "".join(reversed(characters)) - def __init__(self, screen, targets, click: HintClick, label_style: type[HintLabelStyle] = NextLetterHints): super().__init__() diff --git a/tests/test_hint_label_styles.py b/tests/test_hint_label_styles.py index 7bb03677dc..ecb8d7328d 100644 --- a/tests/test_hint_label_styles.py +++ b/tests/test_hint_label_styles.py @@ -2,7 +2,7 @@ import pytest from textual.app import App -from textual.hint_labels import FullKeyHints, NextLetterHints +from formbridge.hint_keys import FullKeyHints, NextLetterHints from textual.screen import Screen from textual.style import Style from textual.widgets import Input, Static @@ -10,7 +10,7 @@ def test_full_key_inverts_the_typed_prefix_against_the_hint_background(): - caption = FullKeyHints.caption("asd", "as") + caption = HintLabel.caption(FullKeyHints, "asd", "as") segments = caption.render_segments(Style(reverse=True)) assert [(segment.text, segment.style.reverse) for segment in segments] == [ ("as", False), ("d", True),