scoop:scoop.ui.component:0.1.0
src/table/resize.scoop
Selected production source · src/table/resize.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.style.Theme8
import scoop.window.*9
import scoop.window.accessibility.*11
internal enum TableResizeOrigin {12
Programmatic, Pointer, Keyboard, Accessibility13
}15
internal class TableResizeInput(16
val width: Binding<Double?>,17
val size: TableColumnSize,18
val theme: Theme,19
val enabled: Boolean,20
val semantics: ComponentSemantics,21
val focus: FocusOptions = FocusOptions(),22
val onResizeStart: () -> Boolean = { true },23
val onResizeEnd: ((TableColumnResizeEnd) -> Unit)? = None,24
val onWidthChange: ((Double?, TableResizeOrigin) -> Unit)? = None25
) {26
val density = theme.tokens.typography.body.fontSize / 14.027
fun write(value: Double?, origin: TableResizeOrigin) {28
when (onWidthChange) {29
case Some(val change) -> change(value, origin)30
case None -> width.value = value31
}32
}33
}35
private class TableResizeGesture(36
val width: Double,37
val initial: Double?,38
val density: Double,39
val direction: LayoutDirection,40
val binding: Binding<Double?>,41
val write: (Double?, TableResizeOrigin) -> Unit,42
val onEnd: ((TableColumnResizeEnd) -> Unit)?43
) {44
var expected = initial45
}47
internal class TableResizeHeader(48
private val props: Props<TableResizeInput>,49
scope: ComponentScope50
) : Element<Unit>(scope) {51
private val window = scope.nativeWindow52
private var accepted: Double? = None53
private var gesture: TableResizeGesture? = None54
private var cursor = false55
private val area = scope56
.createArea(focusable = true) { context, event ->57
if (context.phase == RoutedPhase.Notification) {58
when (event) {59
case FocusChanged(false) -> cancel();60
else -> {}61
}62
if (scope.isMounted) {63
updateCursor();64
scope.requestFrame()65
}66
}67
}68
private val focus = scope.focusTarget(area, props.value.focus)69
private val semantics = createComponentSemantics(scope, SemanticRole.Separator)70
private val drag = scope71
.drag(72
area,73
options(),74
{ _ -> props.value.enabled && accepted != None },75
{ event -> receive(event) }76
)77
init {78
semantics?.bindFocus(Some(focus))79
area.scope.defaultAction = { context, event ->80
when (event) {81
case Keyboard(val key) -> {82
if (key.state ==83
ElementState.Pressed &&84
!key.synthetic &&85
area.isFocusActive &&86
props.value.enabled87
) {88
when (key.logicalKey) {89
case Named(NamedKey.Escape) ->90
if (panelPresent(gesture)) {91
cancel(TableResizeOrigin.Keyboard);92
context.consume()93
}95
case Named(NamedKey.Home) -> {96
cancel();97
props.value.write(None, TableResizeOrigin.Keyboard);98
context.consume()99
}101
case Named(NamedKey.ArrowLeft) -> {102
adjust(false);103
context.consume()104
}106
case Named(NamedKey.ArrowRight) -> {107
adjust(true);108
context.consume()109
}111
else -> {}112
}113
}114
}116
case Pointer(117
InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)118
) -> {119
if (props.value.enabled) focus.handle.requestFocus(FocusOrigin.Pointer)120
}122
else -> {}123
}124
}125
}126
private fun options(): DragOptions = DragOptions(127
DragAxis.Horizontal,128
enabled = props.value.enabled129
)130
private fun adjust(forward: Boolean) {131
val width = accepted ?: return132
val sign = if (forward != (scope.layoutDirection == LayoutDirection.Rtl)) 1.0 else -1.0133
props134
.value135
.write(136
Some(props.value.size.constrain(lowerBound(width + sign * 10.0, 0.0))),137
TableResizeOrigin.Keyboard138
)139
}140
private fun setNumber(value: Double): SemanticActionResult {141
if (!scope.isMounted) return SemanticActionResult.Unavailable142
if (!props.value.enabled) return SemanticActionResult.Rejected143
if (!value.isFinite() || value < 0.0) return SemanticActionResult.InvalidArgument144
if (accepted == None) return SemanticActionResult.Unavailable145
cancel()146
if (!scope.isMounted) return SemanticActionResult.Unavailable147
val next = props.value.size.constrain(value)148
props.value.write(Some(next), TableResizeOrigin.Accessibility)149
if (!scope.isMounted) return SemanticActionResult.Unavailable150
return if (props.value.width.value == Some(next))151
SemanticActionResult.Completed152
else153
SemanticActionResult.Rejected154
}155
private fun stepNumber(amount: Double): SemanticActionResult {156
val current = props.value.width.value ?:157
(accepted ?: return SemanticActionResult.Unavailable)158
return setNumber(lowerBound(current + amount, 0.0))159
}160
private fun updateSemantics() {161
val input = props.value162
val current = when (accepted) {163
case Some(val width) -> Some(input.size.constrain(input.width.value ?: width))164
case None -> None165
}166
semantics?.bindReference(input.semantics.reference)167
semantics168
?.update(169
input170
.semantics171
.properties(172
enabled = input.enabled,173
range = when (current) {174
case Some(val width) ->175
Some(176
SemanticValueRange(177
input.size.minimum,178
input.size.maximum ?: Double.MAX_VALUE,179
width,180
10.0181
)182
)183
case None -> None184
}185
)186
.copy { orientation = Some(SemanticOrientation.Vertical) },187
if (current != None)188
SemanticActions(189
setNumber = Some { value -> setNumber(value) },190
increment = Some { stepNumber(10.0) },191
decrement = Some { stepNumber(-10.0) }192
)193
else194
SemanticActions(),195
input.semantics.relations196
)197
}198
private fun receive(event: DragEvent) {199
when (event) {200
case Started(val sample) -> {201
val width = accepted ?: return202
val start = props.value203
if (!start.onResizeStart() || !scope.isMounted || !props.value.enabled) {204
drag.cancel()205
return206
}207
gesture = Some(208
TableResizeGesture(209
width,210
props.value.width.value,211
props.value.density,212
scope.layoutDirection,213
props.value.width,214
{ value, origin -> start.write(value, origin) },215
start.onResizeEnd216
)217
)218
move(sample.displacement)219
}221
case Moved(val sample) -> move(sample.displacement)223
case Ended(val sample) -> {224
move(sample.displacement)225
val previous = gesture226
gesture = None227
when (previous) {228
case Some(val active) -> active.onEnd?.invoke(TableColumnResizeEnd.Completed)229
case None -> {}230
}231
}233
case Cancelled(_, _) -> cancel(TableResizeOrigin.Pointer)234
}235
if (scope.isMounted) {236
updateCursor();237
scope.requestFrame()238
}239
}240
private fun move(delta: Double) {241
val active = gesture ?: return242
if (props.value.width.value != active.expected) {243
cancel();244
return245
}246
val logical = if (active.direction == LayoutDirection.Rtl) -delta else delta247
val next = props248
.value249
.size250
.constrain(lowerBound(active.width + logical / active.density, 0.0))251
active.expected = Some(next)252
props.value.write(Some(next), TableResizeOrigin.Pointer)253
if (scope.isMounted && props.value.width.value != active.expected) cancel()254
}255
private fun cancel(origin: TableResizeOrigin = TableResizeOrigin.Programmatic) {256
val active = gesture ?: return257
gesture = None258
drag.cancel()259
when (active.onEnd) {260
case Some(val finish) -> finish(TableColumnResizeEnd.Cancelled(active.initial))261
case None ->262
if (active.binding.value == active.expected) active.write(active.initial, origin)263
}264
}265
private fun updateCursor() {266
val next = props.value.enabled && (area.scope.isHovered || drag.isDragging)267
if (next || cursor)268
resizeCursor?.invoke(window, if (next) Some(Orientation.Horizontal) else None)269
cursor = next270
}271
public override fun update(): UiInvalidation {272
when (gesture) {273
case Some(val value) ->274
if (props.value.width.value !=275
value.expected ||276
value.density !=277
props.value.density ||278
value.direction !=279
scope.layoutDirection280
)281
cancel()282
case None -> {}283
}284
drag285
.update(286
options(),287
{ _ -> props.value.enabled && accepted != None },288
{ event -> receive(event) }289
)290
area.scope.enabled = props.value.enabled291
focus.options = props.value.focus292
updateSemantics()293
updateCursor()294
return UiInvalidation.Layout295
}296
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<297
Unit298
> {299
val child = context.measureChild(0L, constraints)300
context.placeChild(0L, Point(0.0, 0.0))301
return LayoutResult(child, Unit)302
}303
public override fun scene(context: SceneContext, value: Unit) {304
val input = props.value305
val density = input.density306
val size = context.size307
val width = size.width / density308
context309
.onFrameComplete { frame ->310
if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {311
accepted = if (size.width > 0.0 && size.height > 0.0) Some(width) else None312
if (accepted == None) cancel()313
updateSemantics()314
}315
}316
context.children()317
val x = if (context.layoutDirection == LayoutDirection.Rtl)318
0.0319
else320
lowerBound(size.width - 8.0 * density, 0.0)321
val bounds = Rect(322
x,323
4.0 * density,324
lowerBound(upperBound(size.width, 8.0 * density), 0.0),325
lowerBound(size.height - 8.0 * density, 0.0)326
)327
context.place(area, bounds)328
when (semantics) {329
case Some(val handle) -> context.semantics(handle, bounds)330
case None -> {}331
}332
if (area.scope.isHovered || area.isFocusVisible || drag.isDragging)333
context334
.draw { paint ->335
val color = if (area.isFocusVisible)336
input.theme.tokens.palette.ring337
else338
input.theme.tokens.palette.border339
paint340
.canvas341
.drawRect(342
Rect(343
bounds.x + bounds.width / 2.0 - density,344
bounds.y,345
2.0 * density,346
bounds.height347
),348
Paint(color)349
)350
}351
}352
public override fun onUnmount() {353
cancel();354
if (cursor &&355
(window.state == WindowState.Open || window.state == WindowState.PendingConfigure)356
)357
resizeCursor?.invoke(window, None)358
}359
}