Skip to content

Commit

Permalink
Fix Scrollbars [part 13/n]
Browse files Browse the repository at this point in the history
* Fix InputField ignoring provided scrollbar style
* Remove useless KDocs from TextArea/TextField
* Clean up code
* Mark non-state-based TextArea/TextField as scheduled for removal
  • Loading branch information
rock3r committed Aug 22, 2024
1 parent b5c5341 commit ea7e175
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 50 deletions.
1 change: 1 addition & 0 deletions ide-laf-bridge/api/ide-laf-bridge.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class org/jetbrains/jewel/bridge/BridgeUtilsKt {
public static final fun retrieveArcAsCornerSize (Ljava/lang/String;)Landroidx/compose/foundation/shape/CornerSize;
public static final fun retrieveArcAsCornerSizeOrDefault (Ljava/lang/String;Landroidx/compose/foundation/shape/CornerSize;)Landroidx/compose/foundation/shape/CornerSize;
public static final fun retrieveArcAsCornerSizeWithFallbacks ([Ljava/lang/String;)Landroidx/compose/foundation/shape/CornerSize;
public static final fun retrieveColor-0YGnOg8 (Ljava/lang/String;ZJJ)J
public static final fun retrieveColor-4WTKRHQ (Ljava/lang/String;J)J
public static final fun retrieveColorOrNull (Ljava/lang/String;)Landroidx/compose/ui/graphics/Color;
public static final fun retrieveColorOrUnspecified (Ljava/lang/String;)J
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval
import org.jetbrains.jewel.foundation.Stroke
import org.jetbrains.jewel.foundation.modifier.border
import org.jetbrains.jewel.foundation.state.CommonStateBitMask.Active
Expand All @@ -46,6 +47,7 @@ import org.jetbrains.jewel.ui.focusOutline
import org.jetbrains.jewel.ui.outline
import org.jetbrains.jewel.ui.util.thenIf

@Suppress("DuplicatedCode") // The dupe is deprecated and is scheduled for removal
@Composable
internal fun InputField(
state: TextFieldState,
Expand Down Expand Up @@ -140,16 +142,19 @@ internal fun InputField(
scrollState = scrollState,
)

if (showScrollbar) {
if (showScrollbar && scrollbarStyle != null) {
VerticalScrollbar(
scrollState = scrollState,
modifier = Modifier.align(Alignment.CenterEnd),
interactionSource = interactionSource,
style = scrollbarStyle
)
}
}
}

@ScheduledForRemoval(inVersion = "Before 1.0")
@Suppress("DuplicatedCode") // This is deprecated and will be removed before 1.0
@Deprecated("Please use InputField(state) instead. If you want to observe text changes, use snapshotFlow { state.text }")
@Composable
internal fun InputField(
Expand Down
47 changes: 13 additions & 34 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/TextArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.offset
import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.foundation.theme.LocalContentColor
import org.jetbrains.jewel.foundation.theme.LocalTextStyle
Expand All @@ -35,18 +36,14 @@ import org.jetbrains.jewel.ui.component.styling.TextAreaStyle
import org.jetbrains.jewel.ui.theme.scrollbarStyle
import org.jetbrains.jewel.ui.theme.textAreaStyle

/**
* @param placeholder the optional placeholder to be displayed over the
* component when the [value] is empty.
*/
@Composable
public fun TextArea(
state: TextFieldState,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
outline: Outline = Outline.None,
placeholder: @Composable() (() -> Unit)? = null,
placeholder: @Composable (() -> Unit)? = null,
undecorated: Boolean = false,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
maxLines: Int = Int.MAX_VALUE,
Expand All @@ -55,7 +52,7 @@ public fun TextArea(
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
decorationBoxModifier: Modifier = Modifier,
showScrollbar: Boolean = true,
scrollbarStyle: ScrollbarStyle = JewelTheme.scrollbarStyle
scrollbarStyle: ScrollbarStyle = JewelTheme.scrollbarStyle,
) {
val minSize = style.metrics.minSize
InputField(
Expand Down Expand Up @@ -86,10 +83,7 @@ public fun TextArea(
)
}

/**
* @param placeholder the optional placeholder to be displayed over the
* component when the [value] is empty.
*/
@ScheduledForRemoval(inVersion = "Before 1.0")
@Deprecated("Please use TextArea(state) instead. If you want to observe text changes, use snapshotFlow { state.text }")
@Composable
public fun TextArea(
Expand All @@ -115,6 +109,7 @@ public fun TextArea(
val textFieldValue = textFieldValueState.copy(text = value)
var lastTextValue by remember(value) { mutableStateOf(value) }

@Suppress("DEPRECATION")
TextArea(
value = textFieldValue,
onValueChange = { newTextFieldValueState ->
Expand Down Expand Up @@ -145,10 +140,7 @@ public fun TextArea(
)
}

/**
* @param placeholder the optional placeholder to be displayed over the
* component when the [value] is empty.
*/
@ScheduledForRemoval(inVersion = "Before 1.0")
@Deprecated("Please use TextArea(state) instead. If you want to observe text changes, use snapshotFlow { state.text }")
@Composable
public fun TextArea(
Expand All @@ -171,6 +163,7 @@ public fun TextArea(
decorationBoxModifier: Modifier = Modifier,
) {
val minSize = style.metrics.minSize
@Suppress("DEPRECATION")
InputField(
value = value,
onValueChange = onValueChange,
Expand Down Expand Up @@ -212,10 +205,7 @@ private fun TextAreaDecorationBox(
Layout(
content = {
if (placeholder != null) {
Box(
modifier = Modifier.layoutId(PLACEHOLDER_ID),
contentAlignment = Alignment.TopStart,
) {
Box(modifier = Modifier.layoutId(PLACEHOLDER_ID), contentAlignment = Alignment.TopStart) {
CompositionLocalProvider(
LocalTextStyle provides textStyle.copy(color = placeholderTextColor),
LocalContentColor provides placeholderTextColor,
Expand All @@ -238,25 +228,16 @@ private fun TextAreaDecorationBox(
val rightPadding = contentPadding.calculateRightPadding(layoutDirection)
val horizontalPadding = (leftPadding + rightPadding).roundToPx()
val verticalPadding =
(contentPadding.calculateTopPadding() + contentPadding.calculateBottomPadding())
.roundToPx()
(contentPadding.calculateTopPadding() + contentPadding.calculateBottomPadding()).roundToPx()

val textAreaConstraints =
incomingConstraints
.offset(horizontal = -horizontalPadding, vertical = -verticalPadding)
.copy(minHeight = 0)
incomingConstraints.offset(horizontal = -horizontalPadding, vertical = -verticalPadding).copy(minHeight = 0)

val textAreaPlaceable =
measurables
.single { it.layoutId == TEXT_AREA_ID }
.measure(textAreaConstraints)
val textAreaPlaceable = measurables.single { it.layoutId == TEXT_AREA_ID }.measure(textAreaConstraints)

// Measure placeholder
val placeholderConstraints = textAreaConstraints.copy(minWidth = 0, minHeight = 0)
val placeholderPlaceable =
measurables
.find { it.layoutId == PLACEHOLDER_ID }
?.measure(placeholderConstraints)
val placeholderPlaceable = measurables.find { it.layoutId == PLACEHOLDER_ID }?.measure(placeholderConstraints)

val width = calculateWidth(textAreaPlaceable, placeholderPlaceable, incomingConstraints)
val height = calculateHeight(textAreaPlaceable, placeholderPlaceable, verticalPadding, incomingConstraints)
Expand All @@ -278,9 +259,7 @@ private fun calculateWidth(
textFieldPlaceable: Placeable,
placeholderPlaceable: Placeable?,
incomingConstraints: Constraints,
): Int =
maxOf(textFieldPlaceable.width, placeholderPlaceable?.width ?: 0)
.coerceAtLeast(incomingConstraints.minWidth)
): Int = maxOf(textFieldPlaceable.width, placeholderPlaceable?.width ?: 0).coerceAtLeast(incomingConstraints.minWidth)

private fun calculateHeight(
textFieldPlaceable: Placeable,
Expand Down
25 changes: 10 additions & 15 deletions ui/src/main/kotlin/org/jetbrains/jewel/ui/component/TextField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.offset
import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.foundation.theme.LocalContentColor
import org.jetbrains.jewel.foundation.theme.LocalTextStyle
Expand All @@ -33,20 +34,17 @@ import org.jetbrains.jewel.ui.component.styling.TextFieldStyle
import org.jetbrains.jewel.ui.theme.textFieldStyle
import kotlin.math.max

/**
* @param placeholder the optional placeholder to be displayed over the
* component when the [value] is empty.
*/
@Suppress("DuplicatedCode") // The dupe is scheduled for removal
@Composable
public fun TextField(
state: TextFieldState,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
outline: Outline = Outline.None,
placeholder: @Composable() (() -> Unit)? = null,
leadingIcon: @Composable() (() -> Unit)? = null,
trailingIcon: @Composable() (() -> Unit)? = null,
placeholder: @Composable (() -> Unit)? = null,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
undecorated: Boolean = false,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
style: TextFieldStyle = JewelTheme.textFieldStyle,
Expand Down Expand Up @@ -86,10 +84,7 @@ public fun TextField(
)
}

/**
* @param placeholder the optional placeholder to be displayed over the
* component when the [value] is empty.
*/
@ScheduledForRemoval(inVersion = "Before 1.0")
@Deprecated("Please use TextField(state) instead. If you want to observe text changes, use snapshotFlow { state.text }")
@Composable
public fun TextField(
Expand All @@ -114,6 +109,7 @@ public fun TextField(
val textFieldValue = textFieldValueState.copy(text = value)
var lastTextValue by remember(value) { mutableStateOf(value) }

@Suppress("DEPRECATION")
TextField(
value = textFieldValue,
onValueChange = { newTextFieldValueState ->
Expand Down Expand Up @@ -143,10 +139,8 @@ public fun TextField(
)
}

/**
* @param placeholder the optional placeholder to be displayed over the
* component when the [value] is empty.
*/
@Suppress("DuplicatedCode") // This is scheduled for removal
@ScheduledForRemoval(inVersion = "Before 1.0")
@Deprecated("Please use TextField(state) instead. If you want to observe text changes, use snapshotFlow { state.text }")
@Composable
public fun TextField(
Expand All @@ -168,6 +162,7 @@ public fun TextField(
textStyle: TextStyle = JewelTheme.defaultTextStyle,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
) {
@Suppress("DEPRECATION")
InputField(
value = value,
onValueChange = onValueChange,
Expand Down

0 comments on commit ea7e175

Please sign in to comment.