QtWebEngine: Implement :scroll-perc via JS

This commit is contained in:
Florian Bruhin 2016-07-13 10:54:42 +02:00
parent e0ab70c8cf
commit b78b89f04f
4 changed files with 47 additions and 6 deletions

View File

@ -349,8 +349,9 @@ class AbstractScroller(QObject):
perc_changed = pyqtSignal(int, int)
def __init__(self, parent=None):
def __init__(self, tab, parent=None):
super().__init__(parent)
self._tab = tab
self._widget = None
def pos_px(self):
@ -493,7 +494,7 @@ class AbstractTab(QWidget):
objreg.register('tab', self, registry=self.registry)
# self.history = AbstractHistory(self)
# self.scroll = AbstractScroller(parent=self)
# self.scroll = AbstractScroller(self, parent=self)
# self.caret = AbstractCaret(win_id=win_id, tab=self, mode_manager=...,
# parent=self)
# self.zoom = AbstractZoom(win_id=win_id)

View File

@ -32,7 +32,7 @@ from PyQt5.QtWebEngineWidgets import QWebEnginePage
from qutebrowser.browser import browsertab
from qutebrowser.browser.webengine import webview
from qutebrowser.utils import usertypes, qtutils, log
from qutebrowser.utils import usertypes, qtutils, log, utils
class WebEnginePrinting(browsertab.AbstractPrinting):
@ -213,7 +213,13 @@ class WebEngineScroller(browsertab.AbstractScroller):
return (perc_x, perc_y)
def to_perc(self, x=None, y=None):
log.stub()
js_code = """
{scroll_js}
scroll_to_perc({x}, {y});
""".format(scroll_js=utils.read_file('javascript/scroll.js'),
x='undefined' if x is None else x,
y='undefined' if y is None else y)
self._tab.run_js_async(js_code)
def to_point(self, point):
log.stub()
@ -303,7 +309,7 @@ class WebEngineTab(browsertab.AbstractTab):
super().__init__(win_id)
widget = webview.WebEngineView()
self.history = WebEngineHistory(self)
self.scroll = WebEngineScroller()
self.scroll = WebEngineScroller(self, parent=self)
self.caret = WebEngineCaret(win_id=win_id, mode_manager=mode_manager,
tab=self, parent=self)
self.zoom = WebEngineZoom(win_id=win_id, parent=self)

View File

@ -499,7 +499,7 @@ class WebKitTab(browsertab.AbstractTab):
super().__init__(win_id)
widget = webview.WebView(win_id, self.tab_id, tab=self)
self.history = WebKitHistory(self)
self.scroll = WebKitScroller(parent=self)
self.scroll = WebKitScroller(self, parent=self)
self.caret = WebKitCaret(win_id=win_id, mode_manager=mode_manager,
tab=self, parent=self)
self.zoom = WebKitZoom(win_id=win_id, parent=self)

View File

@ -0,0 +1,34 @@
/**
* Copyright 2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
*
* This file is part of qutebrowser.
*
* qutebrowser is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* qutebrowser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
*/
function scroll_to_perc(x, y) {
var elem = document.documentElement;
var x_px = window.scrollX;
var y_px = window.scrollY;
if (x !== undefined) {
x_px = (elem.scrollWidth - elem.clientWidth) / 100 * x;
}
if (y !== undefined) {
y_px = (elem.scrollHeight - elem.clientHeight) / 100 * y;
}
window.scroll(x_px, y_px);
}