Skip to content

Commit 6431b0b

Browse files
committed
Fixes to the Kivy OSR example:
- the _keyboard property not set exception when release_keyboard() is called - fixed the bug that caused js bindings to be removed after a call to Navigate() - fixed the lost focus after the call to Navigate()
1 parent 12dbcdc commit 6431b0b

2 files changed

Lines changed: 63 additions & 16 deletions

File tree

cefpython/cef3/linux/binaries_64bit/kivy_.py

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CefBrowser(Widget):
6969

7070
'''Represent a browser widget for kivy, which can be used like a normal widget.
7171
'''
72-
def __init__(self, start_url='http://www.google.com', **kwargs):
72+
def __init__(self, start_url='http://www.google.com/', **kwargs):
7373
super(CefBrowser, self).__init__(**kwargs)
7474

7575
self.start_url = start_url
@@ -88,7 +88,7 @@ def size_changed(self, *kwargs):
8888
'''
8989
if self.starting:
9090
if self.height != 100:
91-
self.start_cef(self.start_url)
91+
self.start_cef()
9292
self.starting = False
9393
else:
9494
self.texture = Texture.create(
@@ -116,7 +116,7 @@ def _update_rect(self, *kwargs):
116116
self.rect.texture = self.texture
117117

118118

119-
def start_cef(self, start_url='http://google.com'):
119+
def start_cef(self):
120120
'''Starts CEF.
121121
'''
122122
# create texture & add it to canvas
@@ -163,22 +163,14 @@ def start_cef(self, start_url='http://google.com'):
163163
# Do not use "about:blank" as navigateUrl - this will cause
164164
# the GoBack() and GoForward() methods to not work.
165165
self.browser = cefpython.CreateBrowserSync(windowInfo, browserSettings,
166-
navigateUrl=start_url)
166+
navigateUrl=self.start_url)
167167

168168
#set focus
169169
self.browser.SendFocusEvent(True)
170170

171-
#Create RenderHandler (in ClientHandler)
172-
CH = ClientHandler(self)
173-
self.browser.SetClientHandler(CH)
174-
175-
jsBindings = cefpython.JavascriptBindings(
176-
bindToFrames=True, bindToPopups=True)
177-
jsBindings.SetFunction("__kivy__request_keyboard",
178-
self.request_keyboard)
179-
jsBindings.SetFunction("__kivy__release_keyboard",
180-
self.release_keyboard)
181-
self.browser.SetJavascriptBindings(jsBindings)
171+
self._client_handler = ClientHandler(self)
172+
self.browser.SetClientHandler(self._client_handler)
173+
self.set_js_bindings()
182174

183175
#Call WasResized() => force cef to call GetViewRect() and OnPaint afterwards
184176
self.browser.WasResized()
@@ -189,6 +181,38 @@ def start_cef(self, start_url='http://google.com'):
189181
if self.keyboard_mode == "global":
190182
self.request_keyboard()
191183

184+
# Clock.schedule_once(self.change_url, 5)
185+
186+
187+
_client_handler = None
188+
_js_bindings = None
189+
190+
def set_js_bindings(self):
191+
# When browser.Navigate() is called, some bug appears in CEF
192+
# that makes CefRenderProcessHandler::OnBrowserDestroyed()
193+
# is being called. This destroys the javascript bindings in
194+
# the Render process. We have to make the js bindings again,
195+
# after the call to Navigate() when OnLoadingStateChange()
196+
# is called with isLoading=False. Problem reported here:
197+
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=11009
198+
199+
if not self._js_bindings:
200+
self._js_bindings = cefpython.JavascriptBindings(
201+
bindToFrames=True, bindToPopups=True)
202+
self._js_bindings.SetFunction("__kivy__request_keyboard",
203+
self.request_keyboard)
204+
self._js_bindings.SetFunction("__kivy__release_keyboard",
205+
self.release_keyboard)
206+
207+
self.browser.SetJavascriptBindings(self._js_bindings)
208+
209+
210+
def change_url(self, *kwargs):
211+
self.browser.Navigate("http://www.google.com/")
212+
self._client_handler._reset_js_bindings = True
213+
214+
215+
_keyboard = None
192216

193217
def request_keyboard(self):
194218
print("request_keyboard()")
@@ -202,6 +226,10 @@ def request_keyboard(self):
202226
self.is_ctrl2 = False
203227
self.is_alt1 = False
204228
self.is_alt2 = False
229+
# Browser lost its focus after the LoadURL() and the
230+
# OnBrowserDestroyed() callback bug. This will only work
231+
# when keyboard mode is local.
232+
self.browser.SendFocusEvent(True)
205233

206234

207235
def release_keyboard(self):
@@ -468,6 +496,8 @@ def on_touch_up(self, touch, *kwargs):
468496

469497
class ClientHandler:
470498

499+
_reset_js_bindings = False
500+
471501
def __init__(self, browserWidget):
472502
self.browserWidget = browserWidget
473503

@@ -509,16 +539,29 @@ def OnLoadStart(self, browser, frame):
509539
__kivy__keyboard_requested = false;
510540
}
511541
}
512-
setInterval(__kivy__keyboard_interval, 13);
542+
setInterval(__kivy__keyboard_interval, 100);
513543
"""
514544
frame.ExecuteJavascript(jsCode,
515545
"kivy_.py > ClientHandler > OnLoadStart")
516546

547+
548+
def OnLoadEnd(self, browser, frame, httpStatusCode):
549+
# Browser lost its focus after the LoadURL() and the
550+
# OnBrowserDestroyed() callback bug. When keyboard mode
551+
# is local the fix is in the request_keyboard() method.
552+
# Call it from OnLoadEnd only when keyboard mode is global.
553+
browserWidget = browser.GetUserData("browserWidget")
554+
if browserWidget and browserWidget.keyboard_mode == "global":
555+
browser.SendFocusEvent(True)
556+
517557

518558
def OnLoadingStateChange(self, browser, isLoading, canGoBack,
519559
canGoForward):
520560
print("OnLoadingStateChange(): isLoading = %s" % isLoading)
521561
browserWidget = browser.GetUserData("browserWidget")
562+
if self._reset_js_bindings and not isLoading:
563+
if browserWidget:
564+
browserWidget.set_js_bindings()
522565
if isLoading and browserWidget \
523566
and browserWidget.keyboard_mode == "local":
524567
# Release keyboard when navigating to a new page.

cefpython/cef3/subprocess/cefpython_app.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ void CefPythonApp::DoJavascriptBindingsForBrowser(
354354
&& jsBindings->GetType("bindToFrames") == VTYPE_BOOL
355355
&& jsBindings->GetBool("bindToFrames")) {
356356
browser->GetFrameIdentifiers(frameIds);
357+
// CEF BUG: frameIds contain just one frame with frameId=0
358+
if (frameIds.size() == 1 && frameIds[0] == 0) {
359+
frameIds.push_back(browser->GetMainFrame()->GetIdentifier());
360+
}
357361
} else {
358362
frameIds.push_back(browser->GetMainFrame()->GetIdentifier());
359363
}

0 commit comments

Comments
 (0)