Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/menu.scoop

Selected production source · src/sidebar/menu.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.component.column as sidebarColumn
9public class SidebarMenu public constructor(
10 private val props: Props<SidebarMenuProps>,
11 scope: ComponentScope
12): Component(scope) {
13 public override fun render(): View {
14 val input = props.value
15 return panelFrame(
16 sidebarColumn {
17 for (item in input.items)
18 add(
19 component(
20 SidebarItemInput(input, item),
21 { current, mount -> SidebarMenuItem(current, scope = mount) },
22 item.key
23 )
24 )
25 spacing = 0.0;
26 alignment = CrossAlignment.Stretch
27 },
28 visual = input.style
29 )
30 }
33internal class SidebarItemInput(val menu: SidebarMenuProps, val item: SidebarMenuItemProps)
35internal class SidebarRowSpace {
36 var highlighted = false
39internal class SidebarRowInput(
40 val input: SidebarItemInput,
41 val density: Double,
42 val space: SidebarRowSpace
45private class SidebarMenuItem(
46 private val props: Props<SidebarItemInput>,
47 scope: ComponentScope
48): Component(scope) {
49 private val themes = ThemeAccess(scope)
50 private val space = SidebarRowSpace()
51 public override fun onMount() {
52 themes.mount(props.value.menu.theme)
53 }
54 public override fun render(): View {
55 val input = props.value
56 val menu = input.menu;
57 val item = input.item
58 val theme = themes.read(menu.theme)
59 val density = theme.tokens.typography.body.fontSize / 14.0
60 val direction = scope.layoutDirection.textDirection()
61 val row = SidebarRowInput(input, density, space)
62 val button =
63 makeResolvedButton(
64 {
65 appearance ->
66 val text =
67 appearance.text.copy {
68 color =
69 if (
70 item.active ||
71 appearance.interaction.hovered ||
72 appearance.interaction.pressed
73 )
74 theme.tokens.palette.sidebar.accent.foreground
75 else
76 theme.tokens.palette.sidebar.background.foreground
77 }
78 val resolved =
79 ActionContentStyle(
80 text,
81 16.0 * density,
82 8.0 * density,
83 appearance.interaction,
84 appearance.underline
85 )
86 val label =
87 item.content ?:
88 component(
89 LabelProps(
90 LabelText.Plain(item.label, None),
91 menu.fonts,
92 Some(
93 paragraphStyle {
94 this.direction = direction;
95 maxLines = Some(1L);
96 ellipsis = "…"
97 }
98 ),
99 baseStyle = Some(text)
100 ),
101 { current, mount -> Label(current, scope = mount) }
102 )
103 element(
104 row,
105 { current, mount -> SidebarCaption(current, scope = mount) },
106 [
107 sidebarAdornment(item.icon, resolved),
108 label,
109 sidebarGate(
110 sidebarAdornment(item.badge, resolved),
111 menu.enabled,
112 !menu.collapsed && panelPresent(item.badge)
113 )
114 ]
115 )
116 },
117 item.onClick
118 ) {
119 enabled = menu.enabled && item.enabled;
120 focus = item.focus;
121 this.theme = menu.theme
122 variant = ButtonVariant.Ghost
123 style = sidebarMenuStyle(theme, item, menu.collapsed)
124 }
125 val main =
126 tooltip(button, item.label) {
127 enabled = menu.collapsed && menu.enabled && item.enabled
128 side = OverlaySide.End;
129 this.fonts = menu.fonts;
130 this.theme = menu.theme
131 }
132 val submenu =
133 when (item.submenu) {
134 case Some(val value) -> {
135 val content = sidebarSubmenu(value) { this.theme = menu.theme }
136 when (item.submenuOpen) {
137 case Some(val open) -> collapsible(open, content) {
138 retention = ContentRetention.KeepMounted;
139 spacing = 0.0;
140 this.theme = menu.theme
141 }
142 case None -> content
143 }
144 }
145 case None -> empty()
146 }
147 return element(
148 row,
149 { current, mount -> SidebarRow(current, scope = mount) },
150 [
151 main,
152 sidebarGate(
153 element(
154 row,
155 { current, mount -> SidebarActionVisibility(current, scope = mount) },
156 [item.action ?: empty()]
157 ),
158 menu.enabled && item.enabled && !menu.collapsed,
159 !menu.collapsed && panelPresent(item.action)
160 ),
161 sidebarGate(
162 submenu,
163 menu.enabled && !menu.collapsed,
164 !menu.collapsed && panelPresent(item.submenu)
165 )
166 ]
167 )
168 }
171internal fun sidebarMenuStyle(
172 theme: Theme,
173 input: SidebarMenuItemProps,
174 collapsed: Boolean
175): ButtonStyle {
176 val d = theme.tokens.typography.body.fontSize / 14.0
177 val colors = theme.tokens.palette.sidebar
178 val local = input.style
179 val hover = styleOverride { background(Some(Paint(colors.accent.background))) }
180 return local.copy {
181 surface {
182 if (suggestedSize == None)
183 suggestedSize = Some(Size(0.0, sidebarRowHeight(input.size, collapsed) * d))
184 if (minimumSize == None)
185 minimumSize = Some(Size(0.0, 0.0))
186 if (padding == None)
187 padding = Some(Insets(8.0 * d, 0.0, 8.0 * d, 0.0))
188 if (focus == None)
189 focus =
190 Some(
191 OutlineStyle(
192 componentTint(
193 theme,
194 colors.ring.withAlpha(1.0),
195 0.5,
196 colors.background.background
197 ),
198 componentFocusWidth(theme, 2.0 * d),
199 0.0
200 )
201 )
202 style =
203 styleOverride {
204 border(
205 if (input.variant == SidebarMenuVariant.Outline)
206 Some(Border.all(d, colors.border))
207 else
208 None
209 )
210 background(
211 if (input.active)
212 Some(Paint(colors.accent.background))
213 else
214 if (input.variant == SidebarMenuVariant.Outline)
215 Some(Paint(theme.tokens.palette.background.background))
216 else
217 None
218 )
219 corners(Corners.all(6.0 * d));
220 boxShadows([])
221 }.then(local.surface.style)
222 }
223 text =
224 Some(
225 (local.text ?: spanStyle()).copy {
226 if (fontSize == None)
227 fontSize =
228 Some(
229 (
230 if (input.size == SidebarMenuSize.Sm && !collapsed)
231 12.0
232 else
233 14.0
234 ) *
235 d
236 )
237 if (weight == None)
238 weight =
239 Some(
240 if (input.active)
241 500
242 else
243 400
244 )
245 }
246 )
247 states {
248 hovered = hover.then(local.surface.style).then(local.states.hovered)
249 pressed = hover.then(local.surface.style).then(local.states.pressed)
250 }
251 }