From a305a91c66eb72c4b51cf65e34ea63d1e4737bd5 Mon Sep 17 00:00:00 2001 From: Dantalian Date: Mon, 23 Nov 2026 23:16:39 +0800 Subject: [PATCH] feat(files): add a plain text editor to the file manager (#2980) * feat(files): add a plain text editor to the file manager * fix(files): address CodeRabbit review on the text editor - compose the editor instead of the toolbar so EXIT can't bypass the unsaved-changes guard - read through a capped stream, keep undecodable files read-only, and preserve POSIX permissions via staging-file rename with no direct-write fallback - serialize saves, keep dirty consistent across concurrent edits, reset match index on text change, fix empty-state counter and read-only replace buttons - scroll match into view horizontally; ignore cursor-only edits for dirty - add tests for literal U+FFFD files and the capped reader * fix(files): treat rename failure as save error and follow symbolic links * test(files): assert the resolved save target without canonicalizing it --------- Co-authored-by: Dantalian --- app/src/main/AndroidManifest.xml | 1 + .../launcher/files/FileManagerItems.kt | 15 + .../launcher/files/FileManagerScreen.kt | 17 +- .../launcher/files/TextEditorScreen.kt | 726 ++++++++++++++++++ app/src/main/res/values/strings.xml | 17 + .../launcher/files/TextEditorLogicTest.kt | 143 ++++ 6 files changed, 918 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/files/TextEditorScreen.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/files/TextEditorLogicTest.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 30650081fc..55b75d36df 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -232,6 +232,7 @@ android:exported="false" android:screenOrientation="user" android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|density" + android:windowSoftInputMode="adjustResize" android:theme="@style/AppTheme" /> Unit, onProperties: () -> Unit, onDismissMenu: () -> Unit, + onOpen: () -> Unit, ) { val isDir = file.isDirectory + if (!isDir) { + DropdownMenuItem( + text = { Text(stringResource(R.string.fm_edit_open)) }, + leadingIcon = { Icon(Icons.Filled.FileOpen, null, tint = MaterialTheme.colorScheme.primary) }, + onClick = { onDismissMenu(); onOpen() }, + modifier = Modifier.focusGlide(), + ) + MenuItemDivider() + } DropdownMenuItem( text = { Text(stringResource(R.string.fm_select)) }, leadingIcon = { Icon(Icons.Filled.Checklist, null, tint = MaterialTheme.colorScheme.primary) }, @@ -180,6 +191,7 @@ internal fun FileItemRow( onCut: () -> Unit, onDelete: () -> Unit, onRename: () -> Unit, + onOpen: () -> Unit = {}, isFavorite: Boolean = false, onToggleFavorite: () -> Unit = {}, onProperties: () -> Unit = {}, @@ -291,6 +303,7 @@ internal fun FileItemRow( file = file, isFavorite = isFavorite, onSelect = onSelect, + onOpen = onOpen, onRename = onRename, onCopy = onCopy, onCut = onCut, @@ -517,6 +530,7 @@ internal fun FileGridTile( isFavorite: Boolean = false, onSelect: () -> Unit = {}, onRename: () -> Unit = {}, + onOpen: () -> Unit = {}, onCopy: () -> Unit = {}, onCut: () -> Unit = {}, onDelete: () -> Unit = {}, @@ -605,6 +619,7 @@ internal fun FileGridTile( file = file, isFavorite = isFavorite, onSelect = onSelect, + onOpen = onOpen, onRename = onRename, onCopy = onCopy, onCut = onCut, diff --git a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt index 9945083226..7025769d87 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt @@ -191,6 +191,7 @@ fun FileManagerScreen( var compactRows by remember { mutableStateOf(browsePrefs.getBoolean("fmCompactRows", false)) } var showNewFolderDialog by remember { mutableStateOf(false) } var renameTarget by remember { mutableStateOf(null) } + var editingTarget by remember { mutableStateOf(null) } var propertiesTarget by remember { mutableStateOf(null) } var isOperationRunning by remember { mutableStateOf(false) } var operationLabel by remember { mutableStateOf("") } @@ -294,7 +295,7 @@ fun FileManagerScreen( if (isOperationActive()) pendingNavigation = action else navigate(action) } - BackHandler(enabled = operationActive || cancellingNavigation || showFavorites || currentDir != currentRoot || history.isNotEmpty()) { + BackHandler(enabled = editingTarget == null && (operationActive || cancellingNavigation || showFavorites || currentDir != currentRoot || history.isNotEmpty())) { requestNavigation(FileManagerNavigation.BACK) } @@ -696,6 +697,14 @@ fun FileManagerScreen( val compactToolbar = LocalConfiguration.current.screenWidthDp < 600 Column(modifier = Modifier.fillMaxSize()) { + editingTarget?.let { target -> + TextEditorScreen( + file = target, + onExit = { editingTarget = null }, + modifier = Modifier.fillMaxSize(), + ) + return@Column + } if (pickMode && !pickerTitle.isNullOrEmpty()) { Text( text = pickerTitle, @@ -1194,6 +1203,8 @@ fun FileManagerScreen( pickPrefs.edit().putString("lastFilePickerDir", currentDir.absolutePath).apply() onPick?.invoke(file) } + } else { + editingTarget = file } }, onMenu = { showMenuFor = file }, @@ -1205,6 +1216,7 @@ fun FileManagerScreen( selectedPaths = selectedPaths + file.absolutePath showMenuFor = null }, + onOpen = { editingTarget = file; showMenuFor = null }, onRename = { renameTarget = file; showMenuFor = null }, onCopy = { clipboardFiles = listOf(file); isCutOperation = false; showMenuFor = null }, onCut = { clipboardFiles = listOf(file); isCutOperation = true; showMenuFor = null }, @@ -1273,6 +1285,8 @@ fun FileManagerScreen( pickPrefs.edit().putString("lastFilePickerDir", currentDir.absolutePath).apply() onPick?.invoke(file) } + } else { + editingTarget = file } }, onMenu = { showMenuFor = file }, @@ -1286,6 +1300,7 @@ fun FileManagerScreen( onCopy = { clipboardFiles = listOf(file); isCutOperation = false; showMenuFor = null }, onCut = { clipboardFiles = listOf(file); isCutOperation = true; showMenuFor = null }, onDelete = { selectedEntry = file; showMenuFor = null }, + onOpen = { editingTarget = file; showMenuFor = null }, onRename = { renameTarget = file; showMenuFor = null }, isFavorite = isFav, onToggleFavorite = { diff --git a/app/src/main/java/com/droiddeck/launcher/files/TextEditorScreen.kt b/app/src/main/java/com/droiddeck/launcher/files/TextEditorScreen.kt new file mode 100644 index 0000000000..2d9c0a3fea --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/files/TextEditorScreen.kt @@ -0,0 +1,726 @@ +package com.droiddeck.launcher.files + +import android.widget.Toast +import androidx.activity.compose.BackHandler +import androidx.compose.foundation.ScrollState +import androidx.compose.foundation.background +import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.IntrinsicSize +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.layout.widthIn +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.text.BasicTextField +import androidx.compose.foundation.text.KeyboardActions +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.ArrowBack +import androidx.compose.material.icons.filled.Close +import androidx.compose.material.icons.filled.KeyboardArrowDown +import androidx.compose.material.icons.filled.KeyboardArrowUp +import androidx.compose.material.icons.filled.Save +import androidx.compose.material.icons.filled.Search +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.SolidColor +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextRange +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.input.ImeAction +import androidx.compose.ui.text.input.KeyboardCapitalization +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.text.input.TextFieldValue +import androidx.compose.ui.text.rememberTextMeasurer +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.ui.focusGlide +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.io.ByteArrayOutputStream +import java.io.File +import java.io.InputStream +import java.nio.ByteBuffer +import java.nio.charset.CharacterCodingException +import java.nio.charset.Charset +import java.nio.charset.CodingErrorAction +import java.nio.file.Files +import kotlin.math.roundToInt + +internal const val EDITOR_FONT_SIZE_SP = 13 +internal const val EDITOR_LINE_HEIGHT_SP = 18 +internal const val EDITOR_TAB_WIDTH = 4 +internal const val EDITOR_MAX_OPEN_BYTES = 4L * 1024 * 1024 +internal const val EDITOR_MAX_EDIT_BYTES = 512L * 1024 + +internal enum class EditorCharset(val label: String, private val charsetName: String) { + UTF8("UTF-8", "UTF-8"), + GB18030("GB18030", "GB18030"), + ; + + val charset: Charset get() = Charset.forName(charsetName) +} + +internal enum class EditorLock { NONE, BINARY, TOO_LARGE, PERMISSION, LOSSY } + +internal enum class EditorLoadState { LOADING, READY, TOO_LARGE, UNREADABLE } + +internal data class DecodedEditorText(val text: String, val charset: EditorCharset, val lossy: Boolean) + +internal fun editorIsBinary(bytes: ByteArray, sample: Int = 8192): Boolean { + val limit = minOf(bytes.size, sample) + for (i in 0 until limit) if (bytes[i].toInt() == 0) return true + return false +} + +private fun strictDecode(bytes: ByteArray, charset: Charset): String? = try { + charset.newDecoder() + .onMalformedInput(CodingErrorAction.REPORT) + .onUnmappableCharacter(CodingErrorAction.REPORT) + .decode(ByteBuffer.wrap(bytes)) + .toString() +} catch (e: CharacterCodingException) { + null +} + +internal fun decodeEditorText(bytes: ByteArray): DecodedEditorText { + strictDecode(bytes, Charsets.UTF_8)?.let { return DecodedEditorText(it, EditorCharset.UTF8, false) } + strictDecode(bytes, EditorCharset.GB18030.charset)?.let { return DecodedEditorText(it, EditorCharset.GB18030, false) } + return DecodedEditorText(String(bytes, Charsets.UTF_8), EditorCharset.UTF8, true) +} + +internal fun resolveEditorSaveTarget(file: File): File { + val isLink = runCatching { Files.isSymbolicLink(file.toPath()) }.getOrDefault(false) + if (!isLink) return file + // rename(2) replaces the link itself, so stage next to and rename onto the real target + return runCatching { file.canonicalFile }.getOrDefault(file) +} + +internal fun readCapped(input: InputStream, limit: Long): ByteArray? { + val out = ByteArrayOutputStream() + val buffer = ByteArray(64 * 1024) + var total = 0L + while (true) { + val n = input.read(buffer) + if (n < 0) return out.toByteArray() + total += n + if (total > limit) return null + out.write(buffer, 0, n) + } +} + +internal fun expandEditorTabs(text: String, width: Int = EDITOR_TAB_WIDTH): String { + if (text.indexOf('\t') < 0) return text + val out = StringBuilder(text.length) + var column = 0 + for (c in text) { + when (c) { + '\n' -> { + out.append(c) + column = 0 + } + '\t' -> { + val pad = width - (column % width) + repeat(pad) { out.append(' ') } + column += pad + } + else -> { + out.append(c) + column++ + } + } + } + return out.toString() +} + +internal fun lineCountOf(text: String): Int { + var lines = 1 + for (i in text.indices) if (text[i] == '\n') lines++ + return lines +} + +internal fun lineIndexOfOffset(text: String, offset: Int): Int { + val end = offset.coerceIn(0, text.length) + var line = 0 + var i = 0 + while (i < end) { + if (text[i] == '\n') line++ + i++ + } + return line +} + +internal fun columnOfOffset(text: String, offset: Int): Int { + val end = offset.coerceIn(0, text.length) + var start = end + while (start > 0 && text[start - 1] != '\n') start-- + return end - start + 1 +} + +internal fun findEditorMatches( + text: String, + query: String, + matchCase: Boolean = false, + limit: Int = 10000, +): List { + if (query.isEmpty()) return emptyList() + val out = ArrayList() + var at = 0 + while (out.size < limit) { + val found = text.indexOf(query, at, ignoreCase = !matchCase) + if (found < 0) break + out.add(found until found + query.length) + at = found + query.length + } + return out +} + +internal fun replaceEditorMatches(text: String, matches: List, replacement: String): String { + if (matches.isEmpty()) return text + val out = StringBuilder(text.length) + var cursor = 0 + for (m in matches) { + if (m.first < cursor || m.last >= text.length) continue + out.append(text, cursor, m.first).append(replacement) + cursor = m.last + 1 + } + out.append(text, cursor, text.length) + return out.toString() +} + +@Composable +internal fun TextEditorScreen( + file: File, + onExit: () -> Unit, + modifier: Modifier = Modifier, +) { + val context = LocalContext.current + val scope = rememberCoroutineScope() + val density = LocalDensity.current + + var text by remember { mutableStateOf("") } + var selection by remember { mutableStateOf(TextRange.Zero) } + var composing by remember { mutableStateOf(null) } + var loadState by remember { mutableStateOf(EditorLoadState.LOADING) } + var lock by remember { mutableStateOf(EditorLock.NONE) } + var charset by remember { mutableStateOf(EditorCharset.UTF8) } + var dirty by remember { mutableStateOf(false) } + var saving by remember { mutableStateOf(false) } + var showDiscard by remember { mutableStateOf(false) } + var showSearch by remember { mutableStateOf(false) } + var query by remember { mutableStateOf("") } + var replacement by remember { mutableStateOf("") } + var matchCase by remember { mutableStateOf(false) } + var matchIndex by remember { mutableIntStateOf(0) } + + val scrollState = rememberScrollState() + val textHorizontalState = rememberScrollState() + var viewportHeight by remember { mutableIntStateOf(0) } + var viewportWidth by remember { mutableIntStateOf(0) } + + val lineHeightDp = with(density) { EDITOR_LINE_HEIGHT_SP.sp.toDp() } + val lineHeightPx = with(density) { lineHeightDp.toPx() } + val totalLines = remember(text) { lineCountOf(text) } + val cursorLine = lineIndexOfOffset(text, selection.end) + val cursorColumn = columnOfOffset(text, selection.end) + val matches = remember(text, query, matchCase) { findEditorMatches(text, query, matchCase) } + val readOnly = lock != EditorLock.NONE + val bodyStyle = TextStyle( + fontFamily = FontFamily.Monospace, + fontSize = EDITOR_FONT_SIZE_SP.sp, + lineHeight = EDITOR_LINE_HEIGHT_SP.sp, + color = MaterialTheme.colorScheme.onBackground, + ) + val textMeasurer = rememberTextMeasurer() + val charWidthPx = remember(bodyStyle) { textMeasurer.measure(text = "0", style = bodyStyle).size.width.toFloat() } + + LaunchedEffect(file) { + loadState = EditorLoadState.LOADING + val size = withContext(Dispatchers.IO) { file.length() } + if (size > EDITOR_MAX_OPEN_BYTES) { + loadState = EditorLoadState.TOO_LARGE + return@LaunchedEffect + } + val read = withContext(Dispatchers.IO) { + runCatching { file.inputStream().use { readCapped(it, EDITOR_MAX_OPEN_BYTES) } } + } + val bytes = read.getOrNull() + if (bytes == null) { + loadState = if (read.isFailure) EditorLoadState.UNREADABLE else EditorLoadState.TOO_LARGE + return@LaunchedEffect + } + val decoded = decodeEditorText(bytes) + text = expandEditorTabs(decoded.text) + charset = decoded.charset + lock = when { + editorIsBinary(bytes) -> EditorLock.BINARY + decoded.lossy -> EditorLock.LOSSY + bytes.size > EDITOR_MAX_EDIT_BYTES -> EditorLock.TOO_LARGE + !file.canWrite() -> EditorLock.PERMISSION + else -> EditorLock.NONE + } + selection = TextRange.Zero + loadState = EditorLoadState.READY + } + + suspend fun revealLine(line: Int) { + val current = scrollState.value + val top = line * lineHeightPx + val target = when { + top < current -> top + top + lineHeightPx > current + viewportHeight -> top + lineHeightPx - viewportHeight + else -> return + } + val clamped = target.roundToInt().coerceIn(0, scrollState.maxValue) + if (clamped != current) scrollState.scrollTo(clamped) + } + + LaunchedEffect(text, selection.end, viewportHeight, viewportWidth) { + if (loadState != EditorLoadState.READY) return@LaunchedEffect + revealLine(lineIndexOfOffset(text, selection.end)) + if (viewportWidth > 0 && charWidthPx > 0) { + val x = (columnOfOffset(text, selection.end) - 1) * charWidthPx + val current = textHorizontalState.value + val target = when { + x < current -> x + x + charWidthPx > current + viewportWidth -> x + charWidthPx - viewportWidth + else -> null + } + if (target != null) { + val clamped = target.roundToInt().coerceIn(0, textHorizontalState.maxValue) + if (clamped != current) textHorizontalState.scrollTo(clamped) + } + } + } + + LaunchedEffect(query, matchCase) { + if (query.isEmpty()) return@LaunchedEffect + matchIndex = 0 + val first = matches.firstOrNull() ?: return@LaunchedEffect + selection = TextRange(first.first, first.last + 1) + composing = TextRange.Zero + } + + LaunchedEffect(text) { matchIndex = 0 } + + fun gotoMatch(delta: Int) { + if (matches.isEmpty()) return + matchIndex = Math.floorMod(matchIndex + delta, matches.size) + val m = matches[matchIndex] + selection = TextRange(m.first, m.last + 1) + composing = TextRange.Zero + } + + fun replaceCurrent() { + if (readOnly) return + val m = matches.getOrNull(matchIndex) ?: return + text = text.replaceRange(m.first, m.last + 1, replacement) + dirty = true + matchIndex = 0 + selection = TextRange(m.first + replacement.length) + composing = TextRange.Zero + } + + fun replaceAll() { + if (readOnly || matches.isEmpty()) return + val all = findEditorMatches(text, query, matchCase, limit = Int.MAX_VALUE) + text = replaceEditorMatches(text, all, replacement) + dirty = true + matchIndex = 0 + selection = TextRange.Zero + composing = TextRange.Zero + } + + fun save() { + if (readOnly || saving || loadState != EditorLoadState.READY) return + val snapshot = text + val bytes = snapshot.toByteArray(charset.charset) + saving = true + scope.launch { + val ok = withContext(Dispatchers.IO) { + runCatching { + val target = resolveEditorSaveTarget(file) + val parent = target.parentFile + if (parent == null) { + target.writeBytes(bytes) + } else { + val tmp = File.createTempFile(".${target.name}.", ".droiddeck-tmp", parent) + try { + tmp.writeBytes(bytes) + runCatching { + Files.setPosixFilePermissions(tmp.toPath(), Files.getPosixFilePermissions(target.toPath())) + } + if (!tmp.renameTo(target)) error("rename to ${target.name} failed") + } finally { + tmp.delete() + } + } + }.isSuccess + } + saving = false + if (ok) { + if (text == snapshot) dirty = false + } else { + Toast.makeText(context, context.getString(R.string.fm_edit_save_failed, file.name), Toast.LENGTH_SHORT).show() + } + } + } + + fun tryExit() { + if (saving) return + if (dirty && !readOnly) showDiscard = true else onExit() + } + + BackHandler { tryExit() } + + if (showDiscard) { + OutlinedAlertDialog( + onDismissRequest = { showDiscard = false }, + title = { Text(stringResource(R.string.fm_edit_discard_title)) }, + text = { Text(stringResource(R.string.fm_edit_discard_text, file.name)) }, + confirmButton = { + TextButton(onClick = onExit, modifier = Modifier.focusGlide(CircleShape)) { + Text(stringResource(R.string.fm_edit_discard)) + } + }, + dismissButton = { + TextButton(onClick = { showDiscard = false }, modifier = Modifier.focusGlide(CircleShape)) { + Text(stringResource(R.string.common_cancel)) + } + }, + ) + } + + Column( + modifier = modifier + .fillMaxSize() + .background(MaterialTheme.colorScheme.background), + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier + .fillMaxWidth() + .background(MaterialTheme.colorScheme.surfaceContainer) + .padding(horizontal = 8.dp, vertical = 6.dp), + ) { + IconButton(onClick = { tryExit() }, modifier = Modifier.focusGlide(CircleShape)) { + Icon( + Icons.Filled.ArrowBack, + stringResource(R.string.fm_back), + tint = MaterialTheme.colorScheme.primary, + ) + } + Column(modifier = Modifier.weight(1f).padding(horizontal = 4.dp)) { + Text( + text = file.name, + color = MaterialTheme.colorScheme.onBackground, + fontSize = 14.sp, + fontWeight = FontWeight.Medium, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + if (dirty) { + Text( + text = "\u2022 " + stringResource(R.string.fm_edit_modified), + color = MaterialTheme.colorScheme.primary, + fontSize = 11.sp, + maxLines = 1, + ) + } + } + if (loadState == EditorLoadState.READY && lock != EditorLock.NONE) { + Text( + text = stringResource( + if (lock == EditorLock.BINARY) R.string.fm_edit_binary else R.string.fm_edit_read_only, + ), + color = MaterialTheme.colorScheme.onSurfaceVariant, + fontSize = 10.sp, + maxLines = 1, + modifier = Modifier + .background(MaterialTheme.colorScheme.surfaceVariant, MaterialTheme.shapes.small) + .padding(horizontal = 6.dp, vertical = 2.dp), + ) + Spacer(Modifier.width(6.dp)) + } + IconButton( + onClick = { showSearch = !showSearch }, + enabled = loadState == EditorLoadState.READY, + modifier = Modifier.focusGlide(CircleShape), + ) { + Icon( + Icons.Filled.Search, + stringResource(R.string.fm_edit_find), + tint = if (showSearch) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + IconButton( + onClick = { save() }, + enabled = loadState == EditorLoadState.READY && !readOnly && dirty && !saving, + modifier = Modifier.focusGlide(CircleShape), + ) { + Icon( + Icons.Filled.Save, + stringResource(R.string.fm_edit_save), + tint = if (dirty) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } + + if (showSearch && loadState == EditorLoadState.READY) { + Column(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceContainer)) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().padding(start = 8.dp, end = 4.dp, top = 4.dp), + ) { + OutlinedTextField( + value = query, + onValueChange = { query = it; matchIndex = 0 }, + singleLine = true, + placeholder = { Text(stringResource(R.string.fm_edit_find), fontSize = 13.sp) }, + textStyle = MaterialTheme.typography.bodySmall, + keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search), + keyboardActions = KeyboardActions(onSearch = { gotoMatch(1) }), + modifier = Modifier.weight(1f).focusGlide(), + ) + Text( + text = if (matches.isEmpty()) "0/${matches.size}" else "${matchIndex.coerceAtMost(matches.size - 1) + 1}/${matches.size}", + color = MaterialTheme.colorScheme.onSurfaceVariant, + fontSize = 12.sp, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + modifier = Modifier.padding(horizontal = 6.dp).widthIn(min = 44.dp, max = 80.dp), + ) + IconButton( + onClick = { gotoMatch(-1) }, + enabled = matches.isNotEmpty(), + modifier = Modifier.focusGlide(CircleShape), + ) { + Icon( + Icons.Filled.KeyboardArrowUp, + stringResource(R.string.fm_edit_find_previous), + tint = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.size(20.dp), + ) + } + IconButton( + onClick = { gotoMatch(1) }, + enabled = matches.isNotEmpty(), + modifier = Modifier.focusGlide(CircleShape), + ) { + Icon( + Icons.Filled.KeyboardArrowDown, + stringResource(R.string.fm_edit_find_next), + tint = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.size(20.dp), + ) + } + TextButton(onClick = { matchCase = !matchCase }, modifier = Modifier.focusGlide(CircleShape)) { + Text( + text = "Aa", + color = if (matchCase) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant, + fontSize = 13.sp, + ) + } + IconButton(onClick = { showSearch = false }, modifier = Modifier.focusGlide(CircleShape)) { + Icon( + Icons.Filled.Close, + stringResource(R.string.common_cancel), + tint = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.size(20.dp), + ) + } + } + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().padding(start = 8.dp, top = 2.dp, end = 4.dp, bottom = 2.dp), + ) { + OutlinedTextField( + value = replacement, + onValueChange = { replacement = it }, + singleLine = true, + placeholder = { Text(stringResource(R.string.fm_edit_replace), fontSize = 13.sp) }, + textStyle = MaterialTheme.typography.bodySmall, + modifier = Modifier.weight(1f).focusGlide(), + ) + TextButton( + onClick = { replaceCurrent() }, + enabled = !readOnly && matches.isNotEmpty(), + modifier = Modifier.focusGlide(CircleShape), + ) { + Text(stringResource(R.string.fm_edit_replace), fontSize = 13.sp) + } + TextButton( + onClick = { replaceAll() }, + enabled = !readOnly && matches.isNotEmpty(), + modifier = Modifier.focusGlide(CircleShape), + ) { + Text(stringResource(R.string.fm_edit_replace_all), fontSize = 13.sp) + } + } + } + } + + Box( + modifier = Modifier + .weight(1f) + .fillMaxWidth() + .onSizeChanged { viewportHeight = it.height }, + ) { + when (loadState) { + EditorLoadState.LOADING -> CircularProgressIndicator( + modifier = Modifier.align(Alignment.Center), + ) + + EditorLoadState.TOO_LARGE, EditorLoadState.UNREADABLE -> Column( + horizontalAlignment = Alignment.CenterHorizontally, + modifier = Modifier.align(Alignment.Center).padding(24.dp), + ) { + Text( + text = stringResource( + if (loadState == EditorLoadState.TOO_LARGE) R.string.fm_edit_too_large + else R.string.fm_edit_open_failed, + if (loadState == EditorLoadState.TOO_LARGE) FileOps.formatBytes(context, file.length()) + else file.name, + ), + color = MaterialTheme.colorScheme.onSurfaceVariant, + fontSize = 14.sp, + textAlign = TextAlign.Center, + ) + } + + EditorLoadState.READY -> Column( + modifier = Modifier + .fillMaxSize() + .verticalScroll(scrollState), + ) { + Row(modifier = Modifier.fillMaxWidth().height(lineHeightDp * totalLines.toFloat())) { + LineNumbers( + scrollState = scrollState, + viewportHeight = viewportHeight, + lineHeightDp = lineHeightDp, + lineHeightPx = lineHeightPx, + totalLines = totalLines, + cursorLine = cursorLine, + style = bodyStyle, + mutedColor = MaterialTheme.colorScheme.onSurfaceVariant, + cursorColor = MaterialTheme.colorScheme.primary, + ) + Box(modifier = Modifier.weight(1f).onSizeChanged { viewportWidth = it.width }.horizontalScroll(textHorizontalState)) { + BasicTextField( + value = TextFieldValue(text = text, selection = selection, composition = composing), + onValueChange = { next -> + val changed = next.text != text + text = next.text + selection = next.selection + composing = next.composition + if (changed) dirty = true + }, + readOnly = readOnly, + textStyle = bodyStyle, + cursorBrush = SolidColor(MaterialTheme.colorScheme.primary), + keyboardOptions = KeyboardOptions( + keyboardType = KeyboardType.Text, + capitalization = KeyboardCapitalization.None, + ), + modifier = Modifier.fillMaxHeight().focusGlide(), + ) + } + } + } + } + } + + if (loadState == EditorLoadState.READY) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 4.dp), + ) { + Text( + text = stringResource(R.string.fm_edit_position, cursorLine + 1, cursorColumn), + color = MaterialTheme.colorScheme.onSurfaceVariant, + fontSize = 12.sp, + maxLines = 1, + ) + Spacer(Modifier.weight(1f)) + Text( + text = charset.label + " \u2022 " + FileOps.formatBytes(context, file.length()), + color = MaterialTheme.colorScheme.onSurfaceVariant, + fontSize = 12.sp, + maxLines = 1, + ) + } + } + } +} + +@Composable +private fun LineNumbers( + scrollState: ScrollState, + viewportHeight: Int, + lineHeightDp: Dp, + lineHeightPx: Float, + totalLines: Int, + cursorLine: Int, + style: TextStyle, + mutedColor: Color, + cursorColor: Color, + modifier: Modifier = Modifier, +) { + val firstLine = (scrollState.value / lineHeightPx).toInt().coerceIn(0, totalLines - 1) + val lastLine = ((scrollState.value + viewportHeight) / lineHeightPx) + .toInt() + .coerceIn(firstLine, totalLines - 1) + Column( + horizontalAlignment = Alignment.End, + modifier = modifier + .width(IntrinsicSize.Min) + .padding(top = lineHeightDp * firstLine.toFloat()), + ) { + for (line in firstLine..lastLine) { + Text( + text = (line + 1).toString(), + color = if (line == cursorLine) cursorColor else mutedColor, + textAlign = TextAlign.End, + maxLines = 1, + style = style, + modifier = Modifier.fillMaxWidth().height(lineHeightDp), + ) + } + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e52b58b21..68940091f6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2216,6 +2216,23 @@ Removed \"%1$s\" from Favorites Empty directory Up one level + Open + Save + Read only + Binary + Modified + Ln %1$d, Col %2$d + Find + Find previous + Find next + Replace + All + Discard changes? + Changes to \"%1$s\" will be lost. + Discard + Couldn\'t save \"%1$s\" + Couldn\'t open \"%1$s\" + Too large to open (%1$s) Choose a folder Choose a file Steam blue diff --git a/app/src/test/java/com/droiddeck/launcher/files/TextEditorLogicTest.kt b/app/src/test/java/com/droiddeck/launcher/files/TextEditorLogicTest.kt new file mode 100644 index 0000000000..7fb3a7a68b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/files/TextEditorLogicTest.kt @@ -0,0 +1,143 @@ +package com.droiddeck.launcher.files + +import java.io.ByteArrayInputStream +import java.io.File +import java.nio.charset.Charset +import java.nio.file.Files +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +class TextEditorLogicTest { + + @Test + fun nulByteMarksBinary() { + val clean = "hello world".toByteArray() + assertFalse(editorIsBinary(clean)) + val withNul = byteArrayOf(1, 2, 0, 3) + assertTrue(editorIsBinary(withNul)) + } + + @Test + fun binaryScanStopsAtSampleLimit() { + val bytes = ByteArray(9000) { if (it == 8999) 0 else 65 } + assertFalse(editorIsBinary(bytes)) + assertTrue(editorIsBinary(bytes, sample = 9000)) + } + + @Test + fun utf8TextDecodesAsUtf8() { + val decoded = decodeEditorText("你好, world".toByteArray(Charsets.UTF_8)) + assertEquals(EditorCharset.UTF8, decoded.charset) + assertFalse(decoded.lossy) + assertEquals("你好, world", decoded.text) + } + + @Test + fun gbkBytesFallBackToGb18030() { + val gbk = "你好".toByteArray(Charset.forName("GBK")) + val decoded = decodeEditorText(gbk) + assertEquals(EditorCharset.GB18030, decoded.charset) + assertFalse(decoded.lossy) + assertEquals("你好", decoded.text) + } + + @Test + fun undecodableBytesStayLossyUtf8() { + val decoded = decodeEditorText(byteArrayOf(0xFF.toByte())) + assertEquals(EditorCharset.UTF8, decoded.charset) + assertTrue(decoded.lossy) + } + + @Test + fun literalReplacementCharStaysCleanUtf8() { + val decoded = decodeEditorText("�A".toByteArray(Charsets.UTF_8)) + assertEquals(EditorCharset.UTF8, decoded.charset) + assertFalse(decoded.lossy) + } + + @Test + fun readCappedRefusesOverLimit() { + val data = ByteArray(100) { 65 } + assertArrayEquals(data, readCapped(ByteArrayInputStream(data), 100L)) + assertNull(readCapped(ByteArrayInputStream(data), 99L)) + } + + @Test + fun tabsExpandToTabStops() { + assertEquals("ab c", expandEditorTabs("ab\tc")) + assertEquals(" x", expandEditorTabs("\tx")) + assertEquals("abcd e", expandEditorTabs("abcd\te")) + assertEquals("abc def g", expandEditorTabs("abc\tdef\tg")) + assertEquals("a \n b", expandEditorTabs("a\t\n\tb")) + assertEquals("plain text", expandEditorTabs("plain text")) + } + + @Test + fun lineCountCountsTrailingEmptyLine() { + assertEquals(1, lineCountOf("")) + assertEquals(1, lineCountOf("abc")) + assertEquals(2, lineCountOf("a\nb")) + assertEquals(2, lineCountOf("a\n")) + } + + @Test + fun offsetMapsToZeroBasedLineAndOneBasedColumn() { + val text = "ab\ncd" + assertEquals(0, lineIndexOfOffset(text, 0)) + assertEquals(0, lineIndexOfOffset(text, 2)) + assertEquals(1, lineIndexOfOffset(text, 3)) + assertEquals(1, lineIndexOfOffset(text, 5)) + assertEquals(1, columnOfOffset(text, 0)) + assertEquals(3, columnOfOffset(text, 2)) + assertEquals(2, columnOfOffset(text, 4)) + assertEquals(3, columnOfOffset(text, 5)) + } + + @Test + fun findMatchesHonorsCaseAndLimit() { + val text = "abcABCabc" + val all = findEditorMatches(text, "abc", matchCase = false) + assertEquals(listOf(0..2, 3..5, 6..8), all) + val exact = findEditorMatches(text, "abc", matchCase = true) + assertEquals(listOf(0..2, 6..8), exact) + assertEquals(2, findEditorMatches(text, "abc", matchCase = false, limit = 2).size) + assertEquals(emptyList(), findEditorMatches(text, "")) + assertEquals(listOf(0..1), findEditorMatches("aaa", "aa")) + } + + @Test + fun replaceSpansEveryMatch() { + val text = "one two one" + val matches = findEditorMatches(text, "one", matchCase = true) + assertEquals("1 two 1", replaceEditorMatches(text, matches, "1")) + assertEquals(text, replaceEditorMatches(text, emptyList(), "x")) + } + + @Test + fun saveTargetResolvesSymbolicLink() { + val dir = Files.createTempDirectory("editor-link").toFile() + try { + val target = File(dir, "real.txt").apply { writeText("hi") } + val link = File(dir, "link.txt") + Files.createSymbolicLink(link.toPath(), target.toPath()) + assertEquals(target.canonicalFile, resolveEditorSaveTarget(link)) + } finally { + dir.deleteRecursively() + } + } + + @Test + fun saveTargetKeepsRegularFile() { + val dir = Files.createTempDirectory("editor-plain").toFile() + try { + val plain = File(dir, "plain.txt").apply { writeText("hi") } + assertEquals(plain, resolveEditorSaveTarget(plain)) + } finally { + dir.deleteRecursively() + } + } +}