Skip to content

Commit

Permalink
Cutouts support
Browse files Browse the repository at this point in the history
Fix empty tabs bar when always on vertical tabs bar and bottom sheets are enabled.
Close #603.
  • Loading branch information
Slion committed Feb 2, 2024
1 parent 351a811 commit 6bdeb9b
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 10 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
android:name="fulguris.activity.IncognitoActivity"
android:alwaysRetainTaskState="true"
android:colorMode="wideColorGamut"
android:resizeableActivity="true"
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|keyboardHidden|keyboard"
android:label="@string/app_name"
android:launchMode="singleInstance"
Expand All @@ -214,6 +215,7 @@
<activity
android:name="fulguris.activity.SettingsActivity"
android:colorMode="wideColorGamut"
android:resizeableActivity="true"
android:label="@string/settings"
android:parentActivityName="fulguris.activity.MainActivity"
android:theme="@style/Theme.App.DayNight"
Expand All @@ -227,6 +229,7 @@
<activity
android:name="fulguris.activity.ReadingActivity"
android:colorMode="wideColorGamut"
android:resizeableActivity="true"
android:label="@string/reading_mode"
android:theme="@style/Theme.App.DayNight"
android:exported="false">
Expand Down
67 changes: 62 additions & 5 deletions app/src/main/java/fulguris/activity/WebBrowserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import androidx.core.net.toUri
import androidx.core.view.GestureDetectorCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.customview.widget.ViewDragHelper
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
Expand Down Expand Up @@ -295,6 +299,8 @@ abstract class WebBrowserActivity : ThemedBrowserActivity(),
Timber.v("onCreate")
// Need to go first to inject our components
super.onCreate(savedInstanceState)
// We want to control our decor
WindowCompat.setDecorFitsSystemWindows(window,false)

// Register lifecycle observers
lifecycle.addObserver(tabsManager)
Expand All @@ -311,6 +317,50 @@ abstract class WebBrowserActivity : ThemedBrowserActivity(),
}

iBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

// Setup a callback when we need to apply window insets
ViewCompat.setOnApplyWindowInsetsListener(iBinding.root) { view, windowInsets ->
Timber.d("OnApplyWindowInsetsListener")
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
Timber.d("System insets: $insets")
//val imeVisible = windowInsets.isVisible(WindowInsetsCompat.Type.ime())
val imeHeight = windowInsets.getInsets(WindowInsetsCompat.Type.ime()).bottom

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
// Don't apply vertical margins here as it would break our drawers status bar color
// Apply horizontal margin to our root view so that we fill the cutout in on Honor Magic V2
leftMargin = insets.left
rightMargin = insets.right
// Make sure our UI does not get stuck below the IME virtual keyboard
// TODO: Do animation synchronization, see:
bottomMargin = imeHeight
}

iBinding.uiLayout.updateLayoutParams<ViewGroup.MarginLayoutParams> {
// Apply vertical margins for status and navigation bar to our UI layout
// Thus the drawers are still showing below the status bar
topMargin = insets.top
bottomMargin = insets.bottom
}

iBinding.leftDrawerContent.updateLayoutParams<ViewGroup.MarginLayoutParams> {
// Apply vertical margins for status and navigation bar to our drawer content
// Thus drawer content does not overlap with system UI
topMargin = insets.top
bottomMargin = insets.bottom
}

iBinding.rightDrawerContent.updateLayoutParams<ViewGroup.MarginLayoutParams> {
// Apply vertical margins for status and navigation bar to our drawer content
// Thus drawer content does not overlap with system UI
topMargin = insets.top
bottomMargin = insets.bottom
}

//windowInsets
WindowInsetsCompat.CONSUMED
}

iTabViewContainerBack = iBinding.tabViewContainerOne
iTabViewContainerFront = iBinding.tabViewContainerTwo

