From 713ebac825230dc7a9b42f557f73aca1de7d03f0 Mon Sep 17 00:00:00 2001 From: Robert Niederreiter Date: Fri, 1 Feb 2019 14:52:34 +0100 Subject: [PATCH 1/2] Sort imports. Some PEP8. Check for __kivy__activeKeyboardElement before accessing in CEFBrowser._on_focus. Check for keyboard_widget in keyboard_position_simple and keyboard_position_optimal before accessing. Adopt OnConsoleMessage signature. Adopt OnBeforeBrowse signature --- cefbrowser/__init__.py | 5 ++- cefbrowser/cefbrowser.py | 93 ++++++++++++++++++++------------------- cefbrowser/cefkeyboard.py | 35 +++++++-------- cefbrowser/cefpython.py | 9 ++-- cefbrowser/version.py | 2 +- 5 files changed, 72 insertions(+), 72 deletions(-) diff --git a/cefbrowser/__init__.py b/cefbrowser/__init__.py index aa9bd13..d9bb0b9 100755 --- a/cefbrowser/__init__.py +++ b/cefbrowser/__init__.py @@ -1,9 +1,10 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- -import os -from .version import __version__ # noqa: F401 from .cefbrowser import CEFBrowser # noqa: F401 +from .version import __version__ # noqa: F401 +import os + cef_test_url = "file://" + os.path.join( os.path.dirname(os.path.realpath(__file__)), diff --git a/cefbrowser/cefbrowser.py b/cefbrowser/cefbrowser.py index 64dbe36..b360a49 100755 --- a/cefbrowser/cefbrowser.py +++ b/cefbrowser/cefbrowser.py @@ -5,31 +5,28 @@ browser. If you need controls or tabs, check out the `examples` """ -import ctypes +from .cefkeyboard import CEFKeyboardManager +from .cefpython import cefpython, cefpython_initialize from functools import partial -import json -import os -import random -import time - from kivy.core.clipboard import Clipboard from kivy.core.window import Window +from kivy.factory import Factory from kivy.graphics import Color, Rectangle from kivy.graphics.texture import Texture -from kivy.factory import Factory from kivy.lang import Builder from kivy.logger import Logger -from kivy.properties import StringProperty -from kivy.properties import NumericProperty from kivy.properties import BooleanProperty +from kivy.properties import NumericProperty from kivy.properties import ReferenceListProperty -from kivy import resources +from kivy.properties import StringProperty from kivy.uix.behaviors import FocusBehavior from kivy.uix.bubble import Bubble, BubbleButton from kivy.uix.widget import Widget - -from .cefpython import cefpython, cefpython_initialize -from .cefkeyboard import CEFKeyboardManager +import ctypes +import json +import os +import random +import time class CEFAlreadyInitialized(Exception): @@ -296,21 +293,23 @@ def _realign(self, *largs): self._browser.NotifyScreenInfoChanged() try: self._keyboard_update(**self.__keyboard_state) - except: + except Exception: pass def _on_parent(self, obj, parent): self._browser.WasHidden(not parent) # optimize the shit out of CEF try: self._keyboard_update(**self.__keyboard_state) - except: + except Exception: pass def _on_focus(self, obj, focus): super(CEFBrowser, self)._on_focus(obj, focus) if not focus and self.__keyboard_state["shown"]: self._browser.GetMainFrame().ExecuteJavascript( - "__kivy__activeKeyboardElement.blur();") + "if (__kivy__activeKeyboardElement) " + "{ __kivy__activeKeyboardElement.blur(); }" + ) def _update_rect(self): if self.__rect: @@ -436,6 +435,8 @@ def keyboard_position_optimal( rect, attributes, ): # TODO: place right, left, etc. see cefkivy + if not keyboard_widget: + return if not keyboard_widget.docked: cls.keyboard_position_simple( browser, keyboard_widget, rect, attributes) @@ -518,10 +519,10 @@ def on_touch_move(self, touch, *kwargs): return x = touch.x - self.pos[0] - y = self.height-touch.y + self.pos[1] + y = self.height - touch.y + self.pos[1] x_start = touch.opos[0] - self.pos[0] - y_start = self.height-touch.opos[1] + self.pos[1] + y_start = self.height - touch.opos[1] + self.pos[1] if len(self._touches) == 1: if not touch.is_scrolling or touch.is_right_click: @@ -579,7 +580,7 @@ def on_touch_move(self, touch, *kwargs): # Set touch state to scrolling _touch.is_scrolling = True self.cef_mouse_wheel( - touch.x, self.height-touch.pos[1], dx, -dy, + touch.x, self.height - touch.pos[1], dx, -dy, ) return True @@ -587,7 +588,7 @@ def on_touch_up(self, touch, *kwargs): if touch.grab_current is not self: return - y = self.height-touch.pos[1] + self.pos[1] + y = self.height - touch.pos[1] + self.pos[1] x = touch.x - self.pos[0] if self.is_html5_drag: @@ -600,11 +601,11 @@ def on_touch_up(self, touch, *kwargs): x = touch.x if x == 0: x = -1 - if x == self.width-1: + if x == self.width - 1: x = self.width if y == 0: y = -1 - if y == self.height-1: + if y == self.height - 1: y = self.height self.cef_drag_source_ended_at(x, y, self.current_drag_operation) @@ -633,7 +634,8 @@ def on_touch_up(self, touch, *kwargs): if touch.is_dragging: # Drag end (mouse up) self.cef_mouse_click( - touch.ppos[0], self.height-touch.ppos[1] + self.pos[1], + touch.ppos[0], + self.height - touch.ppos[1] + self.pos[1], cefpython.MOUSEBUTTON_LEFT, mouse_up=True, click_count=1, ) @@ -703,7 +705,7 @@ def cef_drag_source_system_drag_ended(self): def is_inside_window(self, x, y): # When mouse is out of app window Kivy still generates move events # at the borders with x=0, x=width-1, y=0, y=height-1. - if (0 < x < Window.width-1) and (0 < y < Window.height-1): + if (0 < x < Window.width - 1) and (0 < y < Window.height - 1): return True return False @@ -790,7 +792,7 @@ def __init__(self, browser_widget, key, *largs): self.key = key def __call__(self, *largs): - js_code = str(self.key)+"(" + js_code = str(self.key) + "(" first = True for arg in largs: if not first: @@ -914,7 +916,7 @@ def on_paste(self, *largs): try: t = Clipboard.get(type) break - except: + except Exception: pass print("PASTE", t) @@ -946,7 +948,7 @@ def OnTooltip(self, text_out): # noqa: N802 def OnStatusMessage(self, browser, value): # noqa: N802 Logger.info("CEFBrowser: Status: %s", value) - def OnConsoleMessage(self, browser, message, source, line): # noqa: N802 + def OnConsoleMessage(self, browser, level, message, source, line): # noqa: N802 Logger.info("CEFBrowser: Console: %s - %s(%i)", message, source, line) return True # We handled it @@ -1090,7 +1092,7 @@ def OnBeforePopup( # noqa: N802 ) allow_popup = False if allow_popup: - r = random.randint(1, 2**31-1) + r = random.randint(1, 2 ** 31 - 1) wi = cefpython.WindowInfo() wi.SetAsChild(0, [0, 0, 0, 0]) wi.SetAsOffscreen(r) @@ -1156,7 +1158,7 @@ def DoClose(self, browser): # noqa: N802 "CEFBrowser: Close handler failed with error: %s", err) try: bw.parent.remove_widget(bw) - except: + except Exception: pass del self.browser_widgets[browser] return False @@ -1452,7 +1454,7 @@ def OnPaint( # noqa: N802 pmvfm = ctypes.pythonapi.PyMemoryView_FromMemory pmvfm.restype = ctypes.py_object pmvfm.argtypes = (ctypes.c_void_p, ctypes.c_int64, ctypes.c_int) - view = pmvfm(paint_buffer.GetIntPointer(), width*height*4, 0x200) + view = pmvfm(paint_buffer.GetIntPointer(), width * height * 4, 0x200) except AttributeError: """ # The following code gives a segmentation fault: @@ -1470,14 +1472,14 @@ def OnPaint( # noqa: N802 if element_type != cefpython.PET_VIEW: if ( bw._popup._texture.width * bw._popup._texture.height * 4 != - width*height*4 + width * height * 4 ): return True # prevent segfault bw._popup._texture.blit_buffer( view, colorfmt="bgra", bufferfmt="ubyte") bw._popup._update_rect() return True - if bw._texture.width * bw._texture.height * 4 != width*height * 4: + if bw._texture.width * bw._texture.height * 4 != width * height * 4: return True # prevent segfault bw._texture.blit_buffer(view, colorfmt="bgra", bufferfmt="ubyte") bw._update_rect() @@ -1526,6 +1528,7 @@ def OnBeforeBrowse( # noqa: N802 browser, frame, request, + user_gesture, is_redirect, ): frame.ExecuteJavascript("try {__kivy__on_escape();} catch (err) {}") @@ -1559,9 +1562,9 @@ def GetAuthCredentials( # noqa: N802 callback, ): Logger.debug("GetAuthCredentials: " - "is_proxy: %s, host: %s, port: %s, " - "realm: %s, scheme: %s" - % (is_proxy, host, port, realm, scheme)) + "is_proxy: %s, host: %s, port: %s, " + "realm: %s, scheme: %s" + % (is_proxy, host, port, realm, scheme)) ad = CEFBrowser._auth_dialog ad.host = host ad.callback = callback @@ -1693,7 +1696,7 @@ def OnContextReleased(self, browser, frame): # noqa: N802 from kivy.clock import Clock from kivy.uix.button import Button from kivy.uix.textinput import TextInput - cef_test_url = "file://"+os.path.join( + cef_test_url = "file://" + os.path.join( os.path.dirname(os.path.realpath(__file__)), "test.html", ) @@ -1706,16 +1709,16 @@ def timeout(self, *largs): def build(self): class FocusButton(FocusBehavior, Button): pass - wid = Window.width/2 + wid = Window.width / 2 hei = Window.height ti1 = TextInput( - text="ti1", pos=(0, hei-50), size=(wid-1, 50)) + text="ti1", pos=(0, hei - 50), size=(wid - 1, 50)) ti2 = TextInput( - text="ti2", pos=(wid+1, hei-50), size=(wid-1, 50)) + text="ti2", pos=(wid + 1, hei - 50), size=(wid - 1, 50)) fb1 = FocusButton( - text="ti1", pos=(0, hei-100), size=(wid-1, 50)) + text="ti1", pos=(0, hei - 100), size=(wid - 1, 50)) fb2 = FocusButton( - text="ti2", pos=(wid+1, hei-100), size=(wid-1, 50)) + text="ti2", pos=(wid + 1, hei - 100), size=(wid - 1, 50)) def url_handler(self, url): print("URL HANDLER", url) @@ -1737,15 +1740,15 @@ def popup_handler(self, popup_browser): pw = client_handler.browser_widgets[key].parent if pw: break - popup_browser.pos = (Window.width/4, Window.height/4) - popup_browser.size = (Window.width/2, Window.height/2) + popup_browser.pos = (Window.width / 4, Window.height / 4) + popup_browser.size = (Window.width / 2, Window.height / 2) popup_browser.popup_handler = popup_handler popup_browser.close_handler = close_handler pw.add_widget(popup_browser) self.cb1 = CEFBrowser( url="http://jegger.ch/datapool/app/test_popup.html", - pos=(0, 0), size=(wid-1, hei-100), + pos=(0, 0), size=(wid - 1, hei - 100), ) self.cb1.popup_policy = popup_policy_handler self.cb1.popup_handler = popup_handler @@ -1754,7 +1757,7 @@ def popup_handler(self, popup_browser): self.cb1.bind(title=title_handler) self.cb2 = CEFBrowser( url="https://rentouch.ch/", - pos=(wid+1, 0), size=(wid-1, hei-100), + pos=(wid + 1, 0), size=(wid - 1, hei - 100), ) self.cb2.popup_policy = popup_policy_handler self.cb2.popup_handler = popup_handler diff --git a/cefbrowser/cefkeyboard.py b/cefbrowser/cefkeyboard.py index b84393e..94532d8 100755 --- a/cefbrowser/cefkeyboard.py +++ b/cefbrowser/cefkeyboard.py @@ -6,9 +6,8 @@ better readability. ''' -from kivy.core.window import Window - from .cefpython import cefpython +from kivy.core.window import Window class CEFKeyboardManagerSingleton: @@ -97,14 +96,14 @@ def process_key_down(self, browser, keyboard, key, text, modifiers): charcode = ord(text) # Do not send RAW-key for key-codes 35-40 aka ($#%& - if key[0] not in range(35, 40+1): + if key[0] not in range(35, 40 + 1): # Send key event to cef: RAWKEYDOWN key_event = { - "type": cefpython.KEYEVENT_RAWKEYDOWN, - "windows_key_code": keycode, - "character": charcode, - "unmodified_character": charcode, - "modifiers": cef_modifiers, + "type": cefpython.KEYEVENT_RAWKEYDOWN, + "windows_key_code": keycode, + "character": charcode, + "unmodified_character": charcode, + "modifiers": cef_modifiers } # print("- DOWN RAW SendKeyEvent: %s" % key_event) browser.SendKeyEvent(key_event) @@ -112,11 +111,11 @@ def process_key_down(self, browser, keyboard, key, text, modifiers): # Send key event to cef: CHAR if text: key_event = { - "type": cefpython.KEYEVENT_CHAR, - "windows_key_code": keycode, - "character": charcode, - "unmodified_character": charcode, - "modifiers": cef_modifiers, + "type": cefpython.KEYEVENT_CHAR, + "windows_key_code": keycode, + "character": charcode, + "unmodified_character": charcode, + "modifiers": cef_modifiers } # print("- DOWN text SendKeyEvent: %s" % key_event) browser.SendKeyEvent(key_event) @@ -157,11 +156,11 @@ def kivy_on_key_up(self, browser, keyboard, key): # Send key event to cef: KEYUP key_event = { - "type": cefpython.KEYEVENT_KEYUP, - "windows_key_code": keycode, - "character": charcode, - "unmodified_character": charcode, - "modifiers": cef_modifiers, + "type": cefpython.KEYEVENT_KEYUP, + "windows_key_code": keycode, + "character": charcode, + "unmodified_character": charcode, + "modifiers": cef_modifiers } # print("- UP SendKeyEvent: %s" % key_event) browser.SendKeyEvent(key_event) diff --git a/cefbrowser/cefpython.py b/cefbrowser/cefpython.py index 320ad23..e7214b9 100755 --- a/cefbrowser/cefpython.py +++ b/cefbrowser/cefpython.py @@ -6,18 +6,15 @@ PYTHONPATH. """ +from kivy.app import App +from kivy.clock import Clock +from kivy.logger import Logger import atexit import os import signal import sys import tempfile -import kivy -from kivy.app import App -from kivy.clock import Clock -from kivy.logger import Logger -kivy.require("1.8.0") - # Try import from package (PYTHONPATH) try: diff --git a/cefbrowser/version.py b/cefbrowser/version.py index 7530633..e226649 100755 --- a/cefbrowser/version.py +++ b/cefbrowser/version.py @@ -2,4 +2,4 @@ # 1) we don't load dependencies by storing it in __init__.py # 2) we can import it in setup.py for the same reason # 3) we can import it into your module module -__version__ = '0.5.60' +__version__ = '0.5.61' From 487887e6fc15ba7962f1ddf698c3ed29b7b886be Mon Sep 17 00:00:00 2001 From: Robert Niederreiter Date: Fri, 1 Feb 2019 15:31:11 +0100 Subject: [PATCH 2/2] OnTooltip signature --- cefbrowser/cefbrowser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cefbrowser/cefbrowser.py b/cefbrowser/cefbrowser.py index b360a49..e8d1b35 100755 --- a/cefbrowser/cefbrowser.py +++ b/cefbrowser/cefbrowser.py @@ -416,6 +416,8 @@ def keyboard_position_simple( rect, attributes, ): + if not keyboard_widget: + return if not keyboard_widget.docked: if rect and len(rect) == 4: keyboard_widget.pos = ( @@ -941,7 +943,7 @@ def OnAddressChange(self, browser, frame, url): # noqa: N802 def OnTitleChange(self, browser, title): # noqa: N802 self.browser_widgets[browser].title = title - def OnTooltip(self, text_out): # noqa: N802 + def OnTooltip(self, browser, text_out): # noqa: N802 text_out.append("") return True