scoop:scoop.ui:0.1.0
src/interaction/drag_preview.scoop
Selected production source · src/interaction/drag_preview.scoop
1
package scoop.ui3
import scoop.graphics.*4
import scoop.math.max5
import scoop.math.min6
import scoop.surface.InputArea7
import scoop.window.*9
/** Draws a native preview in source-local coordinates at the current display scale. */10
public fun DragSourceStart.renderPreview(bounds: Rect, draw: (Canvas) -> Unit): DragPreview =11
rasterDragPreview(bounds, start, pointer.scaleFactor, draw)13
private fun rasterDragPreview(14
bounds: Rect,15
anchor: Point,16
scale: Double,17
draw: (Canvas) -> Unit18
): DragPreview {19
val size = LogicalSize(bounds.width, bounds.height)20
val pixels = size.toPhysical(scale)21
return RasterSurface(pixels.width, pixels.height).use {22
surface ->23
surface.draw {24
canvas ->25
canvas.clear(Color.Transparent)26
canvas.scale(27
pixels.width.toDouble() / bounds.width,28
pixels.height.toDouble() / bounds.height29
)30
canvas.translate(-bounds.x, -bounds.y)31
draw(canvas)32
}33
val bytes = surface.readPixels()34
val rep =35
DesktopImageRepresentation(pixels, Array<UInt8>(bytes.size) { bytes[it].toUByte() })36
DragPreview(37
DesktopImage(size, [rep]),38
LogicalPosition(anchor.x - bounds.x, anchor.y - bounds.y)39
)40
}41
}43
/** Paints only the visible selected regions, keeping the press anchored even in a scrolled or transformed area. */44
public fun DragSourceStart.renderSelectionPreview(45
regions: Array<Rect>,46
area: InputArea,47
draw: (Canvas) -> Unit48
): DragPreview? {49
val viewport = area.visibleBounds ?: return None50
val origin = area.toWindow(Rect(0.0, 0.0, 0.0, 0.0))?.origin ?: return None51
val x = area.toWindow(Rect(1.0, 0.0, 0.0, 0.0))!!.origin52
val y = area.toWindow(Rect(0.0, 1.0, 0.0, 0.0))!!.origin53
val transform =54
Transform(55
x.x - origin.x,56
x.y - origin.y,57
y.x - origin.x,58
y.y - origin.y,59
origin.x,60
origin.y61
)62
val anchor = transform.map(start)63
return PathBuilder().use {64
builder ->65
var left = Double.POSITIVE_INFINITY66
var top = Double.POSITIVE_INFINITY67
var right = Double.NEGATIVE_INFINITY68
var bottom = Double.NEGATIVE_INFINITY69
for (region in regions) {70
val rect = area.toWindow(region)!!71
if (intersectReveal(rect, viewport) == None)72
continue73
left = min(left, rect.x)74
top = min(top, rect.y)75
right = max(right, rect.x + rect.width)76
bottom = max(bottom, rect.y + rect.height)77
builder.addRect(region)78
}79
if (right <= left || bottom <= top)80
return@use None81
val horizontal = cropPreviewAxis(left, right, viewport.x, viewport.width, anchor.x, 360.0)82
val vertical = cropPreviewAxis(top, bottom, viewport.y, viewport.height, anchor.y, 240.0)83
val bounds = Rect(horizontal.start, vertical.start, horizontal.size, vertical.size)84
builder.build().use {85
shape -> Some(86
rasterDragPreview(bounds, anchor, pointer.scaleFactor) {87
canvas ->88
canvas.withState {89
content ->90
content.concat(transform)91
content.clipPath(shape)92
content.saveLayer(opacity = 0.8)93
try {94
draw(content)95
} finally {96
content.restore()97
}98
}99
horizontal.fade(canvas, bounds, Point(bounds.x + bounds.width, bounds.y))100
vertical.fade(canvas, bounds, Point(bounds.x, bounds.y + bounds.height))101
}102
)103
}104
}105
}107
private struct PreviewAxis(108
val start: Double,109
val size: Double,110
val cutStart: Boolean,111
val cutEnd: Boolean112
) {113
fun fade(canvas: Canvas, bounds: Rect, end: Point) {114
if (!cutStart && !cutEnd)115
return116
val ramp = min(24.0 / size, 0.25)117
val mask =118
Gradient.linear(119
bounds.origin,120
end,121
[122
GradientStop(123
0.0,124
if (cutStart)125
Color.Transparent126
else127
Color.White128
),129
GradientStop(ramp, Color.White),130
GradientStop(1.0 - ramp, Color.White),131
GradientStop(132
1.0,133
if (cutEnd)134
Color.Transparent135
else136
Color.White137
)138
]139
)140
canvas.saveLayer(blendMode = BlendMode.DstIn, bounds = Some(bounds))141
try {142
canvas.drawRect(bounds, Paint(gradient = Some(mask)))143
} finally {144
canvas.restore()145
}146
}147
}149
private fun cropPreviewAxis(150
first: Double,151
last: Double,152
viewportStart: Double,153
viewportSize: Double,154
anchor: Double,155
limit: Double156
): PreviewAxis {157
val visibleStart = max(first, viewportStart)158
val visibleEnd = min(last, viewportStart + viewportSize)159
val visibleSize = visibleEnd - visibleStart160
val size = min(limit, visibleSize)161
val overflow = visibleSize - size162
val offset = max(0.0, min(overflow, anchor - visibleStart - size * 0.5))163
return PreviewAxis(164
visibleStart + offset,165
size,166
first < visibleStart || offset > 0.0,167
last > visibleEnd || offset < overflow168
)169
}