diff options
| author | JosJuice <josjuice@gmail.com> | 2020-07-28 18:50:51 +0200 |
|---|---|---|
| committer | JosJuice <josjuice@gmail.com> | 2020-09-08 16:35:26 +0200 |
| commit | 48de1333df628caa1774972ca45750d0ae87386f (patch) | |
| tree | d5ae2023d9ede881fcbc73c6835dda50250348ae /Source/Android/app/src/main/java | |
| parent | 48c34bba8a3b4243527c9e4d6c18d7865787acb9 (diff) | |
Android: Close the menu when tapping outside of it
Diffstat (limited to 'Source/Android/app/src/main/java')
| -rw-r--r-- | Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java | 69 |
1 files changed, 62 insertions, 7 deletions
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java index 0af915ddea..888ff331a0 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java @@ -5,6 +5,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; +import android.graphics.Rect; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.TextUtils; @@ -22,6 +23,7 @@ import android.widget.Toast; import androidx.annotation.IntDef; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; @@ -395,9 +397,7 @@ public final class EmulationActivity extends AppCompatActivity @Override public void onBackPressed() { - boolean popResult = getSupportFragmentManager().popBackStackImmediate( - BACKSTACK_NAME_SUBMENU, FragmentManager.POP_BACK_STACK_INCLUSIVE); - if (!popResult) + if (!closeSubmenu()) { toggleMenu(); } @@ -438,13 +438,22 @@ public final class EmulationActivity extends AppCompatActivity ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)); } - private void toggleMenu() + private boolean closeSubmenu() + { + return getSupportFragmentManager().popBackStackImmediate(BACKSTACK_NAME_SUBMENU, + FragmentManager.POP_BACK_STACK_INCLUSIVE); + } + + private boolean closeMenu() { - boolean result = getSupportFragmentManager().popBackStackImmediate( - BACKSTACK_NAME_MENU, FragmentManager.POP_BACK_STACK_INCLUSIVE); mMenuVisible = false; + return getSupportFragmentManager().popBackStackImmediate(BACKSTACK_NAME_MENU, + FragmentManager.POP_BACK_STACK_INCLUSIVE); + } - if (!result) + private void toggleMenu() + { + if (!closeMenu()) { // Removing the menu failed, so that means it wasn't visible. Add it. Fragment fragment = MenuFragment.newInstance(mSelectedTitle); @@ -1129,6 +1138,52 @@ public final class EmulationActivity extends AppCompatActivity .show(); } + private static boolean areCoordinatesOutside(@Nullable View view, float x, float y) + { + if (view == null) + { + return true; + } + + Rect viewBounds = new Rect(); + view.getGlobalVisibleRect(viewBounds); + return !viewBounds.contains(Math.round(x), Math.round(y)); + } + + @Override + public boolean dispatchTouchEvent(MotionEvent event) + { + if (event.getActionMasked() == MotionEvent.ACTION_DOWN) + { + boolean anyMenuClosed = false; + + Fragment submenu = getSupportFragmentManager().findFragmentById(R.id.frame_submenu); + if (submenu != null && areCoordinatesOutside(submenu.getView(), event.getX(), event.getY())) + { + closeSubmenu(); + submenu = null; + anyMenuClosed = true; + } + + if (submenu == null) + { + Fragment menu = getSupportFragmentManager().findFragmentById(R.id.frame_menu); + if (menu != null && areCoordinatesOutside(menu.getView(), event.getX(), event.getY())) + { + closeMenu(); + anyMenuClosed = true; + } + } + + if (anyMenuClosed) + { + return true; + } + } + + return super.dispatchTouchEvent(event); + } + @Override public boolean dispatchGenericMotionEvent(MotionEvent event) { |
