2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# 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
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
from typing import Any
|
2020-02-15 23:22:41 +01:00
|
|
|
|
2020-01-22 01:46:35 +01:00
|
|
|
import aqt
|
2021-01-31 06:55:08 +01:00
|
|
|
from anki.sync import SyncStatus
|
2020-02-15 23:21:23 +01:00
|
|
|
from aqt import gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2020-06-02 05:23:01 +02:00
|
|
|
from aqt.sync import get_sync_status
|
2021-03-26 05:21:04 +01:00
|
|
|
from aqt.utils import tr
|
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:
|
2020-02-27 03:10:38 +01:00
|
|
|
def __init__(self, toolbar: Toolbar) -> None:
|
2020-02-08 23:59:29 +01:00
|
|
|
self.toolbar = toolbar
|
|
|
|
|
|
|
|
|
|
|
|
# wrapper class for set_bridge_command()
|
|
|
|
class BottomToolbar:
|
2020-02-27 03:10:38 +01:00
|
|
|
def __init__(self, toolbar: Toolbar) -> None:
|
2020-02-08 23:59:29 +01:00
|
|
|
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
|
2021-10-03 10:59:42 +02:00
|
|
|
self.link_handlers: dict[str, Callable] = {
|
2012-12-21 08:51:59 +01:00
|
|
|
"study": self._studyLinkHandler,
|
|
|
|
}
|
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 = "",
|
2021-10-03 10:59:42 +02:00
|
|
|
web_context: Any | None = None,
|
|
|
|
link_handler: Callable[[str], Any] | None = None,
|
2020-02-27 03:10:38 +01:00
|
|
|
) -> None:
|
2020-02-12 22:00:13 +01:00
|
|
|
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-06-02 05:23:01 +02:00
|
|
|
self._body % self._centerLinks(),
|
2020-11-01 05:26:58 +01:00
|
|
|
css=["css/toolbar.css"],
|
2020-12-28 14:18:07 +01:00
|
|
|
js=["js/webview.js", "js/vendor/jquery.min.js", "js/toolbar.js"],
|
2020-06-02 05:23:01 +02:00
|
|
|
context=web_context,
|
2020-02-12 22:00:13 +01:00
|
|
|
)
|
2017-08-02 08:22:54 +02:00
|
|
|
self.web.adjustHeightToFit()
|
2020-06-02 05:23:01 +02:00
|
|
|
|
|
|
|
def redraw(self) -> None:
|
|
|
|
self.set_sync_active(self.mw.media_syncer.is_syncing())
|
|
|
|
self.update_sync_status()
|
2020-07-10 17:38:40 +02:00
|
|
|
gui_hooks.top_toolbar_did_redraw(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Available links
|
|
|
|
######################################################################
|
|
|
|
|
2020-02-20 18:22:31 +01:00
|
|
|
def create_link(
|
|
|
|
self,
|
|
|
|
cmd: str,
|
|
|
|
label: str,
|
|
|
|
func: Callable,
|
2021-10-03 10:59:42 +02:00
|
|
|
tip: str | None = None,
|
|
|
|
id: str | None = None,
|
2020-02-20 18:22:31 +01:00
|
|
|
) -> str:
|
|
|
|
"""Generates HTML link element and registers link handler
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-20 18:22:31 +01:00
|
|
|
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
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-20 18:22:31 +01:00
|
|
|
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})
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-02-20 18:22:31 +01:00
|
|
|
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 ""
|
2020-02-20 16:15:50 +01:00
|
|
|
|
|
|
|
return (
|
2020-02-20 18:22:31 +01:00
|
|
|
f"""<a class=hitem tabindex="-1" aria-label="{label}" """
|
|
|
|
f"""{title_attr} {id_attr} href=# onclick="return pycmd('{cmd}')">"""
|
|
|
|
f"""{label}</a>"""
|
2020-02-20 16:15:50 +01:00
|
|
|
)
|
2020-02-15 23:22:41 +01:00
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _centerLinks(self) -> str:
|
2012-12-21 08:51:59 +01:00
|
|
|
links = [
|
2020-02-20 18:22:31 +01:00
|
|
|
self.create_link(
|
|
|
|
"decks",
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.actions_decks(),
|
2020-02-20 18:22:31 +01:00
|
|
|
self._deckLinkHandler,
|
2021-03-26 05:21:04 +01:00
|
|
|
tip=tr.actions_shortcut_key(val="D"),
|
2020-02-20 18:22:31 +01:00
|
|
|
id="decks",
|
|
|
|
),
|
|
|
|
self.create_link(
|
|
|
|
"add",
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.actions_add(),
|
2020-02-20 18:22:31 +01:00
|
|
|
self._addLinkHandler,
|
2021-03-26 05:21:04 +01:00
|
|
|
tip=tr.actions_shortcut_key(val="A"),
|
2020-02-20 18:22:31 +01:00
|
|
|
id="add",
|
|
|
|
),
|
|
|
|
self.create_link(
|
|
|
|
"browse",
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.qt_misc_browse(),
|
2020-02-20 18:22:31 +01:00
|
|
|
self._browseLinkHandler,
|
2021-03-26 05:21:04 +01:00
|
|
|
tip=tr.actions_shortcut_key(val="B"),
|
2020-02-20 18:22:31 +01:00
|
|
|
id="browse",
|
|
|
|
),
|
|
|
|
self.create_link(
|
|
|
|
"stats",
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.qt_misc_stats(),
|
2020-02-20 18:22:31 +01:00
|
|
|
self._statsLinkHandler,
|
2021-03-26 05:21:04 +01:00
|
|
|
tip=tr.actions_shortcut_key(val="T"),
|
2020-02-20 18:22:31 +01:00
|
|
|
id="stats",
|
|
|
|
),
|
2012-12-21 08:51:59 +01:00
|
|
|
]
|
2020-02-20 16:15:50 +01:00
|
|
|
|
|
|
|
links.append(self._create_sync_link())
|
|
|
|
|
2020-02-20 16:23:33 +01:00
|
|
|
gui_hooks.top_toolbar_did_init_links(links, self)
|
2020-02-20 16:15:50 +01:00
|
|
|
|
|
|
|
return "\n".join(links)
|
|
|
|
|
2020-06-02 05:23:01 +02:00
|
|
|
# Sync
|
|
|
|
######################################################################
|
|
|
|
|
2020-02-20 16:15:50 +01:00
|
|
|
def _create_sync_link(self) -> str:
|
2021-03-26 04:48:26 +01:00
|
|
|
name = tr.qt_misc_sync()
|
2021-03-26 05:21:04 +01:00
|
|
|
title = tr.actions_shortcut_key(val="Y")
|
2020-02-04 02:41:20 +01:00
|
|
|
label = "sync"
|
2020-02-20 16:15:50 +01:00
|
|
|
self.link_handlers[label] = self._syncLinkHandler
|
|
|
|
|
2020-02-04 02:41:20 +01:00
|
|
|
return f"""
|
2020-02-20 16:34:02 +01:00
|
|
|
<a class=hitem tabindex="-1" aria-label="{name}" title="{title}" id="{label}" href=# onclick="return pycmd('{label}')">{name}
|
2020-02-04 02:41:20 +01:00
|
|
|
<img id=sync-spinner src='/_anki/imgs/refresh.svg'>
|
|
|
|
</a>"""
|
|
|
|
|
|
|
|
def set_sync_active(self, active: bool) -> None:
|
2021-04-13 19:59:00 +02:00
|
|
|
method = "add" if active else "remove"
|
2021-04-13 20:29:59 +02:00
|
|
|
self.web.eval(
|
|
|
|
f"document.getElementById('sync-spinner').classList.{method}('spin')"
|
|
|
|
)
|
2020-02-04 02:41:20 +01:00
|
|
|
|
2020-06-02 05:23:01 +02:00
|
|
|
def set_sync_status(self, status: SyncStatus) -> None:
|
|
|
|
self.web.eval(f"updateSyncColor({status.required})")
|
|
|
|
|
|
|
|
def update_sync_status(self) -> None:
|
|
|
|
get_sync_status(self.mw, self.mw.toolbar.set_sync_status)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Link handling
|
|
|
|
######################################################################
|
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _linkHandler(self, link: str) -> bool:
|
2012-12-21 08:51:59 +01:00
|
|
|
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
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _deckLinkHandler(self) -> None:
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.moveToState("deckBrowser")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _studyLinkHandler(self) -> None:
|
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
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _addLinkHandler(self) -> None:
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.onAddCard()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _browseLinkHandler(self) -> None:
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.onBrowse()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _statsLinkHandler(self) -> None:
|
2012-12-23 04:28:06 +01:00
|
|
|
self.mw.onStats()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 03:10:38 +01:00
|
|
|
def _syncLinkHandler(self) -> None:
|
2020-05-31 02:53:54 +02:00
|
|
|
self.mw.on_sync_button_clicked()
|
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 = "",
|
2021-10-03 10:59:42 +02:00
|
|
|
web_context: Any | None = None,
|
|
|
|
link_handler: Callable[[str], Any] | None = None,
|
2020-02-27 03:10:38 +01:00
|
|
|
) -> 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,
|
2020-11-01 05:26:58 +01:00
|
|
|
css=["css/toolbar.css", "css/toolbar-bottom.css"],
|
2020-02-12 22:00:13 +01:00
|
|
|
context=web_context,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2017-08-02 08:22:54 +02:00
|
|
|
self.web.adjustHeightToFit()
|