Circumvention for SDL2 focus bug

This commit is contained in:
Mark Qvist 2022-07-06 19:18:26 +02:00
parent efefdbebe8
commit d338e6196c

38
main.py
View File

@ -26,6 +26,10 @@ from kivymd.uix.dialog import MDDialog
__version__ = "0.1.5"
__variant__ = "alpha"
if RNS.vendor.platformutils.get_platform() == "android":
from jnius import autoclass
from android.runnable import run_on_ui_thread
class SidebandApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@ -85,16 +89,48 @@ class SidebandApp(MDApp):
if self.announces_view != None:
self.announces_view.update()
def on_start(self):
def on_start(self):
self.last_exit_event = time.time()
EventLoop.window.bind(on_keyboard=self.keyboard_event)
EventLoop.window.bind(on_key_down=self.keydown_event)
# This incredibly hacky hack circumvents a bug in SDL2
# that prevents focus from being correctly released from
# the software keyboard on Android. Without this the back
# button/gesture does not work after the soft-keyboard has
# appeared for the first time.
if RNS.vendor.platformutils.get_platform() == "android":
BIND_CLASSES = ["kivymd.uix.textfield.textfield.MDTextField",]
for e in self.root.ids:
te = self.root.ids[e]
ts = str(te).split(" ")[0].replace("<", "")
if ts in BIND_CLASSES:
te.bind(focus=self.android_focus_fix)
# RNS.log("Bound "+str(e)+" / "+ts)
# else:
# RNS.log("Did not bind "+str(e)+" / "+ts)
# RNS.log(str(e))
self.root.ids.screen_manager.app = self
self.root.ids.app_version_info.text = "Sideband v"+__version__+" "+__variant__
self.open_conversations()
Clock.schedule_interval(self.jobs, 1)
# Part of the focus hack fix
def android_focus_fix(self, sender, val):
if not val:
@run_on_ui_thread
def fix_back_button():
activity = autoclass('org.kivy.android.PythonActivity').mActivity
activity.onWindowFocusChanged(False)
activity.onWindowFocusChanged(True)
fix_back_button()
def keydown_event(self, instance, keyboard, keycode, text, modifiers):
if len(modifiers) > 0 and modifiers[0] == 'ctrl' and (text == "w" or text == "q"):