scoop:scoop.ui.component:0.1.0
src/actions/badge.scoop
Selected production source · src/actions/badge.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.window.accessibility.*8
public class BadgeProps public constructor(9
public val content: ButtonContent,10
public val onClick: (() -> Unit)? = None,11
public val variant: ButtonVariant = ButtonVariant.Default,12
public val enabled: Boolean = true,13
public val invalid: Boolean = false,14
public val style: ButtonStyle = buttonStyle(),15
public val theme: ThemeSource? = None,16
public val focus: FocusOptions = FocusOptions(),17
public val semantics: ComponentSemantics = ComponentSemantics()18
)20
public class Badge public constructor(21
private val props: Props<BadgeProps>,22
scope: ComponentScope23
): Component(scope) {24
public override fun render(): View {25
val input = props.value26
return when (input.onClick) {27
case Some(val action) -> component(28
BadgeActionInput(input, action),29
{ current, mount -> BadgeAction(current, scope = mount) }30
)31
case None -> component(32
input,33
{ current, mount -> BadgeDisplay(current, scope = mount) }34
)35
}36
}37
}39
public fun badge(text: String, configure: BadgeBuilder.() -> Unit = {}): View =40
makeTextBadge(text, configure)42
public fun badge(text: StyledText, configure: StyledBadgeBuilder.() -> Unit = {}): View =43
makeStyledBadge(text, configure)45
public fun badge(content: View, configure: ContentBadgeBuilder.() -> Unit = {}): View =46
makeContentBadge(content, configure)48
public fun badge(49
content: (ActionContentStyle) -> View,50
configure: ContentBadgeBuilder.() -> Unit = {}51
): View = makeResolvedBadge(content, configure)53
private class BadgeActionInput(val value: BadgeProps, val action: () -> Unit)55
private class BadgeAction(56
private val props: Props<BadgeActionInput>,57
scope: ComponentScope58
): Component(scope) {59
private val control =60
PressControl(61
scope,62
PressOptions(props.value.value.enabled),63
props.value.value.focus,64
{ props.value.action() }65
)66
private val semantics = createComponentSemantics(scope, SemanticRole.Button)67
init {68
semantics?.bindFocus(Some(control.focus))69
when (semantics) {70
case Some(val value) -> value.mergeLabels = true;71
case None -> {}72
}73
scope.routeSemanticsChildren(semantics)74
}75
private val themes = ThemeAccess(scope)76
private val text = ActionTextStyle()77
public override fun onMount() {78
themes.mount(props.value.value.theme)79
}80
public override fun render(): View {81
val input = props.value82
val interaction = control.press.interaction83
return badgeContent(84
input.value,85
themes,86
text,87
interaction,88
ActionBehavior.Press(control, input.action, input.value.focus),89
semantics90
)91
}92
}94
private class BadgeDisplay(private val props: Props<BadgeProps>, scope: ComponentScope): Component(95
scope96
) {97
private val semantics = createComponentSemantics(scope, SemanticRole.Group)98
init {99
when (semantics) {100
case Some(val value) -> value.mergeLabels = true;101
case None -> {}102
}103
scope.routeSemanticsChildren(semantics)104
}105
private val themes = ThemeAccess(scope)106
private val text = ActionTextStyle()107
public override fun onMount() {108
themes.mount(props.value.theme)109
}110
public override fun render(): View =111
badgeContent(112
props.value,113
themes,114
text,115
InteractionState(),116
ActionBehavior.Passive,117
semantics118
)119
}121
private fun badgeContent(122
input: BadgeProps,123
themes: ThemeAccess,124
text: ActionTextStyle,125
interaction: InteractionState,126
behavior: ActionBehavior,127
semantics: SemanticsHandle?128
): View {129
val theme = themes.read(input.theme)130
val shape = ActionShape.Badge131
val visual = SurfaceVisual(input.style.kind, interaction.asVisual(input.enabled, input.invalid))132
val base =133
text.resolve(134
theme.tokens.typography.action,135
actionTextColor(theme, shape, input.variant, visual),136
input.style.text,137
actionFontScale(shape),138
actionLineHeight(shape)139
)140
return element(141
ActionInkProps(142
theme,143
shape,144
input.variant,145
input.style,146
visual,147
behavior,148
semantics = semantics,149
semanticOptions = input.semantics150
),151
{ current, mount -> ActionInk(current, scope = mount) },152
[153
actionContent(154
input.content,155
actionContentStyle(base, shape, input.variant, visual.interaction)156
)157
]158
)159
}