scoop:scoop.ui.component:0.1.0
src/overlays/hover_popup.scoop
Selected production source · src/overlays/hover_popup.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.AnimationSpec6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.ui.style.*9
import scoop.window.accessibility.*11
public class Tooltip public constructor(12
private val props: Props<TooltipProps>,13
scope: ComponentScope14
): Component(scope) {15
private val popup = HoverPopup({ props.value.input }, scope)16
public override fun onMount() {17
popup.mount()18
}19
public override fun render(): View = popup.render()20
}22
public class HoverCard public constructor(23
private val props: Props<HoverCardProps>,24
scope: ComponentScope25
): Component(scope) {26
private val popup = HoverPopup({ props.value.input }, scope)27
public override fun onMount() {28
popup.mount()29
}30
public override fun render(): View = popup.render()31
}33
/** Composition shared by the two hover families; Hover owns all input and deadline state. */34
private class HoverPopup(private val read: () -> HoverPopupInput, scope: ComponentScope) {35
private val local = state(false)36
private val anchor = LayoutAnchor()37
private val semanticPopup = SemanticsReference()38
private val localHandle = OverlayHandle()39
private val link = HoverPopupLink()40
private val themes = ThemeAccess(scope)41
private val bodyStyle = ActionTextStyle()42
private val open = binding({ read().open?.value ?: local.value }, { next -> setOpen(next) })43
private val portalOpen =44
binding(45
{ open.value },46
{47
next -> if (next)48
setOpen(true)49
else50
link.dismiss { setOpen(false) }51
}52
)53
fun mount() {54
themes.mount(read().theme)55
}56
private fun setOpen(next: Boolean) {57
val input = read()58
val previous = open.value59
if (previous == next)60
return61
when (input.open) {62
case Some(val value) -> value.value = next63
case None -> local.value = next64
}65
val accepted = open.value66
if (previous != accepted)67
read().onOpenChange(accepted)68
}69
private fun setExpanded(value: Boolean): SemanticActionResult {70
if (!read().hover.enabled)71
return SemanticActionResult.Rejected72
if (value)73
setOpen(true)74
else75
(read().handle ?: localHandle).requestClose()76
return if (open.value == value)77
SemanticActionResult.Completed78
else79
SemanticActionResult.Rejected80
}81
fun render(): View {82
val input = read()83
val theme = themes.read(input.theme)84
val palette = theme.tokens.palette85
val density = theme.tokens.typography.body.fontSize / 14.086
val passive = input.kind == HoverPopupKind.Tooltip87
val backgroundColor =88
if (passive)89
tooltipBackground(palette)90
else91
palette.popover.background92
val foreground =93
if (passive)94
palette.background.background95
else96
palette.popover.foreground97
val base =98
if (passive)99
theme.tokens.typography.body.copy {100
fontSize = 12.0 * density101
lineHeight = Some(16.0 / 12.0)102
}103
else104
theme.tokens.typography.body105
val text = bodyStyle.resolve(base, foreground, None)106
val visual =107
styleOverride {108
background(Some(Paint(backgroundColor)))109
corners(110
Corners.all(111
(112
if (passive)113
6.0114
else115
8.0116
) *117
density118
)119
)120
if (!passive) {121
border(Some(Border.all(1.0, componentPanelBorder(theme, palette.popover))))122
boxShadows(theme.tokens.effects.floating)123
}124
}.then(input.style)125
val panel =126
panelFrame(127
panelCaption(input.content, text, input.fonts),128
if (passive)129
Insets(12.0 * density, 6.0 * density, 12.0 * density, 6.0 * density)130
else131
Insets.all(10.0 * density),132
visual,133
if (passive)134
None135
else136
Some(input.width * density)137
)138
val motion =139
theme.tokens.motion.resolve(140
input.transition.resolve(Some(AnimationSpec.Tween(Duration.milliseconds(100L))))141
)142
val content =143
element(144
HoverPopupContentProps(145
link,146
passive,147
if (passive)148
Some(input.width * density)149
else150
None151
),152
{ current, mount -> HoverPopupContent(current, scope = mount) },153
[panel]154
)155
val localAnchor = anchor156
val currentInputs = read157
val popup =158
portal(159
portalOpen,160
element(161
PopupMotionProps(162
input.hover.enabled,163
8.0 * density,164
popupArrowInk(input.arrow, density, visual.background.resolve(None))165
),166
{ current, mount -> PopupMotion(current, scope = mount) },167
[168
semanticContent(169
content,170
if (passive)171
SemanticRole.Text172
else173
SemanticRole.Dialog,174
SemanticProperties(175
name = input.semantics.name,176
value =177
if (passive)178
candidateCaption(input.content)179
else180
None,181
readOnly = passive,182
enabled = input.hover.enabled183
),184
actions = SemanticActions(dismiss = Some { setExpanded(false) }),185
mergeLabels = passive,186
reference = Some(semanticPopup)187
)188
]189
)190
) {191
placement =192
OverlayPlacement.Anchored(193
localAnchor,194
input.side,195
input.alignment,196
input.gap * density,197
input.viewportPadding * density198
)199
handle = Some(input.handle ?: this @HoverPopup.localHandle)200
dismiss = OverlayDismiss(true, OutsidePress.CloseAndContinue, true)201
transition = PresenceOptions(motion, motion)202
onClosed = { reason -> currentInputs().onClosed(reason) }203
}204
return element(205
HoverPopupLayoutProps(anchor, link, open, input.hover, passive),206
{ current, mount -> HoverPopupLayout(current, scope = mount) },207
[208
semanticContent(209
input.trigger,210
SemanticRole.Group,211
input.semantics.properties(212
enabled = input.hover.enabled,213
description = candidateCaption(input.content),214
expanded =215
if (passive)216
None217
else218
Some(open.value)219
),220
actions =221
if (passive)222
SemanticActions()223
else224
SemanticActions(setExpanded = Some { value -> setExpanded(value) }),225
relations = semanticControls(input.semantics.relations, semanticPopup.id),226
mergeLabels = false,227
reference = input.semantics.reference228
),229
popup230
]231
)232
}233
}235
/** System text colors can carry alpha; popup surfaces must cover the content behind them. */236
internal fun tooltipBackground(palette: ThemePalette): Color =237
mixThemeColor(238
palette.background.background.withAlpha(1.0),239
palette.background.foreground.withAlpha(1.0),240
palette.background.foreground.alpha241
)