Skip to content

Commit 4e2d576

Browse files
committed
add support for small terminal, added offset scrolling
1 parent d60c25a commit 4e2d576

File tree

1 file changed

+22
-2
lines changed
  • src/commonMain/kotlin

1 file changed

+22
-2
lines changed

src/commonMain/kotlin/ui.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import androidx.compose.runtime.Composable
2+
import androidx.compose.runtime.LaunchedEffect
23
import androidx.compose.runtime.collectAsState
34
import androidx.compose.runtime.getValue
5+
import androidx.compose.runtime.mutableIntStateOf
6+
import androidx.compose.runtime.remember
7+
import androidx.compose.runtime.setValue
48
import com.jakewharton.mosaic.LocalTerminal
59
import com.jakewharton.mosaic.layout.background
610
import com.jakewharton.mosaic.layout.fillMaxWidth
@@ -19,15 +23,31 @@ import com.jakewharton.mosaic.ui.TextStyle
1923
fun App(viewModel: ViewModel) {
2024
val terminal = LocalTerminal.current
2125
val selectedLine by viewModel.selectedLine.collectAsState()
26+
val content by viewModel.content.collectAsState()
27+
// subtraction of one is necessary, because there is a line with a cursor at the bottom, which moves up all the content
28+
val height = terminal.size.height - 1
29+
var offset by remember { mutableIntStateOf(0) }
30+
LaunchedEffect(selectedLine.first) {
31+
if (selectedLine.first < offset) {
32+
offset -= 1
33+
}
34+
}
35+
LaunchedEffect(selectedLine.last) {
36+
if (selectedLine.last > offset + height - 1) {
37+
offset += 1
38+
}
39+
}
40+
2241
Box(
2342
Modifier
2443
.width(terminal.size.width)
25-
.height(terminal.size.height - 1) // subtraction of one is necessary, because there is a line with a cursor at the bottom, which moves up all the content
44+
.height(height)
2645
.onKeyEvent { event -> viewModel.onKeyEvent(event) }
2746
) {
2847
Column {
29-
val content by viewModel.content.collectAsState()
3048
content.forEachIndexed { i, line ->
49+
if (i < offset) return@forEachIndexed
50+
if (i > offset + height - 1) return@forEachIndexed
3151
val selected = i in selectedLine
3252
Row(
3353
modifier = Modifier

0 commit comments

Comments
 (0)