mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-08-29 05:29:08 +00:00
copy button fix
This commit is contained in:
+7
-21
@@ -1139,30 +1139,16 @@ fun ChatLayout(
|
||||
if (appPlatform.isDesktop) {
|
||||
val manager = LocalSelectionManager.current
|
||||
val range = manager?.range
|
||||
if (manager != null && manager.selectionState == SelectionState.Selected && manager.onCopySelection != null && range != null) {
|
||||
if (manager != null && manager.selectionState == SelectionState.Selected && manager.onCopySelection != null && range != null && manager.focusCharRect != Rect.Zero) {
|
||||
val draggingDown = range.startIndex > range.endIndex || (range.startIndex == range.endIndex && range.startOffset < range.endOffset)
|
||||
val gap = with(LocalDensity.current) { 4.dp.toPx() }
|
||||
var buttonSize by remember { mutableStateOf(IntSize.Zero) }
|
||||
// val ls = manager.listState?.value
|
||||
// val itemInfo = ls?.layoutInfo?.visibleItemsInfo?.find { it.index == range.endIndex }
|
||||
// if (ls != null && itemInfo != null && manager.focusCharRect != Rect.Zero) {
|
||||
// val itemWindowY = (ls.layoutInfo.viewportEndOffset - itemInfo.offset - itemInfo.size).toFloat()
|
||||
// val cr = manager.focusCharRect
|
||||
// val vp = manager.viewportPosition
|
||||
// val charX = (if (draggingDown) cr.right else cr.left) - vp.x
|
||||
// val charY = itemWindowY + (if (draggingDown) cr.bottom else cr.top) - vp.y
|
||||
// val x = if (draggingDown) charX
|
||||
// else (charX - buttonSize.width).coerceAtLeast(0f)
|
||||
// val y = if (draggingDown) charY + gap
|
||||
// else (charY - buttonSize.height - gap)
|
||||
// val clampedX = x.coerceIn(0f, (manager.viewportWidth - buttonSize.width).coerceAtLeast(0f))
|
||||
// SelectionCopyButton(
|
||||
// modifier = Modifier
|
||||
// .offset { IntOffset(clampedX.toInt(), y.toInt()) }
|
||||
// .onSizeChanged { buttonSize = it },
|
||||
// onCopy = { manager.onCopySelection?.invoke() }
|
||||
// )
|
||||
// }
|
||||
SelectionCopyButton(
|
||||
modifier = Modifier
|
||||
.offset { manager.copyButtonOffset(draggingDown, gap, buttonSize) }
|
||||
.onSizeChanged { buttonSize = it },
|
||||
onCopy = { manager.onCopySelection?.invoke() }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
@@ -26,6 +26,8 @@ import androidx.compose.ui.layout.positionInWindow
|
||||
import androidx.compose.ui.platform.LocalClipboardManager
|
||||
import androidx.compose.ui.platform.LocalViewConfiguration
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.IntSize
|
||||
import androidx.compose.ui.unit.dp
|
||||
import chat.simplex.common.model.*
|
||||
import chat.simplex.common.platform.*
|
||||
@@ -104,6 +106,22 @@ class SelectionManager {
|
||||
selectionState = SelectionState.Idle
|
||||
}
|
||||
|
||||
fun copyButtonOffset(draggingDown: Boolean, gap: Float, buttonSize: IntSize): IntOffset {
|
||||
val r = range ?: return IntOffset.Zero
|
||||
val ls = listState?.value ?: return IntOffset.Zero
|
||||
val itemInfo = ls.layoutInfo.visibleItemsInfo.find { it.index == r.endIndex }
|
||||
?: return IntOffset(0, -10000)
|
||||
val itemWindowY = (ls.layoutInfo.viewportEndOffset - itemInfo.offset - itemInfo.size).toFloat()
|
||||
val cr = focusCharRect
|
||||
val vp = viewportPosition
|
||||
val charX = (if (draggingDown) cr.right else cr.left) - vp.x
|
||||
val charY = itemWindowY + (if (draggingDown) cr.bottom else cr.top) - vp.y
|
||||
val x = if (draggingDown) charX else (charX - buttonSize.width).coerceAtLeast(0f)
|
||||
val y = if (draggingDown) charY + gap else charY - buttonSize.height - gap
|
||||
val clampedX = x.coerceIn(0f, (viewportWidth - buttonSize.width).coerceAtLeast(0f))
|
||||
return IntOffset(clampedX.toInt(), y.toInt())
|
||||
}
|
||||
|
||||
fun getSelectedText(items: List<MergedItem>, linkMode: SimplexLinkMode): String {
|
||||
val r = range ?: return ""
|
||||
val lo = minOf(r.startIndex, r.endIndex)
|
||||
|
||||
Reference in New Issue
Block a user