Modern Apps git

← Modern-Apps.git

code: add split view with an independently editable second pane

Commit
3c9e40f8b83c5bf70da560a9b45596af41421eed
Author
vayun-mathur <[email protected]>
When
2026-08-07 00:01 UTC (4 months ago)
Parents
7c07ac1

Diff

code/src/main/java/com/vayunmathur/code/ui/CodeEditor.kt
diff --git a/code/src/main/java/com/vayunmathur/code/ui/CodeEditor.kt b/code/src/main/java/com/vayunmathur/code/ui/CodeEditor.ktindex cbb2988974..db49a58b30 100644--- a/code/src/main/java/com/vayunmathur/code/ui/CodeEditor.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/CodeEditor.kt@@ -75,6 +75,8 @@ fun CodeEditor(     completions: List<Completion> = emptyList(),     showCompletions: Boolean = false,     editorTheme: String = com.vayunmathur.code.syntax.EditorThemes.DEFAULT,+    /** When set, edits route here instead of [CodeActions.onEditorChange] (used by the second pane). */+    onValueChangeOverride: ((androidx.compose.ui.text.input.TextFieldValue) -> Unit)? = null, ) {     // Find/replace state is per-tab, so it resets when switching files. Keyed on the file     // name rather than the tab value: the latter is rebuilt on every keystroke, which would@@ -184,7 +186,7 @@ fun CodeEditor(             Box(Modifier.weight(1f).fillMaxHeight()) {                 BasicTextField(                     value = tab.value,-                    onValueChange = actions::onEditorChange,+                    onValueChange = onValueChangeOverride ?: actions::onEditorChange,                     modifier = Modifier                         .fillMaxSize()                         .verticalScroll(verticalScroll)
code/src/main/java/com/vayunmathur/code/ui/EditorScreen.kt
diff --git a/code/src/main/java/com/vayunmathur/code/ui/EditorScreen.kt b/code/src/main/java/com/vayunmathur/code/ui/EditorScreen.ktindex 5a353720eb..034d51772f 100644--- a/code/src/main/java/com/vayunmathur/code/ui/EditorScreen.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/EditorScreen.kt@@ -10,9 +10,11 @@ import androidx.compose.foundation.clickable import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column 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.padding@@ -44,6 +46,7 @@ import androidx.compose.ui.unit.dp import com.vayunmathur.code.util.CodeActions import com.vayunmathur.code.util.CodeUiState import com.vayunmathur.code.util.EditorViewModel+import com.vayunmathur.code.util.TabUiState import com.vayunmathur.code.util.extractSymbols import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.AlertDialog@@ -246,19 +249,33 @@ fun EditorScreen(                         )                         HorizontalDivider()                     }-                    CodeEditor(-                        tab = tab,-                        actions = actions,-                        softWrap = state.softWrap,-                        fontSize = state.fontSize,-                        showFind = showFind,-                        onCloseFind = { showFind = false },-                        modifier = Modifier.weight(1f),-                        initialQuery = initialFind.orEmpty(),-                        completions = state.completions,-                        showCompletions = state.showCompletions,-                        editorTheme = state.editorTheme,-                    )+                    val secondaryTab = state.secondaryTab+                    if (secondaryTab != null) {+                        SplitEditors(+                            state = state,+                            actions = actions,+                            primary = tab,+                            secondary = secondaryTab,+                            showFind = showFind,+                            onCloseFind = { showFind = false },+                            initialQuery = initialFind.orEmpty(),+                            modifier = Modifier.weight(1f),+                        )+                    } else {+                        CodeEditor(+                            tab = tab,+                            actions = actions,+                            softWrap = state.softWrap,+                            fontSize = state.fontSize,+                            showFind = showFind,+                            onCloseFind = { showFind = false },+                            modifier = Modifier.weight(1f),+                            initialQuery = initialFind.orEmpty(),+                            completions = state.completions,+                            showCompletions = state.showCompletions,+                            editorTheme = state.editorTheme,+                        )+                    }                 }             }         }@@ -379,6 +396,99 @@ private fun EmptyEditorState(onOpenFolder: () -> Unit, onOpenFile: () -> Unit) {     } } +/**+ * Two editor panes side by side (or stacked when narrow). The primary pane is the current tab with+ * find/autocomplete; the secondary pane edits its own tab through [CodeActions.onSecondaryEditorChange].+ * Toolbar/tab-strip/find act on the primary pane (focused-pane routing is a Phase-7 follow-up).+ */+@Composable+private fun SplitEditors(+    state: CodeUiState,+    actions: CodeActions,+    primary: TabUiState,+    secondary: TabUiState,+    showFind: Boolean,+    onCloseFind: () -> Unit,+    initialQuery: String,+    modifier: Modifier = Modifier,+) {+    BoxWithConstraints(modifier) {+        val wide = maxWidth >= 640.dp+        val primaryPane: @Composable (Modifier) -> Unit = { paneModifier ->+            Column(paneModifier) {+                PaneHeader(primary.name, onClose = null)+                CodeEditor(+                    tab = primary,+                    actions = actions,+                    softWrap = state.softWrap,+                    fontSize = state.fontSize,+                    showFind = showFind,+                    onCloseFind = onCloseFind,+                    modifier = Modifier.weight(1f),+                    initialQuery = initialQuery,+                    completions = state.completions,+                    showCompletions = state.showCompletions,+                    editorTheme = state.editorTheme,+                )+            }+        }+        val secondaryPane: @Composable (Modifier) -> Unit = { paneModifier ->+            Column(paneModifier) {+                PaneHeader(secondary.name, onClose = { actions.toggleSplit() })+                CodeEditor(+                    tab = secondary,+                    actions = actions,+                    softWrap = state.softWrap,+                    fontSize = state.fontSize,+                    showFind = false,+                    onCloseFind = {},+                    modifier = Modifier.weight(1f),+                    editorTheme = state.editorTheme,+                    onValueChangeOverride = actions::onSecondaryEditorChange,+                )+            }+        }+        if (wide) {+            Row(Modifier.fillMaxSize()) {+                primaryPane(Modifier.weight(1f).fillMaxHeight())+                Box(Modifier.width(1.dp).fillMaxHeight().background(MaterialTheme.colorScheme.outline))+                secondaryPane(Modifier.weight(1f).fillMaxHeight())+            }+        } else {+            Column(Modifier.fillMaxSize()) {+                primaryPane(Modifier.weight(1f).fillMaxWidth())+                HorizontalDivider()+                secondaryPane(Modifier.weight(1f).fillMaxWidth())+            }+        }+    }+}++/** A thin header above a split pane: the file name, plus an optional close (secondary) button. */+@Composable+private fun PaneHeader(name: String, onClose: (() -> Unit)?) {+    Row(+        modifier = Modifier+            .fillMaxWidth()+            .background(MaterialTheme.colorScheme.surfaceVariant)+            .padding(start = 8.dp),+        verticalAlignment = Alignment.CenterVertically,+    ) {+        Text(+            name,+            modifier = Modifier.weight(1f),+            maxLines = 1,+            overflow = TextOverflow.Ellipsis,+            style = MaterialTheme.typography.labelMedium,+        )+        if (onClose != null) {+            IconButton(onClick = onClose, modifier = Modifier.size(28.dp)) {+                IconClose(Modifier.size(16.dp))+            }+        }+    }+}+ /** Horizontally scrollable strip of open tabs, each with a dirty indicator and close button. */ @Composable private fun TabStrip(state: CodeUiState, actions: CodeActions) {@@ -483,6 +593,7 @@ private fun EditorToolbar(             Item(text = stringResource(R.string.preview)) { onOpenPreview() }             Item(text = stringResource(R.string.format_document)) { actions.formatDocument() }             Item(text = stringResource(R.string.resolve_conflicts)) { onResolveConflicts() }+            Item(text = stringResource(R.string.split_view)) { actions.toggleSplit() }         }         Spacer(Modifier.width(8.dp))         Text(@@ -579,6 +690,7 @@ private fun editorCommands(     PickerItem(stringResource(R.string.delete_line)) { actions.deleteLine() },     PickerItem(stringResource(R.string.format_document)) { actions.formatDocument() },     PickerItem(stringResource(R.string.resolve_conflicts)) { onResolveConflicts() },+    PickerItem(stringResource(R.string.split_view)) { actions.toggleSplit() },     PickerItem(stringResource(R.string.soft_wrap)) { actions.toggleSoftWrap() },     PickerItem(stringResource(R.string.undo)) { actions.undo() },     PickerItem(stringResource(R.string.redo)) { actions.redo() },
code/src/main/java/com/vayunmathur/code/util/CodeUiContract.kt
diff --git a/code/src/main/java/com/vayunmathur/code/util/CodeUiContract.kt b/code/src/main/java/com/vayunmathur/code/util/CodeUiContract.ktindex b0ffee1d03..4454f75668 100644--- a/code/src/main/java/com/vayunmathur/code/util/CodeUiContract.kt+++ b/code/src/main/java/com/vayunmathur/code/util/CodeUiContract.kt@@ -61,6 +61,8 @@ data class SearchResult( data class CodeUiState(     val tabs: List<TabUiState> = emptyList(),     val currentIndex: Int = -1,+    /** Index of the tab shown in the second split pane, or -1 when the editor is single-pane. */+    val secondaryIndex: Int = -1,     val softWrap: Boolean = false,     /** Display name of the opened folder, shown as the file pane's header. */     val rootName: String? = null,@@ -82,6 +84,7 @@ data class CodeUiState(     val recentFiles: List<ProjectFileEntry> = emptyList(), ) {     val currentTab: TabUiState? get() = tabs.getOrNull(currentIndex)+    val secondaryTab: TabUiState? get() = tabs.getOrNull(secondaryIndex) }  /**@@ -115,6 +118,12 @@ interface CodeActions {     fun insertText(insert: String) {}     fun onEditorChange(new: TextFieldValue) {} +    /** Apply an edit to the secondary split pane's tab. */+    fun onSecondaryEditorChange(new: TextFieldValue) {}++    /** Open a second editor pane (or close it if already open). */+    fun toggleSplit() {}+     // ---- Line editing ----      /** Toggle the line comment on the line(s) the selection touches (no-op if the language has none). */
code/src/main/java/com/vayunmathur/code/util/EditorViewModel.kt
diff --git a/code/src/main/java/com/vayunmathur/code/util/EditorViewModel.kt b/code/src/main/java/com/vayunmathur/code/util/EditorViewModel.ktindex 446ec07375..1aa5b44a24 100644--- a/code/src/main/java/com/vayunmathur/code/util/EditorViewModel.kt+++ b/code/src/main/java/com/vayunmathur/code/util/EditorViewModel.kt@@ -136,6 +136,10 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),         private set     val currentTab: OpenTab? get() = tabs.getOrNull(currentIndex) +    /** When set, a second pane shows this tab beside the current one (split view). */+    var secondaryIndex by mutableStateOf<Int?>(null)+        private set+     // ---- Preferences ----     var softWrap by mutableStateOf(false)         private set@@ -227,6 +231,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),                 )             },             currentIndex = currentIndex,+            secondaryIndex = secondaryIndex ?: -1,             softWrap = softWrap,             rootName = rootName,             folderOpen = rootDir != null,@@ -572,6 +577,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     override fun selectTab(index: Int) {         if (index in tabs.indices) {             currentIndex = index+            if (secondaryIndex == index) secondaryIndex = null // never show the same tab in both panes             dismissCompletions()             saveSession()         }@@ -587,19 +593,46 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),             removingCurrent -> index.coerceAtMost(tabs.lastIndex)             else -> currentIndex         }+        secondaryIndex = secondaryIndex?.let { s ->+            when {+                s == index -> null+                s > index -> s - 1+                else -> s+            }+        }?.takeIf { it in tabs.indices && it != currentIndex }         saveSession()     }      // ---- Editing ----      override fun onEditorChange(new: TextFieldValue) {-        val tab = currentTab ?: return+        editTab(currentTab ?: return, new, isPrimary = true)+    }++    override fun onSecondaryEditorChange(new: TextFieldValue) {+        val tab = secondaryIndex?.let { tabs.getOrNull(it) } ?: return+        editTab(tab, new, isPrimary = false)+    }++    /** Applies smart input to [tab] as a single undo step; only the primary pane drives completions. */+    private fun editTab(tab: OpenTab, new: TextFieldValue, isPrimary: Boolean) {         val indentUnit = " ".repeat(tabWidth)         val processed = applyEditorInput(tab.value, new, indentUnit, autoIndent, autoCloseBrackets)         if (processed.text != tab.value.text) tab.pushUndo(tab.value)         tab.value = processed         if (autoSave) scheduleAutoSave()-        updateCompletions()+        if (isPrimary) updateCompletions()+    }++    /** Opens/closes the second editor pane, choosing an adjacent tab as the secondary. */+    override fun toggleSplit() {+        if (secondaryIndex != null) {+            secondaryIndex = null+            return+        }+        if (tabs.size < 2 || currentIndex < 0) return+        val other = (currentIndex + 1).takeIf { it in tabs.indices } ?: (currentIndex - 1)+        secondaryIndex = other.takeIf { it in tabs.indices && it != currentIndex }     }      // ---- Autocomplete ----
code/src/main/res/values/strings.xml
diff --git a/code/src/main/res/values/strings.xml b/code/src/main/res/values/strings.xmlindex 60f34aefc3..dc81ceb71e 100644--- a/code/src/main/res/values/strings.xml+++ b/code/src/main/res/values/strings.xml@@ -122,4 +122,5 @@     <string name="conflict_keep_ours">Ours</string>     <string name="conflict_keep_theirs">Theirs</string>     <string name="conflict_keep_both">Both</string>+    <string name="split_view">Split view</string> </resources>