scoop:scoop.ui.component:0.1.0
src/overlays/popover.scoop
Selected production source · src/overlays/popover.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.TextAlignment5
import scoop.surface.*6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.ui.style.*9
import scoop.window.accessibility.*10
import scoop.ui.component.column as popupColumn12
public class Popover public constructor(13
private val props: Props<PopoverProps>,14
scope: ComponentScope15
): Component(scope) {16
private val anchor = LayoutAnchor()17
private val localHandle = OverlayHandle()18
private val semanticReference = SemanticsReference()19
private val themes = ThemeAccess(scope)20
private val titles = ActionTextStyle()21
private val descriptions = ActionTextStyle()22
public override fun onMount() {23
themes.mount(props.value.theme)24
}25
private fun setOpen(value: Boolean) {26
val input = props.value27
val previous = input.open.value28
if (previous == value)29
return30
input.open.value = value31
val accepted = input.open.value32
if (accepted != previous)33
props.value.onOpenChange(accepted)34
}35
private fun toggle() {36
val input = props.value37
if (!input.enabled || !(input.trigger?.enabled ?: false))38
return39
if (input.open.value)40
(input.handle ?: localHandle).requestClose()41
else42
setOpen(true)43
}44
private fun dismissSemantically(): SemanticActionResult {45
if (!props.value.open.value)46
return SemanticActionResult.Unavailable;47
(props.value.handle ?: localHandle).requestClose()48
return if (!props.value.open.value)49
SemanticActionResult.Completed50
else51
SemanticActionResult.Rejected52
}53
public override fun render(): View {54
val inputs = props55
val input = inputs.value56
val theme = themes.read(input.theme)57
val palette = theme.tokens.palette58
val density = theme.tokens.typography.body.fontSize / 14.059
val titleStyle =60
titles.resolve(theme.tokens.typography.action, palette.popover.foreground, None)61
val descriptionStyle =62
descriptions.resolve(63
theme.tokens.typography.body,64
componentForeground(theme, palette.muted.foreground, palette.popover.background),65
None66
)67
val content =68
popupColumn {69
if (panelPresent(input.title) || panelPresent(input.description))70
add(71
panelSlot(72
popupColumn {73
when (input.title) {74
case Some(val value) -> add(75
panelSlot(panelCaption(value, titleStyle, input.fonts), 0L)76
)77
case None -> {}78
}79
when (input.description) {80
case Some(val value) -> add(81
panelSlot(82
panelCaption(value, descriptionStyle, input.fonts),83
1L84
)85
)86
case None -> {}87
}88
spacing = 2.0 * density89
alignment = CrossAlignment.Stretch90
},91
0L92
)93
)94
add(panelSlot(input.content, 1L))95
spacing = 10.0 * density96
alignment = CrossAlignment.Stretch97
}98
val panel =99
panelFrame(100
content,101
Insets.all(10.0 * density),102
styleOverride {103
background(Some(Paint(palette.popover.background)))104
border(Some(Border.all(1.0, componentPanelBorder(theme, palette.popover))))105
corners(Corners.all(8.0 * density))106
boxShadows(theme.tokens.effects.floating)107
}.then(input.style),108
Some(input.width * density)109
)110
val trigger =111
when (input.trigger) {112
case Some(val value) -> component(113
ButtonProps(114
navigationLabel(115
value.content,116
if (panelPresent(value.fonts))117
value.fonts118
else119
input.fonts,120
TextAlignment.Center121
),122
{ toggle() },123
input.enabled && value.enabled,124
value.style,125
input.theme,126
value.focus,127
variant = value.variant,128
size = value.size,129
semantics =130
value.semantics.copy {131
relations =132
semanticControls(133
relations,134
(135
input.semantics.reference ?:136
this @Popover.semanticReference137
).id138
)139
}140
),141
{ current, mount -> Button(current, scope = mount) }142
)143
case None -> empty()144
}145
val motion =146
theme.tokens.motion.resolve(147
input.transition.resolve(Some(AnimationSpec.Tween(Duration.milliseconds(100L))))148
)149
val localAnchor = anchor150
val overlayHandle = input.handle ?: localHandle151
val semantics =152
input.semantics.copy {153
if (name == None && relations.labelledBy.size == 0L)154
name = semanticCaption(input.title)155
if (description == None)156
description = semanticCaption(input.description)157
reference = Some(input.semantics.reference ?: this @Popover.semanticReference)158
}159
val popup =160
portal(161
binding({ props.value.open.value }, { value -> setOpen(value) }),162
element(163
PopupMotionProps(164
input.enabled,165
8.0 * density,166
popupArrowInk(167
input.arrow,168
density,169
input.style.background.resolve(Some(Paint(palette.popover.background)))170
)171
),172
{ current, mount -> PopupMotion(current, scope = mount) },173
[174
semanticContent(175
panel,176
SemanticRole.Dialog,177
semantics.properties(178
enabled = input.enabled,179
modal = input.mode == OverlayMode.Modal180
),181
actions = SemanticActions(dismiss = Some { dismissSemantically() }),182
relations = semantics.relations,183
mergeLabels = false,184
reference = semantics.reference185
)186
]187
)188
) {189
placement =190
OverlayPlacement.Anchored(191
input.anchor ?: localAnchor,192
input.side,193
input.alignment,194
input.gap * density,195
input.viewportPadding * density,196
input.matchAnchorWidth197
)198
handle = Some(overlayHandle)199
anchorVisibility = input.anchorVisibility200
mode = input.mode201
focus = Some(input.focus)202
dismiss = input.dismissal203
transition = PresenceOptions(motion, motion)204
onCloseRequest = { reason -> inputs.value.onCloseRequest(reason) }205
onClosed = { reason -> inputs.value.onClosed(reason) }206
}207
return element({ mount -> PopoverLayout(localAnchor, scope = mount) }, [trigger, popup])208
}209
}211
public fun popover(212
open: Binding<Boolean>,213
content: View,214
configure: PopoverBuilder.() -> Unit = {}215
): View = makePopover(open, content, configure)