scoop:scoop.window:0.1.0
src/drag_drop/preview.scoop
Selected production source · src/drag_drop/preview.scoop
1
package scoop.window3
/** A native drag image and its pointer offset from the logical top-left corner. */4
public class DragPreview public constructor(5
public val image: DesktopImage,6
public val hotSpot: LogicalPosition7
) {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_VALUE16
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 * dy20
if (21
difference <22
distance ||23
(difference == distance && candidate.size.width > best.size.width)24
) {25
best = candidate26
distance = difference27
}28
}29
return best30
}31
}33
internal fun prepareDragPreview(source: Ptr<Unit>, preview: DragPreview, scale: Double) {34
val image = preview.image35
val rep = preview.representation(scale)36
@Unsafe37
{38
val status =39
rep.pixels.withDataPointer {40
bytes,41
_ -> @Unsafe42
{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.y52
)53
}54
}55
checkNativeStatus(status, "set data drag preview")56
}57
}59
@Extern(name = "scoop_window_drag_preview")60
private 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: Double69
): Long