Scoop API

scoop:scoop.window:0.1.0

src/drag_drop/preview.scoop

Selected production source · src/drag_drop/preview.scoop

1package scoop.window
3/** A native drag image and its pointer offset from the logical top-left corner. */
4public class DragPreview public constructor(
5 public val image: DesktopImage,
6 public val hotSpot: LogicalPosition
7) {
8 init {
9 checkPosition(hotSpot)
10 }
12 internal fun representation(scale: Double): DesktopImageRepresentation {
13 val target = image.size.toPhysical(scale)
14 var best = image.representations[0L]
15 var distance = Long.MAX_VALUE
16 for (candidate in image.representations) {
17 val dx = candidate.size.width.toLong() - target.width.toLong()
18 val dy = candidate.size.height.toLong() - target.height.toLong()
19 val difference = dx * dx + dy * dy
20 if (
21 difference <
22 distance ||
23 (difference == distance && candidate.size.width > best.size.width)
24 ) {
25 best = candidate
26 distance = difference
27 }
28 }
29 return best
30 }
33internal fun prepareDragPreview(source: Ptr<Unit>, preview: DragPreview, scale: Double) {
34 val image = preview.image
35 val rep = preview.representation(scale)
36 @Unsafe
37 {
38 val status =
39 rep.pixels.withDataPointer {
40 bytes,
41 _ -> @Unsafe
42 {
43 return@withDataPointer nativeDragPreview(
44 source,
45 rep.size.width,
46 rep.size.height,
47 bytes,
48 image.size.width,
49 image.size.height,
50 preview.hotSpot.x,
51 preview.hotSpot.y
52 )
53 }
54 }
55 checkNativeStatus(status, "set data drag preview")
56 }
59@Extern(name = "scoop_window_drag_preview")
60private fun nativeDragPreview(
61 source: Ptr<Unit>,
62 width: Int,
63 height: Int,
64 bytes: Ptr<UInt8>,
65 logicalWidth: Double,
66 logicalHeight: Double,
67 hotX: Double,
68 hotY: Double
69): Long