1
1
package com.github.minecraft_ta.totalDebugCompanion.ui.editor
2
2
3
+ import androidx.compose.desktop.LocalAppWindow
3
4
import androidx.compose.foundation.*
4
5
import androidx.compose.foundation.gestures.Orientation
5
6
import androidx.compose.foundation.gestures.scrollable
6
7
import androidx.compose.foundation.layout.*
7
8
import androidx.compose.foundation.lazy.LazyColumn
8
9
import androidx.compose.foundation.lazy.LazyListState
9
10
import androidx.compose.foundation.lazy.rememberLazyListState
11
+ import androidx.compose.foundation.text.ClickableText
10
12
import androidx.compose.foundation.text.selection.DisableSelection
11
13
import androidx.compose.foundation.text.selection.SelectionContainer
12
14
import androidx.compose.material.CircularProgressIndicator
@@ -20,8 +22,10 @@ import androidx.compose.runtime.remember
20
22
import androidx.compose.ui.Alignment
21
23
import androidx.compose.ui.Modifier
22
24
import androidx.compose.ui.draw.alpha
25
+ import androidx.compose.ui.platform.Keyboard
23
26
import androidx.compose.ui.platform.LocalDensity
24
27
import androidx.compose.ui.text.SpanStyle
28
+ import androidx.compose.ui.text.TextStyle
25
29
import androidx.compose.ui.text.buildAnnotatedString
26
30
import androidx.compose.ui.text.withStyle
27
31
import androidx.compose.ui.unit.dp
@@ -31,14 +35,16 @@ import com.github.minecraft_ta.totalDebugCompanion.ui.AppTheme
31
35
import com.github.minecraft_ta.totalDebugCompanion.util.Fonts
32
36
import com.github.minecraft_ta.totalDebugCompanion.util.getStylesForJavaCode
33
37
import com.github.minecraft_ta.totalDebugCompanion.util.loadableScoped
38
+ import kotlinx.coroutines.runBlocking
39
+ import java.awt.event.KeyEvent
34
40
import java.util.stream.Collectors
35
41
import java.util.stream.IntStream
36
42
37
43
@OptIn(ExperimentalFoundationApi ::class )
38
44
@Composable
39
45
fun EditorView (model : CodeEditor , settings : Settings ) = key(model) {
40
46
val horizontalScrollState = rememberScrollState()
41
- val verticalScrollState = rememberLazyListState()
47
+ val verticalScrollState = rememberLazyListState(initialFirstVisibleItemIndex = model.initialScrollPosition )
42
48
43
49
val lines by loadableScoped(model.lines)
44
50
@@ -50,7 +56,8 @@ fun EditorView(model: CodeEditor, settings: Settings) = key(model) {
50
56
color = AppTheme .colors.backgroundDark,
51
57
) {
52
58
if (lines != null ) {
53
- Lines (lines!! , verticalScrollState, settings)
59
+ Lines (model, lines!! , verticalScrollState, settings)
60
+ println (model.initialScrollPosition)
54
61
} else {
55
62
CircularProgressIndicator (
56
63
modifier = Modifier
@@ -78,7 +85,7 @@ fun EditorView(model: CodeEditor, settings: Settings) = key(model) {
78
85
79
86
@OptIn(ExperimentalFoundationApi ::class )
80
87
@Composable
81
- private fun Lines (lines : CodeEditor .Lines , verticalScrollState : LazyListState , settings : Settings ) =
88
+ private fun Lines (model : CodeEditor , lines : CodeEditor .Lines , verticalScrollState : LazyListState , settings : Settings ) =
82
89
with (LocalDensity .current) {
83
90
val maxNum = remember(lines.lineNumberDigitCount) {
84
91
(1 .. lines.lineNumberDigitCount).joinToString(separator = " " ) { " 9" }
@@ -109,7 +116,7 @@ private fun Lines(lines: CodeEditor.Lines, verticalScrollState: LazyListState, s
109
116
lines[longestLineIndex],
110
117
listOf (),
111
118
settings
112
- )
119
+ ) {}
113
120
}
114
121
}
115
122
@@ -125,7 +132,19 @@ private fun Lines(lines: CodeEditor.Lines, verticalScrollState: LazyListState, s
125
132
lines[index],
126
133
styles[index] ? : listOf (),
127
134
settings
128
- )
135
+ ) {
136
+ if (! GlobalKeyboardState .isPressed(KeyEvent .VK_CONTROL ) || lines[index].content.value.value[it] == ' ' )
137
+ return @Line
138
+
139
+ val outStream = TotalDebugServer .currentOutputStream ? : return @Line
140
+
141
+ synchronized(outStream) {
142
+ outStream.write(2 )
143
+ outStream.writeUTF(model.fileName)
144
+ outStream.writeInt(index) // row
145
+ outStream.writeInt(it) // column
146
+ }
147
+ }
129
148
}
130
149
}
131
150
}
@@ -139,7 +158,8 @@ private fun Line(
139
158
maxNum : String ,
140
159
line : CodeEditor .Line ,
141
160
styles : List <Triple <SpanStyle , Int , Int >>,
142
- settings : Settings
161
+ settings : Settings ,
162
+ onClick : (Int ) -> Unit
143
163
) {
144
164
Row (modifier = modifier) {
145
165
DisableSelection {
@@ -156,7 +176,8 @@ private fun Line(
156
176
.weight(1f )
157
177
.padding(start = 18 .dp, end = 12 .dp),
158
178
styles,
159
- settings = settings
179
+ settings = settings,
180
+ onClick = onClick
160
181
)
161
182
}
162
183
}
@@ -175,8 +196,9 @@ private fun LineContent(
175
196
content : CodeEditor .Content ,
176
197
modifier : Modifier ,
177
198
styles : List <Triple <SpanStyle , Int , Int >>,
178
- settings : Settings
179
- ) = Text (
199
+ settings : Settings ,
200
+ onClick : (Int ) -> Unit
201
+ ) = ClickableText (
180
202
text = if (content.isCode) {
181
203
codeString(content.value.value, styles)
182
204
} else {
@@ -186,10 +208,10 @@ private fun LineContent(
186
208
}
187
209
}
188
210
},
189
- fontSize = settings.fontSize,
190
- fontFamily = Fonts .jetbrainsMono(),
211
+ style = TextStyle (fontSize = settings.fontSize, fontFamily = Fonts .jetbrainsMono()),
191
212
modifier = modifier,
192
- softWrap = false
213
+ softWrap = false ,
214
+ onClick = onClick
193
215
)
194
216
195
217
private fun codeString (str : String , styles : List <Triple <SpanStyle , Int , Int >>) = buildAnnotatedString {
0 commit comments