Scoop API

scoop:scoop.graphics:0.1.0

src/native/geometry_hit.scoop

Selected production source · src/native/geometry_hit.scoop

1package scoop.graphics.native
3import scoop.graphics.GraphicsException
4import scoop.graphics.Point
5import scoop.graphics.Rect
7/** Native query status: 1 success, 0 unavailable, -1 invalid argument, -2 native failure. */
8@CLayout
9public struct NativeGeometryPoint(val status: Int, val position: Point)
11@CLayout
12public struct NativeGeometryRect(val status: Int, val rect: Rect)
14/**
15 * A borrowed native snapshot. Retain before saving or handing off; release each owned reference.
16 * The paired functions support concurrent native queries without entering Scoop or its runtime.
17 */
18@CLayout
19public struct NativeGeometry(
20 val context: Ptr<Unit>,
21 val retain: FunPtr<(Ptr<Unit>) -> Unit>,
22 val release: FunPtr<(Ptr<Unit>) -> Unit>,
23 val contains: FunPtr<(Ptr<Unit>, Double, Double) -> Int>,
24 val toLocal: FunPtr<(Ptr<Unit>, Double, Double) -> NativeGeometryPoint>,
25 /** clipped is 0 for full transformed bounds or 1 for the exact visible intersection. */
26 val mapRect: FunPtr<(Ptr<Unit>, Rect, Int) -> NativeGeometryRect>
29@CLayout
30private struct NativeGeometryExport(
31 val context: Ptr<Unit>?,
32 val retain: FunPtr<(Ptr<Unit>) -> Unit>,
33 val release: FunPtr<(Ptr<Unit>) -> Unit>,
34 val contains: FunPtr<(Ptr<Unit>, Double, Double) -> Int>,
35 val toLocal: FunPtr<(Ptr<Unit>, Double, Double) -> NativeGeometryPoint>,
36 val mapRect: FunPtr<(Ptr<Unit>, Rect, Int) -> NativeGeometryRect>
39@Unsafe
40internal fun <T> withGeometry(pointer: Ptr<Unit>, block: (NativeGeometry) -> T): T {
41 val exported = nativeGeometryExport(pointer)
42 val context = exported.context ?: throw GraphicsException("export native hit geometry")
43 try {
44 return block(
45 NativeGeometry(
46 context,
47 exported.retain,
48 exported.release,
49 exported.contains,
50 exported.toLocal,
51 exported.mapRect
52 )
53 )
54 } finally {
55 exported.release(context)
56 }
59@Extern(name = "scoop_graphics_geometry_export")
60private fun nativeGeometryExport(geometry: Ptr<Unit>): NativeGeometryExport