scoop:scoop.ui.component:0.1.0
src/button.scoop
Selected production source · src/button.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.text.InlineView8
import scoop.window.accessibility.*10
public enum ButtonContent {11
Label(LabelProps), Custom(View), Resolved((ActionContentStyle) -> View)12
}14
public class ButtonProps public constructor(15
public val content: ButtonContent,16
public val onClick: () -> Unit,17
public val enabled: Boolean = true,18
public val style: ButtonStyle = buttonStyle(),19
public val theme: ThemeSource? = None,20
public val focus: FocusOptions = FocusOptions(),21
public val invalid: Boolean = false,22
public val variant: ButtonVariant = ButtonVariant.Default,23
public val size: ButtonSize = ButtonSize.Default,24
public val group: GroupPosition? = None,25
public val labelTarget: FieldTarget? = None,26
public val semantics: ComponentSemantics = ComponentSemantics()27
)29
public class Button public constructor(30
private val props: Props<ButtonProps>,31
scope: ComponentScope32
) : Component(scope) {33
private val button = ButtonControl({ props.value }, scope)34
public override fun onMount() {35
button.mount()36
}37
public override fun render(): View = button.render()38
}40
internal class ButtonControl(41
private val read: () -> ButtonProps,42
private val scope: ComponentScope,43
role: SemanticRole = SemanticRole.Button,44
private val semanticState: () -> SemanticProperties? = { None },45
private val semanticActions: () -> SemanticActions? = { None },46
private val semanticFallbackName: () -> String? = { None }47
) {48
private val themes = ThemeAccess(scope)49
private val text = ActionTextStyle()50
private val click: () -> Unit = { read().onClick() }51
private val control = PressControl(scope, PressOptions(read().enabled), read().focus, click)52
private val semantics = createComponentSemantics(scope, role)53
init {54
when (semantics) {55
case Some(val handle) -> {56
handle.bindFocus(Some(control.focus))57
handle.mergeLabels = true58
scope.routeSemanticsChildren(Some(handle))59
}61
case None -> {}62
}63
}64
fun mount() {65
themes.mount(read().theme)66
}67
fun render(): View {68
val input = read()69
val theme = themes.read(input.theme)70
val interaction = control.press.interaction71
val shape = ActionShape.Button(input.size)72
val visual = SurfaceVisual(73
actionSurfaceKind(shape, input.variant, input.style.kind),74
interaction.asVisual(input.enabled, input.invalid)75
)76
val base = text77
.resolve(78
theme.tokens.typography.action,79
actionTextColor(theme, shape, input.variant, visual),80
input.style.text,81
actionFontScale(shape),82
actionLineHeight(shape)83
)84
return element(85
ActionInkProps(86
theme,87
shape,88
input.variant,89
input.style,90
visual,91
ActionBehavior.Press(control, click, input.focus),92
input.group,93
input.labelTarget,94
semantics = semantics,95
semanticOptions = input.semantics,96
semanticState = semanticState(),97
semanticActions = semanticActions(),98
semanticFallbackName = semanticFallbackName()99
),100
{ current, mount -> ActionInk(current, scope = mount) },101
[102
actionContent(103
input.content,104
actionContentStyle(base, shape, input.variant, visual.interaction)105
)106
]107
)108
}109
}111
public fun button(112
text: String,113
onClick: () -> Unit,114
configure: ButtonBuilder.() -> Unit = {}115
): View = makeTextButton(text, onClick, configure)117
public fun button(118
text: StyledText,119
onClick: () -> Unit,120
configure: StyledButtonBuilder.() -> Unit = {}121
): View = makeStyledButton(text, onClick, configure)123
public fun button(124
content: View,125
onClick: () -> Unit,126
configure: ContentButtonBuilder.() -> Unit = {}127
): View = makeContentButton(content, onClick, configure)129
public fun button(130
content: (ActionContentStyle) -> View,131
onClick: () -> Unit,132
configure: ContentButtonBuilder.() -> Unit = {}133
): View = makeResolvedButton(content, onClick, configure)