scoop:scoop.ui:0.1.0
src/scroll/keyboard_reveal.scoop
Selected production source · src/scroll/keyboard_reveal.scoop
1
package scoop.ui3
import scoop.graphics.*4
import scoop.math.abs5
import scoop.math.max6
import scoop.math.min8
/** Retains only the scroll insets needed by one active editor and actual OS overlap. */9
internal class KeyboardReveal(val occlusion: Rect): Closeable {10
private val viewports = ArrayList<ScrollViewport>()11
private var live = true12
val active: Boolean get() = live14
fun prepare(boundary: ScrollReveal, caret: Rect): Boolean {15
if (!active)16
return false17
if (intersectReveal(caret, occlusion) == None)18
return false19
val localCaret = localReveal(boundary.geometry, caret) ?: return false20
val localKeyboard = localReveal(boundary.geometry, occlusion) ?: return false21
val padding =22
keyboardRevealPadding(23
boundary.layout.snapshot.viewport,24
boundary.layout.basePadding,25
localCaret,26
localKeyboard27
) ?:28
return false29
if (!boundary.viewport.setKeyboardPadding(padding))30
return false31
var present = false32
for (viewport in viewports)33
if (viewport === boundary.viewport)34
present = true35
if (!present)36
viewports.add(boundary.viewport)37
return true38
}40
public override fun close() {41
live = false42
for (viewport in viewports)43
viewport.setKeyboardPadding(ScrollPadding())44
viewports.clear()45
}46
}48
/** Uses real free rectangles, including all four sides of a floating keyboard. */49
internal fun keyboardRevealPadding(50
viewport: Size,51
base: ScrollPadding,52
caret: Rect,53
keyboard: Rect54
): ScrollPadding? {55
val bounds =56
Rect(57
base.left,58
base.top,59
max(0.0, viewport.width - base.left - base.right),60
max(0.0, viewport.height - base.top - base.bottom)61
)62
val covered = intersectReveal(bounds, keyboard) ?: return None63
if (intersectReveal(caret, covered) == None)64
return None65
val right = bounds.x + bounds.width66
val bottom = bounds.y + bounds.height67
val strips =68
[69
Rect(bounds.x, bounds.y, bounds.width, covered.y - bounds.y),70
Rect(71
bounds.x,72
covered.y + covered.height,73
bounds.width,74
bottom - covered.y - covered.height75
),76
Rect(bounds.x, bounds.y, covered.x - bounds.x, bounds.height),77
Rect(78
covered.x + covered.width,79
bounds.y,80
right - covered.x - covered.width,81
bounds.height82
)83
]84
var selected: Rect? = None85
var distance = 0.086
for (strip in strips) {87
if (strip.width < caret.width || strip.height < caret.height)88
continue89
val x = min(max(caret.x, strip.x), strip.x + strip.width - caret.width)90
val y = min(max(caret.y, strip.y), strip.y + strip.height - caret.height)91
val moved = abs(x - caret.x) + abs(y - caret.y)92
if (selected == None || moved < distance) {93
selected = Some(strip)94
distance = moved95
}96
}97
return when (selected) {98
case Some(val value) -> Some(99
ScrollPadding(100
value.x,101
value.y,102
max(0.0, viewport.width - value.x - value.width),103
max(0.0, viewport.height - value.y - value.height)104
)105
)106
case None -> None107
}108
}