Skip to content

Commit a69c5ce

Browse files
committed
Allow user to click text and added a initial scroll position
1 parent b98e633 commit a69c5ce

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

src/main/kotlin/com/github/minecraft_ta/totalDebugCompanion/main.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.github.minecraft_ta.totalDebugCompanion.ui.fileTree.FileTreeView
2424
import com.github.minecraft_ta.totalDebugCompanion.ui.fileTree.FileTreeViewHeader
2525
import java.io.DataInputStream
2626
import java.io.DataOutputStream
27-
import java.lang.IllegalStateException
2827
import java.net.ServerSocket
2928
import java.nio.file.Files
3029
import java.nio.file.Path
@@ -118,6 +117,7 @@ object TotalDebugServer {
118117
when (inputStream.readUnsignedByte()) {
119118
1 -> { //open a file
120119
val path = Paths.get(inputStream.readUTF())
120+
val row = inputStream.readInt();
121121

122122
if (!Files.exists(path) || !path.isSubPathOf(root))
123123
continue
@@ -129,7 +129,7 @@ object TotalDebugServer {
129129
if (existingEditor != null) {
130130
existingEditor.activate()
131131
} else {
132-
editors.openFile(path)
132+
editors.openFile(path, row)
133133
}
134134

135135
focusWindow()

src/main/kotlin/com/github/minecraft_ta/totalDebugCompanion/model/Editors.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Editors {
2424
val active: AbstractEditor? get() = selection.selected as AbstractEditor?
2525

2626
@OptIn(ExperimentalPathApi::class)
27-
fun openFile(file: Path) {
28-
val editor = Editor(file)
27+
fun openFile(file: Path, row: Int) {
28+
val editor = Editor(file, row)
2929
openEditor(editor)
3030
}
3131

@@ -81,6 +81,7 @@ class SearchEditor(
8181

8282
class CodeEditor(
8383
val fileName: String,
84+
val initialScrollPosition: Int,
8485
val lines: (backgroundScope: CoroutineScope) -> Lines,
8586
) : AbstractEditor() {
8687

@@ -100,8 +101,9 @@ class CodeEditor(
100101
}
101102

102103
@ExperimentalPathApi
103-
fun Editor(file: Path) = CodeEditor(
104-
fileName = file.name
104+
fun Editor(file: Path, row: Int) = CodeEditor(
105+
fileName = file.name,
106+
initialScrollPosition = row
105107
) { backgroundScope ->
106108
val textLines = try {
107109
file.readLines(backgroundScope)

src/main/kotlin/com/github/minecraft_ta/totalDebugCompanion/model/FileTree.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class FileTree(val root: Path, private val editors: Editors) {
5959

6060
fun open() = when (type) {
6161
is ItemType.Folder -> file.toggleExpanded()
62-
is ItemType.File -> editors.openFile(file.file)
62+
is ItemType.File -> editors.openFile(file.file, 0)
6363
}
6464
}
6565

src/main/kotlin/com/github/minecraft_ta/totalDebugCompanion/ui/editor/CodeEditorView.kt

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.github.minecraft_ta.totalDebugCompanion.ui.editor
22

3+
import androidx.compose.desktop.LocalAppWindow
34
import androidx.compose.foundation.*
45
import androidx.compose.foundation.gestures.Orientation
56
import androidx.compose.foundation.gestures.scrollable
67
import androidx.compose.foundation.layout.*
78
import androidx.compose.foundation.lazy.LazyColumn
89
import androidx.compose.foundation.lazy.LazyListState
910
import androidx.compose.foundation.lazy.rememberLazyListState
11+
import androidx.compose.foundation.text.ClickableText
1012
import androidx.compose.foundation.text.selection.DisableSelection
1113
import androidx.compose.foundation.text.selection.SelectionContainer
1214
import androidx.compose.material.CircularProgressIndicator
@@ -20,8 +22,10 @@ import androidx.compose.runtime.remember
2022
import androidx.compose.ui.Alignment
2123
import androidx.compose.ui.Modifier
2224
import androidx.compose.ui.draw.alpha
25+
import androidx.compose.ui.platform.Keyboard
2326
import androidx.compose.ui.platform.LocalDensity
2427
import androidx.compose.ui.text.SpanStyle
28+
import androidx.compose.ui.text.TextStyle
2529
import androidx.compose.ui.text.buildAnnotatedString
2630
import androidx.compose.ui.text.withStyle
2731
import androidx.compose.ui.unit.dp
@@ -31,14 +35,16 @@ import com.github.minecraft_ta.totalDebugCompanion.ui.AppTheme
3135
import com.github.minecraft_ta.totalDebugCompanion.util.Fonts
3236
import com.github.minecraft_ta.totalDebugCompanion.util.getStylesForJavaCode
3337
import com.github.minecraft_ta.totalDebugCompanion.util.loadableScoped
38+
import kotlinx.coroutines.runBlocking
39+
import java.awt.event.KeyEvent
3440
import java.util.stream.Collectors
3541
import java.util.stream.IntStream
3642

3743
@OptIn(ExperimentalFoundationApi::class)
3844
@Composable
3945
fun EditorView(model: CodeEditor, settings: Settings) = key(model) {
4046
val horizontalScrollState = rememberScrollState()
41-
val verticalScrollState = rememberLazyListState()
47+
val verticalScrollState = rememberLazyListState(initialFirstVisibleItemIndex = model.initialScrollPosition)
4248

4349
val lines by loadableScoped(model.lines)
4450

@@ -50,7 +56,8 @@ fun EditorView(model: CodeEditor, settings: Settings) = key(model) {
5056
color = AppTheme.colors.backgroundDark,
5157
) {
5258
if (lines != null) {
53-
Lines(lines!!, verticalScrollState, settings)
59+
Lines(model, lines!!, verticalScrollState, settings)
60+
println(model.initialScrollPosition)
5461
} else {
5562
CircularProgressIndicator(
5663
modifier = Modifier
@@ -78,7 +85,7 @@ fun EditorView(model: CodeEditor, settings: Settings) = key(model) {
7885

7986
@OptIn(ExperimentalFoundationApi::class)
8087
@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) =
8289
with(LocalDensity.current) {
8390
val maxNum = remember(lines.lineNumberDigitCount) {
8491
(1..lines.lineNumberDigitCount).joinToString(separator = "") { "9" }
@@ -109,7 +116,7 @@ private fun Lines(lines: CodeEditor.Lines, verticalScrollState: LazyListState, s
109116
lines[longestLineIndex],
110117
listOf(),
111118
settings
112-
)
119+
) {}
113120
}
114121
}
115122

@@ -125,7 +132,19 @@ private fun Lines(lines: CodeEditor.Lines, verticalScrollState: LazyListState, s
125132
lines[index],
126133
styles[index] ?: listOf(),
127134
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+
}
129148
}
130149
}
131150
}
@@ -139,7 +158,8 @@ private fun Line(
139158
maxNum: String,
140159
line: CodeEditor.Line,
141160
styles: List<Triple<SpanStyle, Int, Int>>,
142-
settings: Settings
161+
settings: Settings,
162+
onClick: (Int) -> Unit
143163
) {
144164
Row(modifier = modifier) {
145165
DisableSelection {
@@ -156,7 +176,8 @@ private fun Line(
156176
.weight(1f)
157177
.padding(start = 18.dp, end = 12.dp),
158178
styles,
159-
settings = settings
179+
settings = settings,
180+
onClick = onClick
160181
)
161182
}
162183
}
@@ -175,8 +196,9 @@ private fun LineContent(
175196
content: CodeEditor.Content,
176197
modifier: Modifier,
177198
styles: List<Triple<SpanStyle, Int, Int>>,
178-
settings: Settings
179-
) = Text(
199+
settings: Settings,
200+
onClick: (Int) -> Unit
201+
) = ClickableText(
180202
text = if (content.isCode) {
181203
codeString(content.value.value, styles)
182204
} else {
@@ -186,10 +208,10 @@ private fun LineContent(
186208
}
187209
}
188210
},
189-
fontSize = settings.fontSize,
190-
fontFamily = Fonts.jetbrainsMono(),
211+
style = TextStyle(fontSize = settings.fontSize, fontFamily = Fonts.jetbrainsMono()),
191212
modifier = modifier,
192-
softWrap = false
213+
softWrap = false,
214+
onClick = onClick
193215
)
194216

195217
private fun codeString(str: String, styles: List<Triple<SpanStyle, Int, Int>>) = buildAnnotatedString {

src/main/kotlin/com/github/minecraft_ta/totalDebugCompanion/ui/editor/SearchEditorView.kt

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
package com.github.minecraft_ta.totalDebugCompanion.ui.editor
22

3+
import androidx.compose.desktop.LocalAppWindow
34
import androidx.compose.foundation.*
45
import androidx.compose.foundation.layout.*
56
import androidx.compose.foundation.lazy.LazyColumn
67
import androidx.compose.foundation.lazy.rememberLazyListState
78
import androidx.compose.foundation.text.selection.DisableSelection
89
import androidx.compose.foundation.text.selection.SelectionContainer
9-
import androidx.compose.material.Icon
10-
import androidx.compose.material.IconButton
11-
import androidx.compose.material.Surface
12-
import androidx.compose.material.Text
10+
import androidx.compose.material.*
1311
import androidx.compose.material.icons.Icons
1412
import androidx.compose.material.icons.filled.Close
1513
import androidx.compose.runtime.Composable
1614
import androidx.compose.runtime.key
1715
import androidx.compose.ui.Alignment
1816
import androidx.compose.ui.Modifier
1917
import androidx.compose.ui.graphics.Color
18+
import androidx.compose.ui.input.key.*
2019
import androidx.compose.ui.platform.LocalDensity
2120
import androidx.compose.ui.text.style.TextDecoration
2221
import androidx.compose.ui.unit.dp
2322
import androidx.compose.ui.unit.sp
2423
import com.github.minecraft_ta.totalDebugCompanion.model.SearchEditor
2524
import com.github.minecraft_ta.totalDebugCompanion.model.Settings
25+
import com.github.minecraft_ta.totalDebugCompanion.model.TextLines
2626
import com.github.minecraft_ta.totalDebugCompanion.ui.AppTheme
2727
import com.github.minecraft_ta.totalDebugCompanion.util.Fonts
28+
import org.jetbrains.skija.paragraph.TextBox
2829

2930
@OptIn(ExperimentalFoundationApi::class)
3031
@Composable
@@ -103,8 +104,10 @@ fun SearchResultLine(model: SearchEditor, index: Int, settings: Settings) {
103104
).width(3_000.dp).clickable {
104105
val outStream = TotalDebugServer.currentOutputStream ?: return@clickable
105106

106-
outStream.write(1)
107-
outStream.writeUTF(model.results[index].substringBefore('#').replace('/', '.'))
107+
synchronized(outStream) {
108+
outStream.write(1)
109+
outStream.writeUTF(model.results[index].substringBefore('#').replace('/', '.'))
110+
}
108111
}
109112
) {
110113
Row(

0 commit comments

Comments
 (0)