Added option to copy git ID to clipboard

This commit is contained in:
Abdelilah El Aissaoui 2022-07-20 12:08:30 +02:00
parent fe77505e7d
commit 57e8482218
2 changed files with 42 additions and 1 deletions

View File

@ -1,9 +1,15 @@
package app.extensions
import app.logging.printLog
import java.awt.Desktop
import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.StringSelection
import java.net.URI
import java.nio.file.FileSystems
private const val TAG = "SystemUtils"
val systemSeparator: String by lazy {
FileSystems.getDefault().separator
}
@ -15,4 +21,15 @@ fun openUrlInBrowser(url: String) {
println("Failed to open URL in browser")
ex.printStackTrace()
}
}
fun copyInBrowser(textToCopy: String) {
try {
val selection = StringSelection(textToCopy)
val clipboard: Clipboard = Toolkit.getDefaultToolkit().systemClipboard
clipboard.setContents(selection, selection)
} catch (ex: Exception) {
printLog(TAG, "Failed to copy text")
ex.printStackTrace()
}
}

View File

@ -22,6 +22,9 @@ import app.ui.components.TooltipText
import app.ui.context_menu.commitedChangesEntriesContextMenuItems
import app.viewmodels.CommitChangesStatus
import app.viewmodels.CommitChangesViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.eclipse.jgit.diff.DiffEntry
import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.lib.PersonIdent
@ -134,6 +137,9 @@ fun Author(
id: ObjectId,
author: PersonIdent,
) {
var copied by remember(id) { mutableStateOf(false) }
val scope = rememberCoroutineScope()
Row(
modifier = Modifier
.fillMaxWidth()
@ -160,14 +166,32 @@ fun Author(
tooltipTitle = author.emailAddress,
)
Row {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = id.abbreviate(7).name(),
color = MaterialTheme.colors.secondaryTextColor,
maxLines = 1,
style = MaterialTheme.typography.body2,
modifier = Modifier.handMouseClickable {
scope.launch {
copyInBrowser(id.name)
copied = true
delay(2000) // 2s
copied = false
}
}
)
if (copied) {
Text(
text = "Copied!",
color = MaterialTheme.colors.primaryVariant,
maxLines = 1,
style = MaterialTheme.typography.caption,
modifier = Modifier.padding(start = 4.dp),
)
}
Spacer(modifier = Modifier.weight(1f, fill = true))