Scoop API

scoop:scoop.ui.component:0.1.0

src/overlays/hover_layout.scoop

Selected production source · src/overlays/hover_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.min
5import scoop.math.max
6import scoop.surface.*
7import scoop.ui.*
8import scoop.window.*
10/** Connects the persistent trigger behavior to its temporarily mounted portal area. */
11internal class HoverPopupLink {
12 private var behavior: Hover? = None
13 private var content: InputArea? = None
14 fun bind(value: Hover) {
15 behavior = Some(value)
16 }
17 fun unbind(value: Hover) {
18 when (behavior) {
19 case Some(val current) -> if (current === value)
20 behavior = None
21 case None -> {}
22 }
23 }
24 fun setContent(value: InputArea?) {
25 content = value
26 val current = behavior ?: return
27 if (current.isClosed)
28 return
29 current.setContent(
30 when (value) {
31 case Some(val area) -> [area]
32 case None -> []
33 }
34 )
35 }
36 fun containsContent(target: InputScope): Boolean {
37 val area = content ?: return false
38 var cursor: InputScope? = Some(target)
39 while (true) {
40 val current = cursor ?: return false
41 if (current === area.scope)
42 return true
43 cursor = current.parent
44 }
45 }
46 fun dismiss(fallback: () -> Unit = {}) {
47 when (behavior) {
48 case Some(val current) -> if (!current.isClosed) {
49 current.dismiss();
50 return
51 }
52 case None -> {}
53 }
54 fallback()
55 }
58internal class HoverPopupLayoutProps(
59 val anchor: LayoutAnchor,
60 val link: HoverPopupLink,
61 val open: Binding<Boolean>,
62 val hover: HoverOptions,
63 val closeOnPress: Boolean
66internal class HoverPopupLayout(
67 private val props: Props<HoverPopupLayoutProps>,
68 scope: ComponentScope
69): Element<Unit>(scope) {
70 private var area: InputArea? = None
71 private var hover: Hover? = None
72 private val space = ShortcutChord(ShortcutKey.Logical(LogicalKey.Named(NamedKey.Space)))
73 private val enter = ShortcutChord(ShortcutKey.Logical(LogicalKey.Named(NamedKey.Enter)))
74 public override fun onMount() {
75 val input = props.value
76 val target = scope.createArea()
77 area = Some(target)
78 scope.routeChildren(target)
79 scope.layoutAnchor(input.anchor)
80 val behavior = scope.hover(target, input.open, input.hover)
81 hover = Some(behavior)
82 input.link.bind(behavior)
83 scope.input.captureHandler =
84 {
85 context,
86 event -> if (
87 props.value.closeOnPress && !props.value.link.containsContent(context.target)
88 ) {
89 val close =
90 when (event) {
91 case Pointer(
92 InputPointerEvent.Button(
93 _,
94 MouseButton.Left,
95 ElementState.Pressed,
96 _
97 )
98 ) -> true
99 case Contact(InputContactEvent.Began(_)) -> true
100 case Keyboard(val key) -> key.state ==
101 ElementState.Pressed &&
102 !key.synthetic &&
103 (space.matches(key) || enter.matches(key))
104 else -> false
105 }
106 if (close)
107 props.value.link.dismiss()
108 }
109 }
110 }
111 public override fun update(): UiInvalidation {
112 hover!!.update(props.value.open, Some(props.value.hover))
113 return UiInvalidation.Layout
114 }
115 public override fun onUnmount() {
116 props.value.link.unbind(hover!!)
117 }
118 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
119 Unit
120 > {
121 val result = context.measureChild(0L, constraints)
122 context.placeChild(0L, Point(0.0, 0.0))
123 context.measureChild(1L, Constraints())
124 context.placeChild(1L, Point(0.0, 0.0))
125 return LayoutResult(result, Unit)
126 }
127 public override fun scene(context: SceneContext, measured: Unit) {
128 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
129 context.anchor(props.value.anchor, bounds)
130 context.place(area!!, bounds)
131 context.children()
132 }
135internal class HoverPopupContentProps(
136 val link: HoverPopupLink,
137 val passive: Boolean,
138 val maxWidth: Double?
141/** Keeps hover geometry live while a tooltip's complete content input subtree is disabled. */
142internal class HoverPopupContent(
143 private val props: Props<HoverPopupContentProps>,
144 scope: ComponentScope
145): Element<Unit>(scope) {
146 private var area: InputArea? = None
147 private var gate: InputArea? = None
148 public override fun onMount() {
149 val hit = scope.createArea()
150 val route = scope.createArea(parent = Some(hit))
151 route.ignoreSelf = true
152 area = Some(hit)
153 gate = Some(route)
154 scope.routeChildren(route)
155 props.value.link.setContent(Some(hit))
156 }
157 public override fun update(): UiInvalidation {
158 gate!!.scope.enabled = !props.value.passive
159 return UiInvalidation.Layout
160 }
161 public override fun onUnmount() {
162 props.value.link.setContent(None)
163 }
164 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
165 Unit
166 > {
167 val limit = props.value.maxWidth
168 val bounded =
169 when (limit) {
170 case Some(val width) -> {
171 val natural =
172 context.measureChild(0L, Constraints(maxHeight = constraints.maxHeight))
173 val limit = min(width, constraints.maxWidth ?: width)
174 val minimum = min(constraints.minWidth, limit)
175 val maximum = max(minimum, min(natural.size.width, limit))
176 Constraints(
177 minimum,
178 Some(maximum),
179 constraints.minHeight,
180 constraints.maxHeight
181 )
182 }
183 case None -> constraints
184 }
185 val result = context.measureChild(0L, bounded)
186 context.placeChild(0L, Point(0.0, 0.0))
187 return LayoutResult(result, Unit)
188 }
189 public override fun scene(context: SceneContext, measured: Unit) {
190 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
191 context.place(area!!, bounds)
192 context.place(gate!!, bounds)
193 context.children()
194 }