Commit a12e5f42 authored by Cédric Bellegarde's avatar Cédric Bellegarde

All snapshot work is done in WebViewArtwork

parent d1997f93
......@@ -14,7 +14,6 @@ from gi.repository import Gtk, GLib, WebKit2, Pango
from eolie.label_indicator import LabelIndicator
from eolie.define import App, ArtSize
from eolie.utils import get_snapshot
class PagesManagerChild(Gtk.FlowBoxChild):
......@@ -72,39 +71,20 @@ class PagesManagerChild(Gtk.FlowBoxChild):
ArtSize.PREVIEW_WIDTH_MARGIN)
self.connect("query-tooltip", self.__on_query_tooltip)
view.connect("destroying", self.__on_view_destroying)
self.__connected_signals = []
self.__connected_signals.append(
self.__view.webview.connect("favicon-changed",
self.__on_webview_favicon_changed))
self.__connected_signals.append(
self.__view.webview.connect(
"notify::is-playing-audio",
self.__on_webview_notify_is_playing_audio))
self.__connected_signals.append(
self.__view.webview.connect("uri-changed",
self.__on_webview_uri_changed))
self.__connected_signals.append(
self.__view.webview.connect("title-changed",
self.__on_webview_title_changed))
self.__connected_signals.append(
self.__view.webview.connect("scroll-event",
self.__on_webview_scroll_event))
self.__connected_signals.append(
self.__view.webview.connect("load-changed",
self.__on_webview_load_changed))
self.__connected_signals.append(
self.__view.webview.connect("shown",
self.__on_webview_shown))
self.__view.webview.connect("snapshot-changed",
self.__on_webview_snapshot_changed)
self.__view.webview.connect("favicon-changed",
self.__on_webview_favicon_changed)
self.__view.webview.connect("notify::is-playing-audio",
self.__on_webview_notify_is_playing_audio)
self.__view.webview.connect("title-changed",
self.__on_webview_title_changed)
self.__view.webview.connect("load-changed",
self.__on_webview_load_changed)
self.__view.webview.connect("shown",
self.__on_webview_shown)
self.__on_webview_favicon_changed(self.__view.webview)
def destroy(self):
"""
Disconnect signals and destroy self
"""
for signal_id in self.__connected_signals:
self.__view.webview.disconnect(signal_id)
Gtk.FlowBoxChild.destroy(self)
@property
def view(self):
"""
......@@ -190,29 +170,6 @@ class PagesManagerChild(Gtk.FlowBoxChild):
elif hasattr(widget, "forall"):
GLib.idle_add(widget.forall, self.__update_popover_internals)
def __set_snapshot(self):
"""
Set webpage preview
"""
if self.__view.webview.ephemeral:
self.__image.set_from_icon_name(
"user-not-tracked-symbolic",
Gtk.IconSize.DIALOG)
else:
self.__view.webview.get_snapshot(
WebKit2.SnapshotRegion.VISIBLE,
WebKit2.SnapshotOptions.NONE,
None,
get_snapshot,
self.__on_snapshot)
def __on_scroll_timeout(self):
"""
Update snapshot
"""
self.__scroll_timeout_id = None
self.__set_snapshot()
def __on_query_tooltip(self, widget, x, y, keyboard, tooltip):
"""
Show tooltip if needed
......@@ -277,34 +234,6 @@ class PagesManagerChild(Gtk.FlowBoxChild):
image.set_from_icon_name("applications-internet", Gtk.IconSize.INVALID)
def __on_webview_scroll_event(self, webview, event):
"""
Update snapshot
@param webview as WebView
@param event as Gdk.EventScroll
"""
if self.__scroll_timeout_id is not None:
GLib.source_remove(self.__scroll_timeout_id)
self.__scroll_timeout_id = GLib.timeout_add(250,
self.__on_scroll_timeout)
def __on_snapshot(self, surface):
"""
Set snapshot
@param surface as cairo.Surface
"""
self.__image.set_from_surface(surface)
def __on_webview_uri_changed(self, webview, uri):
"""
Update uri
@param webview as WebView
@param uri as str
"""
# Js change, update snapshot
if not webview.is_loading() and not webview.ephemeral:
GLib.timeout_add(500, self.__set_snapshot)
def __on_webview_title_changed(self, webview, title):
"""
Update title
......@@ -329,7 +258,19 @@ class PagesManagerChild(Gtk.FlowBoxChild):
self.__indicator_label.set_text(uri)
elif event == WebKit2.LoadEvent.FINISHED:
self.__spinner.stop()
GLib.idle_add(self.__set_snapshot)
def __on_webview_snapshot_changed(self, webview, surface):
"""
Update preview with surface
@param webview as WebView
@param surface as cairo.surface
"""
if self.__view.webview.ephemeral:
self.__image.set_from_icon_name(
"user-not-tracked-symbolic",
Gtk.IconSize.DIALOG)
else:
self.__image.set_from_surface(surface)
def __on_webview_shown(self, webview):
"""
......
......@@ -31,27 +31,14 @@ class WebViewArtwork:
"""
self.__helper = TaskHelper()
self.__snapshot_id = None
self.__scroll_timeout_id = None
self.__save_favicon_timeout_id = None
self.__cancellable = Gio.Cancellable()
self.__initial_uri = None
self.__favicon_width = {}
self.__save_favicon_timeout_id = None
self.__current_netloc = None
def set_snapshot(self):
"""
Set webpage preview
"""
self.stop_snapshot()
if not self.ephemeral and not self._error:
self.__snapshot_id = GLib.timeout_add(3000, self.__set_snapshot)
def stop_snapshot(self):
"""
Stop pending snapshot loading
"""
if self.__snapshot_id is not None:
GLib.source_remove(self.__snapshot_id)
self.__snapshot_id = None
self.connect("notify::uri", self.__on_uri_changed)
self.connect("scroll-event", self.__on_webview_scroll_event)
def set_favicon(self):
"""
......@@ -108,7 +95,9 @@ class WebViewArtwork:
self.__current_netloc not in self._uri:
self.emit("favicon-changed", None)
elif event == WebKit2.LoadEvent.FINISHED:
if parsed.scheme in ["http", "https"]:
is_http = parsed.scheme in ["http", "https"]
GLib.idle_add(self.__set_snapshot, is_http)
if is_http:
favicon_database = self.context.get_favicon_database()
GLib.timeout_add(2000,
favicon_database.get_favicon,
......@@ -123,9 +112,10 @@ class WebViewArtwork:
#######################
# PRIVATE #
#######################
def __set_snapshot(self):
def __set_snapshot(self, save):
"""
Set webpage preview
@param save as bool
"""
self.__snapshot_id = None
self.get_snapshot(WebKit2.SnapshotRegion.FULL_DOCUMENT,
......@@ -133,6 +123,7 @@ class WebViewArtwork:
self.__cancellable,
get_snapshot,
self.__on_snapshot,
save,
True)
def __set_favicon_from_surface(self, surface, uri, initial_uri, builtin):
......@@ -202,6 +193,15 @@ class WebViewArtwork:
surface,
favicon_type)
def __on_uri_changed(self, webview, param):
"""
Handle JS updates
@param webview as WebKit2.WebView
@param param as GObject.ParamSpec
"""
if not webview.is_loading() and not webview.ephemeral:
GLib.timeout_add(500, self.__set_snapshot, True)
def __on_get_favicon(self, favicon_db, result, uri, initial_uri, builtin):
"""
Read favicon and set it
......@@ -217,11 +217,30 @@ class WebViewArtwork:
surface = None
self.__set_favicon_from_surface(surface, uri, initial_uri, builtin)
def __on_snapshot(self, surface, first_pass):
def __on_scroll_timeout(self):
"""
Update snapshot
"""
self.__scroll_timeout_id = None
self.__set_snapshot(False)
def __on_webview_scroll_event(self, webview, event):
"""
Update snapshot
@param webview as WebView
@param event as Gdk.EventScroll
"""
if self.__scroll_timeout_id is not None:
GLib.source_remove(self.__scroll_timeout_id)
self.__scroll_timeout_id = GLib.timeout_add(250,
self.__on_scroll_timeout)
def __on_snapshot(self, surface, save, first_pass):
"""
Cache snapshot
@param surface as cairo.Surface
@param uri as str
@param save as bool
@param first_pass as bool
"""
# The 32767 limit on the width/height dimensions
......@@ -234,8 +253,12 @@ class WebViewArtwork:
self.__cancellable,
get_snapshot,
self.__on_snapshot,
save,
False)
return
self.emit("snapshot-changed", surface)
if not save:
return
uri = self.uri
# We also cache initial URI
uris = [uri.rstrip("/")]
......
......@@ -176,8 +176,6 @@ class WebViewNavigation:
self.update_spell_checking(self._uri)
self.run_javascript_from_gresource(
"/org/gnome/Eolie/Extensions.js", None, None)
if parsed.scheme in ["http", "https"]:
self.set_snapshot()
if App().show_tls:
try:
from OpenSSL import crypto
......@@ -275,12 +273,11 @@ class WebViewNavigation:
def __on_uri_changed(self, webview, param):
"""
Stop running background task and update favicon
Handle JS updates
@param webview as WebKit2.WebView
@param param as GObject.ParamSpec
"""
uri = webview.get_property(param.name)
self.stop_snapshot()
# JS bookmark (Bookmarklet)
if not uri.startswith("javascript:"):
self.emit("uri-changed", uri)
......
......@@ -36,6 +36,8 @@ class WebViewSignals(WebViewMenuSignals, WebViewJsSignals,
"uri-changed": (GObject.SignalFlags.RUN_FIRST, None, (str,)),
"favicon-changed": (GObject.SignalFlags.RUN_FIRST, None,
(GObject.TYPE_PYOBJECT,)),
"snapshot-changed": (GObject.SignalFlags.RUN_FIRST, None,
(GObject.TYPE_PYOBJECT,)),
}
for signal in gsignals:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment