This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoolbar.py
81 lines (64 loc) · 2.19 KB
/
toolbar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from dataclasses import dataclass
from typing import List, Callable
from aqt import gui_hooks
from aqt.editor import Editor
from .helpers import config, default, clean_furigana
from .reading import reading, reading_no_kanji
@dataclass(frozen=True)
class BtnCfg:
id: str
on_press: Callable[[str], str]
tip: str
@staticmethod
def __get(button_id: str):
return config['toolbar'].get(button_id, default['toolbar'][button_id])
@property
def enabled(self) -> bool:
return self.__get(self.id)['enable']
@property
def shortcut(self) -> str:
return self.__get(self.id)['shortcut']
@property
def text(self) -> str:
return self.__get(self.id)['text']
def on_button_press(func: Callable):
def decorator(editor: Editor) -> None:
if (note := editor.note) and (field_n := editor.currentField) is not None:
note.fields[field_n] = func(note.fields[field_n])
editor.loadNoteKeepingFocus()
return decorator
def create_callback(cfg: BtnCfg):
def make_toolbar_button(buttons: List[str], editor: Editor) -> None:
b = editor.addButton(
icon=None,
cmd=cfg.id,
func=on_button_press(cfg.on_press),
tip=f"{cfg.tip} ({cfg.shortcut})",
keys=cfg.shortcut,
label=cfg.text,
)
buttons.append(b)
return make_toolbar_button
def init():
buttons = (
BtnCfg(
id='furigana_button',
on_press=lambda expr: reading(clean_furigana(expr)),
tip='Generate furigana in the field',
),
BtnCfg(
id='clean_furigana_button',
on_press=clean_furigana,
tip='Clean furigana in the field',
),
BtnCfg(
id='furigana_no_kanji_button',
on_press=lambda expr: reading_no_kanji(clean_furigana(expr)),
tip='Generate furigana without kanji in the field'
)
)
for cfg in buttons:
if cfg.enabled:
gui_hooks.editor_did_init_buttons.append(create_callback(cfg))