def __init__(self, url=None, popup=False): if popup: title = "wxPython Popup" else: title = "wxPython CEF 3 example" wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title=title) size=(800,600) # This is an optional code to enable High DPI support. if "auto_zooming" in g_applicationSettings \ and g_applicationSettings["auto_zooming"] == "system_dpi": # This utility function will adjust width/height using # OS DPI settings. For 800/600 with Win7 DPI settings # being set to "Larger 150%" will return 1200/900. size = cefpython.DpiAware.CalculateWindowSize(size[0], size[1]) self.SetSize(size) if not url: url = "file://"+GetApplicationPath("example.html") # Test hash in url. # url += "#test-hash" self.CreateMenu() if TEST_EMBEDDING_IN_PANEL: print("Embedding in a wx.Panel!") # You also have to set the wx.WANTS_CHARS style for # all parent panels/controls, if it's deeply embedded. self.mainPanel = wx.Panel(self, style=wx.WANTS_CHARS) # Global client callbacks must be set before browser is created. self.clientHandler = ClientHandler() cefpython.SetGlobalClientCallback("OnCertificateError", self.clientHandler._OnCertificateError) cefpython.SetGlobalClientCallback("OnBeforePluginLoad", self.clientHandler._OnBeforePluginLoad) cefpython.SetGlobalClientCallback("OnAfterCreated", self.clientHandler._OnAfterCreated) windowInfo = cefpython.WindowInfo() windowInfo.SetAsChild(self.GetHandleForBrowser()) self.browser = cefpython.CreateBrowserSync(windowInfo, browserSettings=g_browserSettings, navigateUrl=url) self.clientHandler.mainBrowser = self.browser self.browser.SetClientHandler(self.clientHandler) jsBindings = cefpython.JavascriptBindings( bindToFrames=True, bindToPopups=True) jsBindings.SetFunction("PyPrint", PyPrint) jsBindings.SetProperty("pyProperty", "This was set in Python") jsBindings.SetProperty("pyConfig", ["This was set in Python", {"name": "Nested dictionary", "isNested": True}, [1,"2", None]]) self.javascriptExternal = JavascriptExternal(self.browser) jsBindings.SetObject("external", self.javascriptExternal) jsBindings.SetProperty("sources", GetSources()) self.browser.SetJavascriptBindings(jsBindings) if self.mainPanel: self.mainPanel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) self.mainPanel.Bind(wx.EVT_SIZE, self.OnSize) else: self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_CLOSE, self.OnClose) if USE_EVT_IDLE and not popup: # Bind EVT_IDLE only for the main application frame. print("Using EVT_IDLE to execute the CEF message loop work") self.Bind(wx.EVT_IDLE, self.OnIdle)
def __init__(self, url=None, popup=False, params = None): if popup: title = os.path.basename(url) else: title = "Kam1n0" wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title=title) size=(500,500) # icon #setup icon object #icon = wx.Icon(GetApplicationPath("www/img/favicon.ico"), wx.BITMAP_TYPE_ICO) #setup taskbar icon #tbicon = wx.TaskBarIcon() #tbicon.SetIcon(icon, "McGill Icon") loc = wx.IconLocation(GetApplicationPath("www/img/favicon.ico"), 0) self.SetIcon(wx.IconFromLocation(loc)) # This is an optional code to enable High DPI support. if "auto_zooming" in g_applicationSettings \ and g_applicationSettings["auto_zooming"] == "system_dpi": # This utility function will adjust width/height using # OS DPI settings. For 800/600 with Win7 DPI settings # being set to "Larger 150%" will return 1200/900. size = cefpython.DpiAware.CalculateWindowSize(size[0], size[1]) self.SetSize(size) if not url: url = "file://"+GetApplicationPath("www/FragmentInput.html") # Test hash in url. # url += "#test-hash" if TEST_EMBEDDING_IN_PANEL: # You also have to set the wx.WANTS_CHARS style for # all parent panels/controls, if it's deeply embedded. self.mainPanel = wx.Panel(self, style=wx.WANTS_CHARS) # Global client callbacks must be set before browser is created. self.clientHandler = ClientHandler() cefpython.SetGlobalClientCallback("OnCertificateError", self.clientHandler._OnCertificateError) cefpython.SetGlobalClientCallback("OnBeforePluginLoad", self.clientHandler._OnBeforePluginLoad) cefpython.SetGlobalClientCallback("OnAfterCreated", self.clientHandler._OnAfterCreated) windowInfo = cefpython.WindowInfo() windowInfo.SetAsChild(self.GetHandleForBrowser()) self.browser = cefpython.CreateBrowserSync(windowInfo, browserSettings=g_browserSettings, navigateUrl=url) self.clientHandler.mainBrowser = self.browser self.browser.SetClientHandler(self.clientHandler) jsBindings = cefpython.JavascriptBindings( bindToFrames=False, bindToPopups=True) jsBindings.SetProperty("pyProperty", "This was set in Python") jsBindings.SetProperty("pyConfig", ["This was set in Python", {"name": "Nested dictionary", "isNested": True}, [1,"2", None]]) global gdata if gdata is None: gdata = GetData() self.javascriptExternal = JavascriptExternal(self.browser, gdata) self.javascriptExternal.frame = self jsBindings.SetObject("external", self.javascriptExternal) jsBindings.SetProperty("GData", gdata) if not params is None: jsBindings.SetProperty("params", params) self.browser.SetJavascriptBindings(jsBindings) if self.mainPanel: self.mainPanel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) self.mainPanel.Bind(wx.EVT_SIZE, self.OnSize) else: self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_CLOSE, self.OnClose) if USE_EVT_IDLE and not popup: # Bind EVT_IDLE only for the main application frame. self.Bind(wx.EVT_IDLE, self.OnIdle)
def start_cef(self): '''Starts CEF. ''' # create texture & 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) self.rect = Rectangle(size=self.size, texture=self.texture, pos=self.pos) #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() + 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 # 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 # false. That's why it is initially navigating to "about:blank". # 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) #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() # The browserWidget instance is required in OnLoadingStateChange(). self.browser.SetUserData("browserWidget", self) if self.keyboard_mode == "global": self.request_keyboard()
def start_cef(self): '''Starts CEF. ''' # create texture & 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) self.rect = Rectangle(size=self.size, texture=self.texture) # Configure CEF settings = { "debug": True, # cefpython debug messages in console and in log_file "log_severity": cefpython.LOGSEVERITY_INFO, "log_file": "debug.log", # 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"), "windowless_rendering_enabled": True, "context_menu": { # Disable context menu, popup widgets not supported "enabled": False, }, } switches = { # Tweaking OSR performance by setting the same Chromium flags # as in upstream cefclient (# Issue #240). "disable-surfaces": "", "disable-gpu": "", "disable-gpu-compositing": "", "enable-begin-frame-scheduling": "", } browserSettings = { # Tweaking OSR performance (Issue #240) "windowless_frame_rate": 60 } # Initialize CEF cefpython.WindowUtils.InstallX11ErrorHandlers() global g_switches g_switches = switches cefpython.Initialize(settings, switches) # Start idle - CEF message loop work. Clock.schedule_once(self._message_loop_work, 0) # CEF needs a valid window handle passed to SetAsOffscreen() # for Printing to work. There is no API to get Kivy's window # handle so creating a dummy hidden Window using GTK. gtkwin = gtk.Window() gtkwin.realize() # WindowInfo offscreen flag windowInfo = cefpython.WindowInfo() windowInfo.SetAsOffscreen(gtkwin.window.xid) # Create Broswer and naviagte to empty page <= OnPaint won't get # called yet # 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 # false. That's why it is initially navigating to "about:blank". # 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) # 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() # The browserWidget instance is required in OnLoadingStateChange(). self.browser.SetUserData("browserWidget", self) if self.keyboard_mode == "global": self.request_keyboard()
def __init__(self, url=None): wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title='wxPython CEF 3 example', size=(1024, 768)) if not url: url = "file://" + GetApplicationPath("wxpython.html") # Test hash in url. # url += "#test-hash" self.CreateMenu() # Cannot attach browser to the main frame as this will cause # the menu not to work. # -- # You also have to set the wx.WANTS_CHARS style for # all parent panels/controls, if it's deeply embedded. self.mainPanel = wx.Panel(self, style=wx.WANTS_CHARS) # Global client callbacks must be set before browser is created. clientHandler = ClientHandler() cefpython.SetGlobalClientCallback("OnCertificateError", clientHandler._OnCertificateError) cefpython.SetGlobalClientCallback("OnBeforePluginLoad", clientHandler._OnBeforePluginLoad) cefpython.SetGlobalClientCallback("OnAfterCreated", clientHandler._OnAfterCreated) windowInfo = cefpython.WindowInfo() windowInfo.SetAsChild(self.mainPanel.GetGtkWidget()) # Linux requires adding "file://" for local files, # otherwise /home/some will be replaced as http://home/some self.browser = cefpython.CreateBrowserSync( windowInfo, # If there are problems with Flash you can disable it here, # by disabling all plugins. browserSettings=g_browserSettings, navigateUrl=url) clientHandler.mainBrowser = self.browser self.browser.SetClientHandler(clientHandler) jsBindings = cefpython.JavascriptBindings(bindToFrames=False, bindToPopups=True) jsBindings.SetFunction("PyPrint", PyPrint) jsBindings.SetProperty("pyProperty", "This was set in Python") jsBindings.SetProperty("pyConfig", [ "This was set in Python", { "name": "Nested dictionary", "isNested": True }, [1, "2", None] ]) jsBindings.SetObject("external", JavascriptExternal(self.browser)) jsBindings.SetProperty("sources", GetSources()) self.browser.SetJavascriptBindings(jsBindings) self.Bind(wx.EVT_CLOSE, self.OnClose) if USE_EVT_IDLE: # Bind EVT_IDLE only for the main application frame. self.Bind(wx.EVT_IDLE, self.OnIdle)
def start_cef(self, start_url='http://google.com'): '''Starts CEF. ''' # create texture & add it to canvas self.texture = Texture.create(size=self.finSize, colorfmt='rgba', bufferfmt='ubyte') self.texture.flip_vertical() with self.canvas: Color(1, 1, 1) self.rect = Rectangle(size=self.finSize, texture=self.texture, pos=self.finPos) #configure cef 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 and naviagte to empty page <= OnPaint won't get called yet browserSettings = {} # 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 # false. That's why it is initially navigating to "about:blank". # 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. # -- # TODO: get rid of the hard-coded path. Use the __file__ variable # to get the current directory. Using a path to a non-existent # local html file seems to not cause any problems, so it can # stay for a moment. self.browser = cefpython.CreateBrowserSync(windowInfo, browserSettings, navigateUrl="file:///home/czarek/cefpython/cefpython/cef3/linux/binaries_64bit/empty2.html") #set focus self.browser.SendFocusEvent(True) #Create RenderHandler (in ClientHandler) CH = ClientHandler(self.texture, self) self.browser.SetClientHandler(CH) #Call WasResized() => force cef to call GetViewRect() and OnPaint afterwards self.browser.WasResized() # Load desired start URL # TODO: let the local html file "empty.html" to finish loading, # call the LoadUrl() method with a 100ms delay. In the # empty.html you could add a "Loading.." text, this would # event useful, user would see some message instead of the # blank page. Sometimes the lag can cause the website to # load for a few seconds and user would be seeing only a # white screen and wonder of what is happening. The other # solution would be to change the mouse cursor to loading # state - but this won't work in touch screen devices? As # there is no cursor there. In wxpython there is the # wx.CallLater() method, is there anything similar in Kivy? self.browser.GetMainFrame().LoadUrl(start_url) #Clock.schedule_interval(self.press_key, 5) self._keyboard = Window.request_keyboard(self._keyboard_closed, self) self._keyboard.bind(on_key_down=self.press_key)