Scoop API

scoop:scoop.ui.component:0.1.0

src/sidebar/menu_layout.scoop

Selected production source · src/sidebar/menu_layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.*
6import scoop.time.Duration
7import scoop.ui.*
9internal class SidebarRow(
10 private val props: Props<SidebarRowInput>,
11 scope: ComponentScope
12): Element<Double>(scope) {
13 private var area: InputArea? = None
14 private var hover: Hover? = None
15 private fun open(): Binding<Boolean> =
16 binding(
17 { props.value.space.highlighted },
18 {
19 value -> if (scope.isMounted && props.value.space.highlighted != value) {
20 props.value.space.highlighted = value;
21 scope.requestFrame()
22 }
23 }
24 )
25 private fun options(): HoverOptions =
26 HoverOptions(
27 Duration.ZERO,
28 Duration.ZERO,
29 Duration.ZERO,
30 focus = true,
31 enabled =
32 props.value.input.menu.enabled &&
33 props.value.input.item.enabled &&
34 props.value.input.item.actionOnHover &&
35 !props.value.input.menu.collapsed,
36 longPress = LongPressOptions()
37 )
38 public override fun onMount() {
39 area = Some(scope.createArea());
40 area!!.ignoreSelf = true;
41 scope.routeChildren(area!!)
42 hover = Some(scope.hover(area!!, open(), options()))
43 }
44 public override fun update(): UiInvalidation {
45 hover!!.update(open(), Some(options()))
46 return UiInvalidation.Layout
47 }
48 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
49 Double
50 > {
51 val input = props.value
52 val d = input.density
53 val compact = input.input.menu.collapsed
54 val rowHeight = sidebarRowHeight(input.input.item.size, compact) * d
55 val action = context.measureChild(1L, Constraints.tight(Size(20.0 * d, 20.0 * d)))
56 val desiredWidth =
57 if (compact)
58 Some(min(32.0 * d, constraints.maxWidth ?: 32.0 * d))
59 else
60 constraints.maxWidth
61 val main =
62 context.measureChild(
63 0L,
64 Constraints(desiredWidth ?: 0.0, desiredWidth, rowHeight, Some(rowHeight))
65 )
66 val width = constraints.constrain(Size(main.size.width, rowHeight)).width
67 val submenu = context.measureChild(2L, Constraints(width, Some(width)))
68 val size = constraints.constrain(Size(width, rowHeight + submenu.size.height))
69 context.placeChild(
70 0L,
71 Point(logicalX(0.0, main.size.width, width, context.layoutDirection), 0.0)
72 )
73 val end = width - 8.0 * d
74 context.placeChild(
75 1L,
76 Point(
77 logicalX(
78 end - action.size.width,
79 action.size.width,
80 width,
81 context.layoutDirection
82 ),
83 (rowHeight - action.size.height) / 2.0
84 )
85 )
86 context.placeChild(2L, Point(0.0, rowHeight))
87 return LayoutResult(LayoutMetrics(size), rowHeight)
88 }
89 public override fun scene(context: SceneContext, rowHeight: Double) {
90 context.place(area!!, Rect(0.0, 0.0, context.size.width, rowHeight))
91 context.children()
92 }
95internal class SidebarCaption(
96 private val props: Props<SidebarRowInput>,
97 scope: ComponentScope
98): Element<Unit>(scope) {
99 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
100 Unit
101 > {
102 val input = props.value
103 val d = input.density
104 val trailing =
105 if (!input.input.menu.collapsed && panelPresent(input.input.item.action))
106 24.0 * d
107 else
108 0.0
109 val hasIcon = panelPresent(input.input.item.icon)
110 val compact = input.input.menu.collapsed
111 val badge =
112 context.measureChild(
113 2L,
114 Constraints(maxWidth = constraints.maxWidth, maxHeight = Some(20.0 * d))
115 )
116 val icon =
117 if (hasIcon)
118 context.measureChild(
119 0L,
120 Constraints(16.0 * d, Some(16.0 * d), 16.0 * d, Some(16.0 * d))
121 )
122 else {
123 context.skipChild(0L);
124 LayoutMetrics(Size(0.0, 0.0))
125 }
126 val gap =
127 if (hasIcon && !compact)
128 8.0 * d
129 else
130 0.0
131 val tail =
132 if (compact)
133 0.0
134 else
135 trailing +
136 if (badge.size.width > 0.0)
137 badge.size.width + 8.0 * d
138 else
139 0.0
140 val maximum =
141 when (constraints.maxWidth) {
142 case Some(val value) -> Some(max(0.0, value - icon.size.width - gap - tail))
143 case None -> None
144 }
145 val label =
146 if (compact) {
147 context.skipChild(1L);
148 LayoutMetrics(Size(0.0, 0.0))
149 }
150 else
151 context.measureChild(
152 1L,
153 Constraints(maxWidth = maximum, maxHeight = constraints.maxHeight)
154 )
155 val size =
156 constraints.constrain(
157 Size(
158 constraints.maxWidth ?: (icon.size.width + gap + label.size.width + tail),
159 max(badge.size.height, max(icon.size.height, label.size.height))
160 )
161 )
162 if (hasIcon)
163 context.placeChild(
164 0L,
165 Point(
166 logicalX(0.0, icon.size.width, size.width, context.layoutDirection),
167 (size.height - icon.size.height) / 2.0
168 )
169 )
170 if (!compact)
171 context.placeChild(
172 1L,
173 Point(
174 logicalX(
175 icon.size.width + gap,
176 label.size.width,
177 size.width,
178 context.layoutDirection
179 ),
180 (size.height - label.size.height) / 2.0
181 )
182 )
183 context.placeChild(
184 2L,
185 Point(
186 logicalX(
187 max(0.0, size.width - trailing - badge.size.width),
188 badge.size.width,
189 size.width,
190 context.layoutDirection
191 ),
192 (size.height - badge.size.height) / 2.0
193 )
194 )
195 return LayoutResult(LayoutMetrics(size), Unit)
196 }
197 public override fun scene(context: SceneContext, measured: Unit) {
198 context.children()
199 }
202internal class SidebarActionVisibility(
203 private val props: Props<SidebarRowInput>,
204 scope: ComponentScope
205): Element<Unit>(scope) {
206 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
207 Unit
208 > {
209 val child = context.measureChild(0L, constraints);
210 context.placeChild(0L, Point(0.0, 0.0));
211 return LayoutResult(child, Unit)
212 }
213 public override fun scene(context: SceneContext, measured: Unit) {
214 val transparent = props.value.input.item.actionOnHover && !props.value.space.highlighted
215 if (transparent)
216 context.saveLayer(0.0)
217 context.children()
218 if (transparent)
219 context.restore()
220 }