Skip to content

Commit

Permalink
text field: align leading/trailing icons to the top when multilined
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 16, 2022
1 parent 60754cf commit 5a843f6
Showing 1 changed file with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ internal fun FieldContent(
Layout(
content = {
if (leadingIcon != null) {
FieldIcon(LeadingId, onLeadingIconClick, leadingIcon, Modifier.padding(start = FieldIconPadding))
FieldIcon(
LeadingId,
onLeadingIconClick,
leadingIcon,
Modifier.padding(start = FieldIconPadding)
)
}
if (trailingIcon != null) {
FieldIcon(TrailingId, onTrailingIconClick, trailingIcon, Modifier.padding(end = FieldIconPadding))
FieldIcon(
TrailingId,
onTrailingIconClick,
trailingIcon,
Modifier.padding(end = FieldIconPadding)
)
}

val padding = Modifier.padding(
Expand All @@ -54,14 +64,20 @@ internal fun FieldContent(
)

if (placeholder != null) {
Box(Modifier.layoutId(PlaceholderId).then(padding)) {
Box(
Modifier
.layoutId(PlaceholderId)
.then(padding)
) {
ProvideMergedTextStyle(OrbitTheme.typography.bodyNormal) {
ProvideContentEmphasis(ContentEmphasis.Subtle, content = placeholder)
}
}
}
Box(
Modifier.layoutId(FieldId).then(padding),
Modifier
.layoutId(FieldId)
.then(padding),
propagateMinConstraints = true,
) {
fieldContent()
Expand Down Expand Up @@ -98,9 +114,9 @@ private class FieldContentMeasurePolicy(
)
val fieldPlaceable = measurables.first { it.layoutId == FieldId }.measure(fieldConstraints)

// Placeholder
val placeholderConstraints = fieldConstraints.copy(minWidth = 0)
val placeholderPlaceable = measurables.find { it.layoutId == PlaceholderId }?.measure(placeholderConstraints)
val placeholderPlaceable = measurables.find { it.layoutId == PlaceholderId }
?.measure(placeholderConstraints)

val width = calculateWidth(
fieldWidth = fieldPlaceable.width,
Expand All @@ -119,23 +135,31 @@ private class FieldContentMeasurePolicy(
)

return layout(width, height) {
val verticalPaddingPadding = (FieldPadding.value * density).roundToInt()
val verticalPaddingPx = FieldPadding.roundToPx()

leadingPlaceable?.placeRelative(
x = 0,
y = Alignment.CenterVertically.align(leadingPlaceable.height, height)
y = if (singleLine) {
Alignment.CenterVertically.align(leadingPlaceable.height, height)
} else {
verticalPaddingPx
},
)
trailingPlaceable?.placeRelative(
x = width - trailingPlaceable.width,
y = Alignment.CenterVertically.align(trailingPlaceable.height, height)
y = if (singleLine) {
Alignment.CenterVertically.align(trailingPlaceable.height, height)
} else {
verticalPaddingPx
},
)

// Single line text field without label places its input center vertically. Multiline text
// field without label places its input at the top with padding
val fieldVerticalPosition = if (singleLine) {
Alignment.CenterVertically.align(fieldPlaceable.height, height)
} else {
verticalPaddingPadding
verticalPaddingPx
}
fieldPlaceable.placeRelative(
x = leadingPlaceable?.width ?: 0,
Expand All @@ -148,7 +172,7 @@ private class FieldContentMeasurePolicy(
val placeholderVerticalPosition = if (singleLine) {
Alignment.CenterVertically.align(placeholderPlaceable.height, height)
} else {
verticalPaddingPadding
verticalPaddingPx
}
placeholderPlaceable.placeRelative(
x = leadingPlaceable?.width ?: 0,
Expand Down

0 comments on commit 5a843f6

Please sign in to comment.