From eadaeeaca79cab54edab408941f705eb20547ea5 Mon Sep 17 00:00:00 2001 From: fscarponi Date: Thu, 21 Dec 2023 10:20:48 +0100 Subject: [PATCH] Update TabData for flexible content --- .../samples/standalone/view/component/Tabs.kt | 4 ++-- .../jetbrains/jewel/ui/component/TabStrip.kt | 7 ++++--- .../org/jetbrains/jewel/ui/component/Tabs.kt | 17 +++++++---------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Tabs.kt b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Tabs.kt index 8fd2105ed6..001493ce0f 100644 --- a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Tabs.kt +++ b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/Tabs.kt @@ -48,7 +48,7 @@ private fun DefaultTabShowcase() { tabIds.mapIndexed { index, id -> TabData.Default( selected = index == selectedTabIndex, - label = "Default tab $id", + content = { Text("Default tab $id") }, onClose = { tabIds = tabIds.toMutableList().apply { removeAt(index) } if (selectedTabIndex >= index) { @@ -83,7 +83,7 @@ private fun EditorTabShowcase() { tabIds.mapIndexed { index, id -> TabData.Editor( selected = index == selectedTabIndex, - label = "Editor tab $id", + content = { Text("Editor tab $id") }, onClose = { tabIds = tabIds.toMutableList().apply { removeAt(index) } if (selectedTabIndex >= index) { diff --git a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/TabStrip.kt b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/TabStrip.kt index 5b37353139..18a6e711ee 100644 --- a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/TabStrip.kt +++ b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/TabStrip.kt @@ -30,6 +30,7 @@ import org.jetbrains.jewel.foundation.GenerateDataFunctions import org.jetbrains.jewel.foundation.modifier.onHover import org.jetbrains.jewel.foundation.state.CommonStateBitMask import org.jetbrains.jewel.foundation.state.FocusableComponentState +import org.jetbrains.jewel.foundation.state.InteractiveComponentState @Composable public fun TabStrip( @@ -81,7 +82,7 @@ public fun TabStrip( public sealed class TabData { public abstract val selected: Boolean - public abstract val label: String + public abstract val content: @Composable (tabState: InteractiveComponentState) -> Unit public abstract val icon: Painter? public abstract val closable: Boolean public abstract val onClose: () -> Unit @@ -91,7 +92,7 @@ public sealed class TabData { @GenerateDataFunctions public class Default( override val selected: Boolean, - override val label: String, + override val content: @Composable (tabState: InteractiveComponentState) -> Unit, override val icon: Painter? = null, override val closable: Boolean = true, override val onClose: () -> Unit = {}, @@ -102,7 +103,7 @@ public sealed class TabData { @GenerateDataFunctions public class Editor( override val selected: Boolean, - override val label: String, + override val content: @Composable (tabState: InteractiveComponentState) -> Unit, override val icon: Painter? = null, override val closable: Boolean = true, override val onClose: () -> Unit = {}, diff --git a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Tabs.kt b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Tabs.kt index 41df851fea..5672edf93e 100644 --- a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Tabs.kt +++ b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Tabs.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.interaction.HoverInteraction import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.PressInteraction import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.height @@ -72,10 +73,7 @@ internal fun TabImpl( interactionSource.interactions.collect { interaction -> when (interaction) { is PressInteraction.Press -> tabState = tabState.copy(pressed = true) - is PressInteraction.Cancel, - is PressInteraction.Release, - -> tabState = tabState.copy(pressed = false) - + is PressInteraction.Cancel, is PressInteraction.Release -> tabState = tabState.copy(pressed = false) is HoverInteraction.Enter -> tabState = tabState.copy(hovered = true) is HoverInteraction.Exit -> tabState = tabState.copy(hovered = false) } @@ -127,11 +125,10 @@ internal fun TabImpl( Image(modifier = Modifier.alpha(iconAlpha), painter = icon, contentDescription = null) } - Text( - modifier = Modifier.alpha(labelAlpha), - text = tabData.label, - color = tabStyle.colors.contentFor(tabState).value, - ) + Box(Modifier.alpha(labelAlpha)) { + tabData.content(tabState) + } + val showCloseIcon = when (tabData) { is TabData.Default -> tabData.closable @@ -166,7 +163,7 @@ internal fun TabImpl( ) .size(16.dp), painter = closePainter, - contentDescription = "Close tab ${tabData.label}", + contentDescription = "Close tab", ) } else if (tabData.closable) { Spacer(Modifier.size(16.dp))