Skip to content

Instantly share code, notes, and snippets.

@lauromoura
Created September 18, 2021 00:03
Show Gist options
  • Save lauromoura/2c7864138472532db3243ad3f5ba0d48 to your computer and use it in GitHub Desktop.
Save lauromoura/2c7864138472532db3243ad3f5ba0d48 to your computer and use it in GitHub Desktop.
WebKitGTK Hello World
#!/usr/bin/env python3
from urllib.parse import urlparse
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.0")
from gi.repository import Gtk
from gi.repository import WebKit2
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Hello World")
self.resize(width=800, height=600)
self.current_url = self.home_url = "https://www.google.com.br"
self.address_bar = Gtk.Box(
orientation=Gtk.Orientation.HORIZONTAL, homogeneous=False, spacing=2
)
home_button = Gtk.Button.new_from_icon_name(
icon_name="go-home", size=Gtk.IconSize.LARGE_TOOLBAR
)
home_button.connect("clicked", self.on_home_button_clicked)
self.address_bar.pack_start(
home_button, expand=False, fill=False, padding=1
)
self.location_entry = Gtk.Entry(text=self.current_url)
self.location_entry.connect("activate", self.on_entry_activated)
self.address_bar.pack_end(
self.location_entry, expand=True, fill=True, padding=1
)
self.vbox = Gtk.Box(
orientation=Gtk.Orientation.VERTICAL, homogeneous=False, spacing=1
)
self.status_bar = Gtk.Statusbar()
self.status_bar.show()
self.status_context_id = self.status_bar.get_context_id("Something")
self.view = WebKit2.WebView()
self.view.load_uri(self.current_url)
self.view.connect("mouse-target-changed", self.mouse_target_changed)
self.view.connect("load-changed", self.load_changed)
self.hover_target = ""
self.vbox.pack_start(self.address_bar, expand=False, fill=False, padding=1)
self.vbox.pack_start(self.view, expand=True, fill=True, padding=1)
self.vbox.pack_end(self.status_bar, expand=False, fill=False, padding=1)
self.add(self.vbox)
def on_entry_activated(self, _widget):
url = self.location_entry.get_text()
parsed = urlparse(url, scheme="http")
self.view.load_uri(parsed.geturl())
def on_home_button_clicked(self, _widget):
self.view.load_uri(self.home_url)
def load_changed(self, _widget, load_event):
print(load_event)
if load_event == WebKit2.LoadEvent.STARTED:
self.current_url = self.view.get_uri()
self.set_status("Waiting for " + self.current_url + "...")
self.location_entry.set_text(self.current_url)
elif load_event == WebKit2.LoadEvent.REDIRECTED:
pass
elif load_event == WebKit2.LoadEvent.COMMITTED:
self.current_url = self.view.get_uri()
self.set_status("Transferring from " + self.current_url + "...")
self.location_entry.set_text(self.current_url)
elif load_event == WebKit2.LoadEvent.FINISHED:
self.set_status("Done.")
def mouse_target_changed(self, _widget, hit_test_result, modifiers):
link_uri = hit_test_result.get_link_uri() or ""
if link_uri != self.hover_target:
self.hover_target = link_uri
self.set_status(link_uri)
def set_status(self, text):
self.status_bar.remove_all(self.status_context_id)
self.status_bar.push(self.status_context_id, text)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment