Skip to content

Instantly share code, notes, and snippets.

@jegger
Created July 18, 2013 08:49
Show Gist options
  • Save jegger/6027784 to your computer and use it in GitHub Desktop.
Save jegger/6027784 to your computer and use it in GitHub Desktop.
from kivy.app import App
from kivy.graphics.fbo import Fbo
from kivy.uix.widget import Widget
from kivy.graphics import Color, Rectangle, GraphicException
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.properties import ObjectProperty
####CEF IMPORT ####
####
import ctypes, os, sys
libcef_so = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'libcef.so')
if os.path.exists(libcef_so):
# Import local module
ctypes.CDLL(libcef_so, ctypes.RTLD_GLOBAL)
if 0x02070000 <= sys.hexversion < 0x03000000:
import cefpython_py27 as cefpython
else:
raise Exception("Unsupported python version: %s" % sys.version)
else:
# Import from package
from cefpython3 import cefpython
####KIVY###########
####
class CEF(Widget):
texture = ObjectProperty(None, allownone=True)
def __init__(self, **kwargs):
super(CEF, self).__init__(**kwargs)
#define size
self.size = (500, 500)
# create the fbo & add it to canvas
self.texture = Texture.create(size=self.size, colorfmt='rgba', bufferfmt='ubyte')
self.texture.flip_vertical()
with self.canvas:
Color(1, 1, 1)
Rectangle(size=self.size, texture=self.texture)
#start cef browser
Clock.schedule_once(self.start, 1)
def cef_mes(self, *kwargs):
cefpython.MessageLoopWork()
def start(self, *kwargs):
cefpython.g_debug = True
settings = {"log_severity": cefpython.LOGSEVERITY_INFO,
#"log_file": GetApplicationPath("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")}
#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
browserSettings = {}
browser = cefpython.CreateBrowserSync(windowInfo, browserSettings, navigateUrl="about:blank")
#Create RenderHandler (in ClientHandler)
CH = ClientHandler(browser, self.texture)
browser.SetClientHandler(CH)
#Call WasResized() => force cef to call GetViewRect()
browser.WasResized()
#Load desired URL
browser.GetMainFrame().LoadUrl("http://www.google.com")
class ClientHandler:
browser = None
size = (500, 500)
def __init__(self, browser, texture):
self.browser = browser
self.texture = texture
def OnPaint(self, browser, paintElementType, dirtyRects, buffer):
print("OnPaint()")
print("paintElementType: ", paintElementType)
print("dirtyRects:", dirtyRects)
if paintElementType != cefpython.PET_VIEW:
return
buffer = buffer.GetString(mode="bgra", origin="top-left")
self.texture.blit_buffer(buffer, colorfmt='bgra', bufferfmt='ubyte')
return True
'''
if not self.texture:
#create texture
self.texture = Texture.create(size=self.size, colorfmt='rgba', bufferfmt='ubyte')
#get string out of cefbuffer
buffer = buffer.GetString(mode="bgra", origin="top-left")
print buffer[0:100]
#blip buffer
self.texture.blit_buffer(buffer, colorfmt='bgra', bufferfmt='ubyte')
self.texture.flip_vertical()
#draw buffer
with self.ui.canvas:
Rectangle(texture=self.texture, pos=self.ui.pos, size=self.size)
self.texture = None
'''
return True
def GetViewRect(self, browser, rect):
#print("GetViewRect()")
width, height = self.size
rect.append(0)
rect.append(0)
rect.append(width)
rect.append(height)
return True
if __name__ == '__main__':
class CEFApp(App):
title = 'CefBrowser'
def build(self):
return CEF()
CEFApp().run()
cefpython.Shutdown()
@tito
Copy link

tito commented Sep 17, 2013

So Awesome jegger! What issues left do you have? If you make a garden package, that would be excellent for the community, and we can avoid berkelium :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment