1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.foundation.text.input
18 
19 import androidx.compose.foundation.internal.requirePrecondition
20 import androidx.compose.foundation.layout.heightIn
21 import androidx.compose.foundation.text.input.TextFieldLineLimits.MultiLine
22 import androidx.compose.foundation.text.input.TextFieldLineLimits.SingleLine
23 import androidx.compose.runtime.Immutable
24 import androidx.compose.runtime.Stable
25 
26 /**
27  * Values that specify the text wrapping, scrolling, and height measurement behavior for text
28  * fields.
29  *
30  * @see SingleLine
31  * @see MultiLine
32  */
33 @Stable
34 sealed interface TextFieldLineLimits {
35 
36     /**
37      * The text field is always a single line tall, ignores newlines in the text, and scrolls
38      * horizontally when the text overflows.
39      */
40     object SingleLine : TextFieldLineLimits {
41 
toStringnull42         override fun toString(): String {
43             return "TextFieldLineLimits.SingleLine"
44         }
45     }
46 
47     /**
48      * The text field will be at least [minHeightInLines] tall, if the text overflows it will wrap,
49      * and if the text ends up being more than one line the field will grow until it is
50      * [maxHeightInLines] tall and then start scrolling vertically.
51      *
52      * It is required that 1 ≤ [minHeightInLines] ≤ [maxHeightInLines].
53      *
54      * To specify the minimum and/or maximum height of the field in non-text units, such as dps, use
55      * the [heightIn] modifier.
56      */
57     @Immutable
58     class MultiLine(val minHeightInLines: Int = 1, val maxHeightInLines: Int = Int.MAX_VALUE) :
59         TextFieldLineLimits {
60         init {
<lambda>null61             requirePrecondition(minHeightInLines in 1..maxHeightInLines) {
62                 "Expected 1 ≤ minHeightInLines ≤ maxHeightInLines, were " +
63                     "$minHeightInLines, $maxHeightInLines"
64             }
65         }
66 
toStringnull67         override fun toString(): String =
68             "MultiLine(minHeightInLines=$minHeightInLines, maxHeightInLines=$maxHeightInLines)"
69 
70         override fun equals(other: Any?): Boolean {
71             if (this === other) return true
72             if (other === null) return false
73             if (this::class != other::class) return false
74             other as MultiLine
75             if (minHeightInLines != other.minHeightInLines) return false
76             if (maxHeightInLines != other.maxHeightInLines) return false
77             return true
78         }
79 
hashCodenull80         override fun hashCode(): Int {
81             var result = minHeightInLines
82             result = 31 * result + maxHeightInLines
83             return result
84         }
85     }
86 
87     companion object {
88         val Default: TextFieldLineLimits = MultiLine()
89     }
90 }
91