Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/textual/_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
28 changes: 0 additions & 28 deletions src/textual/hint_labels.py

This file was deleted.

30 changes: 10 additions & 20 deletions src/textual/widgets/_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 = """
Expand All @@ -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__()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_hint_label_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
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
from textual.widgets._hints import HintLabel, HintSession


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),
Expand Down