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 package androidx.compose.ui.input
18 
19 import androidx.compose.runtime.getValue
20 import androidx.compose.runtime.mutableStateOf
21 import androidx.compose.runtime.setValue
22 
23 /**
24  * The [InputModeManager] is accessible as a CompositionLocal, that provides the current
25  * [InputMode].
26  */
27 interface InputModeManager {
28     /** The current [InputMode]. */
29     val inputMode: InputMode
30 
31     /**
32      * Send a request to change the [InputMode].
33      *
34      * @param inputMode The requested [InputMode].
35      * @return true if the system is in the requested mode, after processing this request.
36      */
requestInputModenull37     fun requestInputMode(inputMode: InputMode): Boolean
38 }
39 
40 /** This value is used to represent the InputMode that the system is currently in. */
41 @kotlin.jvm.JvmInline
42 value class InputMode internal constructor(@Suppress("unused") private val value: Int) {
43     override fun toString() =
44         when (this) {
45             Touch -> "Touch"
46             Keyboard -> "Keyboard"
47             else -> "Error"
48         }
49 
50     companion object {
51         /** The system is put into [Touch] mode when a user touches the screen. */
52         val Touch = InputMode(1)
53 
54         /** The system is put into [Keyboard] mode when a user presses a hardware key. */
55         val Keyboard = InputMode(2)
56     }
57 }
58 
59 internal class InputModeManagerImpl(
60     initialInputMode: InputMode,
61     private val onRequestInputModeChange: (InputMode) -> Boolean
62 ) : InputModeManager {
63     override var inputMode: InputMode by mutableStateOf(initialInputMode)
64 
requestInputModenull65     override fun requestInputMode(inputMode: InputMode) = onRequestInputModeChange.invoke(inputMode)
66 }
67