Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/toggle.scoop

Selected production source · src/actions/toggle.scoop

1package scoop.ui.component
3import scoop.graphics.text.StyledText
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.window.accessibility.*
8public enum ToggleVariant {
9 Default, Outline
12public enum ToggleSize {
13 Sm, Default, Lg
16public 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()
30public class Toggle public constructor(
31 private val props: Props<ToggleProps>,
32 scope: ComponentScope
33): 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.value
40 if (!input.enabled)
41 SemanticActionResult.Unavailable
42 else {
43 val binding = input.pressed
44 val expected = !binding.value
45 activate()
46 if (binding.value == expected)
47 SemanticActionResult.Completed
48 else
49 SemanticActionResult.Rejected
50 }
51 }
52 init {
53 when (semantics) {
54 case Some(val handle) -> {
55 handle.bindFocus(Some(control.focus))
56 handle.mergeLabels = true
57 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.pressed
66 binding.value = !binding.value
67 }
68 public override fun onMount() {
69 themes.mount(props.value.theme)
70 }
71 public override fun render(): View {
72 val input = props.value
73 val theme = themes.read(input.theme)
74 val interaction = control.press.interaction
75 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 }
110public fun toggle(
111 text: String,
112 pressed: Binding<Boolean>,
113 configure: ToggleBuilder.() -> Unit = {}
114): View = makeTextToggle(text, pressed, configure)
116public fun toggle(
117 text: StyledText,
118 pressed: Binding<Boolean>,
119 configure: StyledToggleBuilder.() -> Unit = {}
120): View = makeStyledToggle(text, pressed, configure)
122public fun toggle(
123 content: View,
124 pressed: Binding<Boolean>,
125 configure: ContentToggleBuilder.() -> Unit = {}
126): View = makeContentToggle(ButtonContent.Custom(content), pressed, configure)
128public fun toggle(
129 content: (ActionContentStyle) -> View,
130 pressed: Binding<Boolean>,
131 configure: ContentToggleBuilder.() -> Unit = {}
132): View = makeContentToggle(ButtonContent.Resolved(content), pressed, configure)