Skip to content

Commit

Permalink
Removed blank space and break lines above 70 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Guillemot committed Jan 24, 2014
1 parent 8713cf5 commit 7b763f4
Showing 1 changed file with 96 additions and 73 deletions.
169 changes: 96 additions & 73 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
PYVERSION = sys.version_info[0]
BITS = '64' if sys.maxint > 2 ** 31 else '32'
CURDIR = realpath(dirname(__file__))
cef_dir = join(CURDIR, 'libs',
'{}{}.py{}'.format(platform(), BITS, PYVERSION))
cef_dir = join(
CURDIR,
'libs',
'{}{}.py{}'.format(platform(), BITS, PYVERSION)
)

# correctly locate libcef.so (we need to extend LD_LIBRARY_PATH for subprocess
# executable)
Expand Down Expand Up @@ -68,21 +71,21 @@ class CefBrowser(Widget):
# 2. Local mode forwards keys to CEF only when an editable
# control is focused (input type=text|password or textarea).
keyboard_mode = "global"

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

self.start_url = start_url

#Workaround for flexible size:
#start browser when the height has changed (done by layout)
#This has to be done like this because I wasn't able to change
#This has to be done like this because I wasn't able to change
#the texture size
#until runtime without core-dump.
self.bind(size = self.size_changed)


starting = True
def size_changed(self, *kwargs):
Expand All @@ -94,7 +97,10 @@ def size_changed(self, *kwargs):
self.starting = False
else:
self.texture = Texture.create(
size=self.size, colorfmt='rgba', bufferfmt='ubyte')
size=self.size,
colorfmt='rgba',
bufferfmt='ubyte'
)
self.texture.flip_vertical()
with self.canvas:
Color(1, 1, 1)
Expand All @@ -104,26 +110,29 @@ def size_changed(self, *kwargs):
self.rect.size = self.size
self.browser.WasResized()


def _cef_mes(self, *kwargs):
'''Get called every frame.
'''
cefpython.MessageLoopWork()


def _update_rect(self, *kwargs):
'''Get called whenever the texture got updated.
'''Get called whenever the texture got updated.
=> we need to reset the texture for the rectangle
'''
self.rect.texture = self.texture


def start_cef(self):
'''Starts CEF.
'''Starts CEF.
'''
# create texture & add it to canvas
self.texture = Texture.create(
size=self.size, colorfmt='rgba', bufferfmt='ubyte')
size=self.size,
colorfmt='rgba',
bufferfmt='ubyte'
)
self.texture.flip_vertical()
with self.canvas:
Color(1, 1, 1)
Expand All @@ -132,50 +141,59 @@ def start_cef(self):
#configure cef
cefpython.g_debug = True
cefpython.g_debugFile = "debug.log"
settings = {"log_severity": cefpython.LOGSEVERITY_INFO,
"log_file": "debug.log",
"release_dcheck_enabled": True, # Enable only when debugging.
# This directories must be set on Linux
"locales_dir_path": cefpython.GetModuleDirectory()+"/locales",
"resources_dir_path": cefpython.GetModuleDirectory(),
"browser_subprocess_path": "%s/%s" % (cefpython.GetModuleDirectory(), "subprocess")}

settings = {
"log_severity": cefpython.LOGSEVERITY_INFO,
"log_file": "debug.log",
"release_dcheck_enabled": True, # Enable only when debugging.
# This directories must be set on Linux
"locales_dir_path": cefpython.GetModuleDirectory() +
os.sep +
"locales",
"resources_dir_path": cefpython.GetModuleDirectory(),
"browser_subprocess_path": cefpython.GetModuleDirectory() +
os.sep +
subprocess
}

#start idle
Clock.schedule_interval(self._cef_mes, 0)

#init CEF
cefpython.Initialize(settings)

#WindowInfo offscreen flag
windowInfo = cefpython.WindowInfo()
windowInfo.SetAsOffscreen(0)

#Create Broswer and naviagte to empty page <= OnPaint won't get called yet
browserSettings = {}
# The render handler callbacks are not yet set, thus an
# The render handler callbacks are not yet set, thus an
# error report will be thrown in the console (when release
# DCHECKS are enabled), however don't worry, it is harmless.
# This is happening because calling GetViewRect will return
# This is happening because calling GetViewRect will return
# false. That's why it is initially navigating to "about:blank".
# Later, a real url will be loaded using the LoadUrl() method
# Later, a real url will be loaded using the LoadUrl() method
# and the GetViewRect will be called again. This time the render
# handler callbacks will be available, it will work fine from
# this point.
# --
# Do not use "about:blank" as navigateUrl - this will cause
# the GoBack() and GoForward() methods to not work.
self.browser = cefpython.CreateBrowserSync(windowInfo, browserSettings,
navigateUrl=self.start_url)

self.browser = cefpython.CreateBrowserSync(
windowInfo,
browserSettings,
navigateUrl=self.start_url
)

#set focus
self.browser.SendFocusEvent(True)

self._client_handler = ClientHandler(self)
self.browser.SetClientHandler(self._client_handler)
self.set_js_bindings()

#Call WasResized() => force cef to call GetViewRect() and OnPaint afterwards
self.browser.WasResized()
self.browser.WasResized()

# The browserWidget instance is required in OnLoadingStateChange().
self.browser.SetUserData("browserWidget", self)
Expand All @@ -184,37 +202,38 @@ def start_cef(self):
self.request_keyboard()

# Clock.schedule_once(self.change_url, 5)


_client_handler = None
_js_bindings = None

def set_js_bindings(self):
if not self._js_bindings:
self._js_bindings = cefpython.JavascriptBindings(
bindToFrames=True, bindToPopups=True)
self._js_bindings.SetFunction("__kivy__request_keyboard",
self._js_bindings.SetFunction("__kivy__request_keyboard",
self.request_keyboard)
self._js_bindings.SetFunction("__kivy__release_keyboard",
self.release_keyboard)
self.browser.SetJavascriptBindings(self._js_bindings)


def change_url(self, url, *kwargs):
# Doing a javascript redirect instead of Navigate()
# solves the js bindings error. The url here need to
# be preceded with "http://". Calling StopLoad()
# might be a good idea before making the js navigation.

self.browser.StopLoad()
self.browser.GetMainFrame().ExecuteJavascript(
"window.location='%s'"%url)
"window.location='%s'"%url
)

# Do not use Navigate() or GetMainFrame()->LoadURL(),
# as it causes the js bindings to be removed. There is
# a bug in CEF, that happens after a call to Navigate().
# The OnBrowserDestroyed() callback is fired and causes
# the js bindings to be removed. See this topic for more
# The OnBrowserDestroyed() callback is fired and causes
# the js bindings to be removed. See this topic for more
# details:
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=11009

