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 /** Defines an interactable undo history. */
20 class UndoState internal constructor(private val state: TextFieldState) {
21 
22     /**
23      * Whether it is possible to execute a meaningful undo action right now. If this value is false,
24      * calling `undo` would be a no-op.
25      */
26     @Suppress("GetterSetterNames")
27     @get:Suppress("GetterSetterNames")
28     val canUndo: Boolean
29         get() = state.textUndoManager.canUndo
30 
31     /**
32      * Whether it is possible to execute a meaningful redo action right now. If this value is false,
33      * calling `redo` would be a no-op.
34      */
35     @Suppress("GetterSetterNames")
36     @get:Suppress("GetterSetterNames")
37     val canRedo: Boolean
38         get() = state.textUndoManager.canRedo
39 
40     /**
41      * Reverts the latest edit action or a group of actions that are merged together. Calling it
42      * repeatedly can continue undoing the previous actions.
43      */
undonull44     fun undo() {
45         state.textUndoManager.undo(state)
46     }
47 
48     /** Re-applies a change that was previously reverted via [undo]. */
redonull49     fun redo() {
50         state.textUndoManager.redo(state)
51     }
52 
53     /** Clears all undo and redo history up to this point. */
clearHistorynull54     fun clearHistory() {
55         state.textUndoManager.clearHistory()
56     }
57 }
58