Fix hint position when zoom is used

This commit is contained in:
Florian Bruhin 2016-06-06 10:41:38 +02:00
parent 0e4dbd646c
commit b262580b22
2 changed files with 27 additions and 4 deletions

View File

@ -376,7 +376,7 @@ class HintManager(QObject):
elem: The QWebElement to set the style attributes for. elem: The QWebElement to set the style attributes for.
label: The label QWebElement. label: The label QWebElement.
""" """
rect = self._get_first_rectangle(elem) rect = self._get_first_rectangle(elem, adjust_zoom=False)
left = rect.x() left = rect.x()
top = rect.y() top = rect.y()
log.hints.vdebug("Drawing label '{!r}' at {}/{} for element '{!r}'" log.hints.vdebug("Drawing label '{!r}' at {}/{} for element '{!r}'"
@ -417,7 +417,7 @@ class HintManager(QObject):
message.error(self._win_id, "No suitable link found for this element.", message.error(self._win_id, "No suitable link found for this element.",
immediately=True) immediately=True)
def _get_first_rectangle(self, elem): def _get_first_rectangle(self, elem, *, adjust_zoom=True):
"""Return the element's first client rectangle with positive size. """Return the element's first client rectangle with positive size.
Uses the getClientRects() JavaScript method to obtain the collection of Uses the getClientRects() JavaScript method to obtain the collection of
@ -431,6 +431,8 @@ class HintManager(QObject):
Args: Args:
elem: The QWebElement of interest. elem: The QWebElement of interest.
adjust_zoom: Whether to adjust the element position based on the
current zoom level.
""" """
rects = elem.evaluateJavaScript("this.getClientRects()") rects = elem.evaluateJavaScript("this.getClientRects()")
log.hints.vdebug("Client rectangles of element '{}': {}" log.hints.vdebug("Client rectangles of element '{}': {}"
@ -442,7 +444,7 @@ class HintManager(QObject):
if width > 1 and height > 1: if width > 1 and height > 1:
# fix coordinates according to zoom level # fix coordinates according to zoom level
zoom = elem.webFrame().zoomFactor() zoom = elem.webFrame().zoomFactor()
if not config.get('ui', 'zoom-text-only'): if not config.get('ui', 'zoom-text-only') and adjust_zoom:
rect["left"] *= zoom rect["left"] *= zoom
rect["top"] *= zoom rect["top"] *= zoom
width *= zoom width *= zoom
@ -455,7 +457,14 @@ class HintManager(QObject):
rect.translate(frame.geometry().topLeft()) rect.translate(frame.geometry().topLeft())
frame = frame.parentFrame() frame = frame.parentFrame()
return rect return rect
return elem.rect_on_view()
# No suitable rects found via JS, try via the QWebElement API
rect = elem.rect_on_view()
zoom = elem.webFrame().zoomFactor()
if not config.get('ui', 'zoom-text-only'):
rect.setLeft(rect.left() / zoom)
rect.setTop(rect.top() / zoom)
return rect
def _click(self, elem, context): def _click(self, elem, context):
"""Click an element. """Click an element.

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Drawing hints with zoom</title>
</head>
<body>
<p>
When you press 2+ then f on this page, the hint
should be drawn at the correct position.
<a href="https://www.qutebrowser.org/">link</a>.
</p>
</body>
</html>