-
-
Save lockbooks/2f83a5c9a62180fe6934cf52556b884b to your computer and use it in GitHub Desktop.
How to show the UI example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun rememberSystemUiVisibilityController( | |
window: Window? = findWindow() | |
): SystemUiVisibilityController { | |
val view = LocalView.current | |
return remember(view) { | |
AndroidSystemUiVisibilityController(window, view) | |
} | |
} | |
interface SystemUiVisibilityState { | |
val isVisible: StateFlow<Boolean> | |
} | |
interface SystemUiVisibilityController :SystemUiVisibilityState { | |
var isSystemBarsVisible: Boolean | |
} | |
internal class AndroidSystemUiVisibilityController( | |
window: Window?, | |
private val view: View | |
) : SystemUiVisibilityController { | |
private val windowInsetsController = window?.let { | |
WindowInsetsControllerCompat(window, view).apply { | |
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE | |
} | |
} | |
private val isVisibleStateFlow = MutableStateFlow(isSystemBarsVisible) | |
private fun systemUiBars() = | |
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars() | |
override var isSystemBarsVisible: Boolean | |
get() { | |
return ViewCompat.getRootWindowInsets(view) | |
?.isVisible(systemUiBars()) == true | |
} | |
set(value) { | |
if (value) { | |
windowInsetsController?.show(systemUiBars()) | |
} else { | |
windowInsetsController?.hide(systemUiBars()) | |
} | |
isVisibleStateFlow.value = value | |
} | |
override val isVisible: StateFlow<Boolean> | |
get() = isVisibleStateFlow.asStateFlow() | |
} | |
val SystemUiVisibilityController.toggleUi: () -> Unit | |
get() = { | |
isSystemBarsVisible = !isSystemBarsVisible | |
} | |
@Composable | |
private fun findWindow(): Window? = | |
(LocalView.current.parent as? DialogWindowProvider)?.window | |
?: LocalView.current.context.findWindow() | |
private tailrec fun Context.findWindow(): Window? = | |
when (this) { | |
is Activity -> window | |
is ContextWrapper -> baseContext.findWindow() | |
else -> null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment