diff --git a/composeApp/src/commonMain/kotlin/org/incido/compose/util/PlatformBack.kt b/composeApp/src/commonMain/kotlin/org/incido/compose/util/PlatformBack.kt index 56f86e7..b87257b 100644 --- a/composeApp/src/commonMain/kotlin/org/incido/compose/util/PlatformBack.kt +++ b/composeApp/src/commonMain/kotlin/org/incido/compose/util/PlatformBack.kt @@ -7,9 +7,9 @@ import androidx.compose.runtime.Composable * wrapper around `activity-compose` inside the editor file). * * Android actual = `androidx.activity.compose.BackHandler`, verbatim - * behaviour. The JVM actual is a no-op: a desktop window has no system - * back gesture, the editor's close button owns that path, and physical - * Esc/back-key routing is keyboard-shortcut polish on the M5 list. + * behaviour. The JVM actual (M5.1) registers into the desktop window's + * [PlatformBackStack], which the window drains on Esc — so the editor's + * discard-prompt chain works on both platforms through this one seam. */ @Composable expect fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) \ No newline at end of file diff --git a/composeApp/src/jvmMain/kotlin/org/incido/compose/util/PlatformBackJvm.kt b/composeApp/src/jvmMain/kotlin/org/incido/compose/util/PlatformBackJvm.kt index 950ed74..5212051 100644 --- a/composeApp/src/jvmMain/kotlin/org/incido/compose/util/PlatformBackJvm.kt +++ b/composeApp/src/jvmMain/kotlin/org/incido/compose/util/PlatformBackJvm.kt @@ -1,8 +1,66 @@ package org.incido.compose.util import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.compositionLocalOf +import androidx.compose.runtime.remember -/** M4.4c JVM actual — no system back gesture on a desktop window; the - * editor's close button owns that path (Esc handling is M5 polish). */ +/** + * M5.1 — the M4.4c no-op made real: a desktop back stack. + * + * A desktop window has no system back gesture, so the desktop window + * translates Esc (`Window(onPreviewKeyEvent)`) into [dispatchBack] on the + * [PlatformBackStack] provided through [LocalPlatformBackStack]. Every live + * `PlatformBackHandler` becomes an entry here, so the editor's existing + * tryClose/discard-prompt chain behaves exactly like Android's BackHandler + * stack: the topmost enabled entry wins. + * + * Entries keep their stack position across recompositions: the registration + * object is remembered and its callback slot refreshed per composition. + * Re-registering on every `onBack` lambda change would let a lower handler's + * recomposition jump above an editor or dialog stacked on top of it. + */ +class PlatformBackStack { + internal class Entry(var enabled: Boolean = false, var onBack: () -> Unit = {}) + + private val entries = ArrayDeque() + + internal fun push(entry: Entry) { + entries.addLast(entry) + } + + internal fun remove(entry: Entry) { + entries.remove(entry) + } + + /** + * Runs the topmost enabled handler. Returns true when consumed — the + * window uses that to decide whether Esc falls through to content. + */ + fun dispatchBack(): Boolean { + val top = entries.lastOrNull { it.enabled } ?: return false + top.onBack() + return true + } +} + +/** Provided by the desktop host window (see `:desktopApp` Main.kt, M5.1). */ +val LocalPlatformBackStack = compositionLocalOf { null } + +/** + * M4.4c/M5.1 JVM actual — registers into [LocalPlatformBackStack]. No-op + * when the host provides no stack (headless tests, embeds). + */ @Composable -actual fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) = Unit \ No newline at end of file +actual fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) { + val stack = LocalPlatformBackStack.current + val entry = remember { PlatformBackStack.Entry() } + entry.enabled = enabled + entry.onBack = onBack + if (stack != null) { + DisposableEffect(stack) { + stack.push(entry) + onDispose { stack.remove(entry) } + } + } +} \ No newline at end of file diff --git a/desktopApp/src/main/kotlin/org/incido/desktop/DesktopShell.kt b/desktopApp/src/main/kotlin/org/incido/desktop/DesktopShell.kt index ad69e7b..4f3eabc 100644 --- a/desktopApp/src/main/kotlin/org/incido/desktop/DesktopShell.kt +++ b/desktopApp/src/main/kotlin/org/incido/desktop/DesktopShell.kt @@ -1,5 +1,7 @@ package org.incido.desktop +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.DrawerValue import androidx.compose.material3.ModalNavigationDrawer import androidx.compose.material3.rememberDrawerState @@ -11,12 +13,21 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEventType +import androidx.compose.ui.input.key.isCtrlPressed +import androidx.compose.ui.input.key.key +import androidx.compose.ui.input.key.onPreviewKeyEvent +import androidx.compose.ui.input.key.type import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.incido.compose.di.LocalAppGraph import org.incido.compose.ui.drawer.IncidoDrawer import org.incido.compose.ui.editor.NoteEditorScreen import org.incido.compose.ui.notelist.NoteListScreen +import org.incido.compose.util.LocalPlatformBackStack +import org.incido.compose.util.PlatformBackHandler import org.incido.domain.model.NoteFilter /** @@ -31,8 +42,10 @@ import org.incido.domain.model.NoteFilter * Platform differences handled, not faked (§6.5): * - beginExternalActivity is null on desktop (no auto-lock to suppress), * - the editor's intake seam hides camera/voice on the JVM, - * - PlatformBackHandler is a no-op here — Esc routing is M5 polish, - * the editor closes via its own top-bar arrow like the phone's up button. + * - M5.1: Esc routes through the window back stack — the editor's own + * commonMain PlatformBackHandler gets its discard-prompt tryClose, the + * shell registers drawer + settings below it. + * - M5.1 shortcuts: Ctrl+N new note, Ctrl+comma settings. */ @Composable fun DesktopShell( @@ -41,13 +54,32 @@ fun DesktopShell( onShowSettings: (Boolean) -> Unit, /** M4.5b scripted leg: opens the editor headlessly after first frame. */ editorSmoke: Boolean = false, + /** M5.1 scripted leg: drives backStack.dispatchBack() — the exact path + * Window(onPreviewKeyEvent) runs on Esc. */ + keysSmoke: Boolean = false, ) { val scope = rememberCoroutineScope() val drawerState = rememberDrawerState(DrawerValue.Closed) + val backStack = LocalPlatformBackStack.current var filterMode by remember { mutableStateOf(NoteFilter.Home) } var editorOpen by remember { mutableStateOf(false) } // null id = "new note" (exactly what onOpenEditor(null) means on Android). var editorNoteId by remember { mutableStateOf(null) } + // M5.1 KEYS leg mirror of the parent's showSettings — it flips only + // through the same callback the back handler calls, so asserting on it + // asserts on the real routing. Rendering stays on the parent's state. + var settingsOpen by remember { mutableStateOf(false) } + // Mirror follows the parent so every close path (back arrow, Esc, + // shortcut toggles) keeps the KEYS-leg assertion honest. + LaunchedEffect(showSettings) { settingsOpen = showSettings } + + // M5.1 — Esc registrations below the editor's own (the common seam + // registers while the editor is composed; topmost enabled entry wins). + PlatformBackHandler(enabled = showSettings, onBack = { onShowSettings(false) }) + PlatformBackHandler( + enabled = drawerState.isOpen, + onBack = { scope.launch { drawerState.close() } }, + ) if (editorSmoke) { LaunchedEffect(Unit) { @@ -59,39 +91,107 @@ fun DesktopShell( } } - CompositionLocalProvider(LocalAppGraph provides app) { - when { - showSettings -> DesktopSettingsScreen(app = app, onBack = { onShowSettings(false) }) - editorOpen -> NoteEditorScreen( - noteId = editorNoteId, - onClose = { editorOpen = false }, - repository = app.noteRepository, + if (keysSmoke) { + LaunchedEffect(Unit) { + delay(2200) + var pass = backStack != null + // a) Editor owns Esc through the commonMain seam: a dispatch on + // a clean new note must flush the draft and close — exactly + // the Android BackHandler-chain behaviour. + editorNoteId = null + editorOpen = true + delay(1200) // let the editor register its handler + val consumedEditor = backStack?.dispatchBack() == true + delay(400) // state flip + recomposition + val editorClosed = !editorOpen + pass = pass && consumedEditor && editorClosed + println( + "KEYS editor Esc consumed=$consumedEditor closed=$editorClosed " + + (if (consumedEditor && editorClosed) "PASS" else "FAIL"), ) - else -> ModalNavigationDrawer( - drawerState = drawerState, - drawerContent = { - IncidoDrawer( - currentFilter = filterMode, - onSelectFilter = { picked -> - filterMode = picked - scope.launch { drawerState.close() } - }, - onOpenSettings = { - scope.launch { drawerState.close() } + // b) Nothing enabled must NOT consume — Esc keeps reaching + // popups/text fields through the preview chain. + val consumedEmpty = backStack?.dispatchBack() == true + pass = pass && !consumedEmpty + println( + "KEYS nothing-enabled consumed=$consumedEmpty " + + (if (!consumedEmpty) "PASS" else "FAIL"), + ) + // c) Settings closes on Esc via the shell registration — the + // same onShowSettings(false) the window's Esc callback makes. + settingsOpen = true + onShowSettings(true) + delay(600) + val consumedSettings = backStack?.dispatchBack() == true + delay(400) + val settingsGone = !settingsOpen + pass = pass && consumedSettings && settingsGone + println( + "KEYS settings Esc consumed=$consumedSettings closed=$settingsGone " + + (if (consumedSettings && settingsGone) "PASS" else "FAIL"), + ) + println("KEYS ${if (pass) "ALL-OK" else "PROBLEMS (see FAIL lines above)"}") + } + } + + CompositionLocalProvider(LocalAppGraph provides app) { + // M5.1 shortcuts. Preview-phase, so they win over focused text + // fields — Ctrl+N inside the editor means "new note", not "type n". + Box( + Modifier + .fillMaxSize() + .onPreviewKeyEvent { event -> + if (event.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false + when { + event.isCtrlPressed && event.key == Key.N && + !editorOpen && !showSettings -> { + println("KEYS shortcut ctrl+N -> new note") + editorNoteId = null + editorOpen = true + true + } + event.isCtrlPressed && event.key == Key.Comma && !showSettings -> { + println("KEYS shortcut ctrl+comma -> settings") onShowSettings(true) - }, - ) + true + } + else -> false + } }, - ) { - NoteListScreen( - filterMode = filterMode, - onOpenDrawer = { scope.launch { drawerState.open() } }, - onOpenEditor = { id -> - editorNoteId = id - editorOpen = true - }, + ) { + when { + showSettings -> DesktopSettingsScreen(app = app, onBack = { onShowSettings(false) }) + editorOpen -> NoteEditorScreen( + noteId = editorNoteId, + onClose = { editorOpen = false }, repository = app.noteRepository, ) + else -> ModalNavigationDrawer( + drawerState = drawerState, + drawerContent = { + IncidoDrawer( + currentFilter = filterMode, + onSelectFilter = { picked -> + filterMode = picked + scope.launch { drawerState.close() } + }, + onOpenSettings = { + scope.launch { drawerState.close() } + onShowSettings(true) + }, + ) + }, + ) { + NoteListScreen( + filterMode = filterMode, + onOpenDrawer = { scope.launch { drawerState.open() } }, + onOpenEditor = { id -> + editorNoteId = id + editorOpen = true + }, + repository = app.noteRepository, + ) + } } } } diff --git a/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt b/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt index bd1f3bf..23ee810 100644 --- a/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt +++ b/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt @@ -33,6 +33,7 @@ import androidx.compose.material3.Text import androidx.compose.material3.darkColorScheme import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue @@ -42,6 +43,10 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEventType +import androidx.compose.ui.input.key.key +import androidx.compose.ui.input.key.type import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Window @@ -60,6 +65,8 @@ import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.incido.compose.data.backup.BackupBridge import org.incido.compose.ui.theme.IncidoTheme +import org.incido.compose.util.LocalPlatformBackStack +import org.incido.compose.util.PlatformBackStack import org.incido.compose.data.backup.EncryptedBackupCodec import org.incido.compose.data.backup.EncryptedKeyCodec import org.incido.data.crypto.AesGcmFieldCipher @@ -107,6 +114,10 @@ fun main(args: Array) { // so the shared sections (M4.4d) render on the JVM before auto-exit. val settingsSmoke = "--settings-smoke" in args val editorSmoke = "--editor-smoke" in args + // M5.1 scripted leg: drives the exact Esc dispatch path the window's + // onPreviewKeyEvent uses (backStack.dispatchBack) against the real + // editor and settings registrations. + val keysSmoke = "--keys-smoke" in args val cryptoSelfTest = "--crypto-self-test" in args val cryptoReopen = "--crypto-reopen" in args val cryptoPassphrase = args.argAfter("--crypto-passphrase") ?: "selftest-passphrase" @@ -123,6 +134,12 @@ fun main(args: Array) { val app = DesktopContainer(dataDir) + // M5.1: the desktop back stack. PlatformBackHandler (composeApp jvm + // actual) registers into it; Esc below drains it, topmost enabled + // entry first — the editor's discard-prompt chain included, since its + // BackHandler(enabled=true){tryClose()} rides the same seam. + val backStack = PlatformBackStack() + // M3: window geometry is desktop-only state, kept in its OWN store file // (incido_window.preferences_pb) — settings stay the phone-shared schema. // Position is deliberately NOT stored: KWin owns placement on this stack. @@ -142,27 +159,43 @@ fun main(args: Array) { onCloseRequest = ::exitApplication, state = windowState, title = "Incido", + // M5.1: Esc = system back. Consumed only when a handler is + // live; otherwise it falls through so popups keep their own + // Esc handling. This is the exact dispatcher --keys-smoke + // drives headlessly via backStack.dispatchBack(). + onPreviewKeyEvent = { event -> + if (event.type == KeyEventType.KeyDown && event.key == Key.Escape) { + val consumed = backStack.dispatchBack() + println("KEYS esc(captured) consumed=$consumed") + consumed + } else { + false + } + }, ) { - NotesWindow( - app = app, - repository = app.noteRepository, - windowStore = windowStore, - windowState = windowState, - exitAfterSeconds = exitAfterSeconds, - selfTest = selfTest, - settingsSmoke = settingsSmoke, - editorSmoke = editorSmoke, - backupImport = backupImport, - backupExport = backupExport, - backupExportEnc = backupExportEnc, - backupPassphrase = backupPassphrase, - cryptoSelfTest = cryptoSelfTest, - cryptoReopen = cryptoReopen, - cryptoPassphrase = cryptoPassphrase, - keyExport = keyExport, - keyImport = keyImport, - onExit = ::exitApplication, - ) + CompositionLocalProvider(LocalPlatformBackStack provides backStack) { + NotesWindow( + app = app, + repository = app.noteRepository, + windowStore = windowStore, + windowState = windowState, + exitAfterSeconds = exitAfterSeconds, + selfTest = selfTest, + settingsSmoke = settingsSmoke, + editorSmoke = editorSmoke, + keysSmoke = keysSmoke, + backupImport = backupImport, + backupExport = backupExport, + backupExportEnc = backupExportEnc, + backupPassphrase = backupPassphrase, + cryptoSelfTest = cryptoSelfTest, + cryptoReopen = cryptoReopen, + cryptoPassphrase = cryptoPassphrase, + keyExport = keyExport, + keyImport = keyImport, + onExit = ::exitApplication, + ) + } } } @@ -180,6 +213,7 @@ private fun NotesWindow( selfTest: Boolean, settingsSmoke: Boolean, editorSmoke: Boolean, + keysSmoke: Boolean, backupImport: String?, backupExport: String?, backupExportEnc: String?, @@ -213,6 +247,7 @@ private fun NotesWindow( showSettings = showSettings, onShowSettings = { showSettings = it }, editorSmoke = editorSmoke, + keysSmoke = keysSmoke, ) } diff --git a/tools/DESKTOP_FEASIBILITY.md b/tools/DESKTOP_FEASIBILITY.md index 5f2eda7..5b4ba1d 100644 --- a/tools/DESKTOP_FEASIBILITY.md +++ b/tools/DESKTOP_FEASIBILITY.md @@ -802,6 +802,57 @@ M4.4/M4.5 suite (per commit; final run this entry): window/menu integration, packaging (`createDistributable` re-verify + the M4.3d artifact notes), and the §9 perf pass on the real screens. +**§1 gate re-run this session: GREEN** (fresh dirs; the two crypto legs +as SEPARATE processes; spike probes correctly absent — deleted in +M4.5c): + +``` +:core:data-jvm:smoke RESULT: M1a PASS, exit 0 +:desktopApp:run … --self-test SELFTEST id=1 → RENDER, exit 0 +assembleDebug BUILD SUCCESSFUL, exit 0 +crypto leg A (own process) leg-A ALL-OK (9×PASS), exit 0 +crypto leg B (own process) LEG-B PASS, leg-B ALL-OK, exit 0 +``` + +**M5.1 — Esc routing + keyboard shortcuts: DONE.** + +- `PlatformBackHandler`'s JVM actual is no longer a no-op: + `PlatformBackStack` + `LocalPlatformBackStack` (composeApp jvmMain). + Entries are position-stable (remembered object, callback slot + refreshed per composition) so a lower screen's recomposition can never + jump above the editor. `dispatchBack()` runs the topmost ENABLED + entry, false = unconsumed so Esc still reaches popups. +- `Main.kt` provides the stack and drains it from + `Window(onPreviewKeyEvent)` on Esc (logs `esc(captured)` for manual + verification). **The editor changed zero lines** — its commonMain + `BackHandler { tryClose() }` rides the seam, so desktop Esc gets the + draft flush + "Discard changes?" prompt exactly like Android. +- `DesktopShell` registers Esc for drawer + settings; shortcuts **Ctrl+N** + (new note) and **Ctrl+comma** (settings) go on `onPreviewKeyEvent` — + preview phase wins over focused text fields. +- New `--keys-smoke` leg drives the window's exact dispatch + (`dispatchBack`) headlessly: clean-editor Esc closes, nothing-enabled + does NOT consume, settings Esc closes. One fix en route: the KEYS-leg + settings mirror needed `LaunchedEffect(showSettings)` sync or leg (c) + read a stale mirror. +- **Pending-manual:** real keypresses could not be verified headless — + KWin under Wayland does not deliver xdotool XTEST events to the + XWayland window (focus probes confirmed the app HAS focus; zero + events arrived). One real Esc + Ctrl+N at the desk is the outstanding + check; `esc(captured)` logs on the console when it works. + +``` +M5.1 suite: + assembleDebug :composeApp:compileKotlinJvm :desktopApp:build BUILD SUCCESSFUL + :desktopApp:run … --keys-smoke KEYS ALL-OK (editor/no-consume/settings 3×PASS), exit 0 + :desktopApp:run … --self-test SELFTEST id=1, exit 0 + :desktopApp:run … --settings-smoke SETTINGS shown, exit 0 + :desktopApp:run … --editor-smoke EDITOR shown, exit 0 + :core:data-jvm:smoke RESULT: M1a PASS +``` + +**Next up: M5.2** — tray + single-instance + first-run onboarding. +