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
20 changes: 20 additions & 0 deletions src/formbridge/activation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""What activating a control means, independent of how a toolkit finds the click target."""
from __future__ import annotations

from abc import abstractmethod
from collections.abc import Callable


class PrimaryEffect:
"""A control declares its primary effect: what a click, a hint key or an activate key runs."""

@property
@abstractmethod
def primary_activation(self) -> Callable[[], object]: ...


class ContextEffect:
"""A control declares its contextual effect at the offset where it was activated."""

@abstractmethod
def context_activation(self, offset) -> None: ...
53 changes: 53 additions & 0 deletions src/formbridge/hint_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Which keys click hints get, one fixed-width sequence per target in target order, and
which part of its key a hint label shows."""
from __future__ import annotations

from abc import ABC, abstractmethod


class HintKeyAssignment:
"""Hint keys come from the home-row-first alphabet; every key has the width the target
count needs, so typing a prefix narrows the targets without one key shadowing another."""

alphabet = "asdfghjklqwertyuiopzxcvbnm"

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


class HintLabelStyle(ABC):
"""Which part of a hint's assigned key its label shows. Presentation never changes the
key or its original activation."""

@classmethod
@abstractmethod
def shown(cls, key: str, prefix: str) -> tuple[str, int]:
"""The label's text, and how many of its leading characters are already typed."""


class NextLetterHints(HintLabelStyle):
"""Show the next untyped letter at the target."""

@classmethod
def shown(cls, key: str, prefix: str) -> tuple[str, int]:
return key[len(prefix):len(prefix) + 1], 0


class FullKeyHints(HintLabelStyle):
"""Show the complete assigned sequence, including the typed prefix."""

@classmethod
def shown(cls, key: str, prefix: str) -> tuple[str, int]:
return key, len(prefix)
Loading