2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-02-12 22:15:44 +01:00
|
|
|
from typing import Any, Optional
|
2020-02-12 22:00:13 +01:00
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
import aqt
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2020-01-22 01:46:35 +01:00
|
|
|
from aqt.webview import AnkiWebView
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-08 23:59:29 +01:00
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2017-02-06 23:21:33 +01:00
|
|
|
class Toolbar:
|
2020-01-22 01:46:35 +01:00
|
|
|
def __init__(self, mw: aqt.AnkiQt, web: AnkiWebView) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.web = web
|
|
|
|
self.link_handlers = {
|
|
|
|
"decks": self._deckLinkHandler,
|
|
|
|
"study": self._studyLinkHandler,
|
|
|
|
"add": self._addLinkHandler,
|
|
|
|
"browse": self._browseLinkHandler,
|
2012-12-23 04:28:06 +01:00
|
|
|
"stats": self._statsLinkHandler,
|
2012-12-21 08:51:59 +01:00
|
|
|
"sync": self._syncLinkHandler,
|
|
|
|
}
|
2017-06-22 08:56:29 +02:00
|
|
|
self.web.setFixedHeight(30)
|
2018-10-12 04:08:19 +02:00
|
|
|
self.web.requiresCol = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
def draw(
|
|
|
|
self,
|
|
|
|
buf: str = "",
|
|
|
|
web_context: Optional[Any] = None,
|
|
|
|
link_handler: Optional[Callable[[str], Any]] = None,
|
|
|
|
):
|
|
|
|
web_context = web_context or TopToolbar(self)
|
|
|
|
link_handler = link_handler or self._linkHandler
|
|
|
|
self.web.set_bridge_command(link_handler, web_context)
|
|
|
|
self.web.stdHtml(
|
2020-02-12 22:15:44 +01:00
|
|
|
self._body % self._centerLinks(),
|
|
|
|
css=["_anki/toolbar.css"],
|
|
|
|
context=web_context,
|
2020-02-12 22:00:13 +01:00
|
|
|
)
|
2017-08-02 08:22:54 +02:00
|
|
|
self.web.adjustHeightToFit()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Available links
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _centerLinks(self):
|
|
|
|
links = [
|
|
|
|
["decks", _("Decks"), _("Shortcut key: %s") % "D"],
|
|
|
|
["add", _("Add"), _("Shortcut key: %s") % "A"],
|
|
|
|
["browse", _("Browse"), _("Shortcut key: %s") % "B"],
|
2018-05-31 05:05:30 +02:00
|
|
|
["stats", _("Stats"), _("Shortcut key: %s") % "T"],
|
2017-06-23 06:03:18 +02:00
|
|
|
["sync", _("Sync"), _("Shortcut key: %s") % "Y"],
|
2012-12-21 08:51:59 +01:00
|
|
|
]
|
|
|
|
return self._linkHTML(links)
|
|
|
|
|
|
|
|
def _linkHTML(self, links):
|
|
|
|
buf = ""
|
|
|
|
for ln, name, title in links:
|
2019-12-23 01:34:10 +01:00
|
|
|
buf += """
|
|
|
|
<a class=hitem tabindex="-1" aria-label="%s" title="%s" href=# onclick="return pycmd('%s')">%s</a>""" % (
|
|
|
|
name,
|
|
|
|
title,
|
|
|
|
ln,
|
|
|
|
name,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
return buf
|
|
|
|
|
|
|
|
# Link handling
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _linkHandler(self, link):
|
|
|
|
if link in self.link_handlers:
|
2016-05-31 10:51:40 +02:00
|
|
|
self.link_handlers[link]()
|
|
|
|
return False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _deckLinkHandler(self):
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.moveToState("deckBrowser")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _studyLinkHandler(self):
|
2012-12-23 04:28:06 +01:00
|
|
|
# if overview already shown, switch to review
|
|
|
|
if self.mw.state == "overview":
|
|
|
|
self.mw.col.startTimebox()
|
|
|
|
self.mw.moveToState("review")
|
|
|
|
else:
|
2019-03-04 07:54:22 +01:00
|
|
|
self.mw.onOverview()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _addLinkHandler(self):
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.onAddCard()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _browseLinkHandler(self):
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.onBrowse()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _statsLinkHandler(self):
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.onStats()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _syncLinkHandler(self):
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.onSync()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# HTML & CSS
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
_body = """
|
2016-06-07 06:27:33 +02:00
|
|
|
<center id=outer>
|
2012-12-21 08:51:59 +01:00
|
|
|
<table id=header width=100%%>
|
|
|
|
<tr>
|
2017-06-23 06:03:18 +02:00
|
|
|
<td class=tdcenter align=center>%s</td>
|
2012-12-21 08:51:59 +01:00
|
|
|
</tr></table>
|
2016-06-07 06:27:33 +02:00
|
|
|
</center>
|
2012-12-21 08:51:59 +01:00
|
|
|
"""
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-02 08:22:54 +02:00
|
|
|
# Bottom bar
|
|
|
|
######################################################################
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class BottomBar(Toolbar):
|
|
|
|
|
|
|
|
_centerBody = """
|
2016-06-07 06:27:33 +02:00
|
|
|
<center id=outer><table width=100%% id=header><tr><td align=center>
|
2012-12-21 08:51:59 +01:00
|
|
|
%s</td></tr></table></center>
|
|
|
|
"""
|
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
def draw(
|
|
|
|
self,
|
|
|
|
buf: str = "",
|
|
|
|
web_context: Optional[Any] = None,
|
|
|
|
link_handler: Optional[Callable[[str], Any]] = None,
|
|
|
|
):
|
2020-01-22 01:46:35 +01:00
|
|
|
# note: some screens may override this
|
2020-02-12 22:00:13 +01:00
|
|
|
web_context = web_context or BottomToolbar(self)
|
|
|
|
link_handler = link_handler or self._linkHandler
|
|
|
|
self.web.set_bridge_command(link_handler, web_context)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.web.stdHtml(
|
2020-02-12 22:00:13 +01:00
|
|
|
self._centerBody % buf,
|
|
|
|
css=["_anki/toolbar.css", "_anki/toolbar-bottom.css"],
|
|
|
|
context=web_context,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2017-08-02 08:22:54 +02:00
|
|
|
self.web.adjustHeightToFit()
|