Scoop API

scoop:scoop.ui:0.1.0

src/interaction/drag_preview.scoop

Selected production source · src/interaction/drag_preview.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.math.max
5import scoop.math.min
6import scoop.surface.InputArea
7import scoop.window.*
9/** Draws a native preview in source-local coordinates at the current display scale. */
10public fun DragSourceStart.renderPreview(bounds: Rect, draw: (Canvas) -> Unit): DragPreview =
11 rasterDragPreview(bounds, start, pointer.scaleFactor, draw)
13private fun rasterDragPreview(
14 bounds: Rect,
15 anchor: Point,
16 scale: Double,
17 draw: (Canvas) -> Unit
18): 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.height
29 )
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 }
43/** Paints only the visible selected regions, keeping the press anchored even in a scrolled or transformed area. */
44public fun DragSourceStart.renderSelectionPreview(
45 regions: Array<Rect>,
46 area: InputArea,
47 draw: (Canvas) -> Unit
48): DragPreview? {
49 val viewport = area.visibleBounds ?: return None
50 val origin = area.toWindow(Rect(0.0, 0.0, 0.0, 0.0))?.origin ?: return None
51 val x = area.toWindow(Rect(1.0, 0.0, 0.0, 0.0))!!.origin
52 val y = area.toWindow(Rect(0.0, 1.0, 0.0, 0.0))!!.origin
53 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.y
61 )
62 val anchor = transform.map(start)
63 return PathBuilder().use {
64 builder ->
65 var left = Double.POSITIVE_INFINITY
66 var top = Double.POSITIVE_INFINITY
67 var right = Double.NEGATIVE_INFINITY
68 var bottom = Double.NEGATIVE_INFINITY
69 for (region in regions) {
70 val rect = area.toWindow(region)!!
71 if (intersectReveal(rect, viewport) == None)
72 continue
73 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 None
81 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 }
107private struct PreviewAxis(
108 val start: Double,
109 val size: Double,
110 val cutStart: Boolean,
111 val cutEnd: Boolean
112) {
113 fun fade(canvas: Canvas, bounds: Rect, end: Point) {
114 if (!cutStart && !cutEnd)
115 return
116 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.Transparent
126 else
127 Color.White
128 ),
129 GradientStop(ramp, Color.White),
130 GradientStop(1.0 - ramp, Color.White),
131 GradientStop(
132 1.0,
133 if (cutEnd)
134 Color.Transparent
135 else
136 Color.White
137 )
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 }
149private fun cropPreviewAxis(
150 first: Double,
151 last: Double,
152 viewportStart: Double,
153 viewportSize: Double,
154 anchor: Double,
155 limit: Double
156): PreviewAxis {
157 val visibleStart = max(first, viewportStart)
158 val visibleEnd = min(last, viewportStart + viewportSize)
159 val visibleSize = visibleEnd - visibleStart
160 val size = min(limit, visibleSize)
161 val overflow = visibleSize - size
162 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 < overflow
168 )