Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/component.scoop

Selected production source · src/sidebar/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.*
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.ui.style.*
9import scoop.ui.component.drawer as sidebarDrawer
11public class Sidebar public constructor(
12 private val props: Props<SidebarProps>,
13 scope: ComponentScope
14): Component(scope) {
15 private val themes = ThemeAccess(scope)
16 private val mobile = state(false)
17 private val scroll = ScrollState()
18 private val layer = OverlayHandle()
19 public override fun onMount() {
20 themes.mount(props.value.theme)
21 }
22 private fun toggle() {
23 val input = props.value
24 if (!input.enabled || input.collapsible == SidebarCollapse.None)
25 return
26 if (mobile.value && input.open.value)
27 layer.requestClose()
28 else
29 sidebarWrite(input.open, !input.open.value, { next -> props.value.onOpenChange(next) })
30 }
31 public override fun render(): View {
32 val input = props.value
33 val theme = themes.read(input.theme)
34 val density = theme.tokens.typography.body.fontSize / 14.0
35 val narrow =
36 input.collapsible !=
37 SidebarCollapse.None &&
38 when (input.layout) {
39 case Auto -> mobile.value
40 case Desktop -> false
41 case Mobile -> true
42 }
43 val shown = sidebarPresentation(input, narrow)
44 val palette = theme.tokens.palette.sidebar
45 val header = sidebarRegion(input.header, shown, density)
46 val footer = sidebarRegion(input.footer, shown, density)
47 val body = sidebarContent(input.content, shown)
48 val scroll = input.scrollState ?: this.scroll
49 val layer = this.layer
50 val current = props
51 val toggle = { toggle() }
52 val panel =
53 if (narrow) {
54 sidebarDrawer(input.open, sidebarKeys(body, input, toggle)) {
55 side =
56 if (input.side == SidebarSide.Start)
57 SheetSide.Start
58 else
59 SheetSide.End
60 width = Some(input.mobileWidth)
61 contentPadding = 0.0
62 scrollable = true;
63 scrollState = Some(scroll)
64 focus = input.focus;
65 handle = Some(layer)
66 enabled = input.enabled;
67 fonts = input.fonts;
68 this.theme = input.theme
69 if (panelPresent(input.header))
70 header(sidebarKeys(header, input, toggle))
71 if (panelPresent(input.footer))
72 footer(sidebarKeys(footer, input, toggle))
73 transition = input.transition
74 onOpenChange = { next -> current.value.onOpenChange(next) }
75 onCloseRequest = { reason -> current.value.onCloseRequest(reason) }
76 onClosed = { reason -> current.value.onClosed(reason) }
77 style =
78 styleOverride {
79 background(Some(Paint(palette.background.background)))
80 corners(Corners.all(0.0));
81 border(None)
82 }.then(input.style)
83 }
84 } else {
85 val content =
86 element(
87 SidebarPanelInput(input, theme),
88 { value, mount -> SidebarPanel(value, scope = mount) },
89 [
90 header,
91 scrollArea(body) {
92 state = Some(scroll)
93 scrollbars = ScrollbarVisibility.Hidden
94 focus = FocusOptions(tabStop = false)
95 enabled = input.enabled;
96 this.theme = input.theme
97 },
98 footer
99 ]
100 )
101 element(
102 SidebarPaneInput(input, shown, density),
103 { value, mount -> SidebarPane(value, scope = mount) },
104 [
105 sidebarGate(
106 panelFrame(content, visual = sidebarSurface(input, theme)),
107 input.enabled && (shown.expanded || shown.collapsed),
108 true
109 )
110 ]
111 )
112 }
113 val main =
114 panelFrame(
115 input.inset ?: empty(),
116 visual =
117 styleOverride {
118 background(Some(Paint(theme.tokens.palette.background.background)))
119 if (!narrow && input.variant == SidebarVariant.Inset) {
120 corners(Corners.all(12.0 * density));
121 boxShadows(theme.tokens.effects.raised)
122 }
123 }.then(input.insetStyle)
124 )
125 return element(
126 SidebarHostInput(
127 input,
128 theme,
129 shown,
130 {
131 value -> if (mobile.value != value)
132 mobile.value = value
133 },
134 toggle
135 ),
136 { value, mount -> SidebarHost(value, scope = mount) },
137 [panel, main, sidebarRail(input, theme, toggle)]
138 )
139 }
142private fun sidebarRegion(
143 content: SidebarContent?,
144 shown: SidebarPresentation,
145 density: Double
146): View =
147 when (content) {
148 case Some(val value) -> panelFrame(sidebarContent(value, shown), Insets.all(8.0 * density))
149 case None -> empty()
150 }
152private fun sidebarSurface(input: SidebarProps, theme: Theme): StyleOverride {
153 val density = theme.tokens.typography.body.fontSize / 14.0
154 val colors = theme.tokens.palette.sidebar
155 return styleOverride {
156 background(Some(Paint(colors.background.background)))
157 if (input.variant == SidebarVariant.Floating) {
158 corners(Corners.all(8.0 * density))
159 border(Some(Border.all(density, colors.border)))
160 boxShadows(theme.tokens.effects.raised)
161 } else {
162 corners(Corners.all(0.0))
163 if (input.variant == SidebarVariant.Sidebar)
164 border(
165 Some(
166 if (input.side == SidebarSide.Start)
167 Border.Logical(end = BorderEdge(density, colors.border))
168 else
169 Border.Logical(start = BorderEdge(density, colors.border))
170 )
171 )
172 }
173 }.then(input.style)
176internal fun sidebarTransition(input: SidebarProps, theme: Theme): AnimationSpec? =
177 theme.tokens.motion.resolve(
178 input.transition.resolve(Some(AnimationSpec.Tween(Duration.milliseconds(200L))))
179 )
181internal class SidebarPanelInput(val props: SidebarProps, val theme: Theme)
183internal class SidebarPanel(
184 private val props: Props<SidebarPanelInput>,
185 scope: ComponentScope
186): Element<Unit>(scope) {
187 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
188 Unit
189 > {
190 val width = constraints.maxWidth ?: constraints.minWidth
191 val header = context.measureChild(0L, Constraints(width, Some(width)))
192 val footer = context.measureChild(2L, Constraints(width, Some(width)))
193 val height =
194 when (constraints.maxHeight) {
195 case Some(val value) -> Some(
196 max(0.0, value - header.size.height - footer.size.height)
197 )
198 case None -> None
199 }
200 val body = context.measureChild(1L, Constraints(width, Some(width), maxHeight = height))
201 val size =
202 constraints.constrain(
203 Size(
204 width,
205 constraints.maxHeight ?:
206 (header.size.height + body.size.height + footer.size.height)
207 )
208 )
209 context.placeChild(0L, Point(0.0, 0.0))
210 context.placeChild(1L, Point(0.0, header.size.height))
211 context.placeChild(2L, Point(0.0, max(0.0, size.height - footer.size.height)))
212 return LayoutResult(LayoutMetrics(size), Unit)
213 }
214 public override fun scene(context: SceneContext, measured: Unit) {
215 context.children()
216 }