1 /*
2 * Copyright (C) 2023 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 com.android.settingslib.spa.widget.ui
18
19 import androidx.compose.foundation.gestures.detectTapGestures
20 import androidx.compose.foundation.layout.Box
21 import androidx.compose.foundation.layout.fillMaxWidth
22 import androidx.compose.foundation.layout.padding
23 import androidx.compose.material3.DropdownMenu
24 import androidx.compose.material3.DropdownMenuItem
25 import androidx.compose.material3.MaterialTheme
26 import androidx.compose.material3.MenuDefaults
27 import androidx.compose.material3.Text
28 import androidx.compose.runtime.Composable
29 import androidx.compose.runtime.getValue
30 import androidx.compose.runtime.mutableStateOf
31 import androidx.compose.runtime.remember
32 import androidx.compose.runtime.setValue
33 import androidx.compose.ui.Modifier
34 import androidx.compose.ui.input.pointer.pointerInput
35 import androidx.compose.ui.platform.LocalClipboardManager
36 import androidx.compose.ui.res.stringResource
37 import androidx.compose.ui.text.AnnotatedString
38 import androidx.compose.ui.unit.DpOffset
39 import com.android.settingslib.spa.framework.theme.SettingsDimension
40
41 @Composable
CopyableBodynull42 fun CopyableBody(body: String) {
43 var expanded by remember { mutableStateOf(false) }
44 var dpOffset by remember { mutableStateOf(DpOffset.Unspecified) }
45
46 Box(modifier = Modifier
47 .fillMaxWidth()
48 .pointerInput(Unit) {
49 detectTapGestures(
50 onLongPress = {
51 dpOffset = DpOffset(it.x.toDp(), it.y.toDp())
52 expanded = true
53 },
54 )
55 }
56 ) {
57 SettingsBody(body)
58
59 DropdownMenu(
60 expanded = expanded,
61 onDismissRequest = { expanded = false },
62 offset = dpOffset,
63 ) {
64 DropdownMenuTitle(body)
65 DropdownMenuCopy(body) { expanded = false }
66 }
67 }
68 }
69
70 @Composable
DropdownMenuTitlenull71 private fun DropdownMenuTitle(text: String) {
72 Text(
73 text = text,
74 modifier = Modifier
75 .padding(MenuDefaults.DropdownMenuItemContentPadding)
76 .padding(
77 top = SettingsDimension.itemPaddingAround,
78 bottom = SettingsDimension.buttonPaddingVertical,
79 ),
80 color = MaterialTheme.colorScheme.primary,
81 style = MaterialTheme.typography.labelMedium,
82 )
83 }
84
85 @Composable
DropdownMenuCopynull86 private fun DropdownMenuCopy(body: String, onCopy: () -> Unit) {
87 val clipboardManager = LocalClipboardManager.current
88 DropdownMenuItem(
89 text = {
90 Text(
91 text = stringResource(android.R.string.copy),
92 color = MaterialTheme.colorScheme.onSurface,
93 style = MaterialTheme.typography.bodyLarge,
94 )
95 },
96 onClick = {
97 onCopy()
98 clipboardManager.setText(AnnotatedString(body))
99 }
100 )
101 }
102