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 dataclasses import dataclass
from typing import Any, Dict, Optional
import aqt
@ -26,26 +25,6 @@ class BottomToolbar:
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:
def __init__(self, mw: aqt.AnkiQt, web: AnkiWebView) -> None:
self.mw = mw
@ -75,52 +54,73 @@ class Toolbar:
# Available links
######################################################################
def create_link(self, link: ToolbarLink):
self.link_handlers[link.cmd] = link.func
def create_link(
self,
cmd: str,
label: str,
func: Callable,
tip: Optional[str] = None,
id: Optional[str] = None,
) -> str:
"""Generates HTML link element and registers link handler
title_attr = f'title="{link.tip}"' if link.tip else ""
id_attr = f"id={link.id}" if link.id else ""
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
"""
self.link_handlers[cmd] = func
title_attr = f'title="{tip}"' if tip else ""
id_attr = f'id="{id}"' if id else ""
return (
f"""<a class=hitem tabindex="-1" aria-label="{link.label}" """
f"""{title_attr} {id_attr} href=# onclick="return pycmd('{link.cmd}')">"""
f"""{link.label}</a>"""
f"""<a class=hitem tabindex="-1" aria-label="{label}" """
f"""{title_attr} {id_attr} href=# onclick="return pycmd('{cmd}')">"""
f"""{label}</a>"""
)
def _centerLinks(self):
links = [
self.create_link(link)
for link in [
ToolbarLink(
cmd="decks",
label=_("Decks"),
self.create_link(
"decks",
_("Decks"),
self._deckLinkHandler,
tip=_("Shortcut key: %s") % "D",
id="decks",
func=self._deckLinkHandler,
),
ToolbarLink(
cmd="add",
label=_("Add"),
self.create_link(
"add",
_("Add"),
self._addLinkHandler,
tip=_("Shortcut key: %s") % "A",
id="add",
func=self._addLinkHandler,
),
ToolbarLink(
cmd="browse",
label=_("Browse"),
self.create_link(
"browse",
_("Browse"),
self._browseLinkHandler,
tip=_("Shortcut key: %s") % "B",
id="browse",
func=self._browseLinkHandler,
),
ToolbarLink(
cmd="stats",
label=_("Stats"),
self.create_link(
"stats",
_("Stats"),
self._statsLinkHandler,
tip=_("Shortcut key: %s") % "T",
id="stats",
func=self._statsLinkHandler,
),
]
]
links.append(self._create_sync_link())