X Tutup
Skip to content

Commit 8315fb0

Browse files
committed
Support for setting window title in hello_world.py example on Linux.
Initialize GTK only when Print dialog is launched.
1 parent 77c506b commit 8315fb0

File tree

9 files changed

+64
-43
lines changed

9 files changed

+64
-43
lines changed

api/cefpython.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,20 @@ synchronously. The async call to CefCreateBrowser is yet TODO.
4848
| window_info | [WindowInfo](WindowInfo.md) |
4949
| [settings](BrowserSettings.md) | [BrowserSettings](BrowserSettings.md) |
5050
| url | string |
51-
| request_context | void |
51+
| window_title | string |
5252
| __Return__ | [Browser](Browser.md) |
5353

54-
This function should only be called on the UI thread. The 'request_context' parameter is not yet implemented. You must first create a window and initialize 'window_info' by calling WindowInfo.SetAsChild().
54+
All parameters are optional.
5555

56-
After the call to CreateBrowserSync() the page is not yet loaded, if you want your next lines of code to do some stuff on the webpage you will have to implement [LoadHandler](LoadHandler.md).OnLoadEnd() callback, see example below:
56+
This function can only be called on the UI thread.
5757

58-
```python
59-
def OnLoadEnd(browser, frame, httpCode):
60-
if frame == browser.GetMainFrame():
61-
print("Finished loading main frame: %s (http code = %d)"
62-
% (frame.GetUrl(), httpCode))
58+
The "window_title" parameter will be used only
59+
when parent window provided in window_info was set to 0.
6360

64-
browser = cefpython.CreateBrowserSync(windowInfo, settings, url)
65-
browser.SetClientCallback("OnLoadEnd", OnLoadEnd)
66-
```
61+
After the call to CreateBrowserSync() the page is not yet loaded,
62+
if you want your next lines of code to do some stuff on the webpage
63+
you will have to implement LoadHandler.[OnLoadEnd]((LoadHandler.md#onloadend))
64+
callback.
6765

6866

6967
### ExceptHook

examples/hello_world.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ def main():
1010
check_versions()
1111
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
1212
cef.Initialize()
13-
cef.CreateBrowserSync(url="https://www.google.com/")
13+
cef.CreateBrowserSync(window_title="Hello World!",
14+
url="https://www.google.com/")
1415
cef.MessageLoop()
1516
cef.Shutdown()
1617

1718

1819
def check_versions():
1920
print("[hello_world.py] CEF Python {ver}".format(ver=cef.__version__))
2021
print("[hello_world.py] Python {ver} {arch}".format(
21-
ver=platform.python_version(), arch=platform.architecture()[0]))
22+
ver=platform.python_version(), arch=platform.architecture()[0]))
2223
assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this"
2324

2425

src/cefpython.pyx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ ELIF UNAME_SYSNAME == "Darwin":
376376
from cpp_utils cimport *
377377
from task cimport *
378378

379+
IF UNAME_SYSNAME == "Linux":
380+
cimport x11
381+
379382
from cef_string cimport *
380383
cdef extern from *:
381384
# noinspection PyUnresolvedReferences
@@ -764,6 +767,7 @@ def CreateBrowser(**kwargs):
764767
def CreateBrowserSync(windowInfo=None,
765768
browserSettings=None,
766769
navigateUrl="",
770+
window_title="",
767771
**kwargs):
768772
# Alternative names for existing parameters
769773
if "window_info" in kwargs:
@@ -823,6 +827,9 @@ def CreateBrowserSync(windowInfo=None,
823827
elif not isinstance(windowInfo, WindowInfo):
824828
raise Exception("CreateBrowserSync() failed: windowInfo: invalid object")
825829

830+
if window_title and windowInfo.parentWindowHandle == 0:
831+
windowInfo.windowName = window_title
832+
826833
if not browserSettings:
827834
browserSettings = {}
828835

@@ -895,10 +902,13 @@ def CreateBrowserSync(windowInfo=None,
895902
cef_window.get().RequestFocus()
896903
"""
897904

898-
if windowInfo.parentWindowHandle == 0 and windowInfo.windowType == "child":
905+
if windowInfo.parentWindowHandle == 0\
906+
and windowInfo.windowType == "child"\
907+
and windowInfo.windowName:
899908
# Set window title in hello_world.py example
900909
IF UNAME_SYSNAME == "Linux":
901-
pass
910+
x11.SetX11WindowTitle(cefBrowser,
911+
PyStringToChar(windowInfo.windowName))
902912
ELIF UNAME_SYSNAME == "Darwin":
903913
pass
904914

src/client_handler/util_mac.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <objc/runtime.h>
88
#include "include/cef_app.h"
99
#include "include/cef_application_mac.h"
10+
#include "include/cef_browser.h"
1011

1112
namespace {
1213

@@ -64,3 +65,8 @@ - (void)_swizzled_terminate:(id)sender {
6465
void MacInitialize() {
6566
[NSApplication sharedApplication];
6667
}
68+
69+
void MacSetTitle(CefRefPtr<CefBrowser> browser, char* title) {
70+
NSView* view = browser->GetHost()->GetWindowHandle();
71+
view.window!.title = title;
72+
}

src/client_handler/x11.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22
// All rights reserved. Licensed under BSD 3-clause license.
33
// Project website: https://github.com/cztomczak/cefpython
44

5-
#include <X11/Xlib.h>
5+
#include "x11.h"
66
#include "LOG_DEBUG.h"
7-
#include "include/cef_browser.h"
87

98
int XErrorHandlerImpl(Display *display, XErrorEvent *event) {
10-
LOG_DEBUG
9+
LOG_DEBUG
1110
<< "X error received: "
1211
<< "type " << event->type << ", "
1312
<< "serial " << event->serial << ", "
1413
<< "error_code " << static_cast<int>(event->error_code) << ", "
1514
<< "request_code " << static_cast<int>(event->request_code) << ", "
1615
<< "minor_code " << static_cast<int>(event->minor_code);
17-
return 0;
16+
return 0;
1817
}
1918

2019
int XIOErrorHandlerImpl(Display *display) {
21-
return 0;
20+
return 0;
2221
}
2322

2423
void InstallX11ErrorHandlers() {
@@ -29,20 +28,21 @@ void InstallX11ErrorHandlers() {
2928
XSetIOErrorHandler(XIOErrorHandlerImpl);
3029
}
3130

32-
void SetXWindowBounds(::Window xwindow,
33-
int x, int y, size_t width, size_t height) {
34-
::Display* xdisplay = cef_get_xdisplay();
35-
XWindowChanges changes = {0};
36-
changes.x = x;
37-
changes.y = y;
38-
changes.width = static_cast<int>(width);
39-
changes.height = static_cast<int>(height);
40-
XConfigureWindow(xdisplay, xwindow,
41-
CWX | CWY | CWHeight | CWWidth, &changes);
42-
}
43-
4431
void SetX11WindowBounds(CefRefPtr<CefBrowser> browser,
4532
int x, int y, int width, int height) {
4633
::Window xwindow = browser->GetHost()->GetWindowHandle();
47-
SetXWindowBounds(xwindow, x, y, width, height);
34+
::Display* xdisplay = cef_get_xdisplay();
35+
XWindowChanges changes = {0};
36+
changes.x = x;
37+
changes.y = y;
38+
changes.width = static_cast<int>(width);
39+
changes.height = static_cast<int>(height);
40+
XConfigureWindow(xdisplay, xwindow,
41+
CWX | CWY | CWHeight | CWWidth, &changes);
42+
}
43+
44+
void SetX11WindowTitle(CefRefPtr<CefBrowser> browser, char* title) {
45+
::Window xwindow = browser->GetHost()->GetWindowHandle();
46+
::Display* xdisplay = cef_get_xdisplay();
47+
XStoreName(xdisplay, xwindow, title);
4848
}

src/client_handler/x11.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
// All rights reserved. Licensed under BSD 3-clause license.
33
// Project website: https://github.com/cztomczak/cefpython
44

5+
#pragma once
6+
7+
#include <X11/Xlib.h>
8+
#include "include/cef_browser.h"
9+
510
void InstallX11ErrorHandlers();
611
void SetX11WindowBounds(CefRefPtr<CefBrowser> browser,
712
int x, int y, int width, int height);
13+
void SetX11WindowTitle(CefRefPtr<CefBrowser> browser, char* title);

src/extern/x11.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ cdef extern from "client_handler/x11.h" nogil:
1010
void InstallX11ErrorHandlers()
1111
void SetX11WindowBounds(CefRefPtr[CefBrowser] browser,
1212
int x, int y, int width, int height)
13+
void SetX11WindowTitle(CefRefPtr[CefBrowser] browser, char* title)

src/subprocess/cefpython_app.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ void CefPythonApp::OnContextInitialized() {
101101
#ifdef BROWSER_PROCESS
102102
REQUIRE_UI_THREAD();
103103
#if defined(OS_LINUX)
104-
// For print handler to work GTK must be initialized. This is
105-
// required for hello_world.py example to work.
106-
GdkDisplay* gdk_display = gdk_display_get_default();
107-
if (!gdk_display) {
108-
LOG_DEBUG << "Initialize GTK";
109-
gtk_init(0, NULL);
110-
}
111104
print_handler_ = new ClientPrintHandlerGtk();
112105
#endif // OS_LINUX
113106
#endif // BROWSER_PROCESS
@@ -140,6 +133,15 @@ void CefPythonApp::OnRenderProcessThreadCreated(
140133
}
141134

142135
CefRefPtr<CefPrintHandler> CefPythonApp::GetPrintHandler() {
136+
#if defined(OS_LINUX)
137+
// For print handler to work GTK must be initialized. This is
138+
// required for some of the examples.
139+
GdkDisplay* gdk_display = gdk_display_get_default();
140+
if (!gdk_display) {
141+
LOG_DEBUG << "Initialize GTK";
142+
gtk_init(0, NULL);
143+
}
144+
#endif
143145
return print_handler_;
144146
}
145147

src/window_utils_linux.pyx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
include "cefpython.pyx"
66

7-
IF UNAME_SYSNAME == "Linux":
8-
cimport x11
9-
107
class WindowUtils:
118
# You have to overwrite this class and provide implementations
129
# for these methods.

0 commit comments

Comments
 (0)
X Tutup