scoop:scoop.ui.component:0.1.0
src/forms/switch.scoop
Selected production source · src/forms/switch.scoop
1
package scoop.ui.component3
import scoop.ui.*4
import scoop.ui.style.*5
import scoop.window.accessibility.*7
public enum SwitchSize {8
Default, Sm9
}11
public class SwitchProps public constructor(12
public val checked: Binding<Boolean>,13
public val enabled: Boolean = true,14
public val invalid: Boolean = false,15
public val size: SwitchSize = SwitchSize.Default,16
public val style: SwitchStyle = switchStyle(),17
public val theme: ThemeSource? = None,18
public val focus: FocusOptions = FocusOptions(),19
public val labelTarget: FieldTarget? = None,20
public val semantics: ComponentSemantics = ComponentSemantics()21
)23
public class Switch public constructor(24
private val props: Props<SwitchProps>,25
scope: ComponentScope26
): Component(scope) {27
private val control =28
PressControl(29
scope,30
PressOptions(props.value.enabled, false),31
props.value.focus,32
{ activate() }33
)34
private val semantics = createComponentSemantics(scope, SemanticRole.Switch)35
private val semanticToggle: () -> SemanticActionResult =36
{37
val input = props.value38
if (!input.enabled)39
SemanticActionResult.Unavailable40
else {41
val binding = input.checked42
val expected = !binding.value43
activate()44
if (binding.value == expected)45
SemanticActionResult.Completed46
else47
SemanticActionResult.Rejected48
}49
}50
init {51
semantics?.bindFocus(Some(control.focus))52
scope.routeSemanticsChildren(semantics)53
}54
private val themes = ThemeAccess(scope)55
private fun activate() {56
val binding = props.value.checked57
binding.value = !binding.value58
}59
public override fun onMount() {60
themes.mount(props.value.theme)61
}62
public override fun render(): View {63
val input = props.value64
val theme = themes.read(input.theme)65
val interaction = control.press.interaction66
return element(67
SwitchInkProps(68
input,69
theme,70
input.checked.value,71
control,72
interaction.asVisual(input.enabled, input.invalid),73
{ activate() },74
semantics,75
semanticToggle76
),77
{ current, mount -> SwitchInk(current, scope = mount) }78
)79
}80
}82
public fun switchControl(83
checked: Binding<Boolean>,84
configure: SwitchBuilder.() -> Unit = {}85
): View = makeSwitch(checked, configure)87
internal fun makeSwitch(checked: Binding<Boolean>, configure: SwitchBuilder.() -> Unit): View {88
val builder = SwitchBuilder()89
builder.configure()90
return component(91
builder.snapshot(checked),92
{ current, mount -> Switch(current, scope = mount) },93
builder.key94
)95
}97
public class SwitchBuilder internal constructor() {98
public var enabled: Boolean = true99
public var invalid: Boolean = false100
public var size: SwitchSize = SwitchSize.Default101
public var style: SwitchStyle = switchStyle()102
public var theme: ThemeSource? = None103
public var focus: FocusOptions = FocusOptions()104
public var labelTarget: FieldTarget? = None105
public var semantics: ComponentSemantics = ComponentSemantics()106
public var key: ViewKey = ViewKey.Position107
public fun style(configure: SwitchStyleBuilder.() -> Unit): Unit {108
style = style.copy(configure)109
}110
public fun focus(configure: FocusOptionsBuilder.() -> Unit): Unit {111
focus = focus.copy(configure)112
}113
internal fun snapshot(checked: Binding<Boolean>): SwitchProps =114
SwitchProps(checked, enabled, invalid, size, style, theme, focus, labelTarget, semantics)115
}