1 /*
2  * Copyright 2021 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 @file:Suppress("DEPRECATION")
18 
19 package androidx.compose.ui.platform
20 
21 import androidx.compose.runtime.Stable
22 import androidx.compose.ui.text.input.TextInputService
23 
24 /** Provide software keyboard control. */
25 @Stable
26 interface SoftwareKeyboardController {
27     /**
28      * Request that the system show a software keyboard.
29      *
30      * This request is best effort. If the system can currently show a software keyboard, it will be
31      * shown. However, there is no guarantee that the system will be able to show a software
32      * keyboard. If the system cannot show a software keyboard currently, this call will be silently
33      * ignored.
34      *
35      * The software keyboard will never show if there is no composable that will accept text input,
36      * such as a [TextField][androidx.compose.foundation.text.BasicTextField] when it is focused.
37      * You may find it useful to ensure focus when calling this function.
38      *
39      * You do not need to call this function unless you also call [hide], as the keyboard is
40      * automatically shown and hidden by focus events in the BasicTextField.
41      *
42      * Calling this function is considered a side-effect and should not be called directly from
43      * recomposition.
44      *
45      * @sample androidx.compose.ui.samples.SoftwareKeyboardControllerSample
46      */
shownull47     fun show()
48 
49     /**
50      * Hide the software keyboard.
51      *
52      * This request is best effort, if the system cannot hide the software keyboard this call will
53      * silently be ignored.
54      *
55      * Calling this function is considered a side-effect and should not be called directly from
56      * recomposition.
57      *
58      * @sample androidx.compose.ui.samples.SoftwareKeyboardControllerSample
59      */
60     fun hide()
61 }
62 
63 internal class DelegatingSoftwareKeyboardController(val textInputService: TextInputService) :
64     SoftwareKeyboardController {
65     override fun show() {
66         @Suppress("DEPRECATION") textInputService.showSoftwareKeyboard()
67     }
68 
69     override fun hide() {
70         @Suppress("DEPRECATION") textInputService.hideSoftwareKeyboard()
71     }
72 }
73