1
+ /*
2
+ * Copyright 2025 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.mpp.demo
18
+
19
+ import androidx.compose.foundation.lazy.LazyColumn
20
+ import androidx.compose.foundation.lazy.items
21
+ import androidx.compose.foundation.text.BasicTextField
22
+ import androidx.compose.foundation.text.KeyboardActions
23
+ import androidx.compose.foundation.text.KeyboardOptions
24
+ import androidx.compose.material.Text
25
+ import androidx.compose.mpp.demo.textfield.android.demoTextFieldModifiers
26
+ import androidx.compose.mpp.demo.textfield.android.fontSize8
27
+ import androidx.compose.runtime.Composable
28
+ import androidx.compose.runtime.mutableStateOf
29
+ import androidx.compose.runtime.saveable.rememberSaveable
30
+ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
31
+ import androidx.compose.ui.text.TextStyle
32
+ import androidx.compose.ui.text.input.PlatformImeOptions
33
+ import platform.UIKit.UIKeyboardAppearanceDark
34
+ import platform.UIKit.UIKeyboardAppearanceDefault
35
+ import platform.UIKit.UIKeyboardAppearanceLight
36
+ import platform.UIKit.UIKeyboardTypeDecimalPad
37
+ import platform.UIKit.UIKeyboardTypeDefault
38
+ import platform.UIKit.UIKeyboardTypeEmailAddress
39
+ import platform.UIKit.UIKeyboardTypeNamePhonePad
40
+ import platform.UIKit.UIKeyboardTypeNumberPad
41
+ import platform.UIKit.UIKeyboardTypePhonePad
42
+ import platform.UIKit.UIKeyboardTypeTwitter
43
+ import platform.UIKit.UIKeyboardTypeURL
44
+ import platform.UIKit.UIKeyboardTypeWebSearch
45
+ import platform.UIKit.UIReturnKeyType
46
+ import platform.UIKit.UITextAutocapitalizationType
47
+ import platform.UIKit.UITextAutocorrectionType
48
+
49
+ private val keyboardTypes = listOf (
50
+ " Default" to UIKeyboardTypeDefault ,
51
+ " Email" to UIKeyboardTypeEmailAddress ,
52
+ " Number" to UIKeyboardTypeNumberPad ,
53
+ " Phone" to UIKeyboardTypePhonePad ,
54
+ " Name" to UIKeyboardTypeNamePhonePad ,
55
+ " URL" to UIKeyboardTypeURL ,
56
+ " Decimal" to UIKeyboardTypeDecimalPad ,
57
+ " Twitter" to UIKeyboardTypeTwitter ,
58
+ " WebSearch" to UIKeyboardTypeWebSearch ,
59
+ " None" to null ,
60
+ )
61
+
62
+ private val keyboardAppearances = listOf (
63
+ " Default" to UIKeyboardAppearanceDefault ,
64
+ " Light" to UIKeyboardAppearanceLight ,
65
+ " Dark" to UIKeyboardAppearanceDark ,
66
+ )
67
+
68
+ private val returnKeyTypes = listOf (
69
+ " Default" to UIReturnKeyType .UIReturnKeyDefault ,
70
+ " Go" to UIReturnKeyType .UIReturnKeyGo ,
71
+ " Join" to UIReturnKeyType .UIReturnKeyJoin ,
72
+ " Next" to UIReturnKeyType .UIReturnKeyNext ,
73
+ " Route" to UIReturnKeyType .UIReturnKeyRoute ,
74
+ " Search" to UIReturnKeyType .UIReturnKeySearch ,
75
+ " Done" to UIReturnKeyType .UIReturnKeyDone ,
76
+ " Emergency Call" to UIReturnKeyType .UIReturnKeyEmergencyCall ,
77
+ " Null" to null
78
+ )
79
+
80
+ private val autocorrectionTypes = listOf (
81
+ " Default" to UITextAutocorrectionType .UITextAutocorrectionTypeDefault ,
82
+ " No" to UITextAutocorrectionType .UITextAutocorrectionTypeNo ,
83
+ " Yes" to UITextAutocorrectionType .UITextAutocorrectionTypeYes ,
84
+ )
85
+
86
+ private val autocapitalizationTypes = listOf (
87
+ " None" to UITextAutocapitalizationType .UITextAutocapitalizationTypeNone ,
88
+ " Sentences" to UITextAutocapitalizationType .UITextAutocapitalizationTypeSentences ,
89
+ " Words" to UITextAutocapitalizationType .UITextAutocapitalizationTypeWords ,
90
+ " All Characters" to UITextAutocapitalizationType .UITextAutocapitalizationTypeAllCharacters ,
91
+ " Null" to null
92
+ )
93
+
94
+ private val IosImeOptionsKeyboardTypeExample = Screen .Example (" Keyboard Type" ) {
95
+ LazyColumn {
96
+ items(keyboardTypes) {
97
+ Item (it.first, PlatformImeOptions (keyboardType = it.second))
98
+ }
99
+ }
100
+ }
101
+
102
+ private val IosImeOptionsKeyboardAppearanceExample = Screen .Example (" Keyboard Appearance" ) {
103
+ LazyColumn {
104
+ items(keyboardAppearances) {
105
+ Item (it.first, PlatformImeOptions (keyboardAppearance = it.second))
106
+ }
107
+ }
108
+ }
109
+
110
+ private val IosImeOptionsReturnKeyTypeExample = Screen .Example (" Return Key Type" ) {
111
+ LazyColumn {
112
+ items(returnKeyTypes) {
113
+ Item (it.first, PlatformImeOptions (returnKeyType = it.second))
114
+ }
115
+ }
116
+ }
117
+
118
+ private val IosImeOptionsIsSecureTextEntryExample = Screen .Example (" Is Secure Text Entry" ) {
119
+ LazyColumn {
120
+ item { Item (" Is Secure" , PlatformImeOptions (isSecureTextEntry = true )) }
121
+ item { Item (" Is Not Secure" , PlatformImeOptions (isSecureTextEntry = false )) }
122
+ }
123
+ }
124
+
125
+ private val IosImeOptionsEnablesReturnKeyTypeAutomaticallyExample = Screen .Example (" Enables Return Key Type Automatically" ) {
126
+ LazyColumn {
127
+ item {
128
+ Item (
129
+ " Enables Return Key Type Automatically" ,
130
+ PlatformImeOptions (enablesReturnKeyAutomatically = true )
131
+ )
132
+ }
133
+ item {
134
+ Item (
135
+ " Doesn't Enable Return Key Type Automatically" ,
136
+ PlatformImeOptions (enablesReturnKeyAutomatically = false )
137
+ )
138
+ }
139
+ }
140
+ }
141
+
142
+ private val IosImeOptionsAutocapitalizationTypeExample = Screen .Example (" Autocapitalization Type" ) {
143
+ LazyColumn {
144
+ items(autocapitalizationTypes) {
145
+ Item (it.first, PlatformImeOptions (autocapitalizationType = it.second))
146
+ }
147
+ }
148
+ }
149
+
150
+ private val IosImeOptionsAutocorrectionTypeExample = Screen .Example (" Autocapitalization Type" ) {
151
+ LazyColumn {
152
+ items(autocorrectionTypes) {
153
+ Item (it.first, PlatformImeOptions (autocorrectionType = it.second))
154
+ }
155
+ }
156
+ }
157
+
158
+ val IosImeOptionsExample = Screen .Selection (
159
+ " iOS Platform IME Options" ,
160
+ IosImeOptionsKeyboardTypeExample ,
161
+ IosImeOptionsKeyboardAppearanceExample ,
162
+ IosImeOptionsReturnKeyTypeExample ,
163
+ IosImeOptionsIsSecureTextEntryExample ,
164
+ IosImeOptionsEnablesReturnKeyTypeAutomaticallyExample ,
165
+ IosImeOptionsAutocapitalizationTypeExample ,
166
+ IosImeOptionsAutocorrectionTypeExample
167
+ )
168
+
169
+ @Composable
170
+ private fun Item (
171
+ title : String ,
172
+ options : PlatformImeOptions
173
+ ) {
174
+ Text (title)
175
+ EditLine (options)
176
+ }
177
+
178
+ @Composable
179
+ private fun EditLine (
180
+ options : PlatformImeOptions ,
181
+ text : String = ""
182
+ ) {
183
+ val keyboardController = LocalSoftwareKeyboardController .current
184
+ val state = rememberSaveable { mutableStateOf(text) }
185
+ BasicTextField (
186
+ modifier = demoTextFieldModifiers,
187
+ value = state.value,
188
+ singleLine = true ,
189
+ keyboardOptions = KeyboardOptions (
190
+ platformImeOptions = options
191
+ ),
192
+ keyboardActions = KeyboardActions { keyboardController?.hide() },
193
+ onValueChange = { state.value = it },
194
+ textStyle = TextStyle (fontSize = fontSize8),
195
+ )
196
+ }
0 commit comments