Skip to content

Commit

Permalink
UI: match navbar with previewers' background
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Mar 14, 2024
1 parent c2c991a commit bccd1fc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -174,6 +176,14 @@ class PreviewerFragment :
if (sharedPrefs().getBoolean("safeDisplay", false)) {
view.findViewById<MaterialCardView>(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -97,6 +99,14 @@ class TemplatePreviewerFragment :
if (sharedPrefs().getBoolean("safeDisplay", false)) {
view.findViewById<MaterialCardView>(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 {
Expand Down
26 changes: 26 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/utils/Resources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)

0 comments on commit bccd1fc

Please sign in to comment.