Skip to content
Browse files

giwebkit: use pure Gtk.main(); exit on Ctrl^q (gui) and Ctrl^c (term)

  • Loading branch information...
1 parent f941e89 commit 6138af7d9c3e236664545f86f6ac766947751a8c C Anthony Risinger committed Aug 25, 2012
Showing with 46 additions and 49 deletions.
  1. +46 −49 pyjs/runners/giwebkit.py
View
95 pyjs/runners/giwebkit.py
@@ -22,8 +22,12 @@
from gi.repository import WebKit
import types
+import signal
from traceback import print_exc
+sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
+sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
+
class GIXMLHttpRequestEventTarget(object):
@@ -428,20 +432,27 @@ class ApplicationFrame(object):
class ApplicationView(object):
- def __init__(self, uri, appdir=None, width=800, height=600):
- self.uri = uri
- self.appdir = appdir
- self.width = width = 900
- self.height = height = 1440
+ _resolver = GIResolver(WebKit.DOMObject)
+
+ uri = 'about:blank'
+ #TODO: rename, accidentally removed?
+ appdir = None
+ width = 800
+ height = 600
+ listeners = None
+
+ def __init__(self):
self.listeners = dict()
- self.initialized = False
- def load_app(self):
- uri = self.uri
- if uri.find('://') == -1:
+ def setup(self, uri=uri, **kwds):
+ self.uri = uri
+ for k, v in kwds.iteritems():
+ if hasattr(self, k):
+ setattr(self, k, v)
+ if self.uri.find('://') == -1:
# assume file
- uri = 'file://'+os.path.abspath(uri)
- logger.debug(uri)
+ self.uri = 'file://'+os.path.abspath(self.uri)
+ logger.debug(self.uri)
view = self._view = WebKit.WebView()
view.set_data('__app__', self)
@@ -450,12 +461,18 @@ def load_app(self):
toplevel.set_default_size(self.width, self.height)
toplevel.add(scroller)
scroller.add(view)
- toplevel.show_all()
- # file:/// with # or ? causes error
- view.load_uri(uri)
+ char_q, mod_ctrl = Gtk.accelerator_parse('<Ctrl>q')
+ accel_destroy = Gtk.AccelGroup.new()
+ accel_destroy.connect(char_q, mod_ctrl, 0, self._destroy_cb)
+
+ #TODO: file:/// with # or ? causes error
+ view.load_uri(self.uri)
+
+ signal.signal(signal.SIGINT, self._destroy_cb)
+ toplevel.connect('destroy', self._destroy_cb)
+ toplevel.add_accel_group(accel_destroy)
- toplevel.connect('delete-event', self._delete_event_cb)
view.connect('onload-event', self._frame_loaded_cb)
view.connect('title-changed', self._title_changed_cb)
view.connect('icon-loaded', self._icon_loaded_cb)
@@ -471,6 +488,10 @@ def load_app(self):
settings.set_property('enable-accelerated-compositing', True)
settings.set_property('enable-webgl', True)
+ # display and run mainloop (returns after frame load)
+ toplevel.show_all()
+ Gtk.main()
+
def getUri(self):
return self.uri
@@ -515,6 +536,9 @@ def _frame_loaded_cb(self, view, frame):
self.initialized = True
self.init_app()
+ # return control to setup()
+ Gtk.main_quit()
+
def _icon_loaded_cb(self, view, icon_uri):
current = view.get_property('uri')
dom = wv.getDomDocument()
@@ -612,10 +636,12 @@ def addEventListener(self, element, event_name, cb):
element.add_event_listener(event_name, listener, False)
self.listeners[listener] = element
- def _delete_event_cb(self, window, event):
+ def _destroy_cb(self, *args):
+ logger.info('destroy:draining events...')
while Gtk.events_pending():
Gtk.main_iteration_do(False)
- window.unrealize()
+ Gtk.main_quit()
+ logger.info('destroy:exiting...')
def _title_changed_cb(self, view, frame, title):
self._toplevel.set_title(title)
@@ -626,35 +652,6 @@ def _title_changed_cb(self, view, frame, title):
_addXMLHttpRequestEventListener = addXMLHttpRequestEventListener
-def setup(application, appdir=None, width=800, height=600):
-
- global wv
- global giresolver
-
- giresolver = GIResolver(WebKit.DOMObject)
- wv = ApplicationView(application, appdir, width, height)
- wv.load_app()
-
- while 1:
- if is_loaded():
- return
- run(one_event=True)
-
-
-def is_loaded():
- return wv.initialized
-
-
-def run(one_event=False, block=True):
- if one_event:
- if block or Gtk.events_pending():
- Gtk.main_iteration()
- sys.stdout.flush()
- return Gtk.events_pending()
- else:
- try:
- while wv._toplevel.get_realized():
- Gtk.main_iteration()
- sys.stdout.flush()
- except KeyboardInterrupt:
- logger.info('SIGINT:exiting...')
+application = ApplicationView()
+setup = application.setup
+run = Gtk.main

0 comments on commit 6138af7

Please sign in to comment.
Something went wrong with that request. Please try again.