Refactor ToolbarLink dataclass into create_link arguments

ToolbarLink was more of a vestigial left-over from an interim
implementation. This change simplifies link addition and brings
it closer in line with adding buttons in the editor screen
This commit is contained in:
Glutanimate 2020-02-20 18:22:31 +01:00
parent 075a2792f5
commit 496548d886

View file

@ -4,7 +4,6 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
import aqt import aqt
@ -26,26 +25,6 @@ class BottomToolbar:
self.toolbar = toolbar self.toolbar = toolbar
@dataclass
class ToolbarLink:
"""Bundles together the data fields used to generate a link element in
Anki's top toolbar
Attributes:
cmd {str} -- Command name used for the JS Python bridge
label {str} -- Display label of the link
func {Callable} -- Callable to be called on clicking the link
tip {Optional[str]} -- Optional tooltip text to show on hovering over the link
id: {Optional[str]} -- Optional id attribute to supply the link with
"""
cmd: str
label: str
func: Callable
tip: Optional[str] = None
id: Optional[str] = None
class Toolbar: class Toolbar:
def __init__(self, mw: aqt.AnkiQt, web: AnkiWebView) -> None: def __init__(self, mw: aqt.AnkiQt, web: AnkiWebView) -> None:
self.mw = mw self.mw = mw
@ -75,51 +54,72 @@ class Toolbar:
# Available links # Available links
###################################################################### ######################################################################
def create_link(self, link: ToolbarLink): def create_link(
self.link_handlers[link.cmd] = link.func self,
cmd: str,
label: str,
func: Callable,
tip: Optional[str] = None,
id: Optional[str] = None,
) -> str:
"""Generates HTML link element and registers link handler
Arguments:
cmd {str} -- Command name used for the JS Python bridge
label {str} -- Display label of the link
func {Callable} -- Callable to be called on clicking the link
Keyword Arguments:
tip {Optional[str]} -- Optional tooltip text to show on hovering
over the link (default: {None})
id: {Optional[str]} -- Optional id attribute to supply the link with
(default: {None})
Returns:
str -- HTML link element
"""
title_attr = f'title="{link.tip}"' if link.tip else "" self.link_handlers[cmd] = func
id_attr = f"id={link.id}" if link.id else ""
title_attr = f'title="{tip}"' if tip else ""
id_attr = f'id="{id}"' if id else ""
return ( return (
f"""<a class=hitem tabindex="-1" aria-label="{link.label}" """ f"""<a class=hitem tabindex="-1" aria-label="{label}" """
f"""{title_attr} {id_attr} href=# onclick="return pycmd('{link.cmd}')">""" f"""{title_attr} {id_attr} href=# onclick="return pycmd('{cmd}')">"""
f"""{link.label}</a>""" f"""{label}</a>"""
) )
def _centerLinks(self): def _centerLinks(self):
links = [ links = [
self.create_link(link) self.create_link(
for link in [ "decks",
ToolbarLink( _("Decks"),
cmd="decks", self._deckLinkHandler,
label=_("Decks"), tip=_("Shortcut key: %s") % "D",
tip=_("Shortcut key: %s") % "D", id="decks",
id="decks", ),
func=self._deckLinkHandler, self.create_link(
), "add",
ToolbarLink( _("Add"),
cmd="add", self._addLinkHandler,
label=_("Add"), tip=_("Shortcut key: %s") % "A",
tip=_("Shortcut key: %s") % "A", id="add",
id="add", ),
func=self._addLinkHandler, self.create_link(
), "browse",
ToolbarLink( _("Browse"),
cmd="browse", self._browseLinkHandler,
label=_("Browse"), tip=_("Shortcut key: %s") % "B",
tip=_("Shortcut key: %s") % "B", id="browse",
id="browse", ),
func=self._browseLinkHandler, self.create_link(
), "stats",
ToolbarLink( _("Stats"),
cmd="stats", self._statsLinkHandler,
label=_("Stats"), tip=_("Shortcut key: %s") % "T",
tip=_("Shortcut key: %s") % "T", id="stats",
id="stats", ),
func=self._statsLinkHandler,
),
]
] ]
links.append(self._create_sync_link()) links.append(self._create_sync_link())