Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Make RootWidget focus more reliable. Fixes #2055.
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Oct 25, 2019
1 parent 33fbbff commit 2bdeb14
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.MotionEvent;

public class RootWidget extends UIWidget {
private Runnable mOnClickCallback;
private boolean mTouched = true;

public RootWidget(Context aContext) {
super(aContext);
Expand All @@ -31,14 +32,29 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {

private void initialize(Context aContext) {
setFocusable(true);
}

setOnClickListener(v -> {
requestFocus();
requestFocusFromTouch();
if (mOnClickCallback != null) {
mOnClickCallback.run();
}
});
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouched = true;
break;
case MotionEvent.ACTION_UP:
if (mTouched) {
mTouched = false;
requestFocus();
requestFocusFromTouch();
if (mOnClickCallback != null) {
mOnClickCallback.run();
}
}
break;
case MotionEvent.ACTION_CANCEL:
mTouched = false;
break;
}
return super.onTouchEvent(event);
}

public void setClickCallback(Runnable aRunnable) {
Expand Down

0 comments on commit 2bdeb14

Please sign in to comment.