scoop:scoop.ui.component:0.1.0
src/actions/toggle.scoop
Selected production source · src/actions/toggle.scoop
1
package scoop.ui.component3
import scoop.graphics.text.StyledText4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.window.accessibility.*8
public enum ToggleVariant {9
Default, Outline10
}12
public enum ToggleSize {13
Sm, Default, Lg14
}16
public class ToggleProps public constructor(17
public val content: ButtonContent,18
public val pressed: Binding<Boolean>,19
public val enabled: Boolean = true,20
public val invalid: Boolean = false,21
public val variant: ToggleVariant = ToggleVariant.Default,22
public val size: ToggleSize = ToggleSize.Default,23
public val style: ButtonStyle = buttonStyle { kind = SurfaceKind.Plain },24
public val theme: ThemeSource? = None,25
public val focus: FocusOptions = FocusOptions(),26
public val group: GroupPosition? = None,27
public val semantics: ComponentSemantics = ComponentSemantics()28
)30
public class Toggle public constructor(31
private val props: Props<ToggleProps>,32
scope: ComponentScope33
): Component(scope) {34
private val control =35
PressControl(scope, PressOptions(props.value.enabled), props.value.focus, { activate() })36
private val semantics = createComponentSemantics(scope, SemanticRole.ToggleButton)37
private val semanticToggle: () -> SemanticActionResult =38
{39
val input = props.value40
if (!input.enabled)41
SemanticActionResult.Unavailable42
else {43
val binding = input.pressed44
val expected = !binding.value45
activate()46
if (binding.value == expected)47
SemanticActionResult.Completed48
else49
SemanticActionResult.Rejected50
}51
}52
init {53
when (semantics) {54
case Some(val handle) -> {55
handle.bindFocus(Some(control.focus))56
handle.mergeLabels = true57
scope.routeSemanticsChildren(Some(handle))58
}59
case None -> {}60
}61
}62
private val themes = ThemeAccess(scope)63
private val text = ActionTextStyle()64
private fun activate() {65
val binding = props.value.pressed66
binding.value = !binding.value67
}68
public override fun onMount() {69
themes.mount(props.value.theme)70
}71
public override fun render(): View {72
val input = props.value73
val theme = themes.read(input.theme)74
val interaction = control.press.interaction75
val visual =76
SurfaceVisual(input.style.kind, interaction.asVisual(input.enabled, input.invalid))77
val shape = ActionShape.Toggle(input.variant, input.size, input.pressed.value)78
val base =79
text.resolve(80
theme.tokens.typography.action,81
actionTextColor(theme, shape, ButtonVariant.Ghost, visual),82
input.style.text,83
actionFontScale(shape),84
actionLineHeight(shape)85
)86
return element(87
ActionInkProps(88
theme,89
shape,90
ButtonVariant.Ghost,91
input.style,92
visual,93
ActionBehavior.Press(control, { activate() }, input.focus),94
input.group,95
semantics = semantics,96
semanticOptions = input.semantics,97
semanticToggle = Some(semanticToggle)98
),99
{ current, mount -> ActionInk(current, scope = mount) },100
[101
actionContent(102
input.content,103
actionContentStyle(base, shape, ButtonVariant.Ghost, visual.interaction)104
)105
]106
)107
}108
}110
public fun toggle(111
text: String,112
pressed: Binding<Boolean>,113
configure: ToggleBuilder.() -> Unit = {}114
): View = makeTextToggle(text, pressed, configure)116
public fun toggle(117
text: StyledText,118
pressed: Binding<Boolean>,119
configure: StyledToggleBuilder.() -> Unit = {}120
): View = makeStyledToggle(text, pressed, configure)122
public fun toggle(123
content: View,124
pressed: Binding<Boolean>,125
configure: ContentToggleBuilder.() -> Unit = {}126
): View = makeContentToggle(ButtonContent.Custom(content), pressed, configure)128
public fun toggle(129
content: (ActionContentStyle) -> View,130
pressed: Binding<Boolean>,131
configure: ContentToggleBuilder.() -> Unit = {}132
): View = makeContentToggle(ButtonContent.Resolved(content), pressed, configure)