Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow system plugins to customize stroke display #1658

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions doc/api/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ Each line in this word list consists of a word and a number, separated by
a space. See {data}`ORTHOGRAPHY_WORDS` for more information.
```

```{data} display
:type: Callable[[Tuple[str, ...]], str]

A function called to display a steno outline in this system. The input is a
tuple of normalized strokes. The output is a string shown to the user when
making stroke suggestions or browsing a dictionary.

If not defined, the default is `"/".join`, i.e. simply join the normalized
strokes by slashes.
```

## Computed Fields

The fields below are automatically calculated from the values defined by
Expand Down
2 changes: 2 additions & 0 deletions doc/plugin-dev/systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ KEYMAPS: Dict[str, Dict[str, Union[str, Tuple[str, ...]]]]

DICTIONARIES_ROOT: str
DEFAULT_DICTIONARIES: Tuple[str, ...]

display: Callable[[Tuple[str, ...]], str]
```

Note that there are a lot of possible fields in a system plugin. You must set
Expand Down
1 change: 1 addition & 0 deletions news.d/api/1658.new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
System definitions can now define a function `display`, which is used to render outlines in the UI.
4 changes: 2 additions & 2 deletions plover/gui_qt/add_translation_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from plover import _
from plover.misc import shorten_path
from plover.steno import normalize_steno, sort_steno_strokes
from plover.steno import display_steno, normalize_steno, sort_steno_strokes
from plover.engine import StartingStrokeState
from plover.translation import escape_translation, unescape_translation
from plover.formatting import RetroFormatter
Expand Down Expand Up @@ -232,7 +232,7 @@ def on_dictionary_selected(self, index):

def _format_label(self, fmt, strokes, translation=None, filename=None):
if strokes:
strokes = ', '.join(self._special_fmt % html_escape('/'.join(s))
strokes = ', '.join(self._special_fmt % html_escape(display_steno('/'.join(s)))
for s in sort_steno_strokes(strokes))
if translation:
translation = self._special_fmt_bold % html_escape(escape_translation(translation))
Expand Down
4 changes: 2 additions & 2 deletions plover/gui_qt/dictionary_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from plover import _
from plover.translation import escape_translation, unescape_translation
from plover.misc import expand_path, shorten_path
from plover.steno import normalize_steno, steno_to_sort_key
from plover.steno import display_steno, normalize_steno, steno_to_sort_key

from plover.gui_qt.dictionary_editor_ui import Ui_DictionaryEditor
from plover.gui_qt.steno_validator import StenoValidator
Expand Down Expand Up @@ -193,7 +193,7 @@ def data(self, index, role):
return self._error_icon
return None
if column == _COL_STENO:
return item.steno
return display_steno(item.steno) if role == Qt.DisplayRole else item.steno
if column == _COL_TRANS:
return escape_translation(item.translation)
if column == _COL_DICT:
Expand Down
3 changes: 2 additions & 1 deletion plover/gui_qt/suggestions_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)

from plover import _
from plover.steno import display_steno
from plover.translation import escape_translation

from .utils import ActionCopyViewSelectionToClipboard
Expand Down Expand Up @@ -67,7 +68,7 @@ def _format_suggestion(self, index):
return translation, None
strokes = ''
for strokes_list in suggestion.steno_list[:MAX_SUGGESTIONS_COUNT]:
strokes += '\n ' + '/'.join(strokes_list)
strokes += '\n ' + display_steno('/'.join(strokes_list))
return translation, strokes

def _suggestion_size_hint(self, index):
Expand Down
9 changes: 8 additions & 1 deletion plover/steno.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Stroke(BaseStroke):

@classmethod
def setup(cls, keys, implicit_hyphen_keys, number_key,
numbers, feral_number_key, undo_stroke):
numbers, feral_number_key, undo_stroke, display):
if number_key is None:
assert not numbers
numbers = None
Expand All @@ -40,6 +40,7 @@ def setup(cls, keys, implicit_hyphen_keys, number_key,
cls._class._class = cls._class
cls._class.PREFIX_STROKE = cls.PREFIX_STROKE = cls.from_integer(0)
cls._class.UNDO_STROKE = cls.UNDO_STROKE = cls.from_steno(undo_stroke)
cls._class.display = cls.display = display

@classmethod
def from_steno(cls, steno):
Expand Down Expand Up @@ -71,6 +72,11 @@ def normalize_steno(cls, steno, strict=True):
raise
return tuple(steno.split('/'))

@classmethod
def display_steno(cls, steno):
normalized = cls.normalize_steno(steno, strict=False)
return cls.display(normalized)

@classmethod
def steno_to_sort_key(cls, steno, strict=True):
try:
Expand Down Expand Up @@ -104,6 +110,7 @@ def __str__(self):

normalize_stroke = Stroke.normalize_stroke
normalize_steno = Stroke.normalize_steno
display_steno = Stroke.display_steno
steno_to_sort_key = Stroke.steno_to_sort_key

def sort_steno_strokes(strokes_list):
Expand Down
3 changes: 2 additions & 1 deletion plover/system/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def _suffix_keys(keys):
'KEYMAPS' : lambda mod: mod.KEYMAPS,
'DICTIONARIES_ROOT' : lambda mod: mod.DICTIONARIES_ROOT,
'DEFAULT_DICTIONARIES' : lambda mod: mod.DEFAULT_DICTIONARIES,
'display' : lambda mod: getattr(mod, 'display', '/'.join),
}

def setup(system_name):
Expand All @@ -69,6 +70,6 @@ def setup(system_name):
system_symbols['NAME'] = system_name
globals().update(system_symbols)
Stroke.setup(KEYS, IMPLICIT_HYPHEN_KEYS, NUMBER_KEY, NUMBERS,
FERAL_NUMBER_KEY, UNDO_STROKE_STENO)
FERAL_NUMBER_KEY, UNDO_STROKE_STENO, display)

NAME = None
3 changes: 3 additions & 0 deletions plover/system/english_stenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,6 @@

DICTIONARIES_ROOT = 'asset:plover:assets'
DEFAULT_DICTIONARIES = ('user.json', 'commands.json', 'main.json')

def display(strokes):
return "/".join(strokes)