QtWebEngine: Implement pixel scroll position in JS

This commit is contained in:
Florian Bruhin 2016-07-15 13:04:44 +02:00
parent 7adc8ab2d6
commit 695864281b
2 changed files with 15 additions and 12 deletions

View File

@ -185,6 +185,7 @@ class WebEngineScroller(browsertab.AbstractScroller):
def __init__(self, tab, parent=None):
super().__init__(tab, parent)
self._pos_perc = (None, None)
self._pos_px = QPoint()
def _init_widget(self, widget):
super()._init_widget(widget)
@ -203,20 +204,20 @@ class WebEngineScroller(browsertab.AbstractScroller):
@pyqtSlot()
def _on_scroll_pos_changed(self):
def update_scroll_pos_perc(jsret):
assert len(jsret) == 2, jsret
self._pos_perc = jsret
self.perc_changed.emit(*jsret)
def update_scroll_pos(jsret):
assert isinstance(jsret, dict)
self._pos_perc = (jsret['perc']['x'], jsret['perc']['y'])
self._pos_px = QPoint(jsret['px']['x'], jsret['px']['y'])
self.perc_changed.emit(*self._pos_perc)
js_code = """
{scroll_js}
scroll_pos_perc();
scroll_pos();
""".format(scroll_js=utils.read_file('javascript/scroll.js'))
self._tab.run_js_async(js_code, update_scroll_pos_perc)
self._tab.run_js_async(js_code, update_scroll_pos)
def pos_px(self):
log.stub()
return QPoint(0, 0)
return self._pos_px
def pos_perc(self):
return self._pos_perc

View File

@ -39,7 +39,7 @@ function scroll_delta_page(x, y) {
window.scrollBy(dx, dy);
}
function scroll_pos_perc() {
function scroll_pos() {
var elem = document.documentElement;
var dx = (elem.scrollWidth - elem.clientWidth);
var dy = (elem.scrollHeight - elem.clientHeight);
@ -58,8 +58,10 @@ function scroll_pos_perc() {
perc_y = 100 / dy * window.scrollY;
}
// console.log(perc_x);
// console.log(perc_y);
var pos_perc = {'x': perc_x, 'y': perc_y};
var pos_px = {'x': window.scrollX, 'y': window.scrollY};
var pos = {'perc': pos_perc, 'px': pos_px};
return [perc_x, perc_y];
// console.log(JSON.stringify(pos));
return pos;
}