scoop:scoop.ui.component:0.1.0
src/input_group/addon.scoop
Selected production source · src/input_group/addon.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*7
private enum InputAddonPart {8
Text(String), Button(ButtonProps, ViewKey), Custom(View)9
}11
public class InputGroupAddonBuilder internal constructor(private val align: InputGroupAlign) {12
private val parts = ArrayList<InputAddonPart>()13
public var key: ViewKey = ViewKey.Position14
public var padding: Insets? = None15
public var spacing: Double? = None16
public var style: StyleOverride = styleOverride()17
public fun add(view: View) {18
parts.add(InputAddonPart.Custom(view))19
}20
public fun text(value: String) {21
parts.add(InputAddonPart.Text(value))22
}23
public fun style(configure: StyleOverrideBuilder.() -> Unit) {24
style = style.copy(configure)25
}26
public fun button(text: String, onClick: () -> Unit, configure: ButtonBuilder.() -> Unit = {}) {27
val builder = ButtonBuilder()28
builder.variant = ButtonVariant.Ghost29
builder.size = ButtonSize.Xs30
builder.configure()31
parts.add(InputAddonPart.Button(builder.snapshot(text, onClick), builder.key))32
}33
public fun button(34
text: StyledText,35
onClick: () -> Unit,36
configure: StyledButtonBuilder.() -> Unit = {}37
) {38
val builder = StyledButtonBuilder()39
builder.variant = ButtonVariant.Ghost40
builder.size = ButtonSize.Xs41
builder.configure()42
parts.add(InputAddonPart.Button(builder.snapshot(text, onClick), builder.key))43
}44
public fun button(45
content: View,46
onClick: () -> Unit,47
configure: ContentButtonBuilder.() -> Unit = {}48
) {49
contentButton(ButtonContent.Custom(content), onClick, configure)50
}51
public fun button(52
content: (ActionContentStyle) -> View,53
onClick: () -> Unit,54
configure: ContentButtonBuilder.() -> Unit = {}55
) {56
contentButton(ButtonContent.Resolved(content), onClick, configure)57
}58
private fun contentButton(59
content: ButtonContent,60
onClick: () -> Unit,61
configure: ContentButtonBuilder.() -> Unit62
) {63
val builder = ContentButtonBuilder()64
builder.variant = ButtonVariant.Ghost65
builder.size = ButtonSize.Xs66
builder.configure()67
parts.add(68
InputAddonPart.Button(69
ButtonProps(70
content,71
onClick,72
builder.enabled,73
builder.style,74
builder.theme,75
builder.focus,76
builder.invalid,77
builder.variant,78
builder.size,79
builder.group,80
builder.labelTarget,81
builder.semantics82
),83
builder.key84
)85
)86
}87
internal fun snapshot(fonts: FontCollection?, theme: ThemeSource?): InputGroupAddon {88
when (spacing) {89
case Some(val value) -> nonnegative(value, "invalid input group addon spacing")90
case None -> {}91
}92
padding?.totals()93
val children = ArrayList<View>()94
for (part in parts)95
children.add(96
when (part) {97
case Text(val value) -> typography(value, TypographyRole.Muted) {98
this.fonts = fonts99
this.theme = theme100
this.selectable = false101
}102
case Button(val value, val key) -> component(103
groupButtonProps(value, None, fonts, theme),104
{ current, mount -> Button(current, scope = mount) },105
key106
)107
case Custom(val view) -> view108
}109
)110
return InputGroupAddon(111
component(112
InputAddonBodyProps(children.toArray(), spacing, theme),113
{ current, mount -> InputAddonBody(current, scope = mount) }114
),115
align,116
key,117
padding,118
style119
)120
}121
}123
private class InputAddonBodyProps(124
val children: Array<View>,125
val spacing: Double?,126
val theme: ThemeSource?127
)129
private class InputAddonBody(130
private val props: Props<InputAddonBodyProps>,131
scope: ComponentScope132
): Component(scope) {133
private val themes = ThemeAccess(scope)134
public override fun onMount() {135
themes.mount(props.value.theme)136
}137
public override fun render(): View {138
val value = props.value139
val theme = themes.read(value.theme)140
return row {141
for (child in value.children)142
this.add(child)143
this.spacing = value.spacing ?: 8.0 * theme.tokens.typography.body.fontSize / 14.0144
this.alignment = CrossAlignment.Center145
}146
}147
}