Set SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR when necessary

This commit is contained in:
Aleksandras Kostarevas 2024-04-24 10:20:34 -04:00
parent f261de9fab
commit 645c7d6e49
3 changed files with 44 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package org.futo.inputmethod.latin
import android.content.res.Configuration
import android.graphics.Color
import android.inputmethodservice.InputMethodService
import android.os.Build
import android.os.Bundle
@ -66,6 +67,7 @@ import org.futo.inputmethod.latin.uix.setSetting
import org.futo.inputmethod.latin.uix.theme.DarkColorScheme
import org.futo.inputmethod.latin.uix.theme.ThemeOption
import org.futo.inputmethod.latin.uix.theme.ThemeOptions
import org.futo.inputmethod.latin.uix.theme.applyWindowColors
import org.futo.inputmethod.latin.uix.theme.presets.VoiceInputTheme
import org.futo.inputmethod.latin.xlm.LanguageModelFacilitator
import org.futo.inputmethod.updates.scheduleUpdateCheckingJob
@ -132,11 +134,26 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save
Log.w("LatinIME", "Recreating keyboard")
}
private var isNavigationBarVisible = false
fun updateNavigationBarVisibility(visible: Boolean? = null) {
if(visible != null) isNavigationBarVisible = visible
val color = drawableProvider?.primaryKeyboardColor
window.window?.let { window ->
if(color == null || !isNavigationBarVisible) {
applyWindowColors(window, Color.TRANSPARENT, statusBar = false)
} else {
applyWindowColors(window, color, statusBar = false)
}
}
}
private fun updateDrawableProvider(colorScheme: ColorScheme) {
activeColorScheme = colorScheme
drawableProvider = BasicThemeProvider(this, overrideColorScheme = colorScheme)
window.window?.navigationBarColor = drawableProvider!!.primaryKeyboardColor
updateNavigationBarVisibility()
uixManager.onColorSchemeChanged()
}

View File

@ -2040,13 +2040,7 @@ public class LatinIMELegacy implements KeyboardActionListener,
}
private void setNavigationBarVisibility(final boolean visible) {
int color = ((DynamicThemeProviderOwner)getInputMethodService()).getDrawableProvider().getPrimaryKeyboardColor();
if (BuildCompatUtils.EFFECTIVE_SDK_INT > Build.VERSION_CODES.M) {
// For N and later, IMEs can specify Color.TRANSPARENT to make the navigation bar
// transparent. For other colors the system uses the default color.
mInputMethodService.getWindow().getWindow().setNavigationBarColor(
visible ? color : Color.TRANSPARENT);
}
((LatinIME)mInputMethodService).updateNavigationBarVisibility(visible);
}
public InputMethodService getInputMethodService() {

View File

@ -1,22 +1,18 @@
package org.futo.inputmethod.latin.uix.theme
import android.app.Activity
import android.content.Context
import android.os.Build
import android.view.WindowManager
import android.view.View
import android.view.Window
import androidx.annotation.ColorInt
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
import kotlin.math.sqrt
val DarkColorScheme = darkColorScheme(
primary = Slate600,
@ -49,14 +45,28 @@ val DarkColorScheme = darkColorScheme(
onSurfaceVariant = Slate300
)
fun applyWindowColors(context: Context, backgroundColor: Color) {
val window = (context as Activity).window
val color = backgroundColor.copy(alpha = 0.75f).toArgb()
window.statusBarColor = color
fun applyWindowColors(window: Window, @ColorInt color: Int, statusBar: Boolean) {
if(statusBar) {
window.statusBarColor = color
}
window.navigationBarColor = color
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val view = window.decorView
val uiFlags = view.systemUiVisibility
val luminance = sqrt(
0.299 * android.graphics.Color.red(color) / 255.0
+ 0.587 * android.graphics.Color.green(color) / 255.0
+ 0.114 * android.graphics.Color.blue(color) / 255.0
)
if (luminance > 0.5 && color != android.graphics.Color.TRANSPARENT) {
view.systemUiVisibility = uiFlags or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} else {
view.systemUiVisibility = uiFlags and (View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv())
}
}
}
@Composable
@ -64,7 +74,7 @@ fun StatusBarColorSetter() {
val backgroundColor = MaterialTheme.colorScheme.background
val context = LocalContext.current
LaunchedEffect(backgroundColor) {
applyWindowColors(context, backgroundColor)
applyWindowColors((context as Activity).window, backgroundColor.toArgb(), statusBar = false)
}
}