From b9529a5f595784f9a7abdaefebf96475509a0791 Mon Sep 17 00:00:00 2001 From: Boulay Mathias Date: Sat, 16 Jul 2022 16:59:39 +0200 Subject: [PATCH] Remove dedicated thread from Touchpad --- .../java/net/kdt/pojavlaunch/Touchpad.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Touchpad.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Touchpad.java index dc9d70448..0fb7dfc21 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Touchpad.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Touchpad.java @@ -40,6 +40,20 @@ public class Touchpad extends FrameLayout { private float mPrevX, mPrevY; /* Last first pointer positions non-scaled, used to scroll distance */ private float mScrollLastInitialX, mScrollLastInitialY; + /* Handler and message to check if we are grabbing */ + private final Runnable mGrabRunnable = new Runnable() { + @Override + public void run() { + if (!CallbackBridge.isGrabbing() && mDisplayState && getVisibility() != VISIBLE) { + enable(); + }else{ + if ((CallbackBridge.isGrabbing() && getVisibility() != View.GONE) || !mDisplayState && getVisibility() == VISIBLE) { + disable(); + } + } + postDelayed(this, 250); + } + }; public Touchpad(@NonNull Context context) { this(context, null); @@ -170,20 +184,7 @@ public class Touchpad extends FrameLayout { // When the game is grabbing, we should not display the mouse disable(); mDisplayState = false; - Thread virtualMouseGrabThread = new Thread(() -> { - while (true) { - if (!CallbackBridge.isGrabbing() && mDisplayState && getVisibility() != VISIBLE) { - post(this::enable); - }else{ - if ((CallbackBridge.isGrabbing() && getVisibility() != View.GONE) || !mDisplayState && getVisibility() == VISIBLE) { - post(this::disable); - } - } - - } - }, "VirtualMouseGrabThread"); - virtualMouseGrabThread.setPriority(Thread.MIN_PRIORITY); - virtualMouseGrabThread.start(); + postDelayed(mGrabRunnable, 250); } }