Skip to content

Commit f394728

Browse files
committed
limit offset (vertical and horizontal "scroll") to the unbounded size of the content
1 parent e289ce6 commit f394728

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

  • library/src/main/java/de/markusressel/kodeeditor/library/compose

library/src/main/java/de/markusressel/kodeeditor/library/compose/KodeEditor.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import androidx.compose.ui.graphics.Color
1515
import androidx.compose.ui.graphics.TransformOrigin
1616
import androidx.compose.ui.graphics.graphicsLayer
1717
import androidx.compose.ui.layout.onGloballyPositioned
18+
import androidx.compose.ui.layout.onSizeChanged
1819
import androidx.compose.ui.platform.LocalDensity
1920
import androidx.compose.ui.text.SpanStyle
2021
import androidx.compose.ui.text.TextStyle
2122
import androidx.compose.ui.text.input.TextFieldValue
2223
import androidx.compose.ui.tooling.preview.Preview
24+
import androidx.compose.ui.unit.IntSize
2325
import androidx.compose.ui.unit.dp
2426
import androidx.compose.ui.zIndex
2527
import de.markusressel.kodehighlighter.core.LanguageRuleBook
@@ -92,13 +94,20 @@ fun KodeEditor(
9294
}
9395

9496
// Text Editor
97+
var totalSize by remember { mutableStateOf(IntSize.Zero) }
98+
var maxXOffset by remember(totalSize) { mutableFloatStateOf(Float.MAX_VALUE) }
99+
var maxYOffset by remember(totalSize) { mutableFloatStateOf(Float.MAX_VALUE) }
100+
95101
ZoomLayout(
96102
modifier = Modifier
97103
.zIndex(0f)
98104
.padding(
99105
start = computedPadding,
100106
)
101-
.matchParentSize(),
107+
.matchParentSize()
108+
.onSizeChanged { size ->
109+
totalSize = size
110+
},
102111
offset = offset,
103112
zoom = zoom,
104113
onOffsetChanged = {
@@ -109,8 +118,8 @@ fun KodeEditor(
109118

110119
val newOffset = offset + (it / zoom)
111120
offset = newOffset.copy(
112-
x = newOffset.x.coerceAtLeast(0f),
113-
y = newOffset.y.coerceAtLeast(0f)
121+
x = newOffset.x.coerceIn(0f, maxXOffset),
122+
y = newOffset.y.coerceIn(0f, maxYOffset)
114123
)
115124
},
116125
onZoomChanged = {
@@ -129,7 +138,11 @@ fun KodeEditor(
129138
.padding(
130139
start = 4.dp,
131140
end = 4.dp
132-
),
141+
)
142+
.onSizeChanged { unboundedSize ->
143+
maxXOffset = (unboundedSize.width - totalSize.width).toFloat()
144+
maxYOffset = (unboundedSize.height - totalSize.height).toFloat()
145+
},
133146
value = text,
134147
languageRuleBook = languageRuleBook,
135148
colorScheme = colorScheme,

0 commit comments

Comments
 (0)