scoop:scoop.ui.component:0.1.0
src/sidebar/trigger.scoop
Selected production source · src/sidebar/trigger.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.svg.*5
import scoop.icons.lucide.panelLeft6
import scoop.ui.*7
import scoop.ui.style.*9
public class SidebarTriggerProps public constructor(10
public val open: Binding<Boolean>,11
public val button: ButtonProps,12
public val onOpenChange: (Boolean) -> Unit = {}13
)15
public class SidebarTriggerBuilder internal constructor(): ContentButtonBuilder() {16
public var content: View? = None17
public var onOpenChange: (Boolean) -> Unit = {}18
init {19
variant = ButtonVariant.Ghost;20
size = ButtonSize.IconSm21
}22
}24
public 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.semantics53
),54
b.onOpenChange55
),56
{ current, mount -> SidebarTrigger(current, scope = mount) },57
b.key58
)59
}61
public class SidebarTrigger public constructor(62
private val props: Props<SidebarTriggerProps>,63
scope: ComponentScope64
): Component(scope) {65
public override fun render(): View {66
val input = props.value;67
val b = input.button68
return component(69
ButtonProps(70
b.content,71
{72
val current = props.value73
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.semantics86
),87
{ current, mount -> Button(current, scope = mount) }88
)89
}90
}92
public 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.semantics115
),116
{ current, mount -> SidebarMenuAction(current, scope = mount) },117
b.key118
)119
}121
public class SidebarMenuAction public constructor(122
private val props: Props<ButtonProps>,123
scope: ComponentScope124
): 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.0133
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.background151
),152
componentFocusWidth(theme, 2.0 * d),153
0.0154
)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.semantics172
),173
{ current, mount -> Button(current, scope = mount) }174
)175
}176
}178
private class SidebarIconInput(val size: Double, val color: Color)180
internal fun sidebarIcon(size: Double, color: Color): View =181
element(182
SidebarIconInput(size, color),183
{ current, mount -> SidebarIcon(current, scope = mount) }184
)186
private class SidebarIcon(187
private val props: Props<SidebarIconInput>,188
scope: ComponentScope189
): Element<Unit>(scope) {190
private var document: SvgDocument? = None191
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
Unit196
> =197
LayoutResult(198
LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),199
Unit200
)201
public override fun scene(context: SceneContext, measured: Unit) {202
val input = props.value203
val rtl = context.layoutDirection == LayoutDirection.Rtl204
val size = context.size205
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
}221
}