Scoop API

scoop:scoop.ui.component:0.1.0

src/input_group/layout.scoop

Selected production source · src/input_group/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
7internal fun measureInputGroup(
8 ink: InputGroupInk,
9 context: MeasureContext,
10 constraints: Constraints
11): LayoutMetrics {
12 val input = ink.props.value
13 val addons = input.props.addons
14 val visual = ink.visual()
15 val options = ink.options()
16 val metrics = input.theme.surfaceMetrics(visual, options, context.layoutDirection)
17 val inset = metrics.contentInsets.totals()
18 val width =
19 constraints.constrain(
20 Size(
21 constraints.maxWidth ?:
22 lowerBound(options.suggestedSize?.width ?: 240.0, metrics.minimumSize.width),
23 0.0
24 )
25 ).width
26 val innerWidth = lowerBound(width - inset.width, 0.0)
27 var remainingHeight =
28 when (constraints.maxHeight) {
29 case Some(val value) -> Some(lowerBound(value - inset.height, 0.0))
30 case None -> None
31 }
32 val measured =
33 MutableArray<LayoutMetrics>(context.childCount) { _ -> LayoutMetrics(Size(0.0, 0.0)) }
34 var top = 0.0
35 var bottom = 0.0
36 for (index in 0L ..< addons.size) {
37 val align = addons[index].align
38 if (!inputGroupBlock(align))
39 continue
40 val child =
41 context.measureChild(
42 index + 1L,
43 Constraints(
44 minWidth = innerWidth,
45 maxWidth = Some(innerWidth),
46 maxHeight = remainingHeight
47 )
48 )
49 measured[index + 1L] = child
50 if (align == InputGroupAlign.BlockStart)
51 top += child.size.height
52 else
53 bottom += child.size.height
54 remainingHeight =
55 when (remainingHeight) {
56 case Some(val value) -> Some(lowerBound(value - child.size.height, 0.0))
57 case None -> None
58 }
59 }
60 var remainingWidth = innerWidth
61 var start = 0.0
62 var end = 0.0
63 var rowHeight = 0.0
64 for (index in 0L ..< addons.size) {
65 val align = addons[index].align
66 if (inputGroupBlock(align))
67 continue
68 val child =
69 context.measureChild(
70 index + 1L,
71 Constraints(maxWidth = Some(remainingWidth), maxHeight = remainingHeight)
72 )
73 measured[index + 1L] = child
74 remainingWidth = lowerBound(remainingWidth - child.size.width, 0.0)
75 rowHeight = lowerBound(rowHeight, child.size.height)
76 if (align == InputGroupAlign.InlineStart)
77 start += child.size.width
78 else
79 end += child.size.width
80 }
81 val minimum =
82 lowerBound(
83 constraints.minHeight,
84 lowerBound(metrics.minimumSize.height, metrics.suggestedSize.height)
85 )
86 val naturalRow = lowerBound(rowHeight, lowerBound(minimum - inset.height - top - bottom, 0.0))
87 val minRow = upperBound(naturalRow, remainingHeight ?: naturalRow)
88 val control =
89 context.measureChild(
90 0L,
91 Constraints(
92 minWidth = remainingWidth,
93 maxWidth = Some(remainingWidth),
94 minHeight = minRow,
95 maxHeight = remainingHeight
96 )
97 )
98 rowHeight = control.size.height
99 val size = constraints.constrain(Size(width, inset.height + top + rowHeight + bottom))
100 val geometry =
101 input.theme.surfaceGeometry(
102 Rect(0.0, 0.0, size.width, size.height),
103 visual,
104 options,
105 context.layoutDirection
106 )
107 val content = geometry.content
108 val bodyY = content.y + top
109 if (size.width <= 0.0 || size.height <= 0.0)
110 return offsetMetrics(control, size, bodyY)
111 context.placeChild(
112 0L,
113 Point(
114 content.x + logicalX(start, remainingWidth, innerWidth, context.layoutDirection),
115 bodyY
116 )
117 )
118 var topY = content.y
119 var bottomY = bodyY + rowHeight
120 var startX = 0.0
121 var endX = innerWidth - end
122 for (index in 0L ..< addons.size) {
123 val child = measured[index + 1L].size
124 val origin =
125 when (addons[index].align) {
126 case BlockStart -> {
127 val origin = Point(content.x, topY)
128 topY += child.height
129 origin
130 }
131 case BlockEnd -> {
132 val origin = Point(content.x, bottomY)
133 bottomY += child.height
134 origin
135 }
136 case InlineStart -> {
137 val origin =
138 Point(
139 content.x +
140 logicalX(startX, child.width, innerWidth, context.layoutDirection),
141 bodyY + (rowHeight - child.height) / 2.0
142 )
143 startX += child.width
144 origin
145 }
146 case InlineEnd -> {
147 val origin =
148 Point(
149 content.x +
150 logicalX(endX, child.width, innerWidth, context.layoutDirection),
151 bodyY + (rowHeight - child.height) / 2.0
152 )
153 endX += child.width
154 origin
155 }
156 }
157 context.placeChild(index + 1L, origin)
158 }
159 return offsetMetrics(control, size, bodyY)