Scoop API

scoop:scoop.ui.component:0.1.0

src/select/scroll_button.scoop

Selected production source · src/select/scroll_button.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.platform.time.MonotonicTimeSource
5import scoop.surface.*
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.window.*
10/** Hover repetition is a native deadline, never a perpetual animation or focus target. */
11internal class SelectScrollButton(
12 private val scope: ComponentScope,
13 private val read: () -> SelectPanelProps,
14 private val down: Boolean
15) {
16 private val area = scope.createArea { _, _ -> refresh() }
17 private val timer = scope.createDeadline { tick() }
18 private val press = scope.press(area, PressOptions(true, false)) { step() }
19 private val glyph =
20 scope.resource {
21 path {
22 moveTo(
23 Point(
24 -4.0,
25 if (down)
26 -2.0
27 else
28 2.0
29 )
30 )
31 lineTo(
32 Point(
33 0.0,
34 if (down)
35 2.0
36 else
37 -2.0
38 )
39 )
40 lineTo(
41 Point(
42 4.0,
43 if (down)
44 -2.0
45 else
46 2.0
47 )
48 )
49 }
50 }
51 private var canScroll = false
52 init {
53 scope.resource { area.scope.observeActivity { refresh() } }
54 scope.observeWindowActivity { _ -> refresh() }
55 }
56 fun update() {
57 press.update(PressOptions(read().popup.options.enabled, false)) { step() }
58 area.scope.enabled = read().popup.options.enabled
59 refresh()
60 }
61 private fun allowed(): Boolean =
62 scope.isMounted &&
63 read().popup.open.value &&
64 read().popup.options.enabled &&
65 scope.nativeWindow.state ==
66 WindowState.Open &&
67 scope.nativeWindow.isFocused &&
68 scope.nativeWindow.isVisible &&
69 !scope.nativeWindow.isOccluded &&
70 area.scope.isInputAllowed &&
71 area.visibleBounds !=
72 None &&
73 canScroll
74 private fun refresh() {
75 if (allowed() && area.scope.isHovered) {
76 if (!timer.isScheduled)
77 timer.schedule(MonotonicTimeSource.now() + Duration.milliseconds(60L))
78 } else
79 timer.cancel()
80 }
81 private fun tick() {
82 if (allowed() && area.scope.isHovered)
83 step()
84 refresh()
85 }
86 private fun step() {
87 if (!allowed())
88 return
89 val input = read()
90 input.popup.scroll.scrollBy(
91 Point(
92 0.0,
93 (
94 if (down)
95 28.0
96 else
97 -28.0
98 ) *
99 input.theme.tokens.typography.body.fontSize /
100 14.0
101 )
102 )
103 }
104 fun scene(context: SceneContext, bounds: Rect?, snapshot: ScrollSnapshot) {
105 val can =
106 bounds !=
107 None &&
108 if (down)
109 snapshot.offset.y < snapshot.maximum.y
110 else
111 snapshot.offset.y > 0.0
112 context.onFrameComplete {
113 frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {
114 canScroll = can;
115 refresh()
116 }
117 }
118 when (bounds) {
119 case Some(val rect) -> {
120 context.place(area, rect)
121 val input = read()
122 val density = input.theme.tokens.typography.body.fontSize / 14.0
123 val color = input.theme.tokens.palette.popover.foreground
124 context.draw {
125 paint -> paint.canvas.withState {
126 canvas ->
127 canvas.translate(rect.x + rect.width / 2.0, rect.y + rect.height / 2.0)
128 canvas.scale(density, density)
129 canvas.drawPath(
130 glyph,
131 Paint(
132 if (can)
133 color
134 else
135 componentForeground(
136 input.theme,
137 color.withAlpha(color.alpha * 0.3),
138 input.theme.tokens.palette.popover.background,
139 3.0
140 ),
141 PaintingStyle.Stroke,
142 1.3333333333333333
143 )
144 )
145 }
146 }
147 }
148 case None -> {}
149 }
150 }