Scoop API

scoop:scoop.ui.component:0.1.0

src/forms/switch.scoop

Selected production source · src/forms/switch.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.ui.style.*
5import scoop.window.accessibility.*
7public enum SwitchSize {
8 Default, Sm
9}
11public 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()
23public class Switch public constructor(
24 private val props: Props<SwitchProps>,
25 scope: ComponentScope
26): 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.value
38 if (!input.enabled)
39 SemanticActionResult.Unavailable
40 else {
41 val binding = input.checked
42 val expected = !binding.value
43 activate()
44 if (binding.value == expected)
45 SemanticActionResult.Completed
46 else
47 SemanticActionResult.Rejected
48 }
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.checked
57 binding.value = !binding.value
58 }
59 public override fun onMount() {
60 themes.mount(props.value.theme)
61 }
62 public override fun render(): View {
63 val input = props.value
64 val theme = themes.read(input.theme)
65 val interaction = control.press.interaction
66 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 semanticToggle
76 ),
77 { current, mount -> SwitchInk(current, scope = mount) }
78 )
79 }
82public fun switchControl(
83 checked: Binding<Boolean>,
84 configure: SwitchBuilder.() -> Unit = {}
85): View = makeSwitch(checked, configure)
87internal 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.key
94 )
97public class SwitchBuilder internal constructor() {
98 public var enabled: Boolean = true
99 public var invalid: Boolean = false
100 public var size: SwitchSize = SwitchSize.Default
101 public var style: SwitchStyle = switchStyle()
102 public var theme: ThemeSource? = None
103 public var focus: FocusOptions = FocusOptions()
104 public var labelTarget: FieldTarget? = None
105 public var semantics: ComponentSemantics = ComponentSemantics()
106 public var key: ViewKey = ViewKey.Position
107 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)