source: src/mlx/gui/cef.py@ 648:e98c42b706eb

cef
Last change on this file since 648:e98c42b706eb was 648:e98c42b706eb, checked in by István Váradi <ivaradi@…>, 7 years ago

Added a simple wrapper library for CEF (re #272)

File size: 3.1 KB
Line 
1from common import *
2
3import platform
4import json
5
6from cefpython3 import cefpython
7
8import os
9import re
10
11#------------------------------------------------------------------------------
12
13## @package mlx.gui.cef
14#
15# Some helper stuff related to the Chrome Embedded Framework
16
17#------------------------------------------------------------------------------
18
19# Indicate if we should quit
20_toQuit = False
21
22#------------------------------------------------------------------------------
23
24def initialize():
25 """Initialize the Chrome Embedded Framework."""
26 global _toQuit
27 _toQuit = False
28
29 gobject.threads_init()
30
31 settings = {
32 "debug": True, # cefpython debug messages in console and in log_file
33 "log_severity": cefpython.LOGSEVERITY_VERBOSE, # LOGSEVERITY_VERBOSE
34 "log_file": "", # Set to "" to disable
35 "release_dcheck_enabled": True, # Enable only when debugging
36 # This directories must be set on Linux
37 "locales_dir_path": os.path.join(cefpython.GetModuleDirectory(), "locales"),
38 "resources_dir_path": cefpython.GetModuleDirectory(),
39 "browser_subprocess_path": "%s/%s" % \
40 (cefpython.GetModuleDirectory(), "subprocess"),
41 }
42
43 cefpython.Initialize(settings, {})
44
45 gobject.timeout_add(10, _handleTimeout)
46
47#------------------------------------------------------------------------------
48
49def getContainer():
50 """Get a container object suitable for running a browser instance
51 within."""
52 if os.name=="nt":
53 container = gtk.DrawingArea()
54 container.set_property("can-focus", True)
55 container.connect("size-allocate", _handleSizeAllocate)
56 else:
57 container = gtk.VBox(True, 0)
58
59 container.show()
60
61 return container
62
63#------------------------------------------------------------------------------
64
65def startInContainer(container, url, browserSettings = {}):
66 """Start a browser instance in the given container with the given URL."""
67 if os.name=="nt":
68 windowID = container.get_window().handle
69 else:
70 m = re.search("GtkVBox at 0x(\w+)", str(container))
71 hexID = m.group(1)
72 windowID = int(hexID, 16)
73
74 windowInfo = cefpython.WindowInfo()
75 windowInfo.SetAsChild(windowID)
76
77 return cefpython.CreateBrowserSync(windowInfo,
78 browserSettings = browserSettings,
79 navigateUrl = url)
80
81#------------------------------------------------------------------------------
82
83def finalize():
84 """Finalize the Chrome Embedded Framework."""
85 global _toQuit
86 toQuit = True
87 cefpython.Shutdown()
88
89#------------------------------------------------------------------------------
90
91def _handleTimeout():
92 """Handle the timeout by running the CEF message loop."""
93 if _toQuit:
94 return False
95 else:
96 cefpython.MessageLoopWork()
97 return True
98
99#------------------------------------------------------------------------------
100
101def _handleSizeAllocate(widget, sizeAlloc):
102 """Handle the size-allocate event."""
103 cefpython.WindowUtils.OnSize(widget.get_window().handle, 0, 0, 0)
Note: See TracBrowser for help on using the repository browser.