Expand Down Expand Up @@ -351,7 +370,7 @@ def on_key_up(self, keyboard, keycode):
def translate_to_cef_keycode(self, keycode):
# TODO: this works on Linux, but on Windows the key
# mappings will probably be different.
# TODO: what if the Kivy keyboard layout is changed
# TODO: what if the Kivy keyboard layout is changed
# from qwerty to azerty? (F1 > options..)
cef_keycode = keycode
if self.is_alt2:
Expand All @@ -362,7 +381,7 @@ def translate_to_cef_keycode(self, keycode):
# tilde
"96":172,
# 0-9 (48..57)
"48":125, "49":185, "50":178, "51":179, "52":188,
"48":125, "49":185, "50":178, "51":179, "52":188,
"53":189, "54":190, "55":123, "56":91, "57":93,
# minus
"45":92,
Expand All @@ -383,7 +402,7 @@ def translate_to_cef_keycode(self, keycode):
# tilde
"96":172,
# 0-9 (48..57)
"48":176, "49":161, "50":2755, "51":163, "52":36,
"48":176, "49":161, "50":2755, "51":163, "52":36,
"53":2756, "54":2757, "55":2758, "56":2761, "57":177,
# minus
"45":191,
Expand Down Expand Up @@ -449,10 +468,10 @@ def translate_to_cef_keycode(self, keycode):
"13":65293,
# PrScr, ScrLck, Pause
"316":65377, "302":65300, "19":65299,
# Insert, Delete,
# Home, End,
# Insert, Delete,
# Home, End,
# Pgup, Pgdn
"277":65379, "127":65535,
"277":65379, "127":65535,
"278":65360, "279":65367,
"280":65365, "281":65366,
# Arrows (left, up, right, down)
Expand All @@ -468,15 +487,15 @@ def go_forward(self):
'''
print "go forward"
self.browser.GoForward()


def go_back(self):
'''Going back in browser history
'''
print "go back"
self.browser.GoBack()


def on_touch_down(self, touch, *kwargs):
if not self.collide_point(*touch.pos):
return
Expand Down Expand Up @@ -513,8 +532,8 @@ def __init__(self, browserWidget):

def _fix_select_boxes(self, frame):
# This is just a temporary fix, until proper Popup widgets
# painting is implemented (PET_POPUP in OnPaint). Currently
# there is no way to obtain a native window handle (GtkWindow
# painting is implemented (PET_POPUP in OnPaint). Currently
# there is no way to obtain a native window handle (GtkWindow
# pointer) in Kivy, and this may cause things like context menus,
# select boxes and plugins not to display correctly. Although,
# this needs to be tested. The popup widget buffers are
Expand All @@ -529,7 +548,7 @@ def _fix_select_boxes(self, frame):
# See also a related topic on the Kivy-users group:
# https://groups.google.com/d/topic/kivy-users/WdEQyHI5vTs/discussion
# --
# The javascript select boxes library used:
# The javascript select boxes library used:
# http://marcj.github.io/jquery-selectBox/
# --
# Cannot use "file://" urls to load local resources, error:
Expand Down Expand Up @@ -560,7 +579,7 @@ def _fix_select_boxes(self, frame):
style.appendChild(document.createTextNode("%(css_content)s"));
head.appendChild(style);
""" % locals()
frame.ExecuteJavascript(jsCode,
frame.ExecuteJavascript(jsCode,
"kivy_.py > ClientHandler > OnLoadStart > _fix_select_boxes()")


Expand All @@ -580,7 +599,7 @@ def OnLoadStart(self, browser, frame):
}
var tag = element.tagName;
var type = element.type;
if (tag == "INPUT" && (type == "" || type == "text"
if (tag == "INPUT" && (type == "" || type == "text"
|| type == "password") || tag == "TEXTAREA") {
if (!__kivy__keyboard_requested) {
__kivy__request_keyboard();
Expand All @@ -604,51 +623,55 @@ def OnLoadStart(self, browser, frame):
}
setInterval(__kivy__keyboard_interval, 100);
"""
frame.ExecuteJavascript(jsCode,
frame.ExecuteJavascript(jsCode,
"kivy_.py > ClientHandler > OnLoadStart")


def OnLoadEnd(self, browser, frame, httpStatusCode):
# Browser lost its focus after the LoadURL() and the
# Browser lost its focus after the LoadURL() and the
# OnBrowserDestroyed() callback bug. When keyboard mode
# is local the fix is in the request_keyboard() method.
# Call it from OnLoadEnd only when keyboard mode is global.
browserWidget = browser.GetUserData("browserWidget")
if browserWidget and browserWidget.keyboard_mode == "global":
browser.SendFocusEvent(True)


def OnLoadingStateChange(self, browser, isLoading, canGoBack,
canGoForward):
print("OnLoadingStateChange(): isLoading = %s" % isLoading)
browserWidget = browser.GetUserData("browserWidget")

def OnPaint(self, browser, paintElementType, dirtyRects, buffer, width,
height):

def OnPaint(self, browser, paintElementType, dirtyRects, buffer, width,
height):
# print "OnPaint()"
if paintElementType != cefpython.PET_VIEW:
print "Popups aren't implemented yet"
return

#update buffer
buffer = buffer.GetString(mode="bgra", origin="top-left")

#update texture of canvas rectangle
self.browserWidget.texture.blit_buffer(buffer, colorfmt='bgra',
bufferfmt='ubyte')
self.browserWidget.texture.blit_buffer(
buffer,
colorfmt='bgra',
bufferfmt='ubyte'
)

self.browserWidget._update_rect()

return True


def GetViewRect(self, browser, rect):
width, height = self.browserWidget.texture.size
rect.append(0)
rect.append(0)
rect.append(width)
rect.append(height)
# print("GetViewRect(): %s x %s" % (width, height))
#print("GetViewRect(): %s x %s" % (width, height))
return True


Expand Down Expand Up @@ -679,5 +702,5 @@ def build(self):
return BrowserLayout()

CefBrowserApp().run()

cefpython.Shutdown()

0 comments on commit 7b763f4

Please sign in to comment.