From 08dc2a34bf0f2612785765a4a93ca4c0ace00a4d Mon Sep 17 00:00:00 2001 From: Lynn Date: Mon, 5 Feb 2024 03:29:36 +0100 Subject: [PATCH 1/2] Allow system plugins to customize stroke display --- doc/api/system.md | 11 +++++++++++ doc/plugin-dev/systems.md | 2 ++ plover/gui_qt/add_translation_widget.py | 4 ++-- plover/gui_qt/dictionary_editor.py | 4 ++-- plover/gui_qt/suggestions_widget.py | 3 ++- plover/steno.py | 9 ++++++++- plover/system/__init__.py | 3 ++- plover/system/english_stenotype.py | 3 +++ 8 files changed, 32 insertions(+), 7 deletions(-) diff --git a/doc/api/system.md b/doc/api/system.md index 255558115..b7471cf01 100644 --- a/doc/api/system.md +++ b/doc/api/system.md @@ -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 diff --git a/doc/plugin-dev/systems.md b/doc/plugin-dev/systems.md index be7bbb9a0..2586d7fce 100644 --- a/doc/plugin-dev/systems.md +++ b/doc/plugin-dev/systems.md @@ -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 diff --git a/plover/gui_qt/add_translation_widget.py b/plover/gui_qt/add_translation_widget.py index 1d3643f76..d61abbff4 100644 --- a/plover/gui_qt/add_translation_widget.py +++ b/plover/gui_qt/add_translation_widget.py @@ -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 @@ -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)) diff --git a/plover/gui_qt/dictionary_editor.py b/plover/gui_qt/dictionary_editor.py index a32a7bfe0..f4cf5d87c 100644 --- a/plover/gui_qt/dictionary_editor.py +++ b/plover/gui_qt/dictionary_editor.py @@ -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 @@ -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: diff --git a/plover/gui_qt/suggestions_widget.py b/plover/gui_qt/suggestions_widget.py index c6755a25c..455aec186 100644 --- a/plover/gui_qt/suggestions_widget.py +++ b/plover/gui_qt/suggestions_widget.py @@ -18,6 +18,7 @@ ) from plover import _ +from plover.steno import display_steno from plover.translation import escape_translation from .utils import ActionCopyViewSelectionToClipboard @@ -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): diff --git a/plover/steno.py b/plover/steno.py index 1c56a25be..bf7bb0938 100644 --- a/plover/steno.py +++ b/plover/steno.py @@ -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 @@ -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): @@ -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: @@ -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): diff --git a/plover/system/__init__.py b/plover/system/__init__.py index abfb6a8bd..38b8db126 100644 --- a/plover/system/__init__.py +++ b/plover/system/__init__.py @@ -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): @@ -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 diff --git a/plover/system/english_stenotype.py b/plover/system/english_stenotype.py index 60baec14e..40a5a6f0b 100644 --- a/plover/system/english_stenotype.py +++ b/plover/system/english_stenotype.py @@ -317,3 +317,6 @@ DICTIONARIES_ROOT = 'asset:plover:assets' DEFAULT_DICTIONARIES = ('user.json', 'commands.json', 'main.json') + +def display(strokes): + return "/".join(strokes) From 0605c50012554830dd8a1849f467723233001700 Mon Sep 17 00:00:00 2001 From: Lynn Date: Mon, 5 Feb 2024 13:45:22 +0100 Subject: [PATCH 2/2] Add towncrier entry --- news.d/api/1658.new.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news.d/api/1658.new.md diff --git a/news.d/api/1658.new.md b/news.d/api/1658.new.md new file mode 100644 index 000000000..2bd84dad3 --- /dev/null +++ b/news.d/api/1658.new.md @@ -0,0 +1 @@ +System definitions can now define a function `display`, which is used to render outlines in the UI.