Scoop API

scoop:scoop.ui.component:0.1.0

src/overlays/hover_popup.scoop

Selected production source · src/overlays/hover_popup.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.AnimationSpec
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.ui.style.*
9import scoop.window.accessibility.*
11public class Tooltip public constructor(
12 private val props: Props<TooltipProps>,
13 scope: ComponentScope
14): 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()
22public class HoverCard public constructor(
23 private val props: Props<HoverCardProps>,
24 scope: ComponentScope
25): 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()
33/** Composition shared by the two hover families; Hover owns all input and deadline state. */
34private 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 else
50 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.value
59 if (previous == next)
60 return
61 when (input.open) {
62 case Some(val value) -> value.value = next
63 case None -> local.value = next
64 }
65 val accepted = open.value
66 if (previous != accepted)
67 read().onOpenChange(accepted)
68 }
69 private fun setExpanded(value: Boolean): SemanticActionResult {
70 if (!read().hover.enabled)
71 return SemanticActionResult.Rejected
72 if (value)
73 setOpen(true)
74 else
75 (read().handle ?: localHandle).requestClose()
76 return if (open.value == value)
77 SemanticActionResult.Completed
78 else
79 SemanticActionResult.Rejected
80 }
81 fun render(): View {
82 val input = read()
83 val theme = themes.read(input.theme)
84 val palette = theme.tokens.palette
85 val density = theme.tokens.typography.body.fontSize / 14.0
86 val passive = input.kind == HoverPopupKind.Tooltip
87 val backgroundColor =
88 if (passive)
89 tooltipBackground(palette)
90 else
91 palette.popover.background
92 val foreground =
93 if (passive)
94 palette.background.background
95 else
96 palette.popover.foreground
97 val base =
98 if (passive)
99 theme.tokens.typography.body.copy {
100 fontSize = 12.0 * density
101 lineHeight = Some(16.0 / 12.0)
102 }
103 else
104 theme.tokens.typography.body
105 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.0
114 else
115 8.0
116 ) *
117 density
118 )
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 else
131 Insets.all(10.0 * density),
132 visual,
133 if (passive)
134 None
135 else
136 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 else
150 None
151 ),
152 { current, mount -> HoverPopupContent(current, scope = mount) },
153 [panel]
154 )
155 val localAnchor = anchor
156 val currentInputs = read
157 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.Text
172 else
173 SemanticRole.Dialog,
174 SemanticProperties(
175 name = input.semantics.name,
176 value =
177 if (passive)
178 candidateCaption(input.content)
179 else
180 None,
181 readOnly = passive,
182 enabled = input.hover.enabled
183 ),
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 * density
198 )
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 None
217 else
218 Some(open.value)
219 ),
220 actions =
221 if (passive)
222 SemanticActions()
223 else
224 SemanticActions(setExpanded = Some { value -> setExpanded(value) }),
225 relations = semanticControls(input.semantics.relations, semanticPopup.id),
226 mergeLabels = false,
227 reference = input.semantics.reference
228 ),
229 popup
230 ]
231 )
232 }
235/** System text colors can carry alpha; popup surfaces must cover the content behind them. */
236internal 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.alpha
241 )