Skip to content

Commit

Permalink
Update TabData for flexible content
Browse files Browse the repository at this point in the history
  • Loading branch information
fscarponi committed Dec 21, 2023
1 parent effad57 commit eadaeea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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 = {},
Expand All @@ -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 = {},
Expand Down
17 changes: 7 additions & 10 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Tabs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit eadaeea

Please sign in to comment.