From 10a55f1ba21622373626d866368f54d1446783d9 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Mon, 19 Feb 2024 14:20:45 +0800 Subject: [PATCH] :bug: Pasteboard preview list only shows supported types (#347) --- .../clipevery/ui/clip/ClipPreviewItemView.kt | 102 ++++++++++++++---- .../com/clipevery/ui/clip/ClipPreviewView.kt | 69 ------------ .../com/clipevery/ui/clip/ClipPreviewsView.kt | 4 +- 3 files changed, 83 insertions(+), 92 deletions(-) delete mode 100644 composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewView.kt diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewItemView.kt b/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewItemView.kt index 64ff6cf6f..d9c10b4aa 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewItemView.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewItemView.kt @@ -1,7 +1,9 @@ package com.clipevery.ui.clip import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.material.MaterialTheme @@ -15,34 +17,90 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.input.pointer.PointerEventType import androidx.compose.ui.input.pointer.onPointerEvent import androidx.compose.ui.unit.dp +import com.clipevery.dao.clip.ClipAppearItem +import com.clipevery.dao.clip.ClipContent import com.clipevery.dao.clip.ClipData +import com.clipevery.dao.clip.ClipType +import kotlin.reflect.KClass +import kotlin.reflect.cast + +fun ClipData.getClipItem(kclass: KClass): T? { + return ClipContent.getClipItem(this.clipAppearContent)?.let { + if (kclass.isInstance(it)) { + kclass.cast(it) + } else { + null + } + } +} + +fun ClipData.getClipItem(): ClipAppearItem? { + return ClipContent.getClipItem(this.clipAppearContent) +} @OptIn(ExperimentalComposeUiApi::class) @Composable -fun ClipPreviewItemView(clipData: ClipData) { - - var hover by remember { mutableStateOf(false) } - val backgroundColor = if (hover) MaterialTheme.colors.secondaryVariant else MaterialTheme.colors.background - - Row(modifier = Modifier - .fillMaxWidth() - .height(64.dp) - .onPointerEvent( - eventType = PointerEventType.Enter, - onEvent = { - hover = true - } - ) - .onPointerEvent( - eventType = PointerEventType.Exit, - onEvent = { - hover = false +fun ClipPreviewItemView(clipData: ClipData, clipContent: @Composable ClipAppearItem.() -> Unit) { + + clipData.getClipItem()?.let { + + if (it.getClipType() == ClipType.TEXT) { + + var hover by remember { mutableStateOf(false) } + val backgroundColor = if (hover) MaterialTheme.colors.secondaryVariant else MaterialTheme.colors.background + + Row(modifier = Modifier + .fillMaxWidth() + .height(64.dp) + .onPointerEvent( + eventType = PointerEventType.Enter, + onEvent = { + hover = true + } + ) + .onPointerEvent( + eventType = PointerEventType.Exit, + onEvent = { + hover = false + } + ) + .background(backgroundColor)) { + + Column( + modifier = Modifier.fillMaxWidth() + .height(150.dp) + ) { + Row( + modifier = Modifier.fillMaxWidth() + .height(120.dp) + ) { + it.clipContent() + Column(modifier = Modifier.fillMaxHeight()) { + Row() { + + } + } + } + Row( + modifier = Modifier.fillMaxWidth() + .background(color = MaterialTheme.colors.surface) + .height(30.dp) + ) { + + } + } } - ) - .background(backgroundColor)) { + } + } +} - ClipPreviewView(clipData) { - ClipPreview(clipData) +@Composable +fun ClipSpecificPreviewItemView(clipData: ClipData) { + if (clipData.preCreate) { + // todo preCreate + } else { + when(clipData.clipType) { + ClipType.TEXT -> TextPreviewView(clipData) } } } diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewView.kt b/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewView.kt deleted file mode 100644 index 1a686e245..000000000 --- a/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewView.kt +++ /dev/null @@ -1,69 +0,0 @@ -package com.clipevery.ui.clip - -import androidx.compose.foundation.background -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxHeight -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.material.MaterialTheme -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.unit.dp -import com.clipevery.dao.clip.ClipAppearItem -import com.clipevery.dao.clip.ClipContent -import com.clipevery.dao.clip.ClipData -import com.clipevery.dao.clip.ClipType -import kotlin.reflect.KClass -import kotlin.reflect.cast - -fun ClipData.getClipItem(kclass: KClass): T? { - return ClipContent.getClipItem(this.clipAppearContent)?.let { - if (kclass.isInstance(it)) { - kclass.cast(it) - } else { - null - } - } -} - -fun ClipData.getClipItem(): ClipAppearItem? { - return ClipContent.getClipItem(this.clipAppearContent) -} - -@Composable -fun ClipPreviewView(clipData: ClipData, clipContent: @Composable ClipAppearItem.() -> Unit) { - clipData.getClipItem()?.let { - Column(modifier = Modifier.fillMaxWidth() - .height(150.dp) - ) { - Row(modifier = Modifier.fillMaxWidth() - .height(120.dp) - ) { - it.clipContent() - Column(modifier = Modifier.fillMaxHeight()) { - Row() { - - } - } - } - Row(modifier = Modifier.fillMaxWidth() - .background(color = MaterialTheme.colors.surface) - .height(30.dp) - ) { - - } - } - } -} - -@Composable -fun ClipPreview(clipData: ClipData) { - if (clipData.preCreate) { - // todo preCreate - } else { - when(clipData.clipType) { - ClipType.TEXT -> TextPreviewView(clipData) - } - } -} \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewsView.kt b/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewsView.kt index 7aff82dcc..b43953f80 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewsView.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/ClipPreviewsView.kt @@ -75,7 +75,9 @@ fun ClipPreviewsView() { modifier = Modifier.wrapContentHeight() ) { items(rememberClipDataList) { clipData -> - ClipPreviewItemView(clipData) + ClipPreviewItemView(clipData) { + ClipSpecificPreviewItemView(clipData) + } } }