Update clipboard pinning

This commit is contained in:
Aleksandras Kostarevas 2024-06-15 17:11:57 +03:00
parent 494e27f482
commit a237248fc9
2 changed files with 48 additions and 11 deletions

View File

@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
<path android:fillColor="@android:color/white" android:fillType="evenOdd" android:pathData="M16,9V4l1,0c0.55,0 1,-0.45 1,-1v0c0,-0.55 -0.45,-1 -1,-1H7C6.45,2 6,2.45 6,3v0c0,0.55 0.45,1 1,1l1,0v5c0,1.66 -1.34,3 -3,3h0v2h5.97v7l1,1l1,-1v-7H19v-2h0C17.34,12 16,10.66 16,9z"/>
</vector>

View File

@ -4,8 +4,12 @@ import android.content.ClipDescription
import android.content.ClipboardManager
import android.content.Context
import android.net.Uri
import android.os.Build
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
@ -25,7 +29,9 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
@ -45,6 +51,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import org.futo.inputmethod.latin.R
import org.futo.inputmethod.latin.common.Constants
import org.futo.inputmethod.latin.uix.Action
import org.futo.inputmethod.latin.uix.ActionWindow
import org.futo.inputmethod.latin.uix.PersistentActionState
@ -87,14 +94,21 @@ data class ClipboardEntry(
val mimeTypes: List<String>
)
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ClipboardEntryView(clipboardEntry: ClipboardEntry, onPaste: (ClipboardEntry) -> Unit, onRemove: (ClipboardEntry) -> Unit, onPin: (ClipboardEntry) -> Unit) {
fun ClipboardEntryView(modifier: Modifier, clipboardEntry: ClipboardEntry, onPaste: (ClipboardEntry) -> Unit, onRemove: (ClipboardEntry) -> Unit, onPin: (ClipboardEntry) -> Unit) {
Surface(
color = MaterialTheme.colorScheme.surfaceVariant,
border = BorderStroke(2.dp, MaterialTheme.colorScheme.outlineVariant),
modifier = Modifier.padding(2.dp),
modifier = modifier.padding(2.dp)
.combinedClickable(
interactionSource = remember { MutableInteractionSource() },
indication = androidx.compose.material.ripple.rememberRipple(),
enabled = true,
onClick = { onPaste(clipboardEntry) },
onLongClick = { onPin(clipboardEntry) }
),
shape = RoundedCornerShape(8.dp),
onClick = { onPaste(clipboardEntry) }
) {
Column {
Row(modifier = Modifier.padding(0.dp)) {
@ -102,12 +116,14 @@ fun ClipboardEntryView(clipboardEntry: ClipboardEntry, onPaste: (ClipboardEntry)
onPin(clipboardEntry)
}, modifier = Modifier.size(32.dp)) {
Icon(
painterResource(id = R.drawable.unlock), contentDescription = "Pin",
painterResource(id = R.drawable.push_pin),
contentDescription = "Pin",
tint = if(clipboardEntry.pinned) {
MaterialTheme.colorScheme.onSurfaceVariant
} else {
MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.25f)
}, modifier = Modifier.size(16.dp)
MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.2f)
},
modifier = Modifier.size(16.dp)
)
}
@ -145,7 +161,7 @@ fun ClipboardEntryViewPreview() {
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
items(sampleText.size) {
ClipboardEntryView(clipboardEntry = ClipboardEntry(0L, false, sampleText[it], null, listOf()), onPin = {}, onPaste = {}, onRemove = {})
ClipboardEntryView(modifier = Modifier, clipboardEntry = ClipboardEntry(0L, it % 2 == 0, sampleText[it], null, listOf()), onPin = {}, onPaste = {}, onRemove = {})
}
}
}
@ -176,7 +192,11 @@ class ClipboardHistoryManager(val context: Context, val coroutineScope: Lifecycl
val text = clip?.getItemAt(0)?.coerceToText(context)?.toString()
val uri = clip?.getItemAt(0)?.uri
val timestamp = clip?.description?.timestamp ?: System.currentTimeMillis()
val timestamp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
clip?.description?.timestamp
} else {
null
} ?: System.currentTimeMillis()
val mimeTypes = List(clip?.description?.mimeTypeCount ?: 0) {
clip?.description?.getMimeType(it)
@ -309,6 +329,7 @@ class ClipboardHistoryManager(val context: Context, val coroutineScope: Lifecycl
}
@OptIn(ExperimentalFoundationApi::class)
val ClipboardHistoryAction = Action(
icon = R.drawable.clipboard,
name = R.string.clipboard_manager_action_title,
@ -330,6 +351,7 @@ val ClipboardHistoryAction = Action(
@Composable
override fun WindowContents(keyboardShown: Boolean) {
val view = LocalView.current
val clipboardHistory = useDataStore(ClipboardHistoryEnabled, blocking = true)
if(!clipboardHistory.value) {
ScrollableList {
@ -349,10 +371,17 @@ val ClipboardHistoryAction = Action(
verticalItemSpacing = 4.dp,
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
items(clipboardHistoryManager.clipboardHistory.size) { r_i ->
items(clipboardHistoryManager.clipboardHistory.size, key = { r_i ->
val i = clipboardHistoryManager.clipboardHistory.size - r_i - 1
val entry = clipboardHistoryManager.clipboardHistory[i]
ClipboardEntryView(clipboardEntry = entry, onPaste = {
entry.text ?: i
}) { r_i ->
val i = clipboardHistoryManager.clipboardHistory.size - r_i - 1
val entry = clipboardHistoryManager.clipboardHistory[i]
ClipboardEntryView(
modifier = Modifier.animateItemPlacement(),
clipboardEntry = entry, onPaste = {
if (it.uri != null) {
if (!manager.typeUri(it.uri, it.mimeTypes)) {
val toast = Toast.makeText(
@ -366,10 +395,13 @@ val ClipboardHistoryAction = Action(
manager.typeText(it.text)
}
clipboardHistoryManager.onPaste(it)
manager.performHapticAndAudioFeedback(Constants.CODE_OUTPUT_TEXT, view)
}, onRemove = {
clipboardHistoryManager.onRemove(it)
manager.performHapticAndAudioFeedback(Constants.CODE_TAB, view)
}, onPin = {
clipboardHistoryManager.onPin(it)
manager.performHapticAndAudioFeedback(Constants.CODE_TAB, view)
})
}
}