Expand Down Expand Up @@ -1231,7 +1281,8 @@ abstract class WebBrowserActivity : ThemedBrowserActivity(),
*/
private fun addTabsViewToParent() {
val v = (tabsView as View)
if (verticalTabBar && userPreferences.useBottomSheets) {
// Use bottom sheet if desired unless tab bar is always on screen, vertically or horizontally
if (verticalTabBar && tabBarInDrawer && userPreferences.useBottomSheets) {
// Check if our tabs list already belongs to our bottom sheet
if (tabsDialog.findViewById<ViewGroup>(R.id.tabs_list) != v.findViewById<ViewGroup>(R.id.tabs_list)) {
// It was not found, just put it there then
Expand All @@ -1249,9 +1300,9 @@ abstract class WebBrowserActivity : ThemedBrowserActivity(),
}

private fun getBookmarksContainer(): ViewGroup = if (swapBookmarksAndTabs) {
iBinding.leftDrawer
iBinding.leftDrawerContent
} else {
iBinding.rightDrawer
iBinding.rightDrawerContent
}

/**
Expand All @@ -1261,13 +1312,13 @@ abstract class WebBrowserActivity : ThemedBrowserActivity(),
if (verticalTabBar) {
if (swapBookmarksAndTabs) {
if (tabBarInDrawer) {
iBinding.rightDrawer
iBinding.rightDrawerContent
} else {
iBinding.layoutTabsRight
}
} else {
if (tabBarInDrawer) {
iBinding.leftDrawer
iBinding.leftDrawerContent
} else {
iBinding.layoutTabsLeft
}
Expand Down Expand Up @@ -4335,6 +4386,10 @@ abstract class WebBrowserActivity : ThemedBrowserActivity(),
* @param immersive true to enable immersive mode, false otherwise
*/
private fun setFullscreen(enabled: Boolean, immersive: Boolean) {

// In theory we should be able to use those new APIs
//WindowCompat.getInsetsController(window,window.decorView).show(WindowInsetsCompat.Type.systemBars())

hideStatusBar = enabled
isImmersiveMode = immersive
val window = window
Expand All @@ -4359,6 +4414,8 @@ abstract class WebBrowserActivity : ThemedBrowserActivity(),
tabsDialog.window?.setFlags(window.attributes.flags, WindowManager.LayoutParams.FLAG_FULLSCREEN)
bookmarksDialog.window?.decorView?.systemUiVisibility = window.decorView.systemUiVisibility
bookmarksDialog.window?.setFlags(window.attributes.flags, WindowManager.LayoutParams.FLAG_FULLSCREEN)


}

/**
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/fulguris/extensions/ActivityExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import android.view.View
import android.view.Window
import androidx.annotation.StringRes
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar
import fulguris.app
Expand Down Expand Up @@ -85,6 +87,9 @@ fun Activity.makeSnackbar(message: String, aDuration: Int, aGravity: Int): Snack
*/
fun Window.setStatusBarIconsColor(dark: Boolean)
{
// That's the new API we should use but we had no joy with it on Honor Magic V2
//WindowCompat.getInsetsController(this, decorView).isAppearanceLightNavigationBars = !dark

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (dark) {
decorView.systemUiVisibility = decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
Expand Down
24 changes: 22 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fulguris.activity.WebBrowserActivity"

android:animateLayoutChanges="true"
android:fitsSystemWindows="true">

Expand Down Expand Up @@ -147,7 +148,16 @@
android:fitsSystemWindows="true"
android:weightSum="1"
android:layout_marginLeft="-64dp"
android:clickable="true" />
android:clickable="true" >

<!-- We added the content for cutout support to be able to set margins conveniently -->
<FrameLayout
android:id="@+id/leftDrawerContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground" />

</FrameLayout>

<!--
- android:clickable="true":
Expand All @@ -163,7 +173,17 @@
android:fitsSystemWindows="true"
android:weightSum="1"
android:layout_marginLeft="-64dp"
android:clickable="true" />
android:clickable="true">

<!-- We added the content for cutout support to be able to set margins conveniently -->
<FrameLayout
android:id="@+id/rightDrawerContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground" />

</FrameLayout>


</androidx.drawerlayout.widget.DrawerLayout>

Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/values-v27/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--
We used those to support cutouts
See: https://github.com/Slion/Fulguris/issues/603
However I reckon we better do that in code and make it configurable.
See: https://stackoverflow.com/a/58361683/3969362
-->

<style name="Theme.App.Light" parent="Theme.App.Light.Base">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges <!-- default, shortEdges, or never -->
</item>
</style>>

<style name="Theme.App.Dark" parent="Theme.App.Dark.Base">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges <!-- default, shortEdges, or never -->
</item>
</style>

<style name="Theme.App.Black" parent="Theme.App.Black.Base">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges <!-- default, shortEdges, or never -->
</item>
</style>

</resources>
23 changes: 20 additions & 3 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ colorControlNormal:


<!-- Light theme -->
<style name="Theme.App.Light" parent="Theme.Material3.Light.NoActionBar">
<style name="Theme.App.Light.Base" parent="Theme.Material3.Light.NoActionBar">

<!-- Theme colors
See: https://material.io/develop/android/theming/color
Expand Down Expand Up @@ -242,10 +242,13 @@ colorControlNormal:

<item name="sliderPreferenceStyle">@style/Preference.SliderPreference</item>

<!-- <item name="android:windowFullscreen">true</item>-->

</style>


<!-- Dark theme -->
<style name="Theme.App.Dark" parent="Theme.Material3.Dark.NoActionBar">
<style name="Theme.App.Dark.Base" parent="Theme.Material3.Dark.NoActionBar">

<!-- Theme colors
See: https://material.io/develop/android/theming/color
Expand Down Expand Up @@ -416,9 +419,12 @@ colorControlNormal:
<item name="enableEdgeToEdge">true</item>

<item name="sliderPreferenceStyle">@style/Preference.SliderPreference</item>

<!-- <item name="android:windowFullscreen">true</item>-->
</style>

<style name="Theme.App.Black" parent="Theme.Material3.Dark">

<style name="Theme.App.Black.Base" parent="Theme.Material3.Dark">
<!-- Theme colors
See: https://material.io/develop/android/theming/color
-->
Expand Down Expand Up @@ -586,6 +592,17 @@ colorControlNormal:
<item name="enableEdgeToEdge">true</item>

<item name="sliderPreferenceStyle">@style/Preference.SliderPreference</item>

<!-- <item name="android:windowFullscreen">true</item>-->
</style>

<style name="Theme.App.Light" parent="Theme.App.Light.Base">
</style>>

<style name="Theme.App.Dark" parent="Theme.App.Dark.Base">
</style>

<style name="Theme.App.Black" parent="Theme.App.Black.Base">
</style>

<!-- Day theme to be used when following system default dark theme -->
Expand Down

0 comments on commit 6bdeb9b

Please sign in to comment.