From bccd1fcd3afa52f2831622d1817fdc7d9fee77f0 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:50:49 -0300 Subject: [PATCH] UI: match navbar with previewers' background --- .../ichi2/anki/previewer/PreviewerFragment.kt | 10 +++++++ .../previewer/TemplatePreviewerFragment.kt | 10 +++++++ .../java/com/ichi2/anki/utils/Resources.kt | 26 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt index 7eb7daa4905a..33bf0fe91019 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerFragment.kt @@ -24,6 +24,7 @@ import android.view.MenuItem import android.view.View import android.webkit.WebView import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.widget.ThemeUtils import androidx.appcompat.widget.Toolbar import androidx.core.os.bundleOf import androidx.core.view.isVisible @@ -42,6 +43,7 @@ import com.ichi2.anki.browser.PreviewerIdsFile import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider import com.ichi2.anki.snackbar.SnackbarBuilder import com.ichi2.anki.utils.ext.sharedPrefs +import com.ichi2.anki.utils.navBarNeedsScrim import com.ichi2.annotations.NeedsTest import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat import com.ichi2.utils.performClickIfEnabled @@ -174,6 +176,14 @@ class PreviewerFragment : if (sharedPrefs().getBoolean("safeDisplay", false)) { view.findViewById(R.id.webview_container).elevation = 0F } + + with(requireActivity()) { + // use the screen background color if the nav bar doesn't need a scrim when using a + // transparent background. e.g. when navigation gestures are enabled + if (!navBarNeedsScrim) { + window.navigationBarColor = ThemeUtils.getThemeAttrColor(this, R.attr.alternativeBackgroundColor) + } + } } override fun onMenuItemClick(item: MenuItem): Boolean { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt index caff8ef19404..f8eb22fa6339 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerFragment.kt @@ -20,6 +20,7 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.webkit.WebView +import androidx.appcompat.widget.ThemeUtils import androidx.core.os.BundleCompat import androidx.core.os.bundleOf import androidx.fragment.app.viewModels @@ -33,6 +34,7 @@ import com.ichi2.anki.R import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider import com.ichi2.anki.snackbar.SnackbarBuilder import com.ichi2.anki.utils.ext.sharedPrefs +import com.ichi2.anki.utils.navBarNeedsScrim import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -97,6 +99,14 @@ class TemplatePreviewerFragment : if (sharedPrefs().getBoolean("safeDisplay", false)) { view.findViewById(R.id.webview_container).elevation = 0F } + + with(requireActivity()) { + // use the screen background color if the nav bar doesn't need a scrim when using a + // transparent background. e.g. when navigation gestures are enabled + if (!navBarNeedsScrim) { + window.navigationBarColor = ThemeUtils.getThemeAttrColor(this, R.attr.alternativeBackgroundColor) + } + } } companion object { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/utils/Resources.kt b/AnkiDroid/src/main/java/com/ichi2/anki/utils/Resources.kt index 1075cae1db85..900a60c78266 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/utils/Resources.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/utils/Resources.kt @@ -15,10 +15,14 @@ */ package com.ichi2.anki.utils +import android.annotation.SuppressLint import android.content.Context import android.content.res.Resources import androidx.annotation.PluralsRes import androidx.annotation.StringRes +import androidx.fragment.app.FragmentActivity +import com.ichi2.anki.CrashReportService +import com.ichi2.annotations.NeedsTest /** * @param resId must be a [StringRes] or a [PluralsRes] @@ -37,3 +41,25 @@ fun Resources.getFormattedStringOrPlurals(resId: Int, quantity: Int): String { fun Context.getFormattedStringOrPlurals(resId: Int, quantity: Int): String { return resources.getFormattedStringOrPlurals(resId, quantity) } + +@SuppressLint("DiscouragedApi") // Resources.getIdentifier: Use of this function is discouraged +// because resource reflection makes it harder to perform build optimizations and compile-time +// verification of code. It is much more efficient to retrieve resources by identifier +// (e.g. `R.foo.bar`) than by name (e.g. `getIdentifier("bar", "foo", null)`) +private fun Context.getSystemBoolean(resName: String, fallback: Boolean): Boolean { + return try { + val id = Resources.getSystem().getIdentifier(resName, "bool", "android") + if (id != 0) { + createPackageContext("android", 0).resources.getBoolean(id) + } else { + fallback + } + } catch (e: Exception) { + CrashReportService.sendExceptionReport(e, "Context::getSystemBoolean") + fallback + } +} + +@NeedsTest("true if the navbar is transparent and needs a scrim, false if not") +val FragmentActivity.navBarNeedsScrim: Boolean + get() = getSystemBoolean("config_navBarNeedsScrim", true)