scoop:scoop.ui.component:0.1.0
src/disclosure/trigger.scoop
Selected production source · src/disclosure/trigger.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.svg.*5
import scoop.icons.lucide.chevronDown6
import scoop.graphics.text.*7
import scoop.math.PI8
import scoop.math.min9
import scoop.ui.*10
import scoop.ui.style.*11
import scoop.window.accessibility.*12
import scoop.ui.component.row as makeRow14
/** Non-interactive content for the owning disclosure's single activation target. */15
public class TriggerSpec public constructor(16
public val content: ButtonContent,17
public val enabled: Boolean = true,18
public val indicator: Boolean = true,19
public val variant: ButtonVariant = ButtonVariant.Ghost,20
public val style: ButtonStyle? = None,21
public val focus: FocusOptions = FocusOptions(),22
public val fonts: FontCollection? = None,23
public val semantics: ComponentSemantics = ComponentSemantics()24
)26
internal fun disclosureTrigger(27
input: TriggerSpec,28
enabled: Boolean,29
fonts: FontCollection?,30
theme: ThemeSource?,31
presence: Presence?,32
open: Boolean,33
currentOpen: () -> Boolean,34
setOpen: (Boolean) -> SemanticActionResult,35
semantics: ComponentSemantics36
): View =37
semanticButton(38
ButtonProps(39
ButtonContent.Resolved {40
appearance ->41
val content =42
when (input.content) {43
case Label(val label) -> ButtonContent.Label(44
LabelProps(45
label.text,46
disclosureFonts(label.fonts, disclosureFonts(input.fonts, fonts)),47
label.settings,48
label.inlines,49
label.theme,50
label.baseStyle,51
label.underline,52
label.semantics53
)54
)55
else -> input.content56
}57
makeRow {58
this.item(actionContent(content, appearance)) { this.grow = 1.0 }59
if (input.indicator)60
this.add(61
element(62
DisclosureArrowProps(63
presence,64
open,65
appearance.iconSize,66
appearance.text.color67
),68
{ current, mount -> DisclosureArrow(current, scope = mount) }69
)70
)71
this.spacing = appearance.spacing72
this.alignment = CrossAlignment.Center73
}74
},75
{ setOpen(!currentOpen()) },76
enabled && input.enabled,77
input.style ?: buttonStyle { this.kind = SurfaceKind.Plain },78
theme,79
input.focus,80
variant = input.variant,81
semantics = semantics82
),83
SemanticRole.Disclosure,84
semantics.properties(enabled = enabled && input.enabled, expanded = Some(open)),85
SemanticActions(invoke = Some { setOpen(!currentOpen()) }, setExpanded = Some(setOpen))86
)88
private class DisclosureArrowProps(89
val presence: Presence?,90
val open: Boolean,91
val size: Double,92
val color: Color93
)95
private class DisclosureArrow(96
private val props: Props<DisclosureArrowProps>,97
scope: ComponentScope98
): Element<Unit>(scope) {99
private var outline: SvgDocument? = None100
public override fun onMount() {101
outline = Some(scope.resource { SvgDocument.parse(chevronDown.source) })102
}103
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<104
Unit105
> =106
LayoutResult(107
LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),108
Unit109
)110
public override fun scene(context: SceneContext, measured: Unit) {111
val input = props.value112
val size = context.size113
val side = min(size.width, size.height)114
val angle = disclosureProgress(input.presence, input.open) * PI115
context.draw {116
paint -> paint.canvas.withState {117
canvas ->118
canvas.translate(size.width / 2.0, size.height / 2.0)119
canvas.rotate(angle)120
canvas.scale(side / 24.0, side / 24.0)121
canvas.translate(-12.0, -12.0)122
canvas.drawSvg(123
outline!!,124
Rect(0.0, 0.0, 24.0, 24.0),125
SvgDrawOptions(currentColor = Some(input.color))126
)127
}128
}129
}130
}132
private fun disclosureFonts(primary: FontCollection?, fallback: FontCollection?): FontCollection? =133
when (primary) {134
case Some(_) -> primary135
case None -> fallback136
}