Scoop API

scoop:scoop.ui.component:0.1.0

src/overlays/arrow.scoop

Selected production source · src/overlays/arrow.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.min
5import scoop.math.max
6import scoop.ui.*
8public class PopupArrow public constructor(
9 public val width: Double = 10.0,
10 public val height: Double = 5.0,
11 public val paint: Paint? = None
12) {
13 init {
14 nonnegative(width, "invalid popup arrow width")
15 nonnegative(height, "invalid popup arrow height")
16 }
19internal class PopupArrowInk(
20 val width: Double,
21 val height: Double,
22 val inset: Double,
23 val paint: Paint
26internal fun popupArrowInk(
27 arrow: PopupArrow?,
28 density: Double,
29 background: Paint?
30): PopupArrowInk? {
31 val value = arrow ?: return None
32 val paint = value.paint ?: background ?: return None
33 return Some(PopupArrowInk(value.width * density, value.height * density, 8.0 * density, paint))
36/** Rounded tip on a unit triangle; the same geometry is used on every physical side. */
37internal fun popupArrowPath(): Path =
38 path {
39 moveTo(Point(-0.5, -0.1))
40 lineTo(Point(-0.12, 0.76))
41 quadraticTo(Point(0.0, 1.0), Point(0.12, 0.76))
42 lineTo(Point(0.5, -0.1))
43 closeContour()
44 }
46internal fun drawPopupArrow(context: SceneContext, shape: Path, ink: PopupArrowInk) {
47 val layout = context.overlayLayout ?: return
48 val anchor = layout.anchor ?: return
49 val side = layout.side ?: return
50 val size = context.size
51 val vertical = side == OverlaySide.Top || side == OverlaySide.Bottom
52 val edge =
53 if (vertical)
54 size.width
55 else
56 size.height
57 val width = min(ink.width, max(0.0, edge - ink.inset * 2.0))
58 if (width == 0.0 || ink.height == 0.0)
59 return
60 val wanted =
61 if (vertical)
62 anchor.x + anchor.width / 2.0 - layout.bounds.x
63 else
64 anchor.y + anchor.height / 2.0 - layout.bounds.y
65 val center = max(width / 2.0 + ink.inset, min(wanted, edge - width / 2.0 - ink.inset))
66 context.draw {
67 paint -> paint.canvas.withState {
68 canvas ->
69 when (side) {
70 case Top -> canvas.translate(center, size.height)
71 case Bottom -> {
72 canvas.translate(center, 0.0);
73 canvas.scale(1.0, -1.0)
74 }
75 case Left -> {
76 canvas.translate(size.width, center);
77 canvas.rotate(-1.5707963267948966)
78 }
79 case Right -> {
80 canvas.translate(0.0, center);
81 canvas.rotate(1.5707963267948966)
82 }
83 else -> return@withState
84 }
85 canvas.scale(width, ink.height)
86 canvas.drawPath(shape, ink.paint)
87 }
88 }