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 Dec 28, 2024
1 parent 0019910 commit bb579d5
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)
},

Check warning on line 59 in ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt

View check run for this annotation

Codecov / codecov/patch

ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt#L58-L59

Added lines #L58 - L59 were not covered by tests
transferData = {
val clipData = ClipData.newPlainText(
"cellState",
RunLengthEncodedCellStateSerializer.serializeToString(getCellState())
.joinToString("\n"),

Check warning on line 64 in ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt

View check run for this annotation

Codecov / codecov/patch

ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt#L61-L64

Added lines #L61 - L64 were not covered by tests
)

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

Check warning on line 69 in ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt

View check run for this annotation

Codecov / codecov/patch

ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt#L67-L69

Added lines #L67 - L69 were not covered by tests
flags = if (Build.VERSION.SDK_INT >= 24) {
View.DRAG_FLAG_GLOBAL

Check warning on line 71 in ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt

View check run for this annotation

Codecov / codecov/patch

ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt#L71

Added line #L71 was not covered by tests
} else {
0

Check warning on line 73 in ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt

View check run for this annotation

Codecov / codecov/patch

ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt#L73

Added line #L73 was not covered by tests
},
)

Check warning on line 75 in ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt

View check run for this annotation

Codecov / codecov/patch

ui-cells/src/androidMain/kotlin/com/alexvanyo/composelife/ui/cells/CellStateDragAndDrop.android.kt#L75

Added line #L75 was not covered by tests
},
)
}

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 bb579d5

Please sign in to comment.