Modern Apps git

← Modern-Apps.git

code: route toolbar, find and edit actions to the focused split pane

Commit
723dae6ca0a3ab28f45523b1108086e5898497a1
Author
Vayun Mathur <[email protected]>
When
2026-08-07 02:42 UTC (4 months ago)
Parents
71f0a44

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 db49a58b30..a4af7dbb15 100644--- a/code/src/main/java/com/vayunmathur/code/ui/CodeEditor.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/CodeEditor.kt@@ -28,6 +28,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.TextLayoutResult@@ -75,6 +76,8 @@ fun CodeEditor(     completions: List<Completion> = emptyList(),     showCompletions: Boolean = false,     editorTheme: String = com.vayunmathur.code.syntax.EditorThemes.DEFAULT,+    /** True when this is the secondary split pane, so focus routes shared actions there. */+    secondaryPane: Boolean = false,     /** When set, edits route here instead of [CodeActions.onEditorChange] (used by the second pane). */     onValueChangeOverride: ((androidx.compose.ui.text.input.TextFieldValue) -> Unit)? = null, ) {@@ -189,6 +192,7 @@ fun CodeEditor(                     onValueChange = onValueChangeOverride ?: actions::onEditorChange,                     modifier = Modifier                         .fillMaxSize()+                        .onFocusChanged { if (it.isFocused) actions.focusPane(secondaryPane) }                         .verticalScroll(verticalScroll)                         .then(if (softWrap) Modifier else Modifier.horizontalScroll(horizontalScroll))                         .padding(horizontal = 8.dp),
code/src/main/java/com/vayunmathur/code/ui/CodeEditorView.kt
diff --git a/code/src/main/java/com/vayunmathur/code/ui/CodeEditorView.kt b/code/src/main/java/com/vayunmathur/code/ui/CodeEditorView.ktindex 357efdab1c..d8f0f057d1 100644--- a/code/src/main/java/com/vayunmathur/code/ui/CodeEditorView.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/CodeEditorView.kt@@ -20,6 +20,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester+import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color@@ -78,6 +79,7 @@ fun CodeEditorView(     showWhitespace: Boolean = false,     showIndentGuides: Boolean = false,     showMinimap: Boolean = false,+    secondaryPane: Boolean = false,     onValueChangeOverride: ((TextFieldValue) -> Unit)? = null, ) {     val value = tab.value@@ -184,6 +186,7 @@ fun CodeEditorView(                 }                 .focusRequester(focusRequester)                 .focusable()+                .onFocusChanged { if (it.isFocused) actions.focusPane(secondaryPane) }                 .onPreviewKeyEvent { event ->                     handleEditorKey(                         event = event,
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 a1fc35eecb..96410fae6f 100644--- a/code/src/main/java/com/vayunmathur/code/ui/EditorScreen.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/EditorScreen.kt@@ -335,7 +335,7 @@ fun EditorScreen(             onDismiss = { showGoToLine = false },         )     }-    val outlineTab = state.currentTab+    val outlineTab = state.activeTab     if (showOutline && outlineTab != null) {         val symbols = remember(outlineTab.value.text, outlineTab.language) {             extractSymbols(outlineTab.value.text, outlineTab.language)@@ -353,7 +353,7 @@ fun EditorScreen(             onDismiss = { showOutline = false },         )     }-    val conflictTab = state.currentTab+    val conflictTab = state.activeTab     if (showMergeResolver && conflictTab != null) {         MergeResolverDialog(             text = conflictTab.value.text,@@ -411,9 +411,9 @@ 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).+ * Two editor panes side by side (or stacked when narrow). Each pane edits its own tab and reports+ * focus via [CodeActions.focusPane], so the shared toolbar/find/navigation act on the focused pane.+ * The find bar is shown in whichever pane currently holds focus.  */ @Composable private fun SplitEditors(@@ -426,6 +426,7 @@ private fun SplitEditors(     initialQuery: String,     modifier: Modifier = Modifier, ) {+    val focusSecondary = state.focusedSecondary     BoxWithConstraints(modifier) {         val wide = maxWidth >= 640.dp         val primaryPane: @Composable (Modifier) -> Unit = { paneModifier ->@@ -436,13 +437,14 @@ private fun SplitEditors(                     actions = actions,                     softWrap = state.softWrap,                     fontSize = state.fontSize,-                    showFind = showFind,+                    showFind = showFind && !focusSecondary,                     onCloseFind = onCloseFind,                     modifier = Modifier.weight(1f),                     initialQuery = initialQuery,                     completions = state.completions,-                    showCompletions = state.showCompletions,+                    showCompletions = state.showCompletions && !focusSecondary,                     editorTheme = state.editorTheme,+                    secondaryPane = false,                 )             }         }@@ -454,10 +456,11 @@ private fun SplitEditors(                     actions = actions,                     softWrap = state.softWrap,                     fontSize = state.fontSize,-                    showFind = false,-                    onCloseFind = {},+                    showFind = showFind && focusSecondary,+                    onCloseFind = onCloseFind,                     modifier = Modifier.weight(1f),                     editorTheme = state.editorTheme,+                    secondaryPane = true,                     onValueChangeOverride = actions::onSecondaryEditorChange,                 )             }@@ -575,7 +578,7 @@ private fun EditorToolbar(     onOpenOutline: () -> Unit = {},     onResolveConflicts: () -> Unit = {}, ) {-    val tab = state.currentTab ?: return+    val tab = state.activeTab ?: return     Row(         modifier = Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()),         verticalAlignment = Alignment.CenterVertically,
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 f0362e2d67..a2de6bec5b 100644--- a/code/src/main/java/com/vayunmathur/code/util/CodeUiContract.kt+++ b/code/src/main/java/com/vayunmathur/code/util/CodeUiContract.kt@@ -65,6 +65,8 @@ data class CodeUiState(     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,+    /** True when the secondary split pane holds focus, so shared actions target it. */+    val focusedSecondary: Boolean = false,     val softWrap: Boolean = false,     /** Display name of the opened folder, shown as the file pane's header. */     val rootName: String? = null,@@ -93,6 +95,9 @@ data class CodeUiState( ) {     val currentTab: TabUiState? get() = tabs.getOrNull(currentIndex)     val secondaryTab: TabUiState? get() = tabs.getOrNull(secondaryIndex)++    /** The tab the shared toolbar/find/navigation act on: the focused split pane's tab. */+    val activeTab: TabUiState? get() = if (focusedSecondary) secondaryTab ?: currentTab else currentTab }  /**@@ -132,6 +137,9 @@ interface CodeActions {     /** Open a second editor pane (or close it if already open). */     fun toggleSplit() {} +    /** Report that a split pane gained focus, so shared actions target it ([secondary] = the 2nd pane). */+    fun focusPane(secondary: Boolean) {}+     // ---- 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 07750f0296..aca50b9430 100644--- a/code/src/main/java/com/vayunmathur/code/util/EditorViewModel.kt+++ b/code/src/main/java/com/vayunmathur/code/util/EditorViewModel.kt@@ -143,6 +143,14 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     var secondaryIndex by mutableStateOf<Int?>(null)         private set +    /** True when the secondary split pane holds focus, so shared actions target it instead. */+    var focusedSecondary by mutableStateOf(false)+        private set++    /** The tab shared toolbar/find/navigation actions target: the focused pane's tab. */+    private val activeTab: OpenTab?+        get() = if (focusedSecondary) secondaryIndex?.let { tabs.getOrNull(it) } else currentTab+     // ---- Preferences ----     var softWrap by mutableStateOf(false)         private set@@ -251,6 +259,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),             },             currentIndex = currentIndex,             secondaryIndex = secondaryIndex ?: -1,+            focusedSecondary = focusedSecondary,             softWrap = softWrap,             rootName = rootName,             folderOpen = rootDir != null,@@ -616,6 +625,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),         if (index in tabs.indices) {             currentIndex = index             if (secondaryIndex == index) secondaryIndex = null // never show the same tab in both panes+            focusedSecondary = false             dismissCompletions()             saveSession()         }@@ -638,6 +648,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),                 else -> s             }         }?.takeIf { it in tabs.indices && it != currentIndex }+        if (secondaryIndex == null) focusedSecondary = false         saveSession()     } @@ -666,6 +677,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     override fun toggleSplit() {         if (secondaryIndex != null) {             secondaryIndex = null+            focusedSecondary = false             return         }         if (tabs.size < 2 || currentIndex < 0) return@@ -673,6 +685,11 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),         secondaryIndex = other.takeIf { it in tabs.indices && it != currentIndex }     } +    /** Records which split pane holds focus, so shared toolbar/find/nav actions target it. */+    override fun focusPane(secondary: Boolean) {+        focusedSecondary = secondary && secondaryIndex != null+    }+     // ---- Autocomplete ----      override fun requestCompletions() = updateCompletions()@@ -919,13 +936,21 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),         autoSaveJob?.cancel()         autoSaveJob = viewModelScope.launch {             delay(AUTO_SAVE_DELAY_MS)-            if (currentTab?.isDirty == true) save()+            val tab = activeTab ?: return@launch+            if (tab.isDirty) saveTab(tab)         }     } +    /** Commits a find/replace edit to [tab] as one undo step, bypassing smart input. */+    private fun commitEdit(tab: OpenTab, new: TextFieldValue) {+        if (new.text != tab.value.text) tab.pushUndo(tab.value)+        tab.value = new+        if (autoSave) scheduleAutoSave()+    }+     /** Applies a pure whole-line edit as a single undo step. */     private fun applyLineEdit(transform: (TextFieldValue) -> TextFieldValue) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val next = transform(tab.value)         if (next.text == tab.value.text && next.selection == tab.value.selection) return         tab.pushUndo(tab.value)@@ -934,7 +959,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     }      override fun toggleComment() {-        val prefix = currentTab?.language?.lineCommentPrefix ?: return+        val prefix = activeTab?.language?.lineCommentPrefix ?: return         applyLineEdit { toggleLineComment(it, prefix) }     } @@ -947,7 +972,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     override fun deleteLine() = applyLineEdit(::deleteLine)      override fun formatDocument() {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val formatted = when (tab.language) {             Language.JSON -> formatJson(tab.value.text)             Language.XML -> formatXml(tab.value.text)@@ -960,7 +985,7 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     }      override fun resolveConflicts(resolutions: List<Resolution>) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val resolved = applyResolutions(tab.value.text, resolutions)         if (resolved == tab.value.text) return         tab.pushUndo(tab.value)@@ -970,21 +995,21 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),      /** Moves the caret to the start of [line] (1-based), without recording an undo step. */     override fun goToLine(line: Int) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val offset = lineStartOffset(tab.value.text, line)         setSelection(TextRange(offset))     }      /** Moves the selection without recording an undo step (used by find navigation). */     override fun setSelection(range: TextRange) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         tab.value = tab.value.copy(selection = range)     }      // ---- Folding (experimental editor) ----      override fun toggleFold(headerLine: Int) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         tab.foldedHeaders =             if (headerLine in tab.foldedHeaders) tab.foldedHeaders - headerLine             else tab.foldedHeaders + headerLine@@ -992,13 +1017,13 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     }      override fun foldAllInTab() {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         tab.foldedHeaders = computeFoldRegions(tab.value.text).map { it.startLine }.toSet()         persistFoldState(tab)     }      override fun unfoldAll() {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         tab.foldedHeaders = emptySet()         persistFoldState(tab)     }@@ -1012,15 +1037,15 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     }      override fun undo() {-        currentTab?.undo()+        activeTab?.undo()     }      override fun redo() {-        currentTab?.redo()+        activeTab?.redo()     }      override fun save() {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         saveTab(tab)     } @@ -1109,12 +1134,12 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),      /** Inserts [insert] at the caret, replacing any current selection (used by the Tab button). */     override fun insertText(insert: String) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val v = tab.value         val start = v.selection.min         val end = v.selection.max         val newText = v.text.substring(0, start) + insert + v.text.substring(end)-        onEditorChange(TextFieldValue(newText, TextRange(start + insert.length)))+        editTab(tab, TextFieldValue(newText, TextRange(start + insert.length)), isPrimary = tab === currentTab)     }      override fun toggleSoftWrap() {@@ -1191,15 +1216,15 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     // ---- Find & replace ----      override fun replaceRange(range: IntRange, replacement: String) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val text = tab.value.text         if (range.first < 0 || range.last + 1 > text.length) return         val newText = text.substring(0, range.first) + replacement + text.substring(range.last + 1)-        onEditorChange(TextFieldValue(newText, TextRange(range.first + replacement.length)))+        commitEdit(tab, TextFieldValue(newText, TextRange(range.first + replacement.length)))     }      override fun replaceAll(matches: List<IntRange>, replacement: String) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         if (matches.isEmpty()) return         val text = tab.value.text         val sb = StringBuilder(text.length)@@ -1211,14 +1236,14 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),             last = m.last + 1         }         sb.append(text, last, text.length)-        onEditorChange(TextFieldValue(sb.toString(), TextRange(sb.length)))+        commitEdit(tab, TextFieldValue(sb.toString(), TextRange(sb.length)))     }      private fun buildRegex(pattern: String, caseSensitive: Boolean): Regex =         Regex(pattern, if (caseSensitive) emptySet() else setOf(RegexOption.IGNORE_CASE))      override fun replaceMatchRegex(range: IntRange, pattern: String, replacement: String, caseSensitive: Boolean) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val text = tab.value.text         if (range.first < 0 || range.last + 1 > text.length) return         val regex = runCatching { buildRegex(pattern, caseSensitive) }.getOrNull() ?: return@@ -1228,12 +1253,12 @@ class EditorViewModel(application: Application) : AndroidViewModel(application),     }      override fun replaceAllRegex(pattern: String, replacement: String, caseSensitive: Boolean) {-        val tab = currentTab ?: return+        val tab = activeTab ?: return         val regex = runCatching { buildRegex(pattern, caseSensitive) }.getOrNull() ?: return         val text = tab.value.text         val newText = runCatching { regex.replace(text, replacement) }.getOrNull() ?: return         if (newText == text) return-        onEditorChange(TextFieldValue(newText, TextRange(newText.length)))+        commitEdit(tab, TextFieldValue(newText, TextRange(newText.length)))     }      // ---- Project search ----