diff --git a/android/app/src/androidTest/kotlin/com/logseq/logseq/AndroidOutlinerToolbarTest.kt b/android/app/src/androidTest/kotlin/com/logseq/logseq/AndroidOutlinerToolbarTest.kt index 1acebe7f..6a74c87b 100644 --- a/android/app/src/androidTest/kotlin/com/logseq/logseq/AndroidOutlinerToolbarTest.kt +++ b/android/app/src/androidTest/kotlin/com/logseq/logseq/AndroidOutlinerToolbarTest.kt @@ -54,10 +54,16 @@ class AndroidOutlinerToolbarTest { @After fun closeBridge() { bridge?.let { ui.runOnIdle { it.close() } } } - private fun snapshot(editing: Boolean, caret: Int = title.length, taskSelected: Boolean = false): String { + private fun snapshot( + editing: Boolean, + caret: Int = title.length, + taskSelected: Boolean = false, + selected: Boolean = !editing, + ): String { val state = if (editing) JSONObject().put("editing", JSONObject().put("uuid", "toolbar-block") .put("title", title).put("caretUTF16Offset", caret)) - else JSONObject().put("selectedBlockIds", org.json.JSONArray().put("toolbar-block")) + else if (selected) JSONObject().put("selectedBlockIds", org.json.JSONArray().put("toolbar-block")) + else JSONObject() if (taskSelected) state.put("selectedTaskBlockIds", org.json.JSONArray().put("toolbar-block")) return JSONObject().put("ok", true).put("result", JSONObject() .put("graphName", "Toolbar test").put("selectedGraphId", "toolbar-test") @@ -93,6 +99,18 @@ class AndroidOutlinerToolbarTest { capture("editor-toolbar") } + @Test fun editorChromeDoesNotResizeTheOutlinerViewport() { + toolbar() + val outliner = ui.onNodeWithTag("list.outliner").assertIsDisplayed() + val editingHeight = outliner.fetchSemanticsNode().boundsInRoot.height + ui.runOnIdle { + filter.applyJson(requireNotNull(bridge).applySnapshot(snapshot(editing = false, selected = false))) + } + ui.waitForIdle() + ui.onNodeWithTag("toolbar.outliner.editor").assertDoesNotExist() + assertEquals(editingHeight, outliner.fetchSemanticsNode().boundsInRoot.height, 1f) + } + @Test fun blockSelectionUsesTheNativeFloatingToolbarContainer() { toolbar(editing = false) ui.onNodeWithTag("toolbar.outliner.selection").assertHeightIsAtLeast(64.dp) diff --git a/android/app/src/main/kotlin/com/logseq/logseq/ui/LogseqApp.kt b/android/app/src/main/kotlin/com/logseq/logseq/ui/LogseqApp.kt index c4da1cf1..eb50c516 100644 --- a/android/app/src/main/kotlin/com/logseq/logseq/ui/LogseqApp.kt +++ b/android/app/src/main/kotlin/com/logseq/logseq/ui/LogseqApp.kt @@ -26,6 +26,7 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.SideEffect import androidx.compose.runtime.collectAsState @@ -34,6 +35,7 @@ import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalView import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.res.stringResource import android.content.Context @@ -59,8 +61,13 @@ import androidx.compose.ui.semantics.testTagsAsResourceId import androidx.compose.ui.unit.dp import com.logseq.logseq.applyAndroidSystemBarAppearance import androidx.core.content.edit +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsAnimationCompat +import androidx.core.view.WindowInsetsCompat import com.logseq.logseq.runtime.LogseqRuntime import com.logseq.logseq.runtime.RecordedAudioAsset +import com.logseq.logseq.ui.extensions.ImeDismissalSignal +import com.logseq.logseq.ui.extensions.LocalImeDismissalSignal import com.logseq.logseq.ui.extensions.LogseqDialogs import kotlinx.coroutines.launch @@ -94,9 +101,36 @@ internal fun LogseqApp(runtime: LogseqRuntime) { colorScheme = colorScheme, typography = LogseqTheme.typography, ) { + val imeDismissal = remember { ImeDismissalSignal() } + // Compose owns insets callbacks on its owner view, so listen at the + // window root where the IME animation callback remains registered. + val hostView = LocalView.current.rootView + DisposableEffect(hostView, imeDismissal) { + val callback = object : WindowInsetsAnimationCompat.Callback( + WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE, + ) { + override fun onPrepare(animation: WindowInsetsAnimationCompat) { + val insets = ViewCompat.getRootWindowInsets(hostView) + val hidingIme = insets != null && + insets.isVisible(WindowInsetsCompat.Type.ime()) && + insets.getInsets(WindowInsetsCompat.Type.ime()).bottom > 0 + if ((animation.typeMask and WindowInsetsCompat.Type.ime()) != 0 && hidingIme) { + imeDismissal.notifyStarted() + } + } + + override fun onProgress( + insets: WindowInsetsCompat, + runningAnimations: MutableList, + ): WindowInsetsCompat = insets + } + ViewCompat.setWindowInsetsAnimationCallback(hostView, callback) + onDispose { ViewCompat.setWindowInsetsAnimationCallback(hostView, null) } + } androidx.compose.runtime.CompositionLocalProvider( dev.lui.LocalLuiThemeDark provides dark, dev.lui.LocalLuiSemanticColors provides LogseqTheme.semanticColors(MaterialTheme.colorScheme), + LocalImeDismissalSignal provides imeDismissal, ) { if (BuildConfig.DEBUG) { LaunchedEffect(Unit) { diff --git a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/ImeDismissalSignal.kt b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/ImeDismissalSignal.kt new file mode 100644 index 00000000..765dc049 --- /dev/null +++ b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/ImeDismissalSignal.kt @@ -0,0 +1,16 @@ +package com.logseq.logseq.ui.extensions + +import androidx.compose.runtime.compositionLocalOf +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow + +internal class ImeDismissalSignal { + private val mutableStarted = MutableSharedFlow(extraBufferCapacity = 1) + val started: SharedFlow = mutableStarted + + fun notifyStarted() { + mutableStarted.tryEmit(Unit) + } +} + +internal val LocalImeDismissalSignal = compositionLocalOf { null } diff --git a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt index 54cab9b2..a64f469e 100644 --- a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt +++ b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt @@ -409,7 +409,6 @@ private fun NavigationBoundary(context: LuiExtensionContext) { // route; count=0 asks the core to settle instead of popping. val backDepth = context.int("back-depth") ?: depth val dismissesComposer = context.flag("composer-dismissal-enabled") - val occupies = context.flag("bottom-occupies-layout-space") BackHandler(enabled = !LocalSidebarBlocksMain.current && (backDepth > 0 || dismissesComposer)) { if (dismissesComposer) { context.emit("dismiss-composer") @@ -431,26 +430,20 @@ private fun NavigationBoundary(context: LuiExtensionContext) { if (childIds.size == 2) { // Second child is the bottom chrome. Hoist it out of the content // column like iOS's safeAreaInset/overlay so the composer's - // internal `grow` props can't starve the journal. Unbounded - // height keeps it at natural size without querying intrinsic - // measurements unsupported by subcomposed LUI layouts. - if (occupies) { - Column(Modifier.fillMaxSize()) { - Box(Modifier.weight(1f)) { NavigationContent(context, childIds[0], dismissesComposer) } - Box(Modifier.wrapContentHeight(unbounded = true)) { - context.child(childIds[1]) - } - } - } else { - Box(Modifier.fillMaxSize()) { - Column(Modifier.fillMaxSize()) { NavigationContent(context, childIds[0], dismissesComposer) } - Box( - Modifier - .align(Alignment.BottomCenter) - .wrapContentHeight(unbounded = true), - ) { - context.child(childIds[1]) - } + // internal `grow` props can't starve the journal. Keep the + // content viewport full-size even when the core reserves space + // for editor chrome; resizing it during IME dismissal moves the + // edited block while the keyboard is still animating. Unbounded + // height keeps the chrome at natural size without querying + // intrinsic measurements unsupported by subcomposed LUI layouts. + Box(Modifier.fillMaxSize()) { + Column(Modifier.fillMaxSize()) { NavigationContent(context, childIds[0], dismissesComposer) } + Box( + Modifier + .align(Alignment.BottomCenter) + .wrapContentHeight(unbounded = true), + ) { + context.child(childIds[1]) } } } else { diff --git a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/OutlinerEditor.kt b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/OutlinerEditor.kt index ce64722d..8d7b86ac 100644 --- a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/OutlinerEditor.kt +++ b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/OutlinerEditor.kt @@ -9,7 +9,7 @@ import androidx.annotation.RequiresApi import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.isImeVisible +import androidx.compose.foundation.layout.ime import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.text.KeyboardOptions @@ -53,6 +53,7 @@ import androidx.compose.foundation.text.contextmenu.builder.item import androidx.compose.foundation.text.contextmenu.modifier.appendTextContextMenuComponents import androidx.compose.foundation.text.contextmenu.modifier.filterTextContextMenuComponents import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.InterceptPlatformTextInput import androidx.compose.ui.platform.PlatformTextInputInterceptor import androidx.compose.ui.platform.PlatformTextInputMethodRequest @@ -328,20 +329,84 @@ internal fun OutlinerEditor( } // When the system dismisses the IME while this editor still owns focus, // end the editing session so the editor toolbar doesn't stay orphaned. - // Only fire after the IME was actually shown for this editor — with a - // hardware keyboard it is never visible. - val imeVisible = WindowInsets.isImeVisible - var imeWasVisible by remember { mutableStateOf(false) } - LaunchedEffect(focusEnabled, imeVisible) { - if (imeVisible) { - imeWasVisible = true - return@LaunchedEffect + // The root host signals IME hide animations as they start. Keep an inset + // watcher as a fallback for hosts/tests without that signal: visibility + // itself clears too late, while the animated inset starts shrinking in the + // first frames. Debounce input-connection flicker briefly, but keep the + // bottom chrome transition inside the IME animation. Only fire after the + // IME was actually shown for this editor — with a hardware keyboard it is + // never visible. + val ime = WindowInsets.ime + val density = LocalDensity.current + var dismissalEmitted by remember { mutableStateOf(false) } + var dismissalInProgress by remember { mutableStateOf(false) } + val dismissalSignal = LocalImeDismissalSignal.current + suspend fun emitImeDismissed(): Boolean { + // Structural handoffs buffer typing until the core acknowledges them. + // Wait for that reconciliation before flushing; publishing early can + // either drop those keystrokes or overwrite the pre-split block. + var lastBottom = ime.getBottom(density) + while (awaitingHandoff) { + delay(16) + val bottom = ime.getBottom(density) + // A rising inset means the close was canceled or the editor was + // reopened; don't emit a stale dismissal after the handoff. + if (bottom > lastBottom + 4) return false + lastBottom = bottom } - if (!focusEnabled || !imeWasVisible) return@LaunchedEffect - delay(300) - if (imeWasVisible) { - imeWasVisible = false - context.emit("ime-dismissed") + NativeEditorInput.flush() + context.emit("ime-dismissed") + return true + } + LaunchedEffect(focusEnabled, dismissalSignal) { + if (!focusEnabled || dismissalSignal == null) return@LaunchedEffect + dismissalSignal.started.collect { + if (!dismissalEmitted && !dismissalInProgress) { + dismissalInProgress = true + try { + dismissalEmitted = emitImeDismissed() + } finally { + dismissalInProgress = false + } + } + } + } + LaunchedEffect(focusEnabled, ime, density) { + var imeWasVisible = false + var shownBottom = 0 + var dismissalPending = false + snapshotFlow { ime.getBottom(density) }.collect { bottom -> + val shrinking = imeWasVisible && bottom < shownBottom - 4 + if (bottom > 0 && !shrinking) { + imeWasVisible = true + shownBottom = maxOf(shownBottom, bottom) + dismissalPending = false + dismissalEmitted = false + return@collect + } + if (!shrinking) return@collect + // A predictive Back preview can lower the IME briefly and then + // restore it. When the root animation signal is available, this + // watcher only cleans up a missed signal after the IME reaches + // zero; without that signal, the first shrink is the early path. + if (dismissalSignal != null && bottom > 4) return@collect + if (dismissalPending) return@collect + dismissalPending = true + launch { + delay(32) + if (dismissalPending) { + imeWasVisible = false + shownBottom = 0 + if (!dismissalEmitted && !dismissalInProgress) { + dismissalInProgress = true + try { + dismissalEmitted = emitImeDismissed() + } finally { + dismissalInProgress = false + } + } + } + } } } val inputOwner = remember { Any() }