Refactor search method of AbstractBrowserTab into a field
This commit is contained in:
parent
02f79c2990
commit
a3d41c0467
@ -163,6 +163,8 @@ class AbstractSearch(QObject):
|
|||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._widget = None
|
self._widget = None
|
||||||
self.text = None
|
self.text = None
|
||||||
|
# Implementing classes will set this.
|
||||||
|
self.search_displayed = False
|
||||||
|
|
||||||
def search(self, text, *, ignore_case=False, reverse=False,
|
def search(self, text, *, ignore_case=False, reverse=False,
|
||||||
result_cb=None):
|
result_cb=None):
|
||||||
@ -196,10 +198,6 @@ class AbstractSearch(QObject):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def searching(self):
|
|
||||||
"""Return True if we are currently searching or not."""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractZoom(QObject):
|
class AbstractZoom(QObject):
|
||||||
|
|
||||||
|
@ -127,11 +127,11 @@ class WebEngineSearch(browsertab.AbstractSearch):
|
|||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._flags = QWebEnginePage.FindFlags(0)
|
self._flags = QWebEnginePage.FindFlags(0)
|
||||||
self._searching = False
|
self.search_displayed = False
|
||||||
|
|
||||||
def _find(self, text, flags, callback, caller):
|
def _find(self, text, flags, callback, caller):
|
||||||
"""Call findText on the widget."""
|
"""Call findText on the widget."""
|
||||||
self._searching = True
|
self.search_displayed = True
|
||||||
|
|
||||||
def wrapped_callback(found):
|
def wrapped_callback(found):
|
||||||
"""Wrap the callback to do debug logging."""
|
"""Wrap the callback to do debug logging."""
|
||||||
@ -163,12 +163,9 @@ class WebEngineSearch(browsertab.AbstractSearch):
|
|||||||
self._find(text, flags, result_cb, 'search')
|
self._find(text, flags, result_cb, 'search')
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self._searching = False
|
self.search_displayed = False
|
||||||
self._widget.findText('')
|
self._widget.findText('')
|
||||||
|
|
||||||
def searching(self):
|
|
||||||
return self._searching
|
|
||||||
|
|
||||||
def prev_result(self, *, result_cb=None):
|
def prev_result(self, *, result_cb=None):
|
||||||
# The int() here makes sure we get a copy of the flags.
|
# The int() here makes sure we get a copy of the flags.
|
||||||
flags = QWebEnginePage.FindFlags(int(self._flags))
|
flags = QWebEnginePage.FindFlags(int(self._flags))
|
||||||
@ -256,10 +253,10 @@ class WebEngineCaret(browsertab.AbstractCaret):
|
|||||||
def _follow_selected_cb(self, js_elem, tab=False):
|
def _follow_selected_cb(self, js_elem, tab=False):
|
||||||
"""Callback for javascript which clicks the selected element.
|
"""Callback for javascript which clicks the selected element.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
js_elems: The elements serialized from javascript.
|
js_elems: The elements serialized from javascript.
|
||||||
tab: Open in a new tab or not.
|
tab: Open in a new tab or not.
|
||||||
"""
|
"""
|
||||||
if js_elem is None:
|
if js_elem is None:
|
||||||
return
|
return
|
||||||
assert isinstance(js_elem, dict), js_elem
|
assert isinstance(js_elem, dict), js_elem
|
||||||
@ -276,7 +273,7 @@ class WebEngineCaret(browsertab.AbstractCaret):
|
|||||||
elem.click(click_type)
|
elem.click(click_type)
|
||||||
|
|
||||||
def follow_selected(self, *, tab=False):
|
def follow_selected(self, *, tab=False):
|
||||||
if self._tab.search.searching():
|
if self._tab.search.search_displayed:
|
||||||
# We are currently in search mode.
|
# We are currently in search mode.
|
||||||
# let's click the link via a fake-click
|
# let's click the link via a fake-click
|
||||||
# https://bugreports.qt.io/browse/QTBUG-60673
|
# https://bugreports.qt.io/browse/QTBUG-60673
|
||||||
|
@ -103,7 +103,7 @@ class WebKitSearch(browsertab.AbstractSearch):
|
|||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._flags = QWebPage.FindFlags(0)
|
self._flags = QWebPage.FindFlags(0)
|
||||||
self._searching = True
|
self.search_displayed = True
|
||||||
|
|
||||||
def _call_cb(self, callback, found, text, flags, caller):
|
def _call_cb(self, callback, found, text, flags, caller):
|
||||||
"""Call the given callback if it's non-None.
|
"""Call the given callback if it's non-None.
|
||||||
@ -133,14 +133,14 @@ class WebKitSearch(browsertab.AbstractSearch):
|
|||||||
QTimer.singleShot(0, functools.partial(callback, found))
|
QTimer.singleShot(0, functools.partial(callback, found))
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self._searching = False
|
self.search_displayed = False
|
||||||
# We first clear the marked text, then the highlights
|
# We first clear the marked text, then the highlights
|
||||||
self._widget.findText('')
|
self._widget.findText('')
|
||||||
self._widget.findText('', QWebPage.HighlightAllOccurrences)
|
self._widget.findText('', QWebPage.HighlightAllOccurrences)
|
||||||
|
|
||||||
def search(self, text, *, ignore_case=False, reverse=False,
|
def search(self, text, *, ignore_case=False, reverse=False,
|
||||||
result_cb=None):
|
result_cb=None):
|
||||||
self._searching = True
|
self.search_displayed = True
|
||||||
flags = QWebPage.FindWrapsAroundDocument
|
flags = QWebPage.FindWrapsAroundDocument
|
||||||
if ignore_case == 'smart':
|
if ignore_case == 'smart':
|
||||||
if not text.islower():
|
if not text.islower():
|
||||||
@ -158,12 +158,12 @@ class WebKitSearch(browsertab.AbstractSearch):
|
|||||||
self._call_cb(result_cb, found, text, flags, 'search')
|
self._call_cb(result_cb, found, text, flags, 'search')
|
||||||
|
|
||||||
def next_result(self, *, result_cb=None):
|
def next_result(self, *, result_cb=None):
|
||||||
self._searching = True
|
self.search_displayed = True
|
||||||
found = self._widget.findText(self.text, self._flags)
|
found = self._widget.findText(self.text, self._flags)
|
||||||
self._call_cb(result_cb, found, self.text, self._flags, 'next_result')
|
self._call_cb(result_cb, found, self.text, self._flags, 'next_result')
|
||||||
|
|
||||||
def prev_result(self, *, result_cb=None):
|
def prev_result(self, *, result_cb=None):
|
||||||
self._searching = True
|
self.search_displayed = True
|
||||||
# The int() here makes sure we get a copy of the flags.
|
# The int() here makes sure we get a copy of the flags.
|
||||||
flags = QWebPage.FindFlags(int(self._flags))
|
flags = QWebPage.FindFlags(int(self._flags))
|
||||||
if flags & QWebPage.FindBackward:
|
if flags & QWebPage.FindBackward:
|
||||||
@ -173,9 +173,6 @@ class WebKitSearch(browsertab.AbstractSearch):
|
|||||||
found = self._widget.findText(self.text, flags)
|
found = self._widget.findText(self.text, flags)
|
||||||
self._call_cb(result_cb, found, self.text, flags, 'prev_result')
|
self._call_cb(result_cb, found, self.text, flags, 'prev_result')
|
||||||
|
|
||||||
def searching(self):
|
|
||||||
return self._searching
|
|
||||||
|
|
||||||
|
|
||||||
class WebKitCaret(browsertab.AbstractCaret):
|
class WebKitCaret(browsertab.AbstractCaret):
|
||||||
|
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
/* Select all elements marked with toselect */
|
/* Select all elements marked with toselect */
|
||||||
|
|
||||||
|
|
||||||
var toSelect = document.getElementsByClassName("toselect");
|
var toSelect = document.getElementsByClassName("toselect");
|
||||||
var s = window.getSelection();
|
var s = window.getSelection();
|
||||||
|
|
||||||
if(s.rangeCount > 0) s.removeAllRanges();
|
if(s.rangeCount > 0) s.removeAllRanges();
|
||||||
|
|
||||||
for(var i = 0; i < toSelect.length; i++) {
|
for(var i = 0; i < toSelect.length; i++) {
|
||||||
var range = document.createRange();
|
var range = document.createRange();
|
||||||
range.selectNode(toSelect[i]);
|
range.selectNode(toSelect[i]);
|
||||||
s.addRange(range);
|
s.addRange(range);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user