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.contextmenu.data
18 
19 import androidx.compose.foundation.text.contextmenu.modifier.filterTextContextMenuComponents
20 import androidx.compose.ui.util.fastJoinToString
21 
22 /**
23  * A list of components to be displayed in the context menu.
24  *
25  * @param components the list of components to be rendered in the context menu.
26  */
27 // TODO(grantapher-cm-api-publicize) Make class public
28 internal class TextContextMenuData
29 internal constructor(val components: List<TextContextMenuComponent>) {
toStringnull30     override fun toString(): String {
31         val componentsStr =
32             components.fastJoinToString(prefix = "[\n\t", separator = "\n\t", postfix = "\n]")
33         return "TextContextMenuData(components=$componentsStr)"
34     }
35 }
36 
37 /**
38  * A single component of a text context menu.
39  *
40  * @param key A unique key that identifies this component, mainly for use in filtering a component
41  *   in [Modifier.filterTextContextMenuComponents][filterTextContextMenuComponents]. It is advisable
42  *   to use a `data object` as a key here.
43  */
44 // TODO(grantapher-cm-api-publicize) Make class public
45 internal abstract class TextContextMenuComponent internal constructor(val key: Any)
46 
47 /** A [TextContextMenuComponent] separator in a text context menu. */
48 // TODO(grantapher-cm-api-publicize) Make object public
49 internal data object TextContextMenuSeparator : TextContextMenuComponent(TextContextMenuSeparator)
50 
51 /**
52  * A [TextContextMenuComponent] that represents a clickable item with a label in a context menu.
53  *
54  * @param key A unique key that identifies this component, mainly for use in filtering a component
55  *   in [Modifier.filterTextContextMenuComponents][filterTextContextMenuComponents]. It is advisable
56  *   to use a `data object` as a key here.
57  * @param label The label text to be shown in the context menu.
58  * @param leadingIcon Icon that precedes the label in the context menu. Setting this to null means
59  *   that it will not be displayed.
60  * @param onClick The action to be performed when this item is clicked. Call
61  *   [TextContextMenuSession.close] on the [TextContextMenuSession] receiver to close the context
62  *   menu item as a result of the click.
63  */
64 // TODO(grantapher-cm-api-publicize) Make class public
65 internal class TextContextMenuItem
66 internal constructor(
67     key: Any,
68     val label: String,
69     val leadingIcon: PlatformIcon? = null,
70     val onClick: TextContextMenuSession.() -> Unit,
71 ) : TextContextMenuComponent(key) {
toStringnull72     override fun toString(): String =
73         "TextContextMenuItem(key=$key, label=\"$label\", leadingIcon=$leadingIcon)"
74 }
75 
76 /** A platform-specific icon type used in a [TextContextMenuItem]. */
77 // TODO(grantapher-cm-api-publicize) Make public
78 internal expect class PlatformIcon
79 
80 /** A session for an open text context menu that can be used to close the context menu. */
81 @Suppress("NotCloseable") // AutoCloseable not available in common.
82 // TODO(grantapher-cm-api-publicize) Make interface public
83 internal interface TextContextMenuSession {
84     /** Closes the text context menu. */
85     fun close()
86 }
87 
88 /** Contains the `object`s used as keys for the compose provided context menu items. */
89 // TODO(grantapher-cm-api-publicize) Make object public
90 internal object TextContextMenuKeys {
91     /** Key for the context menu "Cut" item. */
92     data object CutKey
93 
94     /** Key for the context menu "Copy" item. */
95     data object CopyKey
96 
97     /** Key for the context menu "Paste" item. */
98     data object PasteKey
99 
100     /** Key for the context menu "Select All" item. */
101     data object SelectAllKey
102 
103     /** Key for the context menu "Autofill" item. */
104     data object AutofillKey
105 }
106