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 762e295
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jetbrains.jewel.samples.standalone.viewmodel.View
import org.jetbrains.jewel.ui.component.Icon
import org.jetbrains.jewel.ui.component.IconButton
import org.jetbrains.jewel.ui.component.TabData
import org.jetbrains.jewel.ui.component.TabData.Default
import org.jetbrains.jewel.ui.component.TabStrip
import org.jetbrains.jewel.ui.component.Text
import org.jetbrains.jewel.ui.theme.defaultTabStyle
Expand All @@ -46,9 +47,8 @@ private fun DefaultTabShowcase() {

val tabs = remember(tabIds, selectedTabIndex) {
tabIds.mapIndexed { index, id ->
TabData.Default(
selected = index == selectedTabIndex,
label = "Default tab $id",
Default(selected = index == selectedTabIndex,
content = { Text(text = "Default tab $id") },
onClose = {
tabIds = tabIds.toMutableList().apply { removeAt(index) }
if (selectedTabIndex >= index) {
Expand All @@ -57,8 +57,7 @@ private fun DefaultTabShowcase() {
.coerceIn(0..maxPossibleIndex)
}
},
onClick = { selectedTabIndex = index },
)
onClick = { selectedTabIndex = index })
}
}

Expand All @@ -83,7 +82,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
13 changes: 9 additions & 4 deletions ui/api/ui.api
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public final class org/jetbrains/jewel/ui/DefaultComponentStyling : org/jetbrain
public final fun getTooltipStyle ()Lorg/jetbrains/jewel/ui/component/styling/TooltipStyle;
public final fun getUndecoratedDropdownStyle ()Lorg/jetbrains/jewel/ui/component/styling/DropdownStyle;
public fun hashCode ()I
public synthetic fun provide (Lkotlin/jvm/functions/Function0;)Lorg/jetbrains/jewel/ui/ComponentStyling;
public fun provide (Lkotlin/jvm/functions/Function2;)Lorg/jetbrains/jewel/ui/ComponentStyling;
public fun provide ([Landroidx/compose/runtime/ProvidedValue;)Lorg/jetbrains/jewel/ui/ComponentStyling;
public fun styles (Landroidx/compose/runtime/Composer;I)[Landroidx/compose/runtime/ProvidedValue;
public fun toString ()Ljava/lang/String;
public synthetic fun with (Lkotlin/jvm/functions/Function0;)Lorg/jetbrains/jewel/ui/ComponentStyling;
public fun with (Lkotlin/jvm/functions/Function2;)Lorg/jetbrains/jewel/ui/ComponentStyling;
public fun with (Lorg/jetbrains/jewel/ui/ComponentStyling;)Lorg/jetbrains/jewel/ui/ComponentStyling;
}
Expand Down Expand Up @@ -597,8 +599,8 @@ public final class org/jetbrains/jewel/ui/component/SplitLayoutKt {
public abstract class org/jetbrains/jewel/ui/component/TabData {
public static final field $stable I
public abstract fun getClosable ()Z
public abstract fun getContent ()Lkotlin/jvm/functions/Function3;
public abstract fun getIcon ()Landroidx/compose/ui/graphics/painter/Painter;
public abstract fun getLabel ()Ljava/lang/String;
public abstract fun getOnClick ()Lkotlin/jvm/functions/Function0;
public abstract fun getOnClose ()Lkotlin/jvm/functions/Function0;
public abstract fun getSelected ()Z
Expand All @@ -608,10 +610,12 @@ public final class org/jetbrains/jewel/ui/component/TabData$Default : org/jetbra
public static final field $stable I
public fun <init> (ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V
public synthetic fun <init> (ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V
public synthetic fun <init> (ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun equals (Ljava/lang/Object;)Z
public fun getClosable ()Z
public fun getContent ()Lkotlin/jvm/functions/Function3;
public fun getIcon ()Landroidx/compose/ui/graphics/painter/Painter;
public fun getLabel ()Ljava/lang/String;
public fun getOnClick ()Lkotlin/jvm/functions/Function0;
public fun getOnClose ()Lkotlin/jvm/functions/Function0;
public fun getSelected ()Z
Expand All @@ -622,11 +626,12 @@ public final class org/jetbrains/jewel/ui/component/TabData$Default : org/jetbra
public final class org/jetbrains/jewel/ui/component/TabData$Editor : org/jetbrains/jewel/ui/component/TabData {
public static final field $stable I
public fun <init> (ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V
public synthetic fun <init> (ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V
public synthetic fun <init> (ZLkotlin/jvm/functions/Function3;Landroidx/compose/ui/graphics/painter/Painter;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun equals (Ljava/lang/Object;)Z
public fun getClosable ()Z
public fun getContent ()Lkotlin/jvm/functions/Function3;
public fun getIcon ()Landroidx/compose/ui/graphics/painter/Painter;
public fun getLabel ()Ljava/lang/String;
public fun getOnClick ()Lkotlin/jvm/functions/Function0;
public fun getOnClose ()Lkotlin/jvm/functions/Function0;
public fun getSelected ()Z
Expand Down
97 changes: 71 additions & 26 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/TabStrip.kt
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 All @@ -47,19 +48,16 @@ public fun TabStrip(
.onHover { tabStripState = tabStripState.copy(hovered = it) },
) {
Row(
modifier = Modifier.horizontalScroll(scrollState)
.scrollable(
orientation = Orientation.Vertical,
reverseDirection =
ScrollableDefaults.reverseDirection(
LocalLayoutDirection.current,
Orientation.Vertical,
false,
),
state = scrollState,
interactionSource = remember { MutableInteractionSource() },
)
.selectableGroup(),
modifier = Modifier.horizontalScroll(scrollState).scrollable(
orientation = Orientation.Vertical,
reverseDirection = ScrollableDefaults.reverseDirection(
LocalLayoutDirection.current,
Orientation.Vertical,
false,
),
state = scrollState,
interactionSource = remember { MutableInteractionSource() },
).selectableGroup(),
) {
tabs.forEach { TabImpl(isActive = tabStripState.isActive, tabData = it) }
}
Expand All @@ -81,7 +79,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,23 +89,71 @@ 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 = {},
override val onClick: () -> Unit = {},
) : TabData()
) : TabData() {

@Deprecated(
"Use the primary constructor instead",
ReplaceWith(
"Default(selected = selected, content = { Text(text) }, icon = icon, closable = closable, onClose = onClose, onClick = onClick)",
"org.jetbrains.jewel.ui.component.TabData.Default",
),
)
public constructor(
selected: Boolean,
text: String,
icon: Painter? = null,
closable: Boolean = true,
onClose: () -> Unit,
onClick: () -> Unit,
) : this(
selected = selected,
content = { Text(text) },
icon = icon,
closable = closable,
onClose = onClose,
onClick = onClick,
)
}

@Immutable
@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 = {},
override val onClick: () -> Unit = {},
) : TabData()
) : TabData() {

@Deprecated(
"Use the primary constructor instead",
ReplaceWith(
"Editor(selected = selected, content = { Text(text) }, icon = icon, closable = closable, onClose = onClose, onClick = onClick)",
"org.jetbrains.jewel.ui.component.TabData.Default",
),
)
public constructor(
selected: Boolean,
text: String,
icon: Painter?,
closable: Boolean,
onClose: () -> Unit,
onClick: () -> Unit,
) : this(
selected = selected,
content = { Text(text) },
icon = icon,
closable = closable,
onClose = onClose,
onClick = onClick,
)
}
}

@Immutable
Expand Down Expand Up @@ -135,14 +181,13 @@ public value class TabStripState(public val state: ULong) : FocusableComponentSt
pressed: Boolean = isPressed,
hovered: Boolean = isHovered,
active: Boolean = isActive,
): TabStripState =
of(
enabled = enabled,
focused = focused,
pressed = pressed,
hovered = hovered,
active = active,
)
): TabStripState = of(
enabled = enabled,
focused = focused,
pressed = pressed,
hovered = hovered,
active = active,
)

override fun toString(): String =
"${javaClass.simpleName}(isEnabled=$isEnabled, isFocused=$isFocused, isHovered=$isHovered, " +
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 762e295

Please sign in to comment.