diff --git a/iamreader/rc/shortcuts.py b/iamreader/rc/shortcuts.py index 7d3ef22..c52beb8 100644 --- a/iamreader/rc/shortcuts.py +++ b/iamreader/rc/shortcuts.py @@ -1,6 +1,6 @@ from functools import partial from tkinter import Event -from typing import NamedTuple, List, Callable +from typing import List, Callable from .actions import TypeAction, CheckpointAction, ChapterAction, FootnoteAction @@ -12,12 +12,18 @@ ACTION_FOOTNOTE_END = FootnoteAction(begin=False) +class Shortcut: -class Shortcut(NamedTuple): + obj_registry: List['Shortcut'] = [] - keys: List[str] - hint: str - func: Callable + def __init__(self, *, keys: List[str], label: str, hint: str, func: Callable): + self.keys = keys + """Key names: https://docs.huihoo.com/tkinter/tkinter-reference-a-gui-for-python/key-names.html""" + self.label = label + self.hint = hint + self.func = func + + self.__class__.obj_registry.append(self) def label_action(event: Event, *, action: TypeAction, ui: 'RemoteControlUi'): @@ -76,63 +82,80 @@ def label_next(event: Event, *, ui: 'RemoteControlUi'): play(event, ui=ui) -SHORTCUTS: List[Shortcut] = [ - Shortcut( - keys=['e'], - hint='Add a checkpoint', - func=partial(label_action, action=CheckpointAction()) - ), - Shortcut( - keys=['t'], - hint='Record from previous label', - func=rerecord - ), - Shortcut( - keys=['a'], - hint='Record', - func=record - ), - Shortcut( - keys=['s'], - hint='Go to previous label', - func=label_prev - ), - Shortcut( - keys=['d'], - hint='Stop', - func=stop - ), - Shortcut( - keys=['f'], - hint='Go to next label', - func=label_next - ), - Shortcut( - keys=['w'], - hint='Mark a new chapter start', - func=partial(label_action, action=ChapterAction()) - ), - Shortcut( - keys=['q'], - hint='Mark a footnote region start/end', - func=mark_footnote - ), - Shortcut( - keys=['Insert'], - hint='Save project', - func=save - ), - Shortcut( - keys=['minus'], - hint='Decrement playback speed', - func=speed_dec - ), - Shortcut( - keys=['equal'], - hint='Increment playback speed', - func=speed_inc - ), -] -""" -Key names: https://docs.huihoo.com/tkinter/tkinter-reference-a-gui-for-python/key-names.html -""" +SC_MARK = Shortcut( + label='✓', + keys=['e'], + hint='Add a checkpoint mark', + func=partial(label_action, action=CheckpointAction()) +) + +SC_RERECORD = Shortcut( + label='←⚫', + keys=['t'], + hint='Record from previous label', + func=rerecord +) + +SC_RECORD = Shortcut( + label='⚫', + keys=['a'], + hint='Record', + func=record +) + + +SC_PREV = Shortcut( + label='←', + keys=['s'], + hint='Go to previous label', + func=label_prev +) + +SC_STOP = Shortcut( + label='⏹', + keys=['d'], + hint='Stop', + func=stop +) + +SC_NEXT = Shortcut( + label='→', + keys=['f'], + hint='Go to next label', + func=label_next +) + +SC_CHAPT = Shortcut( + label='§', + keys=['w'], + hint='Mark a new chapter start', + func=partial(label_action, action=ChapterAction()) +) + +SC_FOOT = Shortcut( + label='※', + keys=['q'], + hint='Mark a footnote region start/end', + func=mark_footnote +) + +SC_SAVE = Shortcut( + label='🖫', + keys=['F2'], + hint='Save project', + func=save +) + +SC_DECR = Shortcut( + label='⯯', + keys=['-'], + hint='Decrement playback speed', + func=speed_dec +) + +SC_INCR = Shortcut( + label='⯭', + keys=['+'], + hint='Increment playback speed', + func=speed_inc +) diff --git a/iamreader/rc/ui.py b/iamreader/rc/ui.py index b0a0062..d2b9e0e 100644 --- a/iamreader/rc/ui.py +++ b/iamreader/rc/ui.py @@ -1,10 +1,12 @@ from datetime import datetime, timedelta from functools import partial -from tkinter import Tk, Frame, Label, Event, Text, END, DISABLED -from typing import Callable +from tkinter import Tk, Frame, Label, Event, Button +from typing import Callable, List from .audacity import RemoteControl, TypeAction -from .shortcuts import SHORTCUTS +from .shortcuts import ( + Shortcut, SC_MARK, SC_RERECORD, SC_INCR, SC_DECR, SC_SAVE, SC_RECORD, SC_FOOT, SC_CHAPT, SC_PREV, SC_STOP, SC_NEXT +) from .state import RemoteState from ..utils import LOG @@ -75,13 +77,23 @@ def __init__(self, *, remote_control: 'RemoteControl', remote_state: 'RemoteStat statusbar.after(1000, self.on_statusbar_refresh) - hint_area = Text( - app, - takefocus=False, - font=('Arial', 8), + def place_sample_buttons(shortcuts: List[List[Shortcut]]): + for row_idx, row_items in enumerate(shortcuts): + for column_idx, shortcut in enumerate(row_items): + Button( + frame, + text=f'{shortcut.keys[0].upper()} {shortcut.label}', + + ).grid(row=row_idx, column=column_idx, sticky='nesw') + + place_sample_buttons( + [ + [SC_FOOT, SC_CHAPT, SC_MARK, SC_RERECORD], + [SC_RECORD, SC_PREV, SC_STOP, SC_NEXT], + [], + [SC_DECR, SC_INCR, SC_SAVE], + ] ) - hint_area.pack(side='top', fill='both', expand=True) - self.hint_area = hint_area frame.pack(side='top', fill='both', expand=True) @@ -151,19 +163,18 @@ def bind_event(self, *, event: str, func: Callable): def bind_shortcuts(self): LOG.debug('Binding shortcuts ...') - hint_area = self.hint_area - - for shortcut in SHORTCUTS: - hint_area.insert(END, f'{shortcut.keys} - {shortcut.hint}\n') + keymap = { + '+': 'equal', + '-': 'minus', + } + for shortcut in Shortcut.obj_registry: for key in shortcut.keys: self.bind_event( - event=key, + event=keymap.get(key, key), func=partial(shortcut.func, ui=self), ) - hint_area.config(state=DISABLED) - def label_action(self, action: TypeAction): self.rc.cmd_add_action_label( action=action,