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 d70ae71
Show file tree
Hide file tree
Showing 3 changed files with 36 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,49 @@ 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 {
val graphicsLayer = rememberGraphicsLayer()
return drawWithCache {
graphicsLayer.record { drawContent() }
onDrawWithContent { drawLayer(graphicsLayer) }
}
.dragAndDropSource(
drawDragDecoration = {
drawLayer(graphicsLayer)
},

Check warning on line 57 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#L56-L57

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

Check warning on line 62 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#L59-L62

Added lines #L59 - L62 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 67 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#L65-L67

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

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#L69

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

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
},
)

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
},
)
}

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 d70ae71

Please sign in to comment.