Add support for drawing inside the display cutout areas

This commit is contained in:
lynxnb 2022-03-28 10:58:10 +02:00 committed by PixelyIon
parent 6e929e6f6a
commit 08e24915d8
7 changed files with 59 additions and 15 deletions

View File

@ -17,6 +17,7 @@ import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.updateMargins
import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.databinding.EmuActivityBinding
import emu.skyline.input.*
@ -232,15 +233,25 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
requestedOrientation = settings.orientation
window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
setContentView(binding.root)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Android might not allow child views to overlap the system bars
// Override this behavior and force content to extend into the cutout area
window.setDecorFitsSystemWindows(false)
window.insetsController?.let {
it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
it.hide(WindowInsets.Type.systemBars())
}
}
if (settings.respectDisplayCutout) {
binding.perfStats.setOnApplyWindowInsetsListener(insetsOrMarginHandler)
binding.onScreenControllerToggle.setOnApplyWindowInsetsListener(insetsOrMarginHandler)
}
binding.gameView.holder.addCallback(this)
binding.gameView.setAspectRatio(
@ -540,4 +551,17 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
val (major, minor, patch) = BuildConfig.VERSION_NAME.split('.').map { it.toUInt() }
return ((major shl 22) or (minor shl 12) or (patch)).toInt()
}
val insetsOrMarginHandler = View.OnApplyWindowInsetsListener { view, insets ->
insets.displayCutout?.let {
val defaultHorizontalMargin = view.resources.getDimensionPixelSize(R.dimen.onScreenItemHorizontalMargin)
val left = if (it.safeInsetLeft == 0) defaultHorizontalMargin else it.safeInsetLeft
val right = if (it.safeInsetRight == 0) defaultHorizontalMargin else it.safeInsetRight
val params = view.layoutParams as ViewGroup.MarginLayoutParams
params.updateMargins(left = left, right = right)
view.layoutParams = params
}
insets
}
}

View File

@ -7,10 +7,7 @@ package emu.skyline.input.onscreen
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -90,7 +87,20 @@ class OnScreenEditActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
setContentView(binding.root)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Android might not allow child views to overlap the system bars
// Override this behavior and force content to extend into the cutout area
window.setDecorFitsSystemWindows(false)
window.insetsController?.let {
it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
it.hide(WindowInsets.Type.systemBars())
}
}
binding.onScreenControllerView.recenterSticks = settings.onScreenControlRecenterSticks
actions.forEach { pair ->
@ -100,11 +110,6 @@ class OnScreenEditActivity : AppCompatActivity() {
fabMapping[pair.first] = this
})
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.navigationBars() or WindowInsets.Type.systemBars() or WindowInsets.Type.systemGestures() or WindowInsets.Type.statusBars())
window.insetsController?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
override fun onResume() {

View File

@ -44,4 +44,6 @@ class Settings @Inject constructor(@ApplicationContext private val context : Con
var systemLanguage by sharedPreferences(context, 1)
var orientation by sharedPreferences(context, ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
var respectDisplayCutout by sharedPreferences(context, false)
}

View File

@ -4,9 +4,10 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:background="@android:color/black"
tools:context=".EmulationActivity">
android:keepScreenOn="true"
tools:context=".EmulationActivity"
tools:ignore="RtlHardcoded">
<emu.skyline.views.FixedRatioSurfaceView
android:id="@+id/game_view"
@ -23,16 +24,18 @@
android:id="@+id/perf_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="@dimen/onScreenItemHorizontalMargin"
android:layout_marginTop="5dp"
android:textColor="#9fffff00" />
<ImageButton
android:id="@+id/on_screen_controller_toggle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:background="?attr/selectableItemBackgroundBorderless"
android:layout_marginRight="@dimen/onScreenItemHorizontalMargin"
android:background="?android:attr/actionBarItemBackground"
android:padding="8dp"
android:src="@drawable/ic_show"
app:tint="#40FFFFFF"
tools:ignore="ContentDescription" />

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="grid_padding">8dp</dimen>
<dimen name="onScreenItemHorizontalMargin">10dp</dimen>
</resources>

View File

@ -63,6 +63,9 @@
<string name="max_refresh_rate_enabled">Sets the display refresh rate as high as possible (Will break most games)</string>
<string name="max_refresh_rate_disabled">Sets the display refresh rate to 60Hz</string>
<string name="aspect_ratio">Aspect Ratio</string>
<string name="respect_display_cutout">Respect Display Cutout</string>
<string name="respect_display_cutout_enabled">Do not draw UI elements in the cutout area</string>
<string name="respect_display_cutout_disabled">Allow UI elements to be drawn in the cutout area</string>
<!-- Input -->
<string name="input">Input</string>
<string name="osc">On-Screen Controls</string>

View File

@ -116,6 +116,12 @@
app:key="aspect_ratio"
app:title="@string/aspect_ratio"
app:useSimpleSummaryProvider="true" />
<CheckBoxPreference
android:defaultValue="false"
android:summaryOff="@string/respect_display_cutout_disabled"
android:summaryOn="@string/respect_display_cutout_enabled"
app:key="respect_display_cutout"
app:title="@string/respect_display_cutout" />
</PreferenceCategory>
<PreferenceCategory
android:key="category_input"