Skip to content

Commit

Permalink
Merge pull request #1264 from MuhammadMouradG/webview-enhancement
Browse files Browse the repository at this point in the history
Webview enhancement
  • Loading branch information
freakboy3742 authored May 23, 2021
2 parents 5384574 + 353ec95 commit 1d64bee
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/gtk/toga_gtk/widgets/webview.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
from travertino.size import at_least

from ..keys import toga_key
from ..libs import Gtk, WebKit2
from ..libs import WebKit2
from .base import Widget


class WebView(Widget):
""" GTK WebView implementation.
TODO: WebView is not displaying anything when setting a url.
"""
def create(self):
if WebKit2 is None:
raise RuntimeError(
"Import 'from gi.repository import WebKit' failed;" +
" may need to install gir1.2-webkit2-4.0 or gir1.2-webkit2-3.0.")

self.native = Gtk.ScrolledWindow()
self.native.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.native = WebKit2.WebView()
self.native.interface = self.interface

self.webview = WebKit2.WebView()

self.native.add(self.webview)
self.native.set_min_content_width(200)
self.native.set_min_content_height(200)

self.webview.connect('key-press-event', self.gtk_on_key)
self.native.connect('key-press-event', self.gtk_on_key)
self._last_key_time = 0

# self.native.connect('show', lambda event: self.rehint())

def set_on_key_down(self, handler):
pass

Expand All @@ -46,14 +38,14 @@ def gtk_on_key(self, widget, event, *args):

def set_url(self, value):
if value:
self.webview.load_uri(self.interface.url)
self.native.load_uri(self.interface.url)

def set_user_agent(self, value):
# replace user agent of webview (webview has own one)
self.webview.get_settings().props.user_agent = value
self.native.get_settings().props.user_agent = value

def set_content(self, root_url, content):
self.webview.load_html(content, root_url)
self.native.load_html(content, root_url)

def get_dom(self):
self.interface.factory.not_implemented('WebView.get_dom()')
Expand All @@ -76,12 +68,16 @@ def gtk_js_finished(webview, task, *user_data):

# Invoke the javascript method, with a callback that will set
# the future when a result is available.
self.webview.run_javascript(javascript, None, gtk_js_finished)
self.native.run_javascript(javascript, None, gtk_js_finished)

# wait for the future, and return the result
await future
return future.result()

def invoke_javascript(self, javascript):
# Invoke the javascript without a callback.
self.webview.run_javascript(javascript, None, None)
self.native.run_javascript(javascript, None, None)

def rehint(self):
self.interface.intrinsic.width = at_least(self.interface.MIN_WIDTH)
self.interface.intrinsic.height = at_least(self.interface.MIN_HEIGHT)

0 comments on commit 1d64bee

Please sign in to comment.