Skip to content

Commit

Permalink
Fix issues with examples on Windows with v55+ (#294)...
Browse files Browse the repository at this point in the history
All examples do run fine on Windows now.

Add run_examples.py tool to run all examples that can be run on current
configuration.
  • Loading branch information
cztomczak committed Feb 21, 2017
1 parent 97712b3 commit 9bd3acc
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 106 deletions.
60 changes: 41 additions & 19 deletions examples/gtk2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Example of embedding CEF Python browser using PyGTK library (GTK 2).
# Tested with GTK 2.24 and CEF Python v55.3+, only on Linux.
# Tested with GTK 2.24 and CEF Python v55.3+, on Windows/Linux.
# Known issue on Linux: Keyboard focus problem (Issue #284).

from cefpython3 import cefpython as cef
Expand All @@ -8,16 +8,25 @@
import gobject
import sys
import os
import platform

# Fix for PyCharm hints warnings
WindowUtils = cef.WindowUtils()

# Platforms
WINDOWS = (platform.system() == "Windows")
LINUX = (platform.system() == "Linux")
MAC = (platform.system() == "Darwin")

# In CEF you can run message loop in two ways (see API docs for more details):
# 1. By calling cef.MessageLoop() instead of an application-provided
# 1. By calling cef.MessageLoopWork() in a timer - each call performs
# a single iteration of CEF message loop processing.
# 2. By calling cef.MessageLoop() instead of an application-provided
# message loop to get the best balance between performance and CPU
# usage. This function will block until a quit message is received by
# the system.
# 2. By calling cef.MessageLoopWork() in a timer - each call performs
# a single iteration of CEF message loop processing.
MESSAGE_LOOP_BEST = 1
MESSAGE_LOOP_TIMER = 2 # Pass --message-loop-timer flag to script to use this
# the system. This seem to work only on Linux in GTK example.
MESSAGE_LOOP_TIMER = 1
MESSAGE_LOOP_CEF = 2 # Pass --message-loop-cef flag to script on Linux
g_message_loop = None


Expand All @@ -28,7 +37,7 @@ def main():
cef.Initialize()
gobject.threads_init()
Gtk2Example()
if g_message_loop == MESSAGE_LOOP_BEST:
if g_message_loop == MESSAGE_LOOP_CEF:
cef.MessageLoop()
else:
gtk.main()
Expand All @@ -46,13 +55,13 @@ def check_versions():

def configure_message_loop():
global g_message_loop
if "--message-loop-timer" in sys.argv:
if "--message-loop-cef" in sys.argv:
print("[gkt2.py] Message loop mode: CEF (best performance)")
g_message_loop = MESSAGE_LOOP_CEF
sys.argv.remove("--message-loop-cef")
else:
print("[gkt2.py] Message loop mode: TIMER")
g_message_loop = MESSAGE_LOOP_TIMER
sys.argv.remove("--message-loop-timer")
else:
print("[gkt2.py] Message loop mode: BEST")
g_message_loop = MESSAGE_LOOP_BEST
if len(sys.argv) > 1:
print("[gkt2.py] ERROR: unknown argument passed")
sys.exit(1)
Expand Down Expand Up @@ -83,7 +92,7 @@ def __init__(self):
self.main_window.add(self.vbox)

windowInfo = cef.WindowInfo()
windowInfo.SetAsChild(self.main_window.window.xid)
windowInfo.SetAsChild(self.get_handle())
self.browser = cef.CreateBrowserSync(windowInfo, settings={},
url="https://www.google.com/")
self.browser.SetClientHandler(LoadHandler())
Expand All @@ -95,6 +104,12 @@ def __init__(self):
if g_message_loop == MESSAGE_LOOP_TIMER:
gobject.timeout_add(10, self.on_timer)

def get_handle(self):
if LINUX:
return self.main_window.window.xid
else:
return self.main_window.window.handle

def create_menu(self):
item1 = gtk.MenuItem('MenuBar')
item1.show()
Expand Down Expand Up @@ -131,16 +146,22 @@ def on_vbox_size_allocate(self, _, data):
y = data.y + self.menubar_height
width = data.width
height = data.height - self.menubar_height
self.browser.SetBounds(x, y, width, height)
if WINDOWS:
WindowUtils.OnSize(self.get_handle(), 0, 0, 0)
elif LINUX:
self.browser.SetBounds(x, y, width, height)

def on_menubar_size_allocate(self, _, data):
self.menubar_height = data.height

def on_exit(self, *_):
if self.exiting:
print("[gtk2.py] on_exit() called, but already exiting")
return
self.exiting = True
self.browser.CloseBrowser(True)
self.browser = None
if g_message_loop == MESSAGE_LOOP_BEST:
if g_message_loop == MESSAGE_LOOP_CEF:
cef.QuitMessageLoop()
else:
gtk.main_quit()
Expand All @@ -158,9 +179,10 @@ def OnLoadStart(self, browser, **_):
# sometimes during initial loading, keyboard focus may
# break and it is not possible to type anything, even
# though a type cursor blinks in web view.
print("[gtk2.py] LoadHandler.OnLoadStart:"
" keyboard focus fix (#284)")
browser.SetFocus(True)
if LINUX:
print("[gtk2.py] LoadHandler.OnLoadStart:"
" keyboard focus fix (#284)")
browser.SetFocus(True)
self.initial_app_loading = False


Expand Down
59 changes: 52 additions & 7 deletions examples/gtk3.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
# ! CURRENTLY BROKEN ! with v54+ (Issue #261).
# Example of embedding CEF Python browser using PyGObject library (GTK 3).
# Tested with GTK 3.10 and CEF Python v53.1+, only on Linux.
# Example of embedding CEF Python browser using PyGObject/PyGI (GTK 3).

# Linux note: This example is currently broken in v54+ on Linux (Issue #261).
# It works fine with cefpython v53.

# Tested configurations:
# - GTK 3.18 on Windows
# - GTK 3.10 on Linux
# - CEF Python v53.1+

from cefpython3 import cefpython as cef
import ctypes
# noinspection PyUnresolvedReferences
from gi.repository import GdkX11, Gtk, GObject, GdkPixbuf
from gi.repository import Gtk, GObject, Gdk, GdkPixbuf
import sys
import os
import platform

# Fix for PyCharm hints warnings
WindowUtils = cef.WindowUtils()

# Platforms
WINDOWS = (platform.system() == "Windows")
LINUX = (platform.system() == "Linux")
MAC = (platform.system() == "Darwin")

# Linux imports
if LINUX:
# noinspection PyUnresolvedReferences
from gi.repository import GdkX11


def main():
Expand All @@ -25,9 +46,10 @@ def main():
class Gtk3Example(Gtk.Application):

def __init__(self):
super(Gtk3Example, self).__init__(application_id='cefpython.gtk')
super(Gtk3Example, self).__init__(application_id='cefpython.gtk3')
self.browser = None
self.window = None
self.win32_handle = None

def run(self, argv):
GObject.threads_init()
Expand All @@ -36,6 +58,22 @@ def run(self, argv):
self.connect("shutdown", self.on_shutdown)
return super(Gtk3Example, self).run(argv)

def get_handle(self):
if LINUX:
return self.window.get_property("window").get_xid()
elif WINDOWS:
Gdk.threads_enter()
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = \
[ctypes.py_object]
gpointer = ctypes.pythonapi.PyCapsule_GetPointer(
self.window.get_property("window").__gpointer__, None)
gdk_dll = ctypes.CDLL("libgdk-3-0.dll")
self.win32_handle = gdk_dll.gdk_win32_window_get_handle(
gpointer)
Gdk.threads_leave()
return self.win32_handle

def on_timer(self):
cef.MessageLoopWork()
return True
Expand All @@ -51,10 +89,13 @@ def on_activate(self, *_):
self.setup_icon()
self.window.realize()
window_info = cef.WindowInfo()
window_info.SetAsChild(self.window.get_property("window").get_xid())
window_info.SetAsChild(self.get_handle())
self.browser = cef.CreateBrowserSync(window_info,
url="https://www.google.com/")
self.window.show_all()
# Must set size of the window again after it was shown,
# otherwise browser occupies only part of the window area.
self.window.resize(*self.window.get_default_size())

def on_configure(self, *_):
if self.browser:
Expand All @@ -63,7 +104,11 @@ def on_configure(self, *_):

def on_size_allocate(self, _, data):
if self.browser:
self.browser.SetBounds(data.x, data.y, data.width, data.height)
if WINDOWS:
WindowUtils.OnSize(self.win32_handle, 0, 0, 0)
elif LINUX:
self.browser.SetBounds(data.x, data.y,
data.width, data.height)

def on_focus_in(self, *_):
if self.browser:
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Hello world example. Doesn't depend on any third party GUI framework.
# Tested with CEF Python v55.3+, only on Linux.
# Tested with CEF Python v55.3+.

from cefpython3 import cefpython as cef
import sys
Expand Down
Loading

0 comments on commit 9bd3acc

Please sign in to comment.