feat(phase-1/m5.1): desktop Esc back stack + Ctrl+N/Ctrl+comma shortcuts

JVM PlatformBackHandler made real (PlatformBackStack + LocalPlatformBackStack);
window drains it on Esc via Window(onPreviewKeyEvent) — the editor rides the
shared seam unchanged (draft flush + discard prompt). Shell registers
drawer/settings Esc; preview-phase shortcuts. New --keys-smoke leg drives the
exact dispatch headlessly. Keypress capture pending-manual (KWin/Wayland drops
xdotool XTEST events to the XWayland window).

Verify: ./gradlew assembleDebug :composeApp:compileKotlinJvm :desktopApp:build
(SUCCESSFUL); :desktopApp:run --args="--data-dir /tmp/incido-keys --keys-smoke
--exit-after 12" (KEYS ALL-OK); --self-test/--settings-smoke/--editor-smoke
green; :core:data-jvm:smoke M1a PASS.
This commit is contained in:
Incido Developer committed 2026-10-06 22:37:37 +13:00
1 parent faab0cde98
commit 639f39d187
5 files changed
+300 -56

No files matched your search

@@ -7,9 +7,9 @@ import androidx.compose.runtime.Composable
* wrapper around `activity-compose` inside the editor file). * wrapper around `activity-compose` inside the editor file).
* *
* Android actual = `androidx.activity.compose.BackHandler`, verbatim * Android actual = `androidx.activity.compose.BackHandler`, verbatim
* behaviour. The JVM actual is a no-op: a desktop window has no system * behaviour. The JVM actual (M5.1) registers into the desktop window's
* back gesture, the editor's close button owns that path, and physical * [PlatformBackStack], which the window drains on Esc — so the editor's
* Esc/back-key routing is keyboard-shortcut polish on the M5 list. * discard-prompt chain works on both platforms through this one seam.
*/ */
@Composable @Composable
expect fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) expect fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit)
@@ -1,8 +1,66 @@
package org.incido.compose.util package org.incido.compose.util
import androidx.compose.runtime.Composable 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<Entry>()
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<PlatformBackStack?> { null }
/**
* M4.4c/M5.1 JVM actual — registers into [LocalPlatformBackStack]. No-op
* when the host provides no stack (headless tests, embeds).
*/
@Composable @Composable
actual fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) = Unit 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) }
}
}
}
@@ -1,5 +1,7 @@
package org.incido.desktop 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.DrawerValue
import androidx.compose.material3.ModalNavigationDrawer import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.rememberDrawerState import androidx.compose.material3.rememberDrawerState
@@ -11,12 +13,21 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue 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.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.incido.compose.di.LocalAppGraph import org.incido.compose.di.LocalAppGraph
import org.incido.compose.ui.drawer.IncidoDrawer import org.incido.compose.ui.drawer.IncidoDrawer
import org.incido.compose.ui.editor.NoteEditorScreen import org.incido.compose.ui.editor.NoteEditorScreen
import org.incido.compose.ui.notelist.NoteListScreen 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 import org.incido.domain.model.NoteFilter
/** /**
@@ -31,8 +42,10 @@ import org.incido.domain.model.NoteFilter
* Platform differences handled, not faked (§6.5): * Platform differences handled, not faked (§6.5):
* - beginExternalActivity is null on desktop (no auto-lock to suppress), * - beginExternalActivity is null on desktop (no auto-lock to suppress),
* - the editor's intake seam hides camera/voice on the JVM, * - the editor's intake seam hides camera/voice on the JVM,
* - PlatformBackHandler is a no-op here — Esc routing is M5 polish, * - M5.1: Esc routes through the window back stack — the editor's own
* the editor closes via its own top-bar arrow like the phone's up button. * 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 @Composable
fun DesktopShell( fun DesktopShell(
@@ -41,13 +54,32 @@ fun DesktopShell(
onShowSettings: (Boolean) -> Unit, onShowSettings: (Boolean) -> Unit,
/** M4.5b scripted leg: opens the editor headlessly after first frame. */ /** M4.5b scripted leg: opens the editor headlessly after first frame. */
editorSmoke: Boolean = false, 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 scope = rememberCoroutineScope()
val drawerState = rememberDrawerState(DrawerValue.Closed) val drawerState = rememberDrawerState(DrawerValue.Closed)
val backStack = LocalPlatformBackStack.current
var filterMode by remember { mutableStateOf<NoteFilter>(NoteFilter.Home) } var filterMode by remember { mutableStateOf<NoteFilter>(NoteFilter.Home) }
var editorOpen by remember { mutableStateOf(false) } var editorOpen by remember { mutableStateOf(false) }
// null id = "new note" (exactly what onOpenEditor(null) means on Android). // null id = "new note" (exactly what onOpenEditor(null) means on Android).
var editorNoteId by remember { mutableStateOf<Long?>(null) } var editorNoteId by remember { mutableStateOf<Long?>(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) { if (editorSmoke) {
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
@@ -59,39 +91,107 @@ fun DesktopShell(
} }
} }
CompositionLocalProvider(LocalAppGraph provides app) { if (keysSmoke) {
when { LaunchedEffect(Unit) {
showSettings -> DesktopSettingsScreen(app = app, onBack = { onShowSettings(false) }) delay(2200)
editorOpen -> NoteEditorScreen( var pass = backStack != null
noteId = editorNoteId, // a) Editor owns Esc through the commonMain seam: a dispatch on
onClose = { editorOpen = false }, // a clean new note must flush the draft and close — exactly
repository = app.noteRepository, // 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( // b) Nothing enabled must NOT consume — Esc keeps reaching
drawerState = drawerState, // popups/text fields through the preview chain.
drawerContent = { val consumedEmpty = backStack?.dispatchBack() == true
IncidoDrawer( pass = pass && !consumedEmpty
currentFilter = filterMode, println(
onSelectFilter = { picked -> "KEYS nothing-enabled consumed=$consumedEmpty " +
filterMode = picked (if (!consumedEmpty) "PASS" else "FAIL"),
scope.launch { drawerState.close() } )
}, // c) Settings closes on Esc via the shell registration — the
onOpenSettings = { // same onShowSettings(false) the window's Esc callback makes.
scope.launch { drawerState.close() } 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) onShowSettings(true)
}, true
) }
else -> false
}
}, },
) { ) {
NoteListScreen( when {
filterMode = filterMode, showSettings -> DesktopSettingsScreen(app = app, onBack = { onShowSettings(false) })
onOpenDrawer = { scope.launch { drawerState.open() } }, editorOpen -> NoteEditorScreen(
onOpenEditor = { id -> noteId = editorNoteId,
editorNoteId = id onClose = { editorOpen = false },
editorOpen = true
},
repository = app.noteRepository, 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,
)
}
} }
} }
} }
@@ -33,6 +33,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.darkColorScheme import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
@@ -42,6 +43,10 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier 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.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window import androidx.compose.ui.window.Window
@@ -60,6 +65,8 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.incido.compose.data.backup.BackupBridge import org.incido.compose.data.backup.BackupBridge
import org.incido.compose.ui.theme.IncidoTheme 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.EncryptedBackupCodec
import org.incido.compose.data.backup.EncryptedKeyCodec import org.incido.compose.data.backup.EncryptedKeyCodec
import org.incido.data.crypto.AesGcmFieldCipher import org.incido.data.crypto.AesGcmFieldCipher
@@ -107,6 +114,10 @@ fun main(args: Array<String>) {
// so the shared sections (M4.4d) render on the JVM before auto-exit. // so the shared sections (M4.4d) render on the JVM before auto-exit.
val settingsSmoke = "--settings-smoke" in args val settingsSmoke = "--settings-smoke" in args
val editorSmoke = "--editor-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 cryptoSelfTest = "--crypto-self-test" in args
val cryptoReopen = "--crypto-reopen" in args val cryptoReopen = "--crypto-reopen" in args
val cryptoPassphrase = args.argAfter("--crypto-passphrase") ?: "selftest-passphrase" val cryptoPassphrase = args.argAfter("--crypto-passphrase") ?: "selftest-passphrase"
@@ -123,6 +134,12 @@ fun main(args: Array<String>) {
val app = DesktopContainer(dataDir) 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 // M3: window geometry is desktop-only state, kept in its OWN store file
// (incido_window.preferences_pb) — settings stay the phone-shared schema. // (incido_window.preferences_pb) — settings stay the phone-shared schema.
// Position is deliberately NOT stored: KWin owns placement on this stack. // Position is deliberately NOT stored: KWin owns placement on this stack.
@@ -142,27 +159,43 @@ fun main(args: Array<String>) {
onCloseRequest = ::exitApplication, onCloseRequest = ::exitApplication,
state = windowState, state = windowState,
title = "Incido", 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( CompositionLocalProvider(LocalPlatformBackStack provides backStack) {
app = app, NotesWindow(
repository = app.noteRepository, app = app,
windowStore = windowStore, repository = app.noteRepository,
windowState = windowState, windowStore = windowStore,
exitAfterSeconds = exitAfterSeconds, windowState = windowState,
selfTest = selfTest, exitAfterSeconds = exitAfterSeconds,
settingsSmoke = settingsSmoke, selfTest = selfTest,
editorSmoke = editorSmoke, settingsSmoke = settingsSmoke,
backupImport = backupImport, editorSmoke = editorSmoke,
backupExport = backupExport, keysSmoke = keysSmoke,
backupExportEnc = backupExportEnc, backupImport = backupImport,
backupPassphrase = backupPassphrase, backupExport = backupExport,
cryptoSelfTest = cryptoSelfTest, backupExportEnc = backupExportEnc,
cryptoReopen = cryptoReopen, backupPassphrase = backupPassphrase,
cryptoPassphrase = cryptoPassphrase, cryptoSelfTest = cryptoSelfTest,
keyExport = keyExport, cryptoReopen = cryptoReopen,
keyImport = keyImport, cryptoPassphrase = cryptoPassphrase,
onExit = ::exitApplication, keyExport = keyExport,
) keyImport = keyImport,
onExit = ::exitApplication,
)
}
} }
} }
@@ -180,6 +213,7 @@ private fun NotesWindow(
selfTest: Boolean, selfTest: Boolean,
settingsSmoke: Boolean, settingsSmoke: Boolean,
editorSmoke: Boolean, editorSmoke: Boolean,
keysSmoke: Boolean,
backupImport: String?, backupImport: String?,
backupExport: String?, backupExport: String?,
backupExportEnc: String?, backupExportEnc: String?,
@@ -213,6 +247,7 @@ private fun NotesWindow(
showSettings = showSettings, showSettings = showSettings,
onShowSettings = { showSettings = it }, onShowSettings = { showSettings = it },
editorSmoke = editorSmoke, editorSmoke = editorSmoke,
keysSmoke = keysSmoke,
) )
} }
+51
View File
@@ -802,6 +802,57 @@ M4.4/M4.5 suite (per commit; final run this entry):
window/menu integration, packaging (`createDistributable` re-verify + window/menu integration, packaging (`createDistributable` re-verify +
the M4.3d artifact notes), and the §9 perf pass on the real screens. 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.