This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOutlinedTextField.kt
832 lines (793 loc) · 35.3 KB
/
OutlinedTextField.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.material3
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.selection.LocalTextSelectionColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.ClipOp
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.drawscope.clipRect
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.layout.IntrinsicMeasurable
import androidx.compose.ui.layout.IntrinsicMeasureScope
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasurePolicy
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
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.IntOffset
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.coerceAtLeast
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.offset
import kotlin.math.max
import kotlin.math.roundToInt
/**
* <a href="https://m3.material.io/components/text-fields/overview" class="external" target="_blank">Material Design outlined text field</a>.
*
* Text fields allow users to enter text into a UI. They typically appear in forms and dialogs.
* Outlined text fields have less visual emphasis than filled text fields. When they appear in
* places like forms, where many text fields are placed together, their reduced emphasis helps
* simplify the layout.
*
* ![Outlined text field image](https://developer.android.com/images/reference/androidx/compose/material3/outlined-text-field.png)
*
* See example usage:
* @sample androidx.compose.material3.samples.SimpleOutlinedTextFieldSample
*
* If apart from input text change you also want to observe the cursor location, selection range,
* or IME composition use the OutlinedTextField overload with the [TextFieldValue] parameter
* instead.
*
* @param value the input text to be shown in the text field
* @param onValueChange the callback that is triggered when the input service updates the text. An
* updated text comes as a parameter of the callback
* @param modifier the [Modifier] to be applied to this text field
* @param enabled controls the enabled state of this text field. When `false`, this component will
* not respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param readOnly controls the editable state of the text field. When `true`, the text field cannot
* be modified. However, a user can focus it and copy text from it. Read-only text fields are
* usually used to display pre-filled forms that a user cannot edit.
* @param textStyle the style to be applied to the input text. Defaults to [LocalTextStyle].
* @param label the optional label to be displayed inside the text field container. The default
* text style for internal [Text] is [Typography.bodySmall] when the text field is in focus and
* [Typography.bodyLarge] when the text field is not in focus
* @param placeholder the optional placeholder to be displayed when the text field is in focus and
* the input text is empty. The default text style for internal [Text] is [Typography.bodyLarge]
* @param leadingIcon the optional leading icon to be displayed at the beginning of the text field
* container
* @param trailingIcon the optional trailing icon to be displayed at the end of the text field
* container
* @param isError indicates if the text field's current value is in error. If set to true, the
* label, bottom indicator and trailing icon by default will be displayed in error color
* @param visualTransformation transforms the visual representation of the input [value]
* For example, you can use
* [PasswordVisualTransformation][androidx.compose.ui.text.input.PasswordVisualTransformation] to
* create a password text field. By default, no visual transformation is applied.
* @param keyboardOptions software keyboard options that contains configuration such as
* [KeyboardType] and [ImeAction]
* @param keyboardActions when the input service emits an IME action, the corresponding callback
* is called. Note that this IME action may be different from what you specified in
* [KeyboardOptions.imeAction]
* @param singleLine when `true`, this text field becomes a single horizontally scrolling text field
* instead of wrapping onto multiple lines. The keyboard will be informed to not show the return key
* as the [ImeAction]. Note that [maxLines] parameter will be ignored as the maxLines attribute will
* be automatically set to 1.
* @param maxLines the maximum height in terms of maximum number of visible lines. Should be
* equal or greater than 1. Note that this parameter will be ignored and instead maxLines will be
* set to 1 if [singleLine] is set to true.
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this text field. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this text field in different states.
* @param shape defines the shape of this text field's border
* @param colors [TextFieldColors] that will be used to resolve the colors used for this text field
* in different states. See [TextFieldDefaults.outlinedTextFieldColors].
*/
@ExperimentalMaterial3Api
@Composable
fun OutlinedTextField(
value: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
textStyle: TextStyle = LocalTextStyle.current,
label: @Composable (() -> Unit)? = null,
placeholder: @Composable (() -> Unit)? = null,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
isError: Boolean = false,
visualTransformation: VisualTransformation = VisualTransformation.None,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
singleLine: Boolean = false,
maxLines: Int = Int.MAX_VALUE,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = TextFieldDefaults.OutlinedShape,
colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors()
) {
// If color is not provided via the text style, use content color as a default
val textColor = textStyle.color.takeOrElse {
colors.textColor(enabled).value
}
val mergedTextStyle = textStyle.merge(TextStyle(color = textColor))
CompositionLocalProvider(LocalTextSelectionColors provides colors.selectionColors) {
@OptIn(ExperimentalMaterial3Api::class)
BasicTextField(
value = value,
modifier = if (label != null) {
modifier.padding(top = OutlinedTextFieldTopPadding)
} else {
modifier
}
.background(colors.containerColor(enabled).value, shape)
.defaultMinSize(
minWidth = TextFieldDefaults.MinWidth,
minHeight = TextFieldDefaults.MinHeight
),
onValueChange = onValueChange,
enabled = enabled,
readOnly = readOnly,
textStyle = mergedTextStyle,
cursorBrush = SolidColor(colors.cursorColor(isError).value),
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
interactionSource = interactionSource,
singleLine = singleLine,
maxLines = maxLines,
decorationBox = @Composable { innerTextField ->
TextFieldDefaults.OutlinedTextFieldDecorationBox(
value = value,
visualTransformation = visualTransformation,
innerTextField = innerTextField,
placeholder = placeholder,
label = label,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
singleLine = singleLine,
enabled = enabled,
isError = isError,
interactionSource = interactionSource,
colors = colors,
border = {
TextFieldDefaults.BorderBox(
enabled,
isError,
interactionSource,
colors,
shape
)
}
)
}
)
}
}
/**
* <a href="https://m3.material.io/components/text-fields/overview" class="external" target="_blank">Material Design outlined text field</a>.
*
* Text fields allow users to enter text into a UI. They typically appear in forms and dialogs.
* Outlined text fields have less visual emphasis than filled text fields. When they appear in
* places like forms, where many text fields are placed together, their reduced emphasis helps
* simplify the layout.
*
* ![Outlined text field image](https://developer.android.com/images/reference/androidx/compose/material3/outlined-text-field.png)
*
* See example usage:
* @sample androidx.compose.material3.samples.OutlinedTextFieldSample
*
* This overload provides access to the input text, cursor position and selection range and
* IME composition. If you only want to observe an input text change, use the OutlinedTextField
* overload with the [String] parameter instead.
*
* @param value the input [TextFieldValue] to be shown in the text field
* @param onValueChange the callback that is triggered when the input service updates values in
* [TextFieldValue]. An updated [TextFieldValue] comes as a parameter of the callback
* @param modifier the [Modifier] to be applied to this text field
* @param enabled controls the enabled state of this text field. When `false`, this component will
* not respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param readOnly controls the editable state of the text field. When `true`, the text field cannot
* be modified. However, a user can focus it and copy text from it. Read-only text fields are
* usually used to display pre-filled forms that a user cannot edit.
* @param textStyle the style to be applied to the input text. Defaults to [LocalTextStyle].
* @param label the optional label to be displayed inside the text field container. The default
* text style for internal [Text] is [Typography.bodySmall] when the text field is in focus and
* [Typography.bodyLarge] when the text field is not in focus
* @param placeholder the optional placeholder to be displayed when the text field is in focus and
* the input text is empty. The default text style for internal [Text] is [Typography.bodyLarge]
* @param leadingIcon the optional leading icon to be displayed at the beginning of the text field
* container
* @param trailingIcon the optional trailing icon to be displayed at the end of the text field
* container
* @param isError indicates if the text field's current value is in error state. If set to
* true, the label, bottom indicator and trailing icon by default will be displayed in error color
* @param visualTransformation transforms the visual representation of the input [value]
* For example, you can use
* [PasswordVisualTransformation][androidx.compose.ui.text.input.PasswordVisualTransformation] to
* create a password text field. By default, no visual transformation is applied.
* @param keyboardOptions software keyboard options that contains configuration such as
* [KeyboardType] and [ImeAction]
* @param keyboardActions when the input service emits an IME action, the corresponding callback
* is called. Note that this IME action may be different from what you specified in
* [KeyboardOptions.imeAction]
* @param singleLine when `true`, this text field becomes a single horizontally scrolling text field
* instead of wrapping onto multiple lines. The keyboard will be informed to not show the return key
* as the [ImeAction]. Note that [maxLines] parameter will be ignored as the maxLines attribute will
* be automatically set to 1.
* @param maxLines the maximum height in terms of maximum number of visible lines. Should be
* equal or greater than 1. Note that this parameter will be ignored and instead maxLines will be
* set to 1 if [singleLine] is set to true.
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this text field. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this text field in different states.
* @param shape defines the shape of this text field's border
* @param colors [TextFieldColors] that will be used to resolve the colors used for this text field
* in different states. See [TextFieldDefaults.outlinedTextFieldColors].
*/
@ExperimentalMaterial3Api
@Composable
fun OutlinedTextField(
value: TextFieldValue,
onValueChange: (TextFieldValue) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
textStyle: TextStyle = LocalTextStyle.current,
label: @Composable (() -> Unit)? = null,
placeholder: @Composable (() -> Unit)? = null,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
isError: Boolean = false,
visualTransformation: VisualTransformation = VisualTransformation.None,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
singleLine: Boolean = false,
maxLines: Int = Int.MAX_VALUE,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = TextFieldDefaults.OutlinedShape,
colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors()
) {
// If color is not provided via the text style, use content color as a default
val textColor = textStyle.color.takeOrElse {
colors.textColor(enabled).value
}
val mergedTextStyle = textStyle.merge(TextStyle(color = textColor))
CompositionLocalProvider(LocalTextSelectionColors provides colors.selectionColors) {
@OptIn(ExperimentalMaterial3Api::class)
BasicTextField(
value = value,
modifier = if (label != null) {
modifier.padding(top = OutlinedTextFieldTopPadding)
} else {
modifier
}
.background(colors.containerColor(enabled).value, shape)
.defaultMinSize(
minWidth = TextFieldDefaults.MinWidth,
minHeight = TextFieldDefaults.MinHeight
),
onValueChange = onValueChange,
enabled = enabled,
readOnly = readOnly,
textStyle = mergedTextStyle,
cursorBrush = SolidColor(colors.cursorColor(isError).value),
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
interactionSource = interactionSource,
singleLine = singleLine,
maxLines = maxLines,
decorationBox = @Composable { innerTextField ->
TextFieldDefaults.OutlinedTextFieldDecorationBox(
value = value.text,
visualTransformation = visualTransformation,
innerTextField = innerTextField,
placeholder = placeholder,
label = label,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
singleLine = singleLine,
enabled = enabled,
isError = isError,
interactionSource = interactionSource,
colors = colors,
border = {
TextFieldDefaults.BorderBox(
enabled,
isError,
interactionSource,
colors,
shape
)
}
)
}
)
}
}
/**
* Layout of the leading and trailing icons and the text field, label and placeholder in
* [OutlinedTextField].
* It doesn't use Row to position the icons and middle part because label should not be
* positioned in the middle part.
\ */
@Composable
internal fun OutlinedTextFieldLayout(
modifier: Modifier,
textField: @Composable () -> Unit,
placeholder: @Composable ((Modifier) -> Unit)?,
label: @Composable (() -> Unit)?,
leading: @Composable (() -> Unit)?,
trailing: @Composable (() -> Unit)?,
singleLine: Boolean,
animationProgress: Float,
onLabelMeasured: (Size) -> Unit,
border: @Composable () -> Unit,
paddingValues: PaddingValues
) {
val measurePolicy = remember(onLabelMeasured, singleLine, animationProgress, paddingValues) {
OutlinedTextFieldMeasurePolicy(
onLabelMeasured,
singleLine,
animationProgress,
paddingValues
)
}
val layoutDirection = LocalLayoutDirection.current
Layout(
modifier = modifier,
content = {
// We use additional box here to place an outlined cutout border as a sibling after the
// rest of UI. This allows us to use Modifier.border to draw an outline on top of the
// text field. We can't use the border modifier directly on the IconsWithTextFieldLayout
// as we also need to do the clipping (to form the cutout) which should not affect
// the rest of text field UI
border()
if (leading != null) {
Box(
modifier = Modifier.layoutId(LeadingId).then(IconDefaultSizeModifier),
contentAlignment = Alignment.Center
) {
leading()
}
}
if (trailing != null) {
Box(
modifier = Modifier.layoutId(TrailingId).then(IconDefaultSizeModifier),
contentAlignment = Alignment.Center
) {
trailing()
}
}
val startTextFieldPadding = paddingValues.calculateStartPadding(layoutDirection)
val endTextFieldPadding = paddingValues.calculateEndPadding(layoutDirection)
val padding = Modifier.padding(
start = if (leading != null) {
(startTextFieldPadding - HorizontalIconPadding).coerceAtLeast(
0.dp
)
} else {
startTextFieldPadding
},
end = if (trailing != null) {
(endTextFieldPadding - HorizontalIconPadding).coerceAtLeast(0.dp)
} else {
endTextFieldPadding
}
)
if (placeholder != null) {
placeholder(Modifier.layoutId(PlaceholderId).then(padding))
}
Box(
modifier = Modifier.layoutId(TextFieldId).then(padding),
propagateMinConstraints = true
) {
textField()
}
if (label != null) {
Box(modifier = Modifier.layoutId(LabelId)) { label() }
}
},
measurePolicy = measurePolicy
)
}
private class OutlinedTextFieldMeasurePolicy(
private val onLabelMeasured: (Size) -> Unit,
private val singleLine: Boolean,
private val animationProgress: Float,
private val paddingValues: PaddingValues
) : MeasurePolicy {
override fun MeasureScope.measure(
measurables: List<Measurable>,
constraints: Constraints
): MeasureResult {
// used to calculate the constraints for measuring elements that will be placed in a row
var occupiedSpaceHorizontally = 0
val bottomPadding = paddingValues.calculateBottomPadding().roundToPx()
// measure leading icon
val relaxedConstraints = constraints.copy(minWidth = 0, minHeight = 0)
val leadingPlaceable = measurables.find {
it.layoutId == LeadingId
}?.measure(relaxedConstraints)
occupiedSpaceHorizontally += widthOrZero(
leadingPlaceable
)
// measure trailing icon
val trailingPlaceable = measurables.find { it.layoutId == TrailingId }
?.measure(relaxedConstraints.offset(horizontal = -occupiedSpaceHorizontally))
occupiedSpaceHorizontally += widthOrZero(
trailingPlaceable
)
// measure label
val labelConstraints = relaxedConstraints.offset(
horizontal = -occupiedSpaceHorizontally -
paddingValues.calculateLeftPadding(layoutDirection).roundToPx() -
paddingValues.calculateRightPadding(layoutDirection).roundToPx(),
vertical = -bottomPadding
)
val labelPlaceable =
measurables.find { it.layoutId == LabelId }?.measure(labelConstraints)
labelPlaceable?.let {
onLabelMeasured(Size(it.width.toFloat(), it.height.toFloat()))
}
// measure text field
// on top we offset either by default padding or by label's half height if its too big
// minHeight must not be set to 0 due to how foundation TextField treats zero minHeight
val topPadding = max(
heightOrZero(labelPlaceable) / 2,
paddingValues.calculateTopPadding().roundToPx()
)
val textConstraints = constraints.offset(
horizontal = -occupiedSpaceHorizontally,
vertical = -bottomPadding - topPadding
).copy(minHeight = 0)
val textFieldPlaceable =
measurables.first { it.layoutId == TextFieldId }.measure(textConstraints)
// measure placeholder
val placeholderConstraints = textConstraints.copy(minWidth = 0)
val placeholderPlaceable =
measurables.find { it.layoutId == PlaceholderId }?.measure(placeholderConstraints)
val width =
calculateWidth(
widthOrZero(leadingPlaceable),
widthOrZero(trailingPlaceable),
textFieldPlaceable.width,
widthOrZero(labelPlaceable),
widthOrZero(placeholderPlaceable),
constraints
)
val height =
calculateHeight(
heightOrZero(leadingPlaceable),
heightOrZero(trailingPlaceable),
textFieldPlaceable.height,
heightOrZero(labelPlaceable),
heightOrZero(placeholderPlaceable),
constraints,
density,
paddingValues
)
val borderPlaceable = measurables.first { it.layoutId == BorderId }.measure(
Constraints(
minWidth = if (width != Constraints.Infinity) width else 0,
maxWidth = width,
minHeight = if (height != Constraints.Infinity) height else 0,
maxHeight = height
)
)
return layout(width, height) {
place(
height,
width,
leadingPlaceable,
trailingPlaceable,
textFieldPlaceable,
labelPlaceable,
placeholderPlaceable,
borderPlaceable,
animationProgress,
singleLine,
density,
layoutDirection,
paddingValues
)
}
}
override fun IntrinsicMeasureScope.maxIntrinsicHeight(
measurables: List<IntrinsicMeasurable>,
width: Int
): Int {
return intrinsicHeight(measurables, width) { intrinsicMeasurable, w ->
intrinsicMeasurable.maxIntrinsicHeight(w)
}
}
override fun IntrinsicMeasureScope.minIntrinsicHeight(
measurables: List<IntrinsicMeasurable>,
width: Int
): Int {
return intrinsicHeight(measurables, width) { intrinsicMeasurable, w ->
intrinsicMeasurable.minIntrinsicHeight(w)
}
}
override fun IntrinsicMeasureScope.maxIntrinsicWidth(
measurables: List<IntrinsicMeasurable>,
height: Int
): Int {
return intrinsicWidth(measurables, height) { intrinsicMeasurable, h ->
intrinsicMeasurable.maxIntrinsicWidth(h)
}
}
override fun IntrinsicMeasureScope.minIntrinsicWidth(
measurables: List<IntrinsicMeasurable>,
height: Int
): Int {
return intrinsicWidth(measurables, height) { intrinsicMeasurable, h ->
intrinsicMeasurable.minIntrinsicWidth(h)
}
}
private fun intrinsicWidth(
measurables: List<IntrinsicMeasurable>,
height: Int,
intrinsicMeasurer: (IntrinsicMeasurable, Int) -> Int
): Int {
val textFieldWidth =
intrinsicMeasurer(measurables.first { it.layoutId == TextFieldId }, height)
val labelWidth = measurables.find { it.layoutId == LabelId }?.let {
intrinsicMeasurer(it, height)
} ?: 0
val trailingWidth = measurables.find { it.layoutId == TrailingId }?.let {
intrinsicMeasurer(it, height)
} ?: 0
val leadingWidth = measurables.find { it.layoutId == LeadingId }?.let {
intrinsicMeasurer(it, height)
} ?: 0
val placeholderWidth = measurables.find { it.layoutId == PlaceholderId }?.let {
intrinsicMeasurer(it, height)
} ?: 0
return calculateWidth(
leadingPlaceableWidth = leadingWidth,
trailingPlaceableWidth = trailingWidth,
textFieldPlaceableWidth = textFieldWidth,
labelPlaceableWidth = labelWidth,
placeholderPlaceableWidth = placeholderWidth,
constraints = ZeroConstraints
)
}
private fun IntrinsicMeasureScope.intrinsicHeight(
measurables: List<IntrinsicMeasurable>,
width: Int,
intrinsicMeasurer: (IntrinsicMeasurable, Int) -> Int
): Int {
val textFieldHeight =
intrinsicMeasurer(measurables.first { it.layoutId == TextFieldId }, width)
val labelHeight = measurables.find { it.layoutId == LabelId }?.let {
intrinsicMeasurer(it, width)
} ?: 0
val trailingHeight = measurables.find { it.layoutId == TrailingId }?.let {
intrinsicMeasurer(it, width)
} ?: 0
val leadingHeight = measurables.find { it.layoutId == LeadingId }?.let {
intrinsicMeasurer(it, width)
} ?: 0
val placeholderHeight = measurables.find { it.layoutId == PlaceholderId }?.let {
intrinsicMeasurer(it, width)
} ?: 0
return calculateHeight(
leadingPlaceableHeight = leadingHeight,
trailingPlaceableHeight = trailingHeight,
textFieldPlaceableHeight = textFieldHeight,
labelPlaceableHeight = labelHeight,
placeholderPlaceableHeight = placeholderHeight,
constraints = ZeroConstraints,
density = density,
paddingValues = paddingValues
)
}
}
/**
* Calculate the width of the [OutlinedTextField] given all elements that should be placed inside.
*/
private fun calculateWidth(
leadingPlaceableWidth: Int,
trailingPlaceableWidth: Int,
textFieldPlaceableWidth: Int,
labelPlaceableWidth: Int,
placeholderPlaceableWidth: Int,
constraints: Constraints
): Int {
val middleSection = maxOf(
textFieldPlaceableWidth,
labelPlaceableWidth,
placeholderPlaceableWidth
)
val wrappedWidth =
leadingPlaceableWidth + middleSection + trailingPlaceableWidth
return max(wrappedWidth, constraints.minWidth)
}
/**
* Calculate the height of the [OutlinedTextField] given all elements that should be placed inside.
*/
private fun calculateHeight(
leadingPlaceableHeight: Int,
trailingPlaceableHeight: Int,
textFieldPlaceableHeight: Int,
labelPlaceableHeight: Int,
placeholderPlaceableHeight: Int,
constraints: Constraints,
density: Float,
paddingValues: PaddingValues
): Int {
// middle section is defined as a height of the text field or placeholder ( whichever is
// taller) plus 16.dp or half height of the label if it is taller, given that the label
// is vertically centered to the top edge of the resulting text field's container
val inputFieldHeight = max(
textFieldPlaceableHeight,
placeholderPlaceableHeight
)
val topPadding = paddingValues.calculateTopPadding().value * density
val bottomPadding = paddingValues.calculateBottomPadding().value * density
val middleSectionHeight = inputFieldHeight + bottomPadding + max(
topPadding,
labelPlaceableHeight / 2f
)
return max(
constraints.minHeight,
maxOf(
leadingPlaceableHeight,
trailingPlaceableHeight,
middleSectionHeight.roundToInt()
)
)
}
/**
* Places the provided text field, placeholder, label, optional leading and trailing icons inside
* the [OutlinedTextField]
*/
private fun Placeable.PlacementScope.place(
height: Int,
width: Int,
leadingPlaceable: Placeable?,
trailingPlaceable: Placeable?,
textFieldPlaceable: Placeable,
labelPlaceable: Placeable?,
placeholderPlaceable: Placeable?,
borderPlaceable: Placeable,
animationProgress: Float,
singleLine: Boolean,
density: Float,
layoutDirection: LayoutDirection,
paddingValues: PaddingValues
) {
val topPadding = (paddingValues.calculateTopPadding().value * density).roundToInt()
val startPadding =
(paddingValues.calculateStartPadding(layoutDirection).value * density).roundToInt()
val iconPadding = HorizontalIconPadding.value * density
// placed center vertically and to the start edge horizontally
leadingPlaceable?.placeRelative(
0,
Alignment.CenterVertically.align(leadingPlaceable.height, height)
)
// placed center vertically and to the end edge horizontally
trailingPlaceable?.placeRelative(
width - trailingPlaceable.width,
Alignment.CenterVertically.align(trailingPlaceable.height, height)
)
// label position is animated
// in single line text field label is centered vertically before animation starts
labelPlaceable?.let {
val startPositionY = if (singleLine) {
Alignment.CenterVertically.align(it.height, height)
} else {
topPadding
}
val positionY =
startPositionY * (1 - animationProgress) - (it.height / 2) * animationProgress
val positionX = (
if (leadingPlaceable == null) {
0f
} else {
(widthOrZero(leadingPlaceable) - iconPadding) * (1 - animationProgress)
}
).roundToInt() + startPadding
it.placeRelative(positionX, positionY.roundToInt())
}
// placed center vertically and after the leading icon horizontally if single line text field
// placed to the top with padding for multi line text field
val textVerticalPosition = max(
if (singleLine) {
Alignment.CenterVertically.align(textFieldPlaceable.height, height)
} else {
topPadding
},
heightOrZero(labelPlaceable) / 2
)
textFieldPlaceable.placeRelative(widthOrZero(leadingPlaceable), textVerticalPosition)
// placed similar to the input text above
placeholderPlaceable?.let {
val placeholderVerticalPosition = if (singleLine) {
Alignment.CenterVertically.align(it.height, height)
} else {
topPadding
}
it.placeRelative(widthOrZero(leadingPlaceable), placeholderVerticalPosition)
}
// place border
borderPlaceable.place(IntOffset.Zero)
}
internal fun Modifier.outlineCutout(labelSize: Size, paddingValues: PaddingValues) =
this.drawWithContent {
val labelWidth = labelSize.width
if (labelWidth > 0f) {
val innerPadding = OutlinedTextFieldInnerPadding.toPx()
val leftLtr = paddingValues.calculateLeftPadding(layoutDirection).toPx() - innerPadding
val rightLtr = leftLtr + labelWidth + 2 * innerPadding
val left = when (layoutDirection) {
LayoutDirection.Rtl -> size.width - rightLtr
else -> leftLtr.coerceAtLeast(0f)
}
val right = when (layoutDirection) {
LayoutDirection.Rtl -> size.width - leftLtr.coerceAtLeast(0f)
else -> rightLtr
}
val labelHeight = labelSize.height
// using label height as a cutout area to make sure that no hairline artifacts are
// left when we clip the border
clipRect(left, -labelHeight / 2, right, labelHeight / 2, ClipOp.Difference) {
}
} else {
}
}
private val OutlinedTextFieldInnerPadding = 4.dp
/*
This padding is used to allow label not overlap with the content above it. This 8.dp will work
for default cases when developers do not override the label's font size. If they do, they will
need to add additional padding themselves
*/
/* @VisibleForTesting */
internal val OutlinedTextFieldTopPadding = 8.dp
internal const val BorderId = "border"