resize toolbars to fit content

This commit is contained in:
Damien Elmes 2016-06-07 14:27:33 +10:00
parent d7339d9a27
commit 4e2bd3f739
7 changed files with 49 additions and 54 deletions

View file

@ -375,7 +375,6 @@ class Browser(QMainWindow):
def setupToolbar(self): def setupToolbar(self):
self.toolbarWeb = AnkiWebView() self.toolbarWeb = AnkiWebView()
self.toolbarWeb.setFixedHeight(32 + self.mw.fontHeightDelta)
self.toolbar = BrowserToolbar(self.mw, self.toolbarWeb, self) self.toolbar = BrowserToolbar(self.mw, self.toolbarWeb, self)
self.form.verticalLayout_3.insertWidget(0, self.toolbarWeb) self.form.verticalLayout_3.insertWidget(0, self.toolbarWeb)
self.toolbar.draw() self.toolbar.draw()
@ -1717,12 +1716,10 @@ class BrowserToolbar(Toolbar):
"Bulk Remove Tags (Ctrl+Alt+T)"))) "Bulk Remove Tags (Ctrl+Alt+T)")))
right += borderImg("delete", "delete16", False, _("Delete")) right += borderImg("delete", "delete16", False, _("Delete"))
right += "</div>" right += "</div>"
# fixme self.web.onBridgeCmd = self._linkHandler
#self.web.page().currentFrame().setScrollBarPolicy( self.web.onLoadFinished = self.onLoaded
# Qt.Horizontal, Qt.ScrollBarAlwaysOff)
self.web.stdHtml(self._body % ( self.web.stdHtml(self._body % (
"", #<span style='display:inline-block; width: 100px;'></span>", "",
#self._centerLinks(),
right, ""), self._css + """ right, ""), self._css + """
#header { font-weight: normal; } #header { font-weight: normal; }
a { margin-right: 1em; } a { margin-right: 1em; }

View file

@ -349,12 +349,6 @@ where id > ?""", (self.mw.col.sched.dayCutoff-86400)*1000)
buf += """ buf += """
<button title='%s' onclick='pycmd(\"%s\");'>%s</button>""" % tuple(b) <button title='%s' onclick='pycmd(\"%s\");'>%s</button>""" % tuple(b)
self.bottom.draw(buf) self.bottom.draw(buf)
if isMac:
size = 28
else:
size = 36 + self.mw.fontHeightDelta*3
self.bottom.web.setFixedHeight(size)
self.bottom.web.resetHandlers()
self.bottom.web.onBridgeCmd = self._linkHandler self.bottom.web.onBridgeCmd = self._linkHandler
def _onShared(self): def _onShared(self):

View file

@ -502,7 +502,6 @@ title="%s" %s>%s</button>''' % (
tweb = aqt.webview.AnkiWebView() tweb = aqt.webview.AnkiWebView()
tweb.setObjectName("toolbarWeb") tweb.setObjectName("toolbarWeb")
tweb.setFocusPolicy(Qt.WheelFocus) tweb.setFocusPolicy(Qt.WheelFocus)
tweb.setFixedHeight(32+self.fontHeightDelta)
self.toolbar = aqt.toolbar.Toolbar(self, tweb) self.toolbar = aqt.toolbar.Toolbar(self, tweb)
self.toolbar.draw() self.toolbar.draw()
# main area # main area
@ -512,8 +511,6 @@ title="%s" %s>%s</button>''' % (
self.web.setMinimumWidth(400) self.web.setMinimumWidth(400)
# bottom area # bottom area
sweb = self.bottomWeb = aqt.webview.AnkiWebView() sweb = self.bottomWeb = aqt.webview.AnkiWebView()
#sweb.hide()
sweb.setFixedHeight(100)
sweb.setObjectName("bottomWeb") sweb.setObjectName("bottomWeb")
sweb.setFocusPolicy(Qt.WheelFocus) sweb.setFocusPolicy(Qt.WheelFocus)
# add in a layout # add in a layout

View file

@ -201,11 +201,6 @@ text-align: center;
buf += """ buf += """
<button title="%s" onclick='pycmd("%s")'>%s</button>""" % tuple(b) <button title="%s" onclick='pycmd("%s")'>%s</button>""" % tuple(b)
self.bottom.draw(buf) self.bottom.draw(buf)
if isMac:
size = 28
else:
size = 36 + self.mw.fontHeightDelta*3
self.bottom.web.setFixedHeight(size)
self.bottom.web.onBridgeCmd = self._linkHandler self.bottom.web.onBridgeCmd = self._linkHandler
# Studying more # Studying more

View file

@ -44,11 +44,6 @@ class Reviewer(object):
self.web.resetHandlers() self.web.resetHandlers()
self.mw.keyHandler = self._keyHandler self.mw.keyHandler = self._keyHandler
self.web.onBridgeCmd = self._linkHandler self.web.onBridgeCmd = self._linkHandler
if isMac:
self.bottom.web.setFixedHeight(46)
else:
self.bottom.web.setFixedHeight(52+self.mw.fontHeightDelta*4)
self.bottom.web.resetHandlers()
self.bottom.web.onBridgeCmd = self._linkHandler self.bottom.web.onBridgeCmd = self._linkHandler
self._reps = None self._reps = None
self.nextCard() self.nextCard()
@ -182,7 +177,7 @@ function _typeAnsPress() {
self.web.stdHtml(self._revHtml, self._styles(), head=base) self.web.stdHtml(self._revHtml, self._styles(), head=base)
# show answer / ease buttons # show answer / ease buttons
self.bottom.web.show() self.bottom.web.show()
self.bottom.web.onLoadFinished = self._showAnswerButton self.bottom.web.onLoadFinished = self._onBottomLoadFinished
self.bottom.web.stdHtml( self.bottom.web.stdHtml(
self._bottomHTML(), self._bottomHTML(),
self.bottom._css + self._bottomCSS) self.bottom._css + self._bottomCSS)
@ -501,13 +496,7 @@ Please run Tools>Empty Cards""")
_bottomCSS = """ _bottomCSS = """
body { body {
background: -webkit-gradient(linear, left top, left bottom, margin: 0; padding: 0;
from(#fff), to(#ddd));
border-bottom: 0;
border-top: 1px solid #aaa;
margin: 0;
padding: 0px;
padding-left: 5px; padding-right: 5px;
} }
button { button {
min-width: 60px; white-space: nowrap; min-width: 60px; white-space: nowrap;
@ -519,11 +508,21 @@ min-width: 60px; white-space: nowrap;
.nobold { font-weight: normal; display: inline-block; padding-top: 4px; } .nobold { font-weight: normal; display: inline-block; padding-top: 4px; }
.spacer { height: 18px; } .spacer { height: 18px; }
.spacer2 { height: 16px; } .spacer2 { height: 16px; }
#outer {
border-top: 1px solid #aaa;
background: -webkit-gradient(linear, left top, left bottom,
from(#fff), to(#ddd));
}
#innertable {
padding: 3px;
}
""" """
def _bottomHTML(self): def _bottomHTML(self):
return """ return """
<table width=100%% cellspacing=0 cellpadding=0> <center id=outer>
<table id=innertable width=100%% cellspacing=0 cellpadding=0>
<tr> <tr>
<td align=left width=50 valign=top class=stat> <td align=left width=50 valign=top class=stat>
<br> <br>
@ -536,6 +535,7 @@ min-width: 60px; white-space: nowrap;
</td> </td>
</tr> </tr>
</table> </table>
</center>
<script> <script>
var time = %(time)d; var time = %(time)d;
var maxTime = 0; var maxTime = 0;
@ -584,6 +584,9 @@ function showAnswer(txt) {
downArrow=downArrow(), downArrow=downArrow(),
time=self.card.timeTaken() // 1000) time=self.card.timeTaken() // 1000)
def _onBottomLoadFinished(self):
self._showAnswerButton()
def _showAnswerButton(self): def _showAnswerButton(self):
self._bottomReady = True self._bottomReady = True
if not self.typeCorrect: if not self.typeCorrect:
@ -600,6 +603,7 @@ function showAnswer(txt) {
maxTime = 0 maxTime = 0
self.bottom.web.eval("showQuestion(%s,%d);" % ( self.bottom.web.eval("showQuestion(%s,%d);" % (
json.dumps(middle), maxTime)) json.dumps(middle), maxTime))
self.bottom.onLoaded()
def _showEaseButtons(self): def _showEaseButtons(self):
self.bottom.web.setFocus() self.bottom.web.setFocus()

View file

@ -9,8 +9,6 @@ class Toolbar(object):
def __init__(self, mw, web): def __init__(self, mw, web):
self.mw = mw self.mw = mw
self.web = web self.web = web
self.web.resetHandlers()
self.web.onBridgeCmd = self._linkHandler
self.link_handlers = { self.link_handlers = {
"decks": self._deckLinkHandler, "decks": self._deckLinkHandler,
"study": self._studyLinkHandler, "study": self._studyLinkHandler,
@ -20,7 +18,16 @@ class Toolbar(object):
"sync": self._syncLinkHandler, "sync": self._syncLinkHandler,
} }
def onLoaded(self):
self.web.evalWithCallback("$(document.body).height()", self.onHeight)
def onHeight(self, qvar):
height = int(qvar*self.web.zoomFactor())
self.web.setFixedHeight(height)
def draw(self): def draw(self):
self.web.onBridgeCmd = self._linkHandler
self.web.onLoadFinished = self.onLoaded
self.web.stdHtml(self._body % ( self.web.stdHtml(self._body % (
# may want a context menu here in the future # may want a context menu here in the future
'&nbsp;'*20, '&nbsp;'*20,
@ -99,34 +106,29 @@ class Toolbar(object):
###################################################################### ######################################################################
_body = """ _body = """
<center id=outer>
<table id=header width=100%%> <table id=header width=100%%>
<tr> <tr>
<td width=16%% align=left>%s</td> <td width=16%% align=left>%s</td>
<td align=center>%s</td> <td align=center>%s</td>
<td width=15%% align=right>%s</td> <td width=15%% valign=middle align=right>%s</td>
</tr></table> </tr></table>
</center>
""" """
_css = """ _css = """
#header { #header {
margin:0; padding:3px;
margin-top: 4px;
font-weight: bold; font-weight: bold;
} border-bottom: 1px solid #aaa;
html {
height: 100%;
background: -webkit-gradient(linear, left top, left bottom, background: -webkit-gradient(linear, left top, left bottom,
from(#ddd), to(#fff)); from(#ddd), to(#fff));
margin:0; padding:0;
} }
body { body {
margin:0; padding:0; margin:0; padding:0;
position:absolute;
top:0;left:0;right:0;bottom:0;
-webkit-user-select: none; -webkit-user-select: none;
border-bottom: 1px solid #aaa; overflow: hidden;
} }
* { -webkit-user-drag: none; } * { -webkit-user-drag: none; }
@ -139,6 +141,7 @@ color: #000;
.hitem:hover { .hitem:hover {
text-decoration: underline; text-decoration: underline;
} }
""" """
class BottomBar(Toolbar): class BottomBar(Toolbar):
@ -155,12 +158,13 @@ margin-top: 0;
""" """
_centerBody = """ _centerBody = """
<center><table width=100%% height=100%% id=header><tr><td align=center> <center id=outer><table width=100%% id=header><tr><td align=center>
%s</td></tr></table></center> %s</td></tr></table></center>
""" """
def draw(self, buf): def draw(self, buf):
self.web.show() self.web.onBridgeCmd = self._linkHandler
self.web.onLoadFinished = self.onLoaded
self.web.stdHtml( self.web.stdHtml(
self._centerBody % buf, self._centerBody % buf,
self._css) self._css)

View file

@ -134,16 +134,17 @@ class AnkiWebView(QWebEngineView):
if oldFocus: if oldFocus:
oldFocus.setFocus() oldFocus.setFocus()
def zoomFactor(self):
screen = QApplication.desktop().screen()
dpi = screen.logicalDpiX()
return max(1, dpi / 96.0)
def stdHtml(self, body, css="", bodyClass="", js=None, head=""): def stdHtml(self, body, css="", bodyClass="", js=None, head=""):
if isMac: if isMac:
button = "font-weight: bold; height: 24px;" button = "font-weight: bold; height: 24px;"
else: else:
button = "font-weight: normal;" button = "font-weight: normal;"
screen = QApplication.desktop().screen()
dpi = screen.logicalDpiX()
zoomFactor = max(1, dpi / 96.0)
self.setHtml(""" self.setHtml("""
<!doctype html> <!doctype html>
<html><head><style> <html><head><style>
@ -159,7 +160,7 @@ button {
</head> </head>
<body class="%s">%s</body></html>""" % ( <body class="%s">%s</body></html>""" % (
zoomFactor, button, css, js or anki.js.jquery+anki.js.browserSel, self.zoomFactor(), button, css, js or anki.js.jquery+anki.js.browserSel,
head, bodyClass, body)) head, bodyClass, body))
def setCanFocus(self, canFocus=False): def setCanFocus(self, canFocus=False):
@ -172,6 +173,9 @@ button {
def eval(self, js): def eval(self, js):
self.page().runJavaScript(js) self.page().runJavaScript(js)
def evalWithCallback(self, js, cb):
self.page().runJavaScript(js, cb)
def _openLinksExternally(self, url): def _openLinksExternally(self, url):
openLink(url) openLink(url)