| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 97712b3 commit 9bd3acc
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | # Example of embedding CEF Python browser using PyGTK library (GTK 2). | |
| 2 | - # Tested with GTK 2.24 and CEF Python v55.3+, only on Linux. | ||
| 2 | + # Tested with GTK 2.24 and CEF Python v55.3+, on Windows/Linux. | ||
| 3 | 3 | # Known issue on Linux: Keyboard focus problem (Issue #284). | |
| 4 | 4 | ||
| 5 | 5 | from cefpython3 import cefpython as cef | |
@@ -8,16 +8,25 @@ | |||
| 8 | 8 | import gobject | |
| 9 | 9 | import sys | |
| 10 | 10 | import os | |
| 11 | + import platform | ||
| 12 | + | ||
| 13 | + # Fix for PyCharm hints warnings | ||
| 14 | + WindowUtils = cef.WindowUtils() | ||
| 15 | + | ||
| 16 | + # Platforms | ||
| 17 | + WINDOWS = (platform.system() == "Windows") | ||
| 18 | + LINUX = (platform.system() == "Linux") | ||
| 19 | + MAC = (platform.system() == "Darwin") | ||
| 11 | 20 | ||
| 12 | 21 | # In CEF you can run message loop in two ways (see API docs for more details): | |
| 13 | - # 1. By calling cef.MessageLoop() instead of an application-provided | ||
| 22 | + # 1. By calling cef.MessageLoopWork() in a timer - each call performs | ||
| 23 | + # a single iteration of CEF message loop processing. | ||
| 24 | + # 2. By calling cef.MessageLoop() instead of an application-provided | ||
| 14 | 25 | # message loop to get the best balance between performance and CPU | |
| 15 | 26 | # usage. This function will block until a quit message is received by | |
| 16 | - # the system. | ||
| 17 | - # 2. By calling cef.MessageLoopWork() in a timer - each call performs | ||
| 18 | - # a single iteration of CEF message loop processing. | ||
| 19 | - MESSAGE_LOOP_BEST = 1 | ||
| 20 | - MESSAGE_LOOP_TIMER = 2 # Pass --message-loop-timer flag to script to use this | ||
| 27 | + # the system. This seem to work only on Linux in GTK example. | ||
| 28 | + MESSAGE_LOOP_TIMER = 1 | ||
| 29 | + MESSAGE_LOOP_CEF = 2 # Pass --message-loop-cef flag to script on Linux | ||
| 21 | 30 | g_message_loop = None | |
| 22 | 31 | ||
| 23 | 32 | ||
@@ -28,7 +37,7 @@ def main(): | |||
| 28 | 37 | cef.Initialize() | |
| 29 | 38 | gobject.threads_init() | |
| 30 | 39 | Gtk2Example() | |
| 31 | - if g_message_loop == MESSAGE_LOOP_BEST: | ||
| 40 | + if g_message_loop == MESSAGE_LOOP_CEF: | ||
| 32 | 41 | cef.MessageLoop() | |
| 33 | 42 | else: | |
| 34 | 43 | gtk.main() | |
@@ -46,13 +55,13 @@ def check_versions(): | |||
| 46 | 55 | ||
| 47 | 56 | def configure_message_loop(): | |
| 48 | 57 | global g_message_loop | |
| 49 | - if "--message-loop-timer" in sys.argv: | ||
| 58 | + if "--message-loop-cef" in sys.argv: | ||
| 59 | + print("[gkt2.py] Message loop mode: CEF (best performance)") | ||
| 60 | + g_message_loop = MESSAGE_LOOP_CEF | ||
| 61 | + sys.argv.remove("--message-loop-cef") | ||
| 62 | + else: | ||
| 50 | 63 | print("[gkt2.py] Message loop mode: TIMER") | |
| 51 | 64 | g_message_loop = MESSAGE_LOOP_TIMER | |
| 52 | - sys.argv.remove("--message-loop-timer") | ||
| 53 | - else: | ||
| 54 | - print("[gkt2.py] Message loop mode: BEST") | ||
| 55 | - g_message_loop = MESSAGE_LOOP_BEST | ||
| 56 | 65 | if len(sys.argv) > 1: | |
| 57 | 66 | print("[gkt2.py] ERROR: unknown argument passed") | |
| 58 | 67 | sys.exit(1) | |
@@ -83,7 +92,7 @@ def __init__(self): | |||
| 83 | 92 | self.main_window.add(self.vbox) | |
| 84 | 93 | ||
| 85 | 94 | windowInfo = cef.WindowInfo() | |
| 86 | - windowInfo.SetAsChild(self.main_window.window.xid) | ||
| 95 | + windowInfo.SetAsChild(self.get_handle()) | ||
| 87 | 96 | self.browser = cef.CreateBrowserSync(windowInfo, settings={}, | |
| 88 | 97 | url="https://www.google.com/") | |
| 89 | 98 | self.browser.SetClientHandler(LoadHandler()) | |
@@ -95,6 +104,12 @@ def __init__(self): | |||
| 95 | 104 | if g_message_loop == MESSAGE_LOOP_TIMER: | |
| 96 | 105 | gobject.timeout_add(10, self.on_timer) | |
| 97 | 106 | ||
| 107 | + def get_handle(self): | ||
| 108 | + if LINUX: | ||
| 109 | + return self.main_window.window.xid | ||
| 110 | + else: | ||
| 111 | + return self.main_window.window.handle | ||
| 112 | + | ||
| 98 | 113 | def create_menu(self): | |
| 99 | 114 | item1 = gtk.MenuItem('MenuBar') | |
| 100 | 115 | item1.show() | |
@@ -131,16 +146,22 @@ def on_vbox_size_allocate(self, _, data): | |||
| 131 | 146 | y = data.y + self.menubar_height | |
| 132 | 147 | width = data.width | |
| 133 | 148 | height = data.height - self.menubar_height | |
| 134 | - self.browser.SetBounds(x, y, width, height) | ||
| 149 | + if WINDOWS: | ||
| 150 | + WindowUtils.OnSize(self.get_handle(), 0, 0, 0) | ||
| 151 | + elif LINUX: | ||
| 152 | + self.browser.SetBounds(x, y, width, height) | ||
| 135 | 153 | ||
| 136 | 154 | def on_menubar_size_allocate(self, _, data): | |
| 137 | 155 | self.menubar_height = data.height | |
| 138 | 156 | ||
| 139 | 157 | def on_exit(self, *_): | |
| 158 | + if self.exiting: | ||
| 159 | + print("[gtk2.py] on_exit() called, but already exiting") | ||
| 160 | + return | ||
| 140 | 161 | self.exiting = True | |
| 141 | 162 | self.browser.CloseBrowser(True) | |
| 142 | 163 | self.browser = None | |
| 143 | - if g_message_loop == MESSAGE_LOOP_BEST: | ||
| 164 | + if g_message_loop == MESSAGE_LOOP_CEF: | ||
| 144 | 165 | cef.QuitMessageLoop() | |
| 145 | 166 | else: | |
| 146 | 167 | gtk.main_quit() | |
@@ -158,9 +179,10 @@ def OnLoadStart(self, browser, **_): | |||
| 158 | 179 | # sometimes during initial loading, keyboard focus may | |
| 159 | 180 | # break and it is not possible to type anything, even | |
| 160 | 181 | # though a type cursor blinks in web view. | |
| 161 | - print("[gtk2.py] LoadHandler.OnLoadStart:" | ||
| 162 | - " keyboard focus fix (#284)") | ||
| 163 | - browser.SetFocus(True) | ||
| 182 | + if LINUX: | ||
| 183 | + print("[gtk2.py] LoadHandler.OnLoadStart:" | ||
| 184 | + " keyboard focus fix (#284)") | ||
| 185 | + browser.SetFocus(True) | ||
| 164 | 186 | self.initial_app_loading = False | |
| 165 | 187 | ||
| 166 | 188 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,33 @@ | |||
| 1 | - # ! CURRENTLY BROKEN ! with v54+ (Issue #261). | ||
| 2 | - # Example of embedding CEF Python browser using PyGObject library (GTK 3). | ||
| 3 | - # Tested with GTK 3.10 and CEF Python v53.1+, only on Linux. | ||
| 1 | + # Example of embedding CEF Python browser using PyGObject/PyGI (GTK 3). | ||
| 2 | + | ||
| 3 | + # Linux note: This example is currently broken in v54+ on Linux (Issue #261). | ||
| 4 | + # It works fine with cefpython v53. | ||
| 5 | + | ||
| 6 | + # Tested configurations: | ||
| 7 | + # - GTK 3.18 on Windows | ||
| 8 | + # - GTK 3.10 on Linux | ||
| 9 | + # - CEF Python v53.1+ | ||
| 4 | 10 | ||
| 5 | 11 | from cefpython3 import cefpython as cef | |
| 12 | + import ctypes | ||
| 6 | 13 | # noinspection PyUnresolvedReferences | |
| 7 | - from gi.repository import GdkX11, Gtk, GObject, GdkPixbuf | ||
| 14 | + from gi.repository import Gtk, GObject, Gdk, GdkPixbuf | ||
| 8 | 15 | import sys | |
| 9 | 16 | import os | |
| 17 | + import platform | ||
| 18 | + | ||
| 19 | + # Fix for PyCharm hints warnings | ||
| 20 | + WindowUtils = cef.WindowUtils() | ||
| 21 | + | ||
| 22 | + # Platforms | ||
| 23 | + WINDOWS = (platform.system() == "Windows") | ||
| 24 | + LINUX = (platform.system() == "Linux") | ||
| 25 | + MAC = (platform.system() == "Darwin") | ||
| 26 | + | ||
| 27 | + # Linux imports | ||
| 28 | + if LINUX: | ||
| 29 | + # noinspection PyUnresolvedReferences | ||
| 30 | + from gi.repository import GdkX11 | ||
| 10 | 31 | ||
| 11 | 32 | ||
| 12 | 33 | def main(): | |
@@ -25,9 +46,10 @@ def main(): | |||
| 25 | 46 | class Gtk3Example(Gtk.Application): | |
| 26 | 47 | ||
| 27 | 48 | def __init__(self): | |
| 28 | - super(Gtk3Example, self).__init__(application_id='cefpython.gtk') | ||
| 49 | + super(Gtk3Example, self).__init__(application_id='cefpython.gtk3') | ||
| 29 | 50 | self.browser = None | |
| 30 | 51 | self.window = None | |
| 52 | + self.win32_handle = None | ||
| 31 | 53 | ||
| 32 | 54 | def run(self, argv): | |
| 33 | 55 | GObject.threads_init() | |
@@ -36,6 +58,22 @@ def run(self, argv): | |||
| 36 | 58 | self.connect("shutdown", self.on_shutdown) | |
| 37 | 59 | return super(Gtk3Example, self).run(argv) | |
| 38 | 60 | ||
| 61 | + def get_handle(self): | ||
| 62 | + if LINUX: | ||
| 63 | + return self.window.get_property("window").get_xid() | ||
| 64 | + elif WINDOWS: | ||
| 65 | + Gdk.threads_enter() | ||
| 66 | + ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p | ||
| 67 | + ctypes.pythonapi.PyCapsule_GetPointer.argtypes = \ | ||
| 68 | + [ctypes.py_object] | ||
| 69 | + gpointer = ctypes.pythonapi.PyCapsule_GetPointer( | ||
| 70 | + self.window.get_property("window").__gpointer__, None) | ||
| 71 | + gdk_dll = ctypes.CDLL("libgdk-3-0.dll") | ||
| 72 | + self.win32_handle = gdk_dll.gdk_win32_window_get_handle( | ||
| 73 | + gpointer) | ||
| 74 | + Gdk.threads_leave() | ||
| 75 | + return self.win32_handle | ||
| 76 | + | ||
| 39 | 77 | def on_timer(self): | |
| 40 | 78 | cef.MessageLoopWork() | |
| 41 | 79 | return True | |
@@ -51,10 +89,13 @@ def on_activate(self, *_): | |||
| 51 | 89 | self.setup_icon() | |
| 52 | 90 | self.window.realize() | |
| 53 | 91 | window_info = cef.WindowInfo() | |
| 54 | - window_info.SetAsChild(self.window.get_property("window").get_xid()) | ||
| 92 | + window_info.SetAsChild(self.get_handle()) | ||
| 55 | 93 | self.browser = cef.CreateBrowserSync(window_info, | |
| 56 | 94 | url="https://www.google.com/") | |
| 57 | 95 | self.window.show_all() | |
| 96 | + # Must set size of the window again after it was shown, | ||
| 97 | + # otherwise browser occupies only part of the window area. | ||
| 98 | + self.window.resize(*self.window.get_default_size()) | ||
| 58 | 99 | ||
| 59 | 100 | def on_configure(self, *_): | |
| 60 | 101 | if self.browser: | |
@@ -63,7 +104,11 @@ def on_configure(self, *_): | |||
| 63 | 104 | ||
| 64 | 105 | def on_size_allocate(self, _, data): | |
| 65 | 106 | if self.browser: | |
| 66 | - self.browser.SetBounds(data.x, data.y, data.width, data.height) | ||
| 107 | + if WINDOWS: | ||
| 108 | + WindowUtils.OnSize(self.win32_handle, 0, 0, 0) | ||
| 109 | + elif LINUX: | ||
| 110 | + self.browser.SetBounds(data.x, data.y, | ||
| 111 | + data.width, data.height) | ||
| 67 | 112 | ||
| 68 | 113 | def on_focus_in(self, *_): | |
| 69 | 114 | if self.browser: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | # Hello world example. Doesn't depend on any third party GUI framework. | |
| 2 | - # Tested with CEF Python v55.3+, only on Linux. | ||
| 2 | + # Tested with CEF Python v55.3+. | ||
| 3 | 3 | ||
| 4 | 4 | from cefpython3 import cefpython as cef | |
| 5 | 5 | import sys | |
| Back | FazBrowse Home | New Git URL |
0 commit comments