# Copyright: Ankitects Pty Ltd and contributors # -*- coding: utf-8 -*- # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html from __future__ import annotations from typing import Dict import aqt from anki.lang import _ from aqt import gui_hooks from aqt.qt import * from aqt.webview import AnkiWebView # wrapper class for set_bridge_command() class TopToolbar: def __init__(self, toolbar: Toolbar): self.toolbar = toolbar # wrapper class for set_bridge_command() class BottomToolbar: def __init__(self, toolbar: Toolbar): self.toolbar = toolbar class Toolbar: def __init__(self, mw: aqt.AnkiQt, web: AnkiWebView) -> None: self.mw = mw self.web = web self.link_handlers: Dict[str, Callable] = { "decks": self._deckLinkHandler, "study": self._studyLinkHandler, "add": self._addLinkHandler, "browse": self._browseLinkHandler, "stats": self._statsLinkHandler, "sync": self._syncLinkHandler, } self.web.setFixedHeight(30) self.web.requiresCol = False def draw(self): self.web.set_bridge_command(self._linkHandler, TopToolbar(self)) self.web.stdHtml(self._body % self._centerLinks(), css=["toolbar.css"]) self.web.adjustHeightToFit() # Available links ###################################################################### def addLink( self, name: str, cmd: str, func: Callable, tip: str = "", ): self.link_handlers[cmd] = func return (cmd, name, tip) def _centerLinks(self): links = [ ("decks", _("Decks"), _("Shortcut key: %s") % "D"), ("add", _("Add"), _("Shortcut key: %s") % "A"), ("browse", _("Browse"), _("Shortcut key: %s") % "B"), ("stats", _("Stats"), _("Shortcut key: %s") % "T"), ("sync", _("Sync"), _("Shortcut key: %s") % "Y"), ] gui_hooks.top_toolbar_did_init_links(links, self) return self._linkHTML(links) def _linkHTML(self, links): buf = "" for ln, name, title in links: buf += """ %s""" % ( name, title, ln, name, ) return buf # Link handling ###################################################################### def _linkHandler(self, link): if link in self.link_handlers: self.link_handlers[link]() return False def _deckLinkHandler(self): self.mw.moveToState("deckBrowser") def _studyLinkHandler(self): # if overview already shown, switch to review if self.mw.state == "overview": self.mw.col.startTimebox() self.mw.moveToState("review") else: self.mw.onOverview() def _addLinkHandler(self): self.mw.onAddCard() def _browseLinkHandler(self): self.mw.onBrowse() def _statsLinkHandler(self): self.mw.onStats() def _syncLinkHandler(self): self.mw.onSync() # HTML & CSS ###################################################################### _body = """
""" # Bottom bar ###################################################################### class BottomBar(Toolbar): _centerBody = """
""" def draw(self, buf): # note: some screens may override this self.web.set_bridge_command(self._linkHandler, BottomToolbar(self)) self.web.stdHtml( self._centerBody % buf, css=["toolbar.css", "toolbar-bottom.css"] ) self.web.adjustHeightToFit()