scoop:scoop.ui.component:0.1.0
src/overlays/hover_layout.scoop
Selected production source · src/overlays/hover_layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.min5
import scoop.math.max6
import scoop.surface.*7
import scoop.ui.*8
import scoop.window.*10
/** Connects the persistent trigger behavior to its temporarily mounted portal area. */11
internal class HoverPopupLink {12
private var behavior: Hover? = None13
private var content: InputArea? = None14
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 = None21
case None -> {}22
}23
}24
fun setContent(value: InputArea?) {25
content = value26
val current = behavior ?: return27
if (current.isClosed)28
return29
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 false38
var cursor: InputScope? = Some(target)39
while (true) {40
val current = cursor ?: return false41
if (current === area.scope)42
return true43
cursor = current.parent44
}45
}46
fun dismiss(fallback: () -> Unit = {}) {47
when (behavior) {48
case Some(val current) -> if (!current.isClosed) {49
current.dismiss();50
return51
}52
case None -> {}53
}54
fallback()55
}56
}58
internal class HoverPopupLayoutProps(59
val anchor: LayoutAnchor,60
val link: HoverPopupLink,61
val open: Binding<Boolean>,62
val hover: HoverOptions,63
val closeOnPress: Boolean64
)66
internal class HoverPopupLayout(67
private val props: Props<HoverPopupLayoutProps>,68
scope: ComponentScope69
): Element<Unit>(scope) {70
private var area: InputArea? = None71
private var hover: Hover? = None72
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.value76
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
) -> true99
case Contact(InputContactEvent.Began(_)) -> true100
case Keyboard(val key) -> key.state ==101
ElementState.Pressed &&102
!key.synthetic &&103
(space.matches(key) || enter.matches(key))104
else -> false105
}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.Layout114
}115
public override fun onUnmount() {116
props.value.link.unbind(hover!!)117
}118
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<119
Unit120
> {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
}133
}135
internal class HoverPopupContentProps(136
val link: HoverPopupLink,137
val passive: Boolean,138
val maxWidth: Double?139
)141
/** Keeps hover geometry live while a tooltip's complete content input subtree is disabled. */142
internal class HoverPopupContent(143
private val props: Props<HoverPopupContentProps>,144
scope: ComponentScope145
): Element<Unit>(scope) {146
private var area: InputArea? = None147
private var gate: InputArea? = None148
public override fun onMount() {149
val hit = scope.createArea()150
val route = scope.createArea(parent = Some(hit))151
route.ignoreSelf = true152
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.passive159
return UiInvalidation.Layout160
}161
public override fun onUnmount() {162
props.value.link.setContent(None)163
}164
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<165
Unit166
> {167
val limit = props.value.maxWidth168
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.maxHeight181
)182
}183
case None -> constraints184
}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
}195
}