Skip to content

Instantly share code, notes, and snippets.

@ilhooq
Last active March 20, 2022 22:41
Show Gist options
  • Save ilhooq/0a879ee0ae2c27691fdcc2b4ecdeee61 to your computer and use it in GitHub Desktop.
Save ilhooq/0a879ee0ae2c27691fdcc2b4ecdeee61 to your computer and use it in GitHub Desktop.
A minimalist Gtk3 web browser that looks like Epiphany
#!/usr/bin/python3
import gi, sys
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gtk, WebKit2 as WebKit, Pango
class Tab(Gtk.VBox):
def __init__(self):
super().__init__()
self.webview = WebKit.WebView()
self.webview.parent_tab = self
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.add(self.webview)
self.add(scrolled_window)
settings = self.webview.get_settings()
# Disable javascript
# settings.props.enable_javascript = False
class Browser(Gtk.Window):
def __init__(self):
super().__init__(title="Browser")
self.set_default_size(800, 600)
self.set_position(Gtk.WindowPosition.CENTER)
self.address_bar = Gtk.Entry(expand=True)
self.address_bar.connect('activate', lambda entry: self.currentTab().webview.load_uri(entry.get_text()))
btnPrevious = Gtk.Button().new_from_icon_name(
'go-previous', Gtk.IconSize.BUTTON)
btnPrevious.set_tooltip_text("Go back")
btnPrevious.connect('clicked', lambda btn: self.currentTab().webview.go_back())
btnNext = Gtk.Button().new_from_icon_name('go-next', Gtk.IconSize.BUTTON)
btnNext.set_tooltip_text("Go forward")
btnNext.connect('clicked', lambda btn: self.currentTab().webview.go_forward())
btnRefresh = Gtk.Button().new_from_icon_name('view-refresh', Gtk.IconSize.BUTTON)
btnRefresh.set_tooltip_text("Reload page")
btnRefresh.connect('clicked', lambda btn: self.currentTab().webview.reload())
btnNewTab = Gtk.Button().new_from_icon_name('tab-new', Gtk.IconSize.BUTTON)
btnNewTab.set_tooltip_text("New tab")
btnNewTab.connect('clicked', lambda btn: self.addTab())
headerBtns = Gtk.HBox(expand=True)
headerBtns.pack_start(btnPrevious, False, False, 0)
headerBtns.pack_start(btnNext, False, False, 0)
headerBtns.pack_start(btnRefresh, False, False, 10)
headerBtns.pack_start(btnNewTab, False, False, 0)
self.headerbar = Gtk.HeaderBar(show_close_button=True)
self.headerbar.set_custom_title(self.address_bar)
self.headerbar.pack_start(headerBtns)
self.set_titlebar(self.headerbar)
self.notebook = Gtk.Notebook(scrollable=True)
self.notebook.connect('switch-page', self._ontabSwitched)
self.add(self.notebook)
self.show_all()
def addTab(self):
tab = Tab()
tab.webview.connect('load-changed', self._onLoadChanged)
btnClose = Gtk.Button(margin_start=3).new_from_icon_name('application-exit', Gtk.IconSize.BUTTON)
btnClose.get_style_context().add_class("flat")
btnClose.connect('clicked', lambda btn: self.notebook.remove_page(self.notebook.page_num(tab)))
tabLabel = Gtk.HBox()
label = Gtk.Label(label="New page",
width_chars=20,
ellipsize=Pango.EllipsizeMode.END,
halign=Gtk.Align.CENTER)
tabLabel.pack_start(label, False, False, 0)
tabLabel.pack_end(btnClose, False, False, 0)
page_num = self.notebook.append_page(tab, tabLabel)
self.notebook.set_tab_reorderable(tab, True)
tabLabel.show_all()
tab.show_all()
if page_num > 0:
self.address_bar.set_text("")
self.notebook.set_current_page(page_num)
return page_num
def currentTab(self):
page_num = self.notebook.get_current_page()
if page_num == -1:
page_num = self.addTab()
return self.notebook.get_nth_page(page_num)
def _ontabSwitched(self, notebook, page, page_num):
if notebook.get_n_pages() > 1:
uri = page.webview.get_uri()
self.address_bar.set_text(uri or "")
def _onLoadChanged(self, webview, loadEvent):
if loadEvent == WebKit.LoadEvent.COMMITTED:
uri = webview.get_uri()
if uri:
page_num = self.notebook.page_num(webview.parent_tab)
if self.notebook.get_current_page() == page_num:
self.address_bar.set_text(uri)
elif loadEvent == WebKit.LoadEvent.FINISHED:
title = webview.get_title()
if title:
tabLabel = self.notebook.get_tab_label(self.currentTab())
label = tabLabel.get_children()[0]
label.set_text(title)
if __name__ == "__main__":
Gtk.init(sys.argv)
win = Browser()
win.connect("delete-event", Gtk.main_quit)
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment