diff --git a/desktopApp/build.gradle.kts b/desktopApp/build.gradle.kts new file mode 100644 index 0000000..0a3096a --- /dev/null +++ b/desktopApp/build.gradle.kts @@ -0,0 +1,50 @@ +// desktopApp — Incido for the desktop (Development Plan §6, Phase 1, M1b). +// +// A Compose Multiplatform window over :core:data-jvm, i.e. the SAME data layer +// the Android app compiles (shared sources, sibling ObjectBox codegen). The +// desktop build owns its own library in the XDG data dir; the encrypted backup +// file is the only bridge to the phone (M2) because ObjectBox is single-writer. + +import org.jetbrains.compose.desktop.application.dsl.TargetFormat + +plugins { + alias(libs.plugins.kotlinJvm) + alias(libs.plugins.compose) + alias(libs.plugins.kotlinCompose) + // SPIKE FINDING: the `application` plugin is NOT applied explicitly — + // org.jetbrains.compose applies it itself, and applying it first collides + // on task registration. `compose.desktop { application { } }` gives `run`. +} + +kotlin { + jvmToolchain(17) +} + +dependencies { + implementation(compose.desktop.currentOs) + implementation(compose.ui) + implementation(compose.foundation) + implementation(compose.material3) + implementation(compose.materialIconsExtended) + implementation(libs.kotlinx.coroutines.core) + implementation(libs.kotlinx.coroutines.swing) + + implementation(project(":core:domain")) + implementation(project(":core:data-jvm")) +} + +// Packaging works (proven in Phase 0), but per-OS installers and icons are +// milestone M5. Version is pinned numerically here because jpackage only +// accepts major.minor.patch; M5 wires it to the app's version scheme. +compose.desktop { + application { + mainClass = "org.incido.desktop.MainKt" + nativeDistributions { + targetFormats(TargetFormat.Deb, TargetFormat.Rpm, TargetFormat.AppImage) + packageName = "incido" + packageVersion = "0.4.2" + vendor = "Incido" + description = "Incido — offline-first notes (desktop)." + } + } +} \ No newline at end of file diff --git a/desktopApp/libobjectbox-jni-linux-x64.so b/desktopApp/libobjectbox-jni-linux-x64.so new file mode 100644 index 0000000..4f28e4d Binary files /dev/null and b/desktopApp/libobjectbox-jni-linux-x64.so differ diff --git a/desktopApp/src/main/kotlin/org/incido/desktop/DesktopContainer.kt b/desktopApp/src/main/kotlin/org/incido/desktop/DesktopContainer.kt new file mode 100644 index 0000000..fcdde79 --- /dev/null +++ b/desktopApp/src/main/kotlin/org/incido/desktop/DesktopContainer.kt @@ -0,0 +1,51 @@ +package org.incido.desktop + +import java.io.File +import org.incido.data.ImageOrientationNormalizer +import org.incido.data.ObjectBoxStore +import org.incido.data.crypto.CipherHolder +import org.incido.data.repository.NoteRepositoryImpl +import org.incido.domain.repository.NoteRepository + +/** + * Hand-rolled DI container for the desktop build — the JVM counterpart of + * the Android app's `AppContainer` (Development Plan §6, Phase 1, M1b). + * + * Deliberately mirrors Android's construction order: open the store, then + * build the repository over a shared [CipherHolder] so installing or removing + * at-rest encryption later is a one-line change here instead of a rebuild of + * the graph. + * + * Two Android behaviours are intentionally absent, both planned work: + * - at-rest encryption (milestone M3 — needs a JVM KeyProvider; the + * recovery-key envelope is already proven portable), and + * - EXIF rotation (milestone M4 — needs a JVM image decoder, so inbound + * images are stored exactly as received for now). + */ +class DesktopContainer(dataDir: File) { + + val cipherHolder = CipherHolder() + + val boxStore = ObjectBoxStore.init(dataDir) + + val repository: NoteRepository = NoteRepositoryImpl( + boxStore = boxStore, + cipherHolder = cipherHolder, + orientationNormalizer = ImageOrientationNormalizer.NoOp, + ) + + fun shutdown() = ObjectBoxStore.reset() +} + +/** + * Where the desktop library lives: `$XDG_DATA_HOME/incido`, falling back to + * `~/.local/share/incido` (Development Plan §6 — a *separate* library per + * machine; the backup file is the bridge to the phone, never the DB itself, + * because ObjectBox is single-writer). + */ +fun defaultDataDir(): File { + val sep = File.separator + val base = System.getenv("XDG_DATA_HOME")?.takeIf { it.isNotBlank() } + ?: System.getProperty("user.home") + sep + ".local" + sep + "share" + return File(base + sep + "incido").also { it.mkdirs() } +} \ No newline at end of file diff --git a/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt b/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt new file mode 100644 index 0000000..2e70e01 --- /dev/null +++ b/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt @@ -0,0 +1,302 @@ +package org.incido.desktop + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Add +import androidx.compose.material.icons.filled.Star +import androidx.compose.material.icons.outlined.StarBorder +import androidx.compose.material3.CenterAlignedTopAppBar +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Window +import androidx.compose.ui.window.application +import androidx.compose.ui.window.rememberWindowState +import java.io.File +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import org.incido.domain.model.Note +import org.incido.domain.repository.NoteRepository + +/** + * Incido desktop, Phase 1 milestone M1b. + * + * The window is intentionally plain: Material 3 defaults, two panes, no + * filters yet. The point of M1b is that the *real* repository drives a real + * desktop UI; the app's own theme and screens arrive with the M4 `composeApp` + * conversion, at which point this file shrinks to just the window. + * + * What is already real: notes are read through `observeAll()` and written + * through `saveNoteWithHashtags`, so they land in the same ObjectBox schema + * the Android app writes — in its own library per machine (see + * [DesktopContainer]), bridged to the phone only by the backup file. + * + * Args: `--data-dir ` opens another library (e.g. a database pulled off + * a phone, to prove schema compatibility); `--exit-after ` auto-closes + * for scripted smoke runs. + */ +fun main(args: Array) { + val dataDir = args.argAfter("--data-dir") + ?.let { File(it).also { dir -> dir.mkdirs() } } + ?: defaultDataDir() + val exitAfterSeconds = args.argAfter("--exit-after")?.toLongOrNull() + // Headless end-to-end check: write a note from inside the composition and + // watch it come back through observeAll(). Used by the M1b smoke run. + val selfTest = "--self-test" in args + + println("=== Incido (desktop) — Phase 1 M1b ===") + println("JVM : ${System.getProperty("java.vendor")} ${System.getProperty("java.version")}") + println("library : $dataDir") + + val app = DesktopContainer(dataDir) + + application { + Window( + onCloseRequest = ::exitApplication, + state = rememberWindowState(width = 1100.dp, height = 760.dp), + title = "Incido", + ) { + NotesWindow( + repository = app.repository, + exitAfterSeconds = exitAfterSeconds, + selfTest = selfTest, + onExit = ::exitApplication, + ) + } + } + + // application{} returns once every window is closed. + app.shutdown() +} +@OptIn(ExperimentalMaterial3Api::class) +@Composable +private fun NotesWindow( + repository: NoteRepository, + exitAfterSeconds: Long?, + selfTest: Boolean, + onExit: () -> Unit, +) { + val notes by remember(repository) { repository.observeAll() } + .collectAsState(initial = emptyList()) + var selectedId by remember { mutableStateOf(null) } + val scope = rememberCoroutineScope() + + // Keep a valid selection as the library changes. + val selected = notes.firstOrNull { it.id == selectedId } ?: notes.firstOrNull() + + MaterialTheme { + Scaffold( + topBar = { + CenterAlignedTopAppBar( + title = { Text("Incido") }, + actions = { + IconButton( + onClick = { + scope.launch { + val now = System.currentTimeMillis() + val saved = repository.saveNoteWithHashtags( + note = Note( + id = 0L, + content = "", + createdAt = now, + updatedAt = now, + ), + hashtagsToAdd = emptySet(), + hashtagsToRemove = emptySet(), + ) + selectedId = saved.id + } + }, + ) { Icon(Icons.Filled.Add, contentDescription = "New note") } + }, + ) + }, + ) { padding -> + Row(Modifier.fillMaxSize().padding(padding)) { + Surface( + modifier = Modifier.width(340.dp).fillMaxHeight(), + tonalElevation = 2.dp, + ) { + LazyColumn(modifier = Modifier.fillMaxSize()) { + items(notes, key = { it.id }) { note -> + NoteRow( + note = note, + selected = note.id == selected?.id, + onClick = { selectedId = note.id }, + onToggleFavorite = { + scope.launch { + repository.setFavorite(note.id, !note.isFavorite) + } + }, + ) + HorizontalDivider() + } + if (notes.isEmpty()) { + item { + Text( + "No notes yet — press + to write one.", + modifier = Modifier.padding(24.dp), + style = MaterialTheme.typography.bodyMedium, + ) + } + } + } + } + Editor(repository = repository, note = selected) + } + } + } + + LaunchedEffect(notes.size) { + // Logged on every change: proves the ObjectBox subscription actually + // delivers to the composition (the first frame is always the empty + // initial value of collectAsState). + println("RENDER notes=${notes.size} selected=${selected?.id}") + } + if (selfTest) { + LaunchedEffect(Unit) { + // Exactly the calls the "+" button and the star make, driven + // headlessly so the write path is verified without a mouse. + delay(1200) + val now = System.currentTimeMillis() + val saved = repository.saveNoteWithHashtags( + note = Note( + id = 0L, + content = "Written by the desktop app\nself-test line", + createdAt = now, + updatedAt = now, + ), + hashtagsToAdd = emptySet(), + hashtagsToRemove = emptySet(), + ) + repository.setFavorite(saved.id, true) + println("SELFTEST wrote note id=${saved.id}") + } + } + if (exitAfterSeconds != null) { + LaunchedEffect(Unit) { + delay(exitAfterSeconds * 1000) + onExit() + } + } +} +@Composable +private fun NoteRow( + note: Note, + selected: Boolean, + onClick: () -> Unit, + onToggleFavorite: () -> Unit, +) { + val (title, body) = Note.titleAndBody(note.content) + Row( + modifier = Modifier + .fillMaxWidth() + .clickable(onClick = onClick) + .padding(horizontal = 12.dp, vertical = 10.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Column(Modifier.weight(1f)) { + Text( + title, + style = MaterialTheme.typography.titleSmall, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + color = if (selected) { + MaterialTheme.colorScheme.primary + } else { + MaterialTheme.colorScheme.onSurface + }, + ) + if (body.isNotBlank()) { + Text( + body, + style = MaterialTheme.typography.bodySmall, + maxLines = 2, + overflow = TextOverflow.Ellipsis, + ) + } + } + IconButton(onClick = onToggleFavorite) { + if (note.isFavorite) { + Icon(Icons.Filled.Star, contentDescription = "Unfavourite") + } else { + Icon(Icons.Outlined.StarBorder, contentDescription = "Favourite") + } + } + } +} + +/** Debounced write-through editor — the same "save as you type" feel as Android. */ +@Composable +private fun Editor(repository: NoteRepository, note: Note?) { + if (note == null) { + Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { + Text("Nothing to show yet", color = MaterialTheme.colorScheme.outline) + } + return + } + + var text by remember(note.id) { mutableStateOf(note.content) } + val scope = rememberCoroutineScope() + + // The saved note flows back through observeAll(); once the snapshot + // matches what is on screen there is nothing to write. + LaunchedEffect(text, note.content, note.id) { + if (text == note.content) return@LaunchedEffect + delay(400) + scope.launch { + repository.saveNoteWithHashtags( + note = note.copy(content = text), + hashtagsToAdd = emptySet(), + hashtagsToRemove = emptySet(), + ) + } + } + + Column( + modifier = Modifier.fillMaxSize().padding(16.dp), + verticalArrangement = Arrangement.spacedBy(8.dp), + ) { + Text("Editing #${note.id}", style = MaterialTheme.typography.labelMedium) + OutlinedTextField( + value = text, + onValueChange = { text = it }, + modifier = Modifier.fillMaxSize(), + placeholder = { Text("Write something…") }, + ) + } +} + +private fun Array.argAfter(flag: String): String? { + val index = indexOf(flag) + return takeIf { index >= 0 && index + 1 < size }?.get(index + 1) +} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2dc13e1..70e06df 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -68,6 +68,10 @@ kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serializa # Coroutines kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" } +# Phase 1 (M1b): Compose Desktop drives its event loop through Swing/AWT, which +# needs the Swing dispatcher for Dispatchers.Main (the desktop analogue of +# kotlinx-coroutines-android). +kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "coroutines" } # Data objectbox-android = { module = "io.objectbox:objectbox-android", version.ref = "objectbox" } diff --git a/settings.gradle.kts b/settings.gradle.kts index 3b6ffa3..af5412a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -32,6 +32,11 @@ include(":core:data") // a multiplatform one. include(":core:data-jvm") +// Phase 1 (M1b): the desktop application — a Compose Multiplatform window over +// :core:data-jvm. Owns its own library (XDG data dir); the encrypted backup +// file is the only bridge to the phone (M2). +include(":desktopApp") + // --- Desktop feasibility spike (Development Plan §6.3, throwaway) --- // Throwaway probes that answer "can the existing stack run on the JVM" // before any of Phases 1-6 are committed to. They are additive: no module diff --git a/tools/DESKTOP_FEASIBILITY.md b/tools/DESKTOP_FEASIBILITY.md index 874db37..8d1c40b 100644 --- a/tools/DESKTOP_FEASIBILITY.md +++ b/tools/DESKTOP_FEASIBILITY.md @@ -233,3 +233,33 @@ Also fixed while here: `.gitignore` had `**/objectbox-models/`, which meant the schema contract was **not in version control** — a fresh clone regenerated UIDs. `default.json` is now tracked (everything else in that directory stays ignored). This matters more now that two modules generate against it. + +**M1b — desktop shell over the real repository: DONE.** New `:desktopApp` +(Compose Multiplatform, `compose.desktop` DSL, no explicit `application` +plugin — see the spike finding). `DesktopContainer` mirrors Android's +`AppContainer` construction order and owns its own library at +`$XDG_DATA_HOME/incido`; `--data-dir` opens any other library. + +Verified headlessly (`--self-test` drives the same repository calls the "+" +button and the star make, `--exit-after` auto-closes): + +``` +library : /tmp/incido-desktop-smoke # written by :core:data-jvm:smoke +RENDER notes=0 selected=null # first frame (collectAsState initial) +RENDER notes=1 selected=1 # ObjectBox flow delivered +SELFTEST wrote note id=2 # write path through the UI's calls +RENDER notes=2 selected=2 # and it came back through the flow +--- reopen --- +RENDER notes=2 selected=2 # persisted +``` + +Two things that run proves beyond what the spike did: the desktop build reads a +library written by a *different* module's generated `MyObjectBox` (one schema, +two codegens), and ObjectBox's reactive flow feeds a Compose Desktop +composition. Note the first frame is always the empty initial value — anything +asserting on note counts must wait for the flow, not the first composition. + +Still synthetic in this build: the theme (Material 3 defaults, not Incido's) +and the screens (this is a two-pane stand-in). Both are replaced by the M4 +`composeApp` commonMain conversion. Not yet wired: settings, encryption (M3), +backups (M2), packaging (M5).