forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender_handler.pyx
More file actions
334 lines (315 loc) · 12.8 KB
/
Copy pathrender_handler.pyx
File metadata and controls
334 lines (315 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright (c) 2013 CEF Python, see the Authors file.
# All rights reserved. Licensed under BSD 3-clause license.
# Project website: https://github.com/cztomczak/cefpython
include "../cefpython.pyx"
include "../browser.pyx"
include "../string_utils.pyx"
cimport cef_types
from libc.stdint cimport uint32_t
from cef_types cimport CefRange
# cef_paint_element_type_t, PaintElementType
PET_VIEW = cef_types.PET_VIEW
PET_POPUP = cef_types.PET_POPUP
# cef_drag_operations_mask_t, DragOperation, DragOperationsMask
DRAG_OPERATION_NONE = cef_types.DRAG_OPERATION_NONE
DRAG_OPERATION_COPY = cef_types.DRAG_OPERATION_COPY
DRAG_OPERATION_LINK = cef_types.DRAG_OPERATION_LINK
DRAG_OPERATION_GENERIC = cef_types.DRAG_OPERATION_GENERIC
DRAG_OPERATION_PRIVATE = cef_types.DRAG_OPERATION_PRIVATE
DRAG_OPERATION_MOVE = cef_types.DRAG_OPERATION_MOVE
DRAG_OPERATION_DELETE = cef_types.DRAG_OPERATION_DELETE
DRAG_OPERATION_EVERY = cef_types.DRAG_OPERATION_EVERY
cdef public cpp_bool RenderHandler_GetRootScreenRect(
CefRefPtr[CefBrowser] cefBrowser,
CefRect& cefRect
) except * with gil:
cdef PyBrowser pyBrowser
cdef list pyRect = []
cdef py_bool ret
try:
pyBrowser = GetPyBrowser(cefBrowser, "GetRootScreenRect")
callback = pyBrowser.GetClientCallback("GetRootScreenRect")
if callback:
ret = callback(browser=pyBrowser, rect_out=pyRect)
if ret:
assert (pyRect and len(pyRect) == 4), "rectangle not provided"
cefRect.x = pyRect[0]
cefRect.y = pyRect[1]
cefRect.width = pyRect[2]
cefRect.height = pyRect[3]
return True
else:
return False
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RenderHandler_GetViewRect(
CefRefPtr[CefBrowser] cefBrowser,
CefRect& cefRect
) except * with gil:
cdef PyBrowser pyBrowser
cdef list pyRect = []
cdef py_bool ret
try:
pyBrowser = GetPyBrowser(cefBrowser, "GetViewRect")
callback = pyBrowser.GetClientCallback("GetViewRect")
if callback:
ret = callback(browser=pyBrowser, rect_out=pyRect)
if ret:
assert (pyRect and len(pyRect) == 4), "rectangle not provided"
cefRect.x = pyRect[0]
cefRect.y = pyRect[1]
cefRect.width = pyRect[2]
cefRect.height = pyRect[3]
return True
else:
return False
else:
# without a default cefRect, pysdl2 example will fail
# the value is inspired by https://github.com/obsproject/obs-browser/blob/master/browser-client.cpp#L280
cefRect.x = 0
cefRect.y = 0
cefRect.width = 16
cefRect.height = 16
return True
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RenderHandler_GetScreenPoint(
CefRefPtr[CefBrowser] cefBrowser,
int viewX, int viewY,
int& screenX, int& screenY
) except * with gil:
cdef PyBrowser pyBrowser
cdef list screenCoordinates = []
cdef py_bool ret
try:
pyBrowser = GetPyBrowser(cefBrowser, "GetScreenPoint")
callback = pyBrowser.GetClientCallback("GetScreenPoint")
if callback:
ret = callback(browser=pyBrowser,
view_x=viewX,
view_y=viewY,
screen_coordinates_out=screenCoordinates)
if ret:
assert (screenCoordinates and len(screenCoordinates) == 2), (
"screenCoordinates not provided or invalid")
(&screenX)[0] = int(screenCoordinates[0])
(&screenY)[0] = int(screenCoordinates[1])
return True
else:
return False
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RenderHandler_GetScreenInfo(
CefRefPtr[CefBrowser] cefBrowser,
CefScreenInfo& cefScreenInfo
) except * with gil:
cdef PyBrowser pyBrowser
cdef dict pyScreenInfo = {}
cdef py_bool ret
cdef list pyRect
cdef double deviceScaleFactor
try:
pyBrowser = GetPyBrowser(cefBrowser, "GetScreenInfo")
callback = pyBrowser.GetClientCallback("GetScreenInfo")
if not callback:
return False
ret = callback(browser=pyBrowser, screen_info_out=pyScreenInfo)
if not ret:
return False
# device_scale_factor is the ratio between physical and logical
# pixels. On HiDPI displays this is typically 2.0 (1.25/1.5/1.75
# for fractional scaling). It MUST be > 0 -- a value of 0 will
# divide-by-zero inside Chromium's compositor. Setting this also
# scales the OnPaint buffer: a 800x600 view rect with
# device_scale_factor=2.0 yields a 1600x1200 BGRA buffer.
if "device_scale_factor" in pyScreenInfo:
deviceScaleFactor = float(pyScreenInfo["device_scale_factor"])
assert deviceScaleFactor > 0.0, (
"device_scale_factor must be > 0")
cefScreenInfo.device_scale_factor = <float>deviceScaleFactor
else:
cefScreenInfo.device_scale_factor = 1.0
cefScreenInfo.depth = int(pyScreenInfo.get("depth", 24))
cefScreenInfo.depth_per_component = int(
pyScreenInfo.get("depth_per_component", 8))
cefScreenInfo.is_monochrome = <cpp_bool>bool(
pyScreenInfo.get("is_monochrome", False))
# rect/available_rect are in DIP (logical pixels). Leaving them
# zero-initialized tells CEF to fall back to GetViewRect (see
# cef_render_handler.h:107).
if "rect" in pyScreenInfo:
pyRect = list(pyScreenInfo["rect"])
assert len(pyRect) == 4, "rect must be [x, y, width, height]"
cefScreenInfo.rect.x = int(pyRect[0])
cefScreenInfo.rect.y = int(pyRect[1])
cefScreenInfo.rect.width = int(pyRect[2])
cefScreenInfo.rect.height = int(pyRect[3])
if "available_rect" in pyScreenInfo:
pyRect = list(pyScreenInfo["available_rect"])
assert len(pyRect) == 4, (
"available_rect must be [x, y, width, height]")
cefScreenInfo.available_rect.x = int(pyRect[0])
cefScreenInfo.available_rect.y = int(pyRect[1])
cefScreenInfo.available_rect.width = int(pyRect[2])
cefScreenInfo.available_rect.height = int(pyRect[3])
elif "rect" in pyScreenInfo:
# Mirror rect into available_rect so popups place correctly
# when the caller only supplied one.
cefScreenInfo.available_rect.x = cefScreenInfo.rect.x
cefScreenInfo.available_rect.y = cefScreenInfo.rect.y
cefScreenInfo.available_rect.width = cefScreenInfo.rect.width
cefScreenInfo.available_rect.height = cefScreenInfo.rect.height
return True
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RenderHandler_OnPopupShow(
CefRefPtr[CefBrowser] cefBrowser,
cpp_bool show
) noexcept with gil:
cdef PyBrowser pyBrowser
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnPopupShow")
callback = pyBrowser.GetClientCallback("OnPopupShow")
if callback:
callback(browser=pyBrowser, show=show)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RenderHandler_OnPopupSize(
CefRefPtr[CefBrowser] cefBrowser,
const CefRect& cefRect
) noexcept with gil:
cdef PyBrowser pyBrowser
cdef list pyRect
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnPopupSize")
callback = pyBrowser.GetClientCallback("OnPopupSize")
if callback:
pyRect = [cefRect.x, cefRect.y, cefRect.width, cefRect.height]
callback(browser=pyBrowser, rect_out=pyRect)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RenderHandler_OnPaint(
CefRefPtr[CefBrowser] cefBrowser,
cef_types.cef_paint_element_type_t paintElementType,
cpp_vector[CefRect]& cefDirtyRects,
const void* cefBuffer,
int width,
int height
) noexcept with gil:
cdef PyBrowser pyBrowser
cdef list pyDirtyRects = []
cdef list pyRect
# TODO: cefDirtyRects should be const, but const_iterator is
# not yet implemented in libcpp.vector.
cdef cpp_vector[CefRect].iterator iterator
cdef CefRect cefRect
cdef PaintBuffer paintBuffer
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnPaint")
iterator = cefDirtyRects.begin()
while iterator != cefDirtyRects.end():
cefRect = deref(iterator)
pyRect = [cefRect.x, cefRect.y, cefRect.width, cefRect.height]
pyDirtyRects.append(pyRect)
preinc(iterator)
# In CEF 1 width and height were fetched using GetSize(),
# but in CEF 3 they are passed as arguments to OnPaint().
# OFF: | (width, height) = pyBrowser.GetSize(paintElementType)
paintBuffer = CreatePaintBuffer(cefBuffer, width, height)
callback = pyBrowser.GetClientCallback("OnPaint")
if callback:
callback(
browser=pyBrowser,
element_type=paintElementType,
dirty_rects=pyDirtyRects,
paint_buffer=paintBuffer,
width=width,
height=height)
else:
return
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RenderHandler_OnScrollOffsetChanged(
CefRefPtr[CefBrowser] cefBrowser,
double x,
double y
) noexcept with gil:
cdef PyBrowser pyBrowser
try:
pyBrowser = GetPyBrowser(cefBrowser, "OnScrollOffsetChanged")
callback = pyBrowser.GetClientCallback("OnScrollOffsetChanged")
if callback:
callback(browser=pyBrowser, x=x, y=y)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public cpp_bool RenderHandler_StartDragging(
CefRefPtr[CefBrowser] cef_browser,
CefRefPtr[CefDragData] cef_drag_data,
uint32_t allowed_ops,
int x, int y
) except * with gil:
cdef PyBrowser browser
cdef DragData drag_data
cdef py_bool ret
try:
browser = GetPyBrowser(cef_browser, "StartDragging")
drag_data = DragData_Init(cef_drag_data)
callback = browser.GetClientCallback("StartDragging")
if callback:
ret = callback(
browser=browser,
drag_data=drag_data,
allowed_ops=allowed_ops,
x=x,
y=y)
if ret:
return True
else:
return False
else:
return False
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RenderHandler_UpdateDragCursor(
CefRefPtr[CefBrowser] cef_browser,
uint32_t operation,
) noexcept with gil:
cdef PyBrowser browser
try:
browser = GetPyBrowser(cef_browser, "UpdateDragCursor")
callback = browser.GetClientCallback("UpdateDragCursor")
if callback:
callback(browser=browser, operation=operation)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
cdef public void RenderHandler_OnTextSelectionChanged(
CefRefPtr[CefBrowser] cef_browser,
const CefString& selected_text,
const CefRange& selected_range
) noexcept with gil:
cdef PyBrowser browser
try:
browser = GetPyBrowser(cef_browser, "OnTextSelectionChanged")
callback = browser.GetClientCallback("OnTextSelectionChanged")
if callback:
callback(browser=browser,
selected_text=CefToPyString(selected_text),
selected_range=[selected_range.from_val,
selected_range.to_val])
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)