scoop:scoop.ui.component:0.1.0
src/resizable/allocation.scoop
Selected production source · src/resizable/allocation.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.math.abs5
import scoop.ui.*7
internal fun resizableAllocation(8
panels: Array<ResizablePanelProps>,9
values: HashMap<ViewKey, ResizableSize>,10
target: Double11
): Array<Double> {12
var fixed = 0.013
var weight = 0.014
var expanded = 0L15
for (panel in panels) {16
val value = values[panel.key] ?: ResizableSize(panel.key, 1.0)17
if (panel.collapsible && value.collapsed)18
fixed += panel.collapsedSize19
else {20
weight += value.fraction21
expanded += 1L22
}23
}24
nonnegative(fixed, "collapsed panel total overflow")25
val space = lowerBound(0.0, target - fixed)26
val totalWeight = weight27
val count = expanded28
val bases =29
Array<Double>(panels.size) {30
index ->31
val panel = panels[index]32
val value = values[panel.key] ?: ResizableSize(panel.key, 1.0)33
if (panel.collapsible && value.collapsed)34
panel.collapsedSize35
else36
space *37
(38
if (totalWeight > 0.0)39
value.fraction / totalWeight40
else41
1.0 / count.toDouble()42
)43
}44
val options =45
Array<FlexItemOptions>(panels.size) {46
index ->47
val panel = panels[index]48
val value = values[panel.key] ?: ResizableSize(panel.key, 1.0)49
if (panel.collapsible && value.collapsed)50
FlexItemOptions(51
grow = 0.0,52
shrink = 0.0,53
min = panel.collapsedSize,54
max = Some(panel.collapsedSize)55
)56
else57
FlexItemOptions(58
grow =59
if (value.fraction > 0.0)60
value.fraction61
else62
1.0,63
min = panel.minimum,64
max = panel.maximum65
)66
}67
return allocateFlex(bases, options, target)68
}70
private fun resizableExtentAllowed(value: Double, panel: ResizablePanelProps): Boolean =71
(panel.collapsible && value == panel.collapsedSize) ||72
(value >= panel.minimum && value <= (panel.maximum ?: value))74
/** Projects the pair onto its actual allowed sizes, including discrete collapsed extents. */75
internal fun resizablePair(76
first: Double,77
second: Double,78
delta: Double,79
a: ResizablePanelProps,80
b: ResizablePanelProps81
): SplitSizes {82
val total = first + second83
val lowA =84
if (a.collapsible)85
a.collapsedSize86
else87
a.minimum88
val lowB =89
if (b.collapsible)90
b.collapsedSize91
else92
b.minimum93
val raw = resizePair(first, second, delta, lowA, a.maximum, lowB, b.maximum)94
val candidates =95
[raw.first, a.minimum, a.collapsedSize, total - b.minimum, total - b.collapsedSize, first]96
var selected = first97
var distance = abs(first - raw.first)98
for (candidate in candidates) {99
if (!resizableExtentAllowed(candidate, a) || !resizableExtentAllowed(total - candidate, b))100
continue101
val next = abs(candidate - raw.first)102
if (103
next <104
distance ||105
(106
next ==107
distance &&108
(109
if (delta > 0.0)110
candidate > selected111
else112
candidate < selected113
)114
)115
) {116
selected = candidate117
distance = next118
}119
}120
// Keep the exact collapsed boundary; another delta round-trip can introduce a tiny extent.121
return if (selected == first)122
SplitSizes(first, second)123
else124
SplitSizes(selected, total - selected)125
}127
internal fun resizableModel(128
panels: Array<ResizablePanelProps>,129
initial: Array<ResizableSize>,130
before: Array<Double>,131
next: Array<Double>132
): Array<ResizableSize> {133
val previous = resizableValues(initial)134
var total = 0.0135
for (extent in next)136
total += extent137
if (total == 0.0)138
return initial139
val changes = HashMap<ViewKey, ResizableSize>()140
for (index in 0L ..< panels.size) {141
val panel = panels[index]142
val old = previous[panel.key]143
val collapsed =144
panel.collapsible &&145
next[index] ==146
panel.collapsedSize &&147
(before[index] != next[index] || (old?.collapsed ?: false))148
val fraction =149
if (collapsed) {150
if (old?.collapsed ?: false)151
old!!.fraction152
else153
before[index] / total154
} else155
next[index] / total156
changes[panel.key] =157
ResizableSize(panel.key, upperBound(1.0, lowerBound(0.0, fraction)), collapsed)158
}159
val result = ArrayList<ResizableSize>()160
for (value in initial)161
result.add(changes[value.key] ?: value)162
for (panel in panels)163
if (previous[panel.key] == None)164
result.add(changes[panel.key]!!)165
return result.toArray()166
}168
internal fun sameResizablePanels(169
a: Array<ResizablePanelProps>,170
b: Array<ResizablePanelProps>171
): Boolean {172
if (a.size != b.size)173
return false174
for (index in 0L ..< a.size) {175
val x = a[index];176
val y = b[index]177
if (178
x.key !=179
y.key ||180
x.minimum !=181
y.minimum ||182
x.maximum !=183
y.maximum ||184
x.collapsible !=185
y.collapsible ||186
x.collapsedSize !=187
y.collapsedSize188
)189
return false190
}191
return true192
}