Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/trigger.scoop

Selected production source · src/sidebar/trigger.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.svg.*
5import scoop.icons.lucide.panelLeft
6import scoop.ui.*
7import scoop.ui.style.*
9public class SidebarTriggerProps public constructor(
10 public val open: Binding<Boolean>,
11 public val button: ButtonProps,
12 public val onOpenChange: (Boolean) -> Unit = {}
15public class SidebarTriggerBuilder internal constructor(): ContentButtonBuilder() {
16 public var content: View? = None
17 public var onOpenChange: (Boolean) -> Unit = {}
18 init {
19 variant = ButtonVariant.Ghost;
20 size = ButtonSize.IconSm
21 }
24public fun sidebarTrigger(
25 open: Binding<Boolean>,
26 configure: SidebarTriggerBuilder.() -> Unit = {}
27): View {
28 val b = SidebarTriggerBuilder();
29 b.configure()
30 val content =
31 when (b.content) {
32 case Some(val value) -> ButtonContent.Custom(value)
33 case None -> ButtonContent.Resolved(
34 { appearance -> sidebarIcon(appearance.iconSize, appearance.text.color) }
35 )
36 }
37 return component(
38 SidebarTriggerProps(
39 open,
40 ButtonProps(
41 content,
42 {},
43 b.enabled,
44 b.style,
45 b.theme,
46 b.focus,
47 b.invalid,
48 b.variant,
49 b.size,
50 b.group,
51 b.labelTarget,
52 b.semantics
53 ),
54 b.onOpenChange
55 ),
56 { current, mount -> SidebarTrigger(current, scope = mount) },
57 b.key
58 )
61public class SidebarTrigger public constructor(
62 private val props: Props<SidebarTriggerProps>,
63 scope: ComponentScope
64): Component(scope) {
65 public override fun render(): View {
66 val input = props.value;
67 val b = input.button
68 return component(
69 ButtonProps(
70 b.content,
71 {
72 val current = props.value
73 if (current.button.enabled)
74 sidebarWrite(current.open, !current.open.value, current.onOpenChange)
75 },
76 b.enabled,
77 b.style,
78 b.theme,
79 b.focus,
80 b.invalid,
81 b.variant,
82 b.size,
83 b.group,
84 b.labelTarget,
85 b.semantics
86 ),
87 { current, mount -> Button(current, scope = mount) }
88 )
89 }
92public fun sidebarMenuAction(
93 content: View,
94 onClick: () -> Unit,
95 configure: ContentButtonBuilder.() -> Unit = {}
96): View {
97 val b = ContentButtonBuilder();
98 b.variant = ButtonVariant.Ghost;
99 b.size = ButtonSize.IconXs;
100 b.configure()
101 return component(
102 ButtonProps(
103 ButtonContent.Custom(content),
104 onClick,
105 b.enabled,
106 b.style,
107 b.theme,
108 b.focus,
109 b.invalid,
110 b.variant,
111 b.size,
112 b.group,
113 b.labelTarget,
114 b.semantics
115 ),
116 { current, mount -> SidebarMenuAction(current, scope = mount) },
117 b.key
118 )
121public class SidebarMenuAction public constructor(
122 private val props: Props<ButtonProps>,
123 scope: ComponentScope
124): Component(scope) {
125 private val themes = ThemeAccess(scope)
126 public override fun onMount() {
127 themes.mount(props.value.theme)
128 }
129 public override fun render(): View {
130 val b = props.value;
131 val theme = themes.read(b.theme)
132 val d = theme.tokens.typography.body.fontSize / 14.0
133 val style =
134 b.style.copy {
135 surface {
136 if (suggestedSize == None)
137 suggestedSize = Some(Size(20.0 * d, 20.0 * d))
138 if (minimumSize == None)
139 minimumSize = Some(Size(0.0, 0.0))
140 if (padding == None)
141 padding = Some(Insets())
142 if (focus == None)
143 focus =
144 Some(
145 OutlineStyle(
146 componentTint(
147 theme,
148 theme.tokens.palette.sidebar.ring.withAlpha(1.0),
149 0.5,
150 theme.tokens.palette.sidebar.background.background
151 ),
152 componentFocusWidth(theme, 2.0 * d),
153 0.0
154 )
155 )
156 }
157 }
158 return component(
159 ButtonProps(
160 b.content,
161 b.onClick,
162 b.enabled,
163 style,
164 b.theme,
165 b.focus,
166 b.invalid,
167 b.variant,
168 b.size,
169 b.group,
170 b.labelTarget,
171 b.semantics
172 ),
173 { current, mount -> Button(current, scope = mount) }
174 )
175 }
178private class SidebarIconInput(val size: Double, val color: Color)
180internal fun sidebarIcon(size: Double, color: Color): View =
181 element(
182 SidebarIconInput(size, color),
183 { current, mount -> SidebarIcon(current, scope = mount) }
184 )
186private class SidebarIcon(
187 private val props: Props<SidebarIconInput>,
188 scope: ComponentScope
189): Element<Unit>(scope) {
190 private var document: SvgDocument? = None
191 public override fun onMount() {
192 document = Some(scope.resource { SvgDocument.parse(panelLeft.source) })
193 }
194 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
195 Unit
196 > =
197 LayoutResult(
198 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
199 Unit
200 )
201 public override fun scene(context: SceneContext, measured: Unit) {
202 val input = props.value
203 val rtl = context.layoutDirection == LayoutDirection.Rtl
204 val size = context.size
205 context.draw {
206 paint -> paint.canvas.withState {
207 canvas ->
208 canvas.scale(size.width / 24.0, size.height / 24.0)
209 if (rtl) {
210 canvas.translate(24.0, 0.0);
211 canvas.scale(-1.0, 1.0)
212 }
213 canvas.drawSvg(
214 document!!,
215 Rect(0.0, 0.0, 24.0, 24.0),
216 SvgDrawOptions(currentColor = Some(input.color))
217 )
218 }
219 }
220 }