scoop:scoop.ui.component:0.1.0
src/resizable/component.scoop
Selected production source · src/resizable/component.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.graphics.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.ui.style.*9
internal class ResizableOwner {10
var control: ResizableControl? = None11
}13
internal class ResizableInput(14
val props: ResizableProps,15
val values: Array<ResizableSize>,16
val model: HashMap<ViewKey, ResizableSize>,17
val theme: Theme,18
val owner: ResizableOwner19
)21
internal class ResizableGeometry(22
val size: Size,23
val target: Double,24
val lengths: Array<Double>,25
val input: ResizableProps,26
val direction: LayoutDirection27
)29
public class Resizable public constructor(30
private val props: Props<ResizableProps>,31
scope: ComponentScope32
): Component(scope) {33
private val themes = ThemeAccess(scope)34
private val owner = ResizableOwner()35
private var checked: Array<ResizableSize>? = None36
private var model = HashMap<ViewKey, ResizableSize>()37
public override fun onMount() {38
themes.mount(props.value.theme)39
}40
public override fun render(): View {41
val input = props.value42
val values = input.sizes.value43
val same =44
when (checked) {45
case Some(val previous) -> previous === values;46
case None -> false47
}48
if (!same) {49
model = resizableValues(values)50
checked = Some(values)51
}52
val config = ResizableInput(input, values, model, themes.read(input.theme), owner)53
val children =54
Array<View>(input.panels.size) {55
index ->56
val panel = input.panels[index]57
val part = ResizablePanelInput(config, index)58
element(59
part,60
{ current, mount -> ResizablePanelLayout(current, scope = mount) },61
[62
element(63
panel.style,64
{ current, mount -> ResizableContent(current, scope = mount) },65
[panel.content],66
key = ViewKey.Text("content")67
),68
if (part.hasHandle)69
element(70
part,71
{ current, mount -> ResizableHandle(current, scope = mount) },72
key = ViewKey.Text("handle")73
)74
else75
empty()76
],77
key = panel.key78
)79
}80
return element(81
config,82
{ current, mount -> ResizableLayout(current, scope = mount) },83
children84
)85
}86
}88
private class ResizableLayout(89
private val props: Props<ResizableInput>,90
scope: ComponentScope91
): Element<ResizableGeometry>(scope) {92
private var painter: StylePainter? = None93
public override fun onMount() {94
props.value.owner.control = Some(ResizableControl(scope, { props.value }))95
painter = Some(scope.resource { StylePainter() })96
}97
public override fun update(): UiInvalidation {98
props.value.owner.control!!.update()99
return UiInvalidation.Layout100
}101
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<102
ResizableGeometry103
> {104
val input = props.value105
val horizontal = input.props.orientation == Orientation.Horizontal106
val thickness = input.theme.tokens.typography.body.fontSize / 14.0107
val length =108
input.props.length ?:109
(110
if (horizontal)111
constraints.maxWidth112
else113
constraints.maxHeight114
) ?:115
320.0116
val base = constraints.constrain(resizableSize(length, 0.0, horizontal))117
val main =118
if (horizontal)119
base.width120
else121
base.height122
val gap = thickness * lowerBound(0.0, (context.childCount - 1L).toDouble())123
nonnegative(gap, "resizable separator total overflow")124
val target = lowerBound(0.0, main - gap)125
val sizes = resizableAllocation(input.props.panels, input.model, target)126
val maxCross =127
if (horizontal)128
constraints.maxHeight129
else130
constraints.maxWidth131
var cross = maxCross ?: 0.0132
if (maxCross == None) {133
for (index in 0L ..< context.childCount) {134
val extent =135
sizes[index] +136
(137
if (index + 1L < context.childCount)138
thickness139
else140
0.0141
)142
val measured =143
context.measureChild(144
index,145
if (horizontal)146
Constraints(minWidth = extent, maxWidth = Some(extent))147
else148
Constraints(minHeight = extent, maxHeight = Some(extent))149
)150
cross =151
lowerBound(152
cross,153
if (horizontal)154
measured.size.height155
else156
measured.size.width157
)158
}159
}160
val size = constraints.constrain(resizableSize(main, cross, horizontal))161
val actualCross =162
if (horizontal)163
size.height164
else165
size.width166
var offset = 0.0167
for (index in 0L ..< context.childCount) {168
val extent =169
sizes[index] +170
(171
if (index + 1L < context.childCount)172
thickness173
else174
0.0175
)176
context.measureChild(177
index,178
Constraints.tight(resizableSize(extent, actualCross, horizontal))179
)180
context.placeChild(181
index,182
if (horizontal)183
Point(logicalX(offset, extent, size.width, context.layoutDirection), 0.0)184
else185
Point(0.0, offset)186
)187
offset += extent188
}189
val shape = ResizableGeometry(size, target, sizes, input.props, context.layoutDirection)190
return LayoutResult(LayoutMetrics(size), shape)191
}192
public override fun scene(context: SceneContext, shape: ResizableGeometry) {193
val order = Array<Long>(shape.lengths.size) { index -> shape.lengths.size - 1L - index }194
painter!!.scene(context, props.value.props.style.resolve(style())) {195
local -> containerScene(local, props.value.props.clip, Some(order))196
}197
context.onFrameComplete {198
result -> if (result.submission == FrameSubmission.Accepted)199
props.value.owner.control!!.accept(shape)200
}201
}202
public override fun onUnmount() {203
props.value.owner.control!!.retire()204
}205
}207
internal fun resizableSize(main: Double, cross: Double, horizontal: Boolean): Size =208
if (horizontal)209
Size(main, cross)210
else211
Size(cross, main)