Skip to content

Commit

Permalink
Fix initial Coil rendering with graphicsLayer
Browse files Browse the repository at this point in the history
Fixes #2282
  • Loading branch information
alexvanyo committed Feb 4, 2025
1 parent 176a1ec commit a37cfbe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.content.ClipDescription
import android.os.Build
import android.view.View
import androidx.compose.foundation.draganddrop.dragAndDropSource
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -30,36 +31,51 @@ import androidx.compose.ui.draganddrop.DragAndDropEvent
import androidx.compose.ui.draganddrop.DragAndDropTransferData
import androidx.compose.ui.draganddrop.mimeTypes
import androidx.compose.ui.draganddrop.toAndroidDragEvent
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.layer.drawLayer
import androidx.compose.ui.graphics.rememberGraphicsLayer
import com.alexvanyo.composelife.model.CellState
import com.alexvanyo.composelife.model.CellStateParser
import com.alexvanyo.composelife.model.DeserializationResult
import com.alexvanyo.composelife.model.RunLengthEncodedCellStateSerializer
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first

@Composable
actual fun Modifier.cellStateDragAndDropSource(
getCellState: () -> CellState,
): Modifier =
dragAndDropSource(
transferData = {
val clipData = ClipData.newPlainText(
"cellState",
RunLengthEncodedCellStateSerializer.serializeToString(getCellState())
.joinToString("\n"),
)
): Modifier {
// TODO: Remove graphics layer workaround once default drag decoration works with Coil.
// https://github.com/coil-kt/coil/issues/2150
val graphicsLayer = rememberGraphicsLayer()
return drawWithCache {
graphicsLayer.record { drawContent() }
onDrawWithContent { drawLayer(graphicsLayer) }
}
.dragAndDropSource(
drawDragDecoration = {
drawLayer(graphicsLayer)
},
transferData = {
val clipData = ClipData.newPlainText(
"cellState",
RunLengthEncodedCellStateSerializer.serializeToString(getCellState())
.joinToString("\n"),
)

DragAndDropTransferData(
clipData = clipData,
localState = clipData,
flags = if (Build.VERSION.SDK_INT >= 24) {
View.DRAG_FLAG_GLOBAL
} else {
0
},
)
},
)
DragAndDropTransferData(
clipData = clipData,
localState = clipData,
flags = if (Build.VERSION.SDK_INT >= 24) {
View.DRAG_FLAG_GLOBAL
} else {
0
},
)
},
)
}

internal actual fun cellStateShouldStartDragAndDrop(event: DragAndDropEvent): Boolean =
event.mimeTypes().contains(ClipDescription.MIMETYPE_TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alexvanyo.composelife.ui.cells

import androidx.compose.foundation.draganddrop.dragAndDropSource
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -41,6 +42,7 @@ import java.awt.dnd.DropTargetDragEvent
import java.awt.dnd.DropTargetDropEvent

@OptIn(ExperimentalComposeUiApi::class)
@Composable
actual fun Modifier.cellStateDragAndDropSource(getCellState: () -> CellState): Modifier =
dragAndDropSource { offset ->
DragAndDropTransferData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.alexvanyo.composelife.model.CellState
/**
* A [Modifier] for a drag-and-drop source for a [CellState].
*/
@Composable
expect fun Modifier.cellStateDragAndDropSource(getCellState: () -> CellState): Modifier

/**
Expand Down

0 comments on commit a37cfbe

Please sign in to comment.