refactor: separate colors from style

This commit is contained in:
2023-11-16 01:09:06 +08:00
parent 3d994a0543
commit 56440553be
2 changed files with 41 additions and 11 deletions

View File

@@ -28,14 +28,23 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import com.highcapable.flexiui.component.DesktopContextMenu import com.highcapable.flexiui.component.DesktopContextMenu
import com.highcapable.flexiui.component.DesktopContextMenuRepresentation import com.highcapable.flexiui.component.DesktopContextMenuRepresentation
import com.highcapable.flexiui.component.LocalContextMenuColors
import com.highcapable.flexiui.component.LocalContextMenuStyle import com.highcapable.flexiui.component.LocalContextMenuStyle
import com.highcapable.flexiui.component.defaultContextMenuColors
import com.highcapable.flexiui.component.defaultContextMenuStyle import com.highcapable.flexiui.component.defaultContextMenuStyle
@Composable @Composable
internal actual fun FlexiThemeContent(content: @Composable () -> Unit) { internal actual fun FlexiThemeContent(content: @Composable () -> Unit) {
CompositionLocalProvider(LocalContextMenuStyle provides defaultContextMenuStyle()) { CompositionLocalProvider(
LocalContextMenuColors provides defaultContextMenuColors(),
LocalContextMenuStyle provides defaultContextMenuStyle()
) {
CompositionLocalProvider( CompositionLocalProvider(
LocalContextMenuRepresentation provides DesktopContextMenuRepresentation(DesktopContextMenu.style), LocalContextMenuRepresentation provides
DesktopContextMenuRepresentation(
colors = DesktopContextMenu.colors,
style = DesktopContextMenu.style
),
content = content content = content
) )
} }

View File

@@ -69,14 +69,21 @@ import com.highcapable.flexiui.utils.orElse
import java.awt.event.KeyEvent import java.awt.event.KeyEvent
@Immutable @Immutable
data class ContextMenuStyle( data class ContextMenuColors(
val contentColor: Color, val contentColor: Color,
val borderColor: Color, val borderColor: Color
)
@Immutable
data class ContextMenuStyle(
val contentStyle: AreaBoxStyle?, val contentStyle: AreaBoxStyle?,
val borderStyle: AreaBoxStyle? val borderStyle: AreaBoxStyle?
) )
internal class DesktopContextMenuRepresentation(private val style: ContextMenuStyle) : ContextMenuRepresentation { internal class DesktopContextMenuRepresentation(
private val colors: ContextMenuColors,
private val style: ContextMenuStyle
) : ContextMenuRepresentation {
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
override fun Representation(state: ContextMenuState, items: () -> List<ContextMenuItem>) { override fun Representation(state: ContextMenuState, items: () -> List<ContextMenuItem>) {
@@ -112,7 +119,7 @@ internal class DesktopContextMenuRepresentation(private val style: ContextMenuSt
modifier = Modifier modifier = Modifier
.width(IntrinsicSize.Max) .width(IntrinsicSize.Max)
.verticalScroll(rememberScrollState()), .verticalScroll(rememberScrollState()),
color = style.borderColor, color = colors.borderColor,
style = borderStyle style = borderStyle
) { ) {
items().forEach { item -> items().forEach { item ->
@@ -122,7 +129,7 @@ internal class DesktopContextMenuRepresentation(private val style: ContextMenuSt
state.status = ContextMenuState.Status.Closed state.status = ContextMenuState.Status.Closed
item.onClick() item.onClick()
} }
) { Text(text = item.label, color = style.contentColor) } ) { Text(text = item.label, color = colors.contentColor) }
} }
} }
} }
@@ -166,26 +173,40 @@ private fun Modifier.onHover(onHover: (Boolean) -> Unit) = pointerInput(Unit) {
} }
object DesktopContextMenu { object DesktopContextMenu {
val colors: ContextMenuColors
@Composable
@ReadOnlyComposable
get() = LocalContextMenuColors.current
val style: ContextMenuStyle val style: ContextMenuStyle
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = LocalContextMenuStyle.current get() = LocalContextMenuStyle.current
} }
val LocalContextMenuColors = compositionLocalOf {
ContextMenuColors(
borderColor = Color.Unspecified,
contentColor = Color.Unspecified
)
}
val LocalContextMenuStyle = compositionLocalOf { val LocalContextMenuStyle = compositionLocalOf {
ContextMenuStyle( ContextMenuStyle(
borderColor = Color.Unspecified,
contentColor = Color.Unspecified,
contentStyle = null, contentStyle = null,
borderStyle = null borderStyle = null
) )
} }
@Composable
@ReadOnlyComposable
internal fun defaultContextMenuColors() = ContextMenuColors(
contentColor = LocalContextMenuColors.current.contentColor.orElse() ?: LocalColors.current.textPrimary,
borderColor = LocalContextMenuColors.current.borderColor.orElse() ?: AreaBox.color
)
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
internal fun defaultContextMenuStyle() = ContextMenuStyle( internal fun defaultContextMenuStyle() = ContextMenuStyle(
contentColor = LocalContextMenuStyle.current.contentColor.orElse() ?: LocalColors.current.textPrimary,
borderColor = LocalContextMenuStyle.current.borderColor.orElse() ?: AreaBox.color,
contentStyle = LocalContextMenuStyle.current.contentStyle ?: AreaBox.style.copy( contentStyle = LocalContextMenuStyle.current.contentStyle ?: AreaBox.style.copy(
padding = 0.dp, padding = 0.dp,
startPadding = DefaultDesktopContextMenuContentPadding, startPadding = DefaultDesktopContextMenuContentPadding,