scoop:scoop.ui.component:0.1.0
src/resizable/keyboard.scoop
Selected production source · src/resizable/keyboard.scoop
1
package scoop.ui.component3
import scoop.ui.*4
import scoop.ui.style.*5
import scoop.window.*7
internal enum ResizableKey {8
Minimum, Maximum, Step(Double), Page(Double), Toggle9
}11
internal fun resizableKey(12
orientation: Orientation,13
direction: LayoutDirection,14
key: LogicalKey15
): ResizableKey? {16
val horizontal = orientation == Orientation.Horizontal17
val forward =18
if (horizontal && direction == LayoutDirection.Rtl)19
-1.020
else21
1.022
return when (key) {23
case Named(NamedKey.Home) -> Some(ResizableKey.Minimum)24
case Named(NamedKey.End) -> Some(ResizableKey.Maximum)25
case Named(NamedKey.PageUp) -> Some(ResizableKey.Page(1.0))26
case Named(NamedKey.PageDown) -> Some(ResizableKey.Page(-1.0))27
case Named(NamedKey.Enter) -> Some(ResizableKey.Toggle)28
case Named(NamedKey.ArrowRight) -> if (horizontal)29
Some(ResizableKey.Step(forward))30
else31
None32
case Named(NamedKey.ArrowLeft) -> if (horizontal)33
Some(ResizableKey.Step(-forward))34
else35
None36
case Named(NamedKey.ArrowDown) -> if (!horizontal)37
Some(ResizableKey.Step(1.0))38
else39
None40
case Named(NamedKey.ArrowUp) -> if (!horizontal)41
Some(ResizableKey.Step(-1.0))42
else43
None44
else -> None45
}46
}48
internal fun resizableKeyboard(49
props: ResizableProps,50
values: Array<ResizableSize>,51
target: Double,52
index: Long,53
key: ResizableKey54
): Array<ResizableSize> {55
val model = resizableValues(values)56
val sizes = resizableAllocation(props.panels, model, target)57
val total = sizes[index] + sizes[index + 1L]58
val step = upperBound(total, props.keyboardStep)59
val delta =60
when (key) {61
case Minimum -> -total62
case Maximum -> total63
case Step(val sign) -> sign * step64
case Page(val sign) -> sign *65
(66
if (step >= total / 10.0)67
total68
else69
step * 10.070
)71
case Toggle -> {72
val selected =73
if (props.panels[index].collapsible)74
index75
else76
index + 1L77
val panel = props.panels[selected]78
if (!panel.collapsible)79
return values80
val value = model[panel.key] ?: ResizableSize(panel.key, 1.0)81
if (value.collapsed && total == 0.0) {82
val restored = ArrayList<ResizableSize>()83
for (old in values)84
restored.add(85
if (old.key == panel.key)86
ResizableSize(old.key, old.fraction)87
else88
old89
)90
return restored.toArray()91
}92
var all = 0.093
for (size in sizes)94
all += size95
val desired =96
if (value.collapsed)97
lowerBound(98
panel.minimum,99
if (value.fraction > 0.0)100
all * value.fraction101
else102
total / 2.0103
)104
else105
panel.collapsedSize;106
(desired - sizes[selected]) *107
(108
if (selected == index)109
1.0110
else111
-1.0112
)113
}114
}115
val pair =116
resizablePair(117
sizes[index],118
sizes[index + 1L],119
delta,120
props.panels[index],121
props.panels[index + 1L]122
)123
if (pair.first == sizes[index] && pair.second == sizes[index + 1L])124
return values125
val next =126
Array<Double>(sizes.size) {127
i -> if (i == index)128
pair.first129
else130
if (i == index + 1L)131
pair.second132
else133
sizes[i]134
}135
return resizableModel(props.panels, values, sizes, next)136
}