From f40059e4babb4db53bfe58e360c787e80ca51398 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Mon, 13 Mar 2023 23:47:39 -0400 Subject: [PATCH] android: Use edge to edge --- .../features/settings/ui/SettingsActivity.kt | 28 ++++++++++++++--- .../features/settings/ui/SettingsFragment.kt | 17 +++++++++- .../org/yuzu/yuzu_emu/ui/main/MainActivity.kt | 24 ++++++++++++++ .../ui/platform/PlatformGamesFragment.kt | 14 +++++++++ .../org/yuzu/yuzu_emu/utils/InsetsHelper.kt | 10 ++++++ .../app/src/main/res/layout/activity_main.xml | 1 + .../src/main/res/layout/activity_settings.xml | 3 +- .../app/src/main/res/layout/fragment_grid.xml | 31 ++++++++++--------- .../src/main/res/layout/fragment_settings.xml | 3 +- 9 files changed, 110 insertions(+), 21 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt index 1705a72e8..e403aa6d7 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt @@ -8,20 +8,24 @@ import android.content.Intent import android.content.IntentFilter import android.os.Bundle import android.view.Menu +import android.view.View +import android.widget.FrameLayout import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.ViewCompat +import androidx.core.view.WindowCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updatePadding import androidx.localbroadcastmanager.content.LocalBroadcastManager +import com.google.android.material.appbar.AppBarLayout import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.progressindicator.LinearProgressIndicator import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.features.settings.model.Settings import org.yuzu.yuzu_emu.features.settings.ui.SettingsFragment.Companion.newInstance -import org.yuzu.yuzu_emu.utils.DirectoryInitialization -import org.yuzu.yuzu_emu.utils.DirectoryStateReceiver -import org.yuzu.yuzu_emu.utils.EmulationMenuSettings -import org.yuzu.yuzu_emu.utils.ThemeHelper +import org.yuzu.yuzu_emu.utils.* class SettingsActivity : AppCompatActivity(), SettingsActivityView { private val presenter = SettingsActivityPresenter(this) @@ -32,6 +36,9 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { super.onCreate(savedInstanceState) setContentView(R.layout.activity_settings) + + WindowCompat.setDecorFitsSystemWindows(window, false) + val launcher = intent val gameID = launcher.getStringExtra(ARG_GAME_ID) val menuTag = launcher.getStringExtra(ARG_MENU_TAG) @@ -40,6 +47,8 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { // Show "Back" button in the action bar for navigation setSupportActionBar(findViewById(R.id.toolbar_settings)) supportActionBar!!.setDisplayHomeAsUpEnabled(true) + + setInsets() } override fun onSupportNavigateUp(): Boolean { @@ -185,6 +194,17 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { private val fragment: SettingsFragment? get() = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as SettingsFragment? + private fun setInsets() { + val appBar = findViewById(R.id.appbar_settings) + val frame = findViewById(R.id.frame_content) + ViewCompat.setOnApplyWindowInsetsListener(frame) { view: View, windowInsets: WindowInsetsCompat -> + val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) + view.updatePadding(left = insets.left, right = insets.right) + InsetsHelper.insetAppBar(insets, appBar) + windowInsets + } + } + companion object { private const val ARG_MENU_TAG = "menu_tag" private const val ARG_GAME_ID = "game_id" diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt index fe04c9651..bef5b9705 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt @@ -8,6 +8,9 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.recyclerview.widget.LinearLayoutManager @@ -25,6 +28,8 @@ class SettingsFragment : Fragment(), SettingsFragmentView { private var activityView: SettingsActivityView? = null private var adapter: SettingsAdapter? = null + private lateinit var recyclerView: RecyclerView + override fun onAttach(context: Context) { super.onAttach(context) activityView = context as SettingsActivityView @@ -50,7 +55,7 @@ class SettingsFragment : Fragment(), SettingsFragmentView { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val manager = LinearLayoutManager(activity) - val recyclerView = view.findViewById(R.id.list_settings) + recyclerView = view.findViewById(R.id.list_settings) recyclerView.adapter = adapter recyclerView.layoutManager = manager val dividerDecoration = MaterialDividerItemDecoration(requireContext(), LinearLayoutManager.VERTICAL) @@ -58,6 +63,8 @@ class SettingsFragment : Fragment(), SettingsFragmentView { recyclerView.addItemDecoration(dividerDecoration) val activity = activity as SettingsActivityView? presenter.onViewCreated(activity!!.settings) + + setInsets() } override fun onDetach() { @@ -106,6 +113,14 @@ class SettingsFragment : Fragment(), SettingsFragmentView { activityView!!.onSettingChanged() } + private fun setInsets() { + ViewCompat.setOnApplyWindowInsetsListener(recyclerView) { view: View, windowInsets: WindowInsetsCompat -> + val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) + view.updatePadding(bottom = insets.bottom) + windowInsets + } + } + companion object { private const val ARGUMENT_MENU_TAG = "menu_tag" private const val ARGUMENT_GAME_ID = "game_id" diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt index 266403885..ba60a26e8 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt @@ -9,9 +9,17 @@ import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.view.View +import android.widget.FrameLayout import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.core.view.ViewCompat +import androidx.core.view.WindowCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updatePadding +import com.google.android.material.appbar.AppBarLayout +import com.google.android.material.color.MaterialColors import com.google.android.material.dialog.MaterialAlertDialogBuilder import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R @@ -31,6 +39,9 @@ class MainActivity : AppCompatActivity(), MainView { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + WindowCompat.setDecorFitsSystemWindows(window, false) + findViews() setSupportActionBar(toolbar) presenter.onCreate() @@ -50,6 +61,8 @@ class MainActivity : AppCompatActivity(), MainView { // Dismiss previous notifications (should not happen unless a crash occurred) EmulationActivity.tryDismissRunningNotification(this) + + setInsets() } override fun onSaveInstanceState(outState: Bundle) { @@ -230,4 +243,15 @@ class MainActivity : AppCompatActivity(), MainView { EmulationActivity.tryDismissRunningNotification(this) super.onDestroy() } + + private fun setInsets() { + val appBar = findViewById(R.id.appbar_main) + val frame = findViewById(R.id.games_platform_frame) + ViewCompat.setOnApplyWindowInsetsListener(frame) { view: View, windowInsets: WindowInsetsCompat -> + val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) + view.updatePadding(left = insets.left, right = insets.right) + InsetsHelper.insetAppBar(insets, appBar) + windowInsets + } + } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/platform/PlatformGamesFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/platform/PlatformGamesFragment.kt index efd77ea7e..42b61a81a 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/platform/PlatformGamesFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/platform/PlatformGamesFragment.kt @@ -10,6 +10,9 @@ import android.view.View import android.view.ViewGroup import android.view.ViewTreeObserver.OnGlobalLayoutListener import android.widget.TextView +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -17,6 +20,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.YuzuApplication import org.yuzu.yuzu_emu.adapters.GameAdapter +import org.yuzu.yuzu_emu.utils.InsetsHelper class PlatformGamesFragment : Fragment(), PlatformGamesView { private val presenter = PlatformGamesPresenter(this) @@ -65,6 +69,8 @@ class PlatformGamesFragment : Fragment(), PlatformGamesView { refresh() pullToRefresh.isRefreshing = false } + + setInsets() } override fun refresh() { @@ -91,6 +97,14 @@ class PlatformGamesFragment : Fragment(), PlatformGamesView { textView = root.findViewById(R.id.gamelist_empty_text) } + private fun setInsets() { + ViewCompat.setOnApplyWindowInsetsListener(recyclerView) { view: View, windowInsets: WindowInsetsCompat -> + val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) + view.updatePadding(bottom = insets.bottom) + windowInsets + } + } + companion object { const val TAG = "PlatformGamesFragment" } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InsetsHelper.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InsetsHelper.kt index 3f9acc5b0..2eae77a1d 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InsetsHelper.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InsetsHelper.kt @@ -1,12 +1,22 @@ package org.yuzu.yuzu_emu.utils import android.content.Context +import android.view.ViewGroup.MarginLayoutParams +import androidx.core.graphics.Insets +import com.google.android.material.appbar.AppBarLayout object InsetsHelper { const val THREE_BUTTON_NAVIGATION = 0 const val TWO_BUTTON_NAVIGATION = 1 const val GESTURE_NAVIGATION = 2 + fun insetAppBar(insets: Insets, appBarLayout: AppBarLayout) { + val mlpAppBar = appBarLayout.layoutParams as MarginLayoutParams + mlpAppBar.leftMargin = insets.left + mlpAppBar.rightMargin = insets.right + appBarLayout.layoutParams = mlpAppBar + } + fun getSystemGestureType(context: Context): Int { val resources = context.resources val resourceId = diff --git a/src/android/app/src/main/res/layout/activity_main.xml b/src/android/app/src/main/res/layout/activity_main.xml index 34abf955d..059aaa9b4 100644 --- a/src/android/app/src/main/res/layout/activity_main.xml +++ b/src/android/app/src/main/res/layout/activity_main.xml @@ -9,6 +9,7 @@ android:id="@+id/appbar_main" android:layout_width="match_parent" android:layout_height="wrap_content" + android:fitsSystemWindows="true" app:liftOnScrollTargetViewId="@id/grid_games"> + android:layout_height="wrap_content" + android:fitsSystemWindows="true"> - + + android:layout_width="match_parent" + android:layout_height="match_parent"> + android:visibility="gone" /> + + - + diff --git a/src/android/app/src/main/res/layout/fragment_settings.xml b/src/android/app/src/main/res/layout/fragment_settings.xml index 13780927e..6feb092ff 100644 --- a/src/android/app/src/main/res/layout/fragment_settings.xml +++ b/src/android/app/src/main/res/layout/fragment_settings.xml @@ -8,6 +8,7 @@ android:id="@+id/list_settings" android:layout_width="match_parent" android:layout_height="match_parent" - android:background="?attr/colorSurface" /> + android:background="?attr/colorSurface" + android:clipToPadding="false" />