openmedialibrary/oml/gtkwebkit.py

56 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2015-11-03 23:59:00 +00:00
import os
from os.path import dirname, abspath
import subprocess
2015-11-19 14:11:18 +00:00
import webbrowser
2015-11-03 23:59:00 +00:00
import gi
gi.require_version('WebKit2', '4.0')
2015-11-18 12:36:24 +00:00
from gi.repository import WebKit2, Gtk, Gdk
2015-11-03 23:59:00 +00:00
base = dirname(dirname(dirname(abspath(__file__))))
icon = os.path.join(base, 'openmedialibrary/static/png/oml.png')
title = "Open Media Library"
2015-11-03 23:59:00 +00:00
def drop_cb(wid, context, x, y, time):
print([str(t) for t in context.targets])
context.finish(True, False, time)
return True
2015-11-18 12:36:24 +00:00
def on_key_press_event(widget, event):
if event.state & Gdk.ModifierType.CONTROL_MASK and event.keyval == 113:
Gtk.main_quit()
2015-11-19 14:11:18 +00:00
def on_decide_policy(view, decision, dtype):
uri = decision.get_request().get_uri()
if uri.startswith("http") and not '127.0.0.1' in uri:
decision.ignore()
webbrowser.open_new(uri)
return True
return False
wnd = Gtk.Window()
2015-11-18 12:36:24 +00:00
wnd.set_icon_from_file(icon)
2015-11-03 23:59:00 +00:00
wnd.set_wmclass(title, title)
wnd.set_title(title)
wnd.set_default_size(1366, 768)
ctx = WebKit2.WebContext.get_default()
web = WebKit2.WebView.new_with_context(ctx)
wnd.connect("destroy", Gtk.main_quit)
2015-11-18 12:36:24 +00:00
wnd.connect('key_press_event', on_key_press_event)
wnd.add(web)
wnd.show_all()
2015-11-19 14:11:18 +00:00
web.connect("decide-policy", on_decide_policy)
url = 'file://' + base + '/openmedialibrary/static/html/load.html'
web.load_uri(url)
ctl = base + '/ctl'
p = subprocess.Popen([ctl, 'start'])
Gtk.main()
p = subprocess.Popen([ctl, 'stop'])