
net.peanuuutz.fork.ui.preset.Background.kt Maven / Gradle / Ivy
package net.peanuuutz.fork.ui.preset
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import net.peanuuutz.fork.ui.foundation.draw.background
import net.peanuuutz.fork.ui.foundation.layout.Box
import net.peanuuutz.fork.ui.foundation.layout.BoxScope
import net.peanuuutz.fork.ui.foundation.layout.fillMaxSize
import net.peanuuutz.fork.ui.preset.theme.Theme
import net.peanuuutz.fork.ui.ui.draw.Painter
import net.peanuuutz.fork.ui.ui.modifier.Modifier
import net.peanuuutz.fork.util.common.Color
@Composable
fun Background(
modifier: Modifier = Modifier,
backgroundStyle: BackgroundStyle = Theme.background,
content: @Composable BoxScope.() -> Unit
) {
val background by backgroundStyle.background()
Box(
modifier = modifier
.fillMaxSize()
.background(background)
) {
val contentColor by backgroundStyle.content()
CompositionLocalProvider(
LocalContentColor provides contentColor
) {
content()
}
}
}
@Stable
interface BackgroundStyle {
@Composable
fun background(): State
@Composable
fun content(): State
@Stable
abstract class Delegated(
val delegate: BackgroundStyle
) : BackgroundStyle by delegate
}
val Theme.background: BackgroundStyle
@ReadOnlyComposable
@Composable
get() = LocalBackground.current
@NonRestartableComposable
@Composable
fun BackgroundStyleProvider(
backgroundStyle: BackgroundStyle,
content: @Composable () -> Unit
) {
CompositionLocalProvider(
LocalBackground provides backgroundStyle,
content = content
)
}
@Stable
class DefaultBackgroundStyle(
val background: Painter,
val content: Color
) : BackgroundStyle {
@Composable
override fun background(): State {
return rememberUpdatedState(background)
}
@Composable
override fun content(): State {
return rememberUpdatedState(content)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is DefaultBackgroundStyle) return false
if (background != other.background) return false
if (content != other.content) return false
return true
}
override fun hashCode(): Int {
var result = background.hashCode()
result = 31 * result + content.hashCode()
return result
}
override fun toString(): String {
return "DefaultBackgroundStyle(background=$background, content=$content)"
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy