Scoop API

scoop:scoop.ui:0.1.0

src/scroll/keyboard_reveal.scoop

Selected production source · src/scroll/keyboard_reveal.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.math.abs
5import scoop.math.max
6import scoop.math.min
8/** Retains only the scroll insets needed by one active editor and actual OS overlap. */
9internal class KeyboardReveal(val occlusion: Rect): Closeable {
10 private val viewports = ArrayList<ScrollViewport>()
11 private var live = true
12 val active: Boolean get() = live
14 fun prepare(boundary: ScrollReveal, caret: Rect): Boolean {
15 if (!active)
16 return false
17 if (intersectReveal(caret, occlusion) == None)
18 return false
19 val localCaret = localReveal(boundary.geometry, caret) ?: return false
20 val localKeyboard = localReveal(boundary.geometry, occlusion) ?: return false
21 val padding =
22 keyboardRevealPadding(
23 boundary.layout.snapshot.viewport,
24 boundary.layout.basePadding,
25 localCaret,
26 localKeyboard
27 ) ?:
28 return false
29 if (!boundary.viewport.setKeyboardPadding(padding))
30 return false
31 var present = false
32 for (viewport in viewports)
33 if (viewport === boundary.viewport)
34 present = true
35 if (!present)
36 viewports.add(boundary.viewport)
37 return true
38 }
40 public override fun close() {
41 live = false
42 for (viewport in viewports)
43 viewport.setKeyboardPadding(ScrollPadding())
44 viewports.clear()
45 }
48/** Uses real free rectangles, including all four sides of a floating keyboard. */
49internal fun keyboardRevealPadding(
50 viewport: Size,
51 base: ScrollPadding,
52 caret: Rect,
53 keyboard: Rect
54): 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 None
63 if (intersectReveal(caret, covered) == None)
64 return None
65 val right = bounds.x + bounds.width
66 val bottom = bounds.y + bounds.height
67 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.height
75 ),
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.height
82 )
83 ]
84 var selected: Rect? = None
85 var distance = 0.0
86 for (strip in strips) {
87 if (strip.width < caret.width || strip.height < caret.height)
88 continue
89 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 = moved
95 }
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 -> None
107 }