|
| 1 | +# Kotlin Desktop Template |
| 2 | + |
| 3 | +A starting point for desktop apps in Kotlin with their own window frame (no OS title bar), rendered by Skia |
| 4 | +through Skiko. No embedded browser: Compose Desktop draws everything on a Skia canvas. |
| 5 | + |
| 6 | +- Kotlin 2.4 on JDK 21, Gradle 9.8 (Kotlin DSL) and a version catalog (`gradle/libs.versions.toml`) |
| 7 | +- Compose Multiplatform Desktop 1.12 (Skiko is its renderer, the Skia API is also available directly, see `Gradient.kt`) |
| 8 | +- Undecorated window with a `TitleBar` component: drag, double-click to maximize, minimize/maximize/close, edge resize |
| 9 | +- Auto-update from GitHub Releases, written on the JDK `HttpClient` and kotlinx.serialization (no extra HTTP library) |
| 10 | +- i18n with Compose resources (`en`, `pt`, `de` as examples) and a persisted language choice |
| 11 | +- Native installers through jpackage (`.msi`/`.exe`, `.dmg`, `.deb`/`.rpm`) with a bundled, jlinked JRE |
| 12 | +- GitHub Actions for build and release, shipped commented out |
| 13 | +- Many useful libraries ready to enable in `build.gradle.kts`, each one explained |
| 14 | + |
| 15 | +## Quick start |
| 16 | + |
| 17 | +```bash |
| 18 | +./gradlew run # start the app |
| 19 | +./gradlew test # unit tests |
| 20 | +./gradlew packageDistributionForCurrentOS # installer for the OS you are on (build/compose/binaries/main/) |
| 21 | +./gradlew createDistributable # portable folder with launcher executable and bundled JRE |
| 22 | +``` |
| 23 | + |
| 24 | +jpackage only builds installers for the OS it runs on, so use the CI matrix in `.github/workflows/release.yml` |
| 25 | +to produce all of them. |
| 26 | + |
| 27 | +## Creating a project from this template |
| 28 | + |
| 29 | +1. `gradle.properties`: `app.name`, `app.group`, `app.version`, `app.vendor`, `github.owner`, `github.repo`. |
| 30 | +2. Rename the package `com.example.app` to your `app.group` (directories under `src/main/kotlin` and `src/test/kotlin`). |
| 31 | + `compose.resources.packageOfResClass` and `mainClass` already follow `app.group`. |
| 32 | +3. `build.gradle.kts`: generate a new `upgradeUuid` for the Windows MSI and keep it stable from then on. |
| 33 | +4. Replace the icons in `packaging/` and `src/main/composeResources/drawable/app_icon.png`. |
| 34 | +5. `settings.gradle.kts`: change `rootProject.name`. |
| 35 | +6. Enable the workflows: remove the leading `# ` from the lines in `.github/workflows/*.yml` and `.github/dependabot.yml`. |
| 36 | + |
| 37 | +## Auto-update |
| 38 | + |
| 39 | +`GitHubUpdater` calls `GET /repos/{owner}/{repo}/releases/latest`, compares the tag (`v1.2.0`) with the running |
| 40 | +version using semantic versioning, picks the installer for the current OS and architecture, downloads it with |
| 41 | +progress and verifies the SHA-256 digest GitHub reports for the asset. `UpdateManager` exposes the state as a |
| 42 | +`StateFlow` for the UI, and `Installer` hands the file to the OS (`msiexec`, `open`, `xdg-open`) so the user |
| 43 | +confirms the install, then the app quits. |
| 44 | + |
| 45 | +- The release workflow prefixes asset names with `windows-x64`, `macos-arm64`, `macos-x64`, `linux-x64`. |
| 46 | + The updater matches those tokens, so keep that naming if you change the workflow. |
| 47 | +- Linux: `.deb` or `.rpm` is chosen from `/etc/os-release`. On other distributions (Arch and derivatives) no |
| 48 | + installer matches, and the UI offers the release page instead. |
| 49 | +- Only public repositories work as written. Private repositories need a token, which should not be shipped in an app. |
| 50 | +- Unsigned installers trigger SmartScreen and Gatekeeper warnings. Signing is configured in the `windows`/`macOS` blocks. |
| 51 | + |
| 52 | +## Internationalization |
| 53 | + |
| 54 | +Strings live in `src/main/composeResources/values*/strings.xml` and are read with |
| 55 | +`stringResource(Res.string.key)`. To add a language, add a `values-xx/strings.xml` and list the locale in |
| 56 | +`Languages.supported`. The chosen language is stored with `java.util.prefs`. Switching happens at runtime by |
| 57 | +setting the default `Locale` and recomposing the window content under `key(locale)` (see `Main.kt`). |
| 58 | + |
| 59 | +## Custom window frame: what you get and what you give up |
| 60 | + |
| 61 | +Drawing the title bar yourself means the OS no longer provides some behaviours: |
| 62 | + |
| 63 | +- Edge resizing and dragging are implemented by Compose for undecorated windows. Window snapping (Windows Snap |
| 64 | + Layouts, macOS tiling, some Linux compositors) may not work. |
| 65 | +- A maximized undecorated window can cover the Windows taskbar. `TitleBar` sets `maximizedBounds` to the usable |
| 66 | + screen area on Windows to avoid that. Test it on your multi-monitor setups. |
| 67 | +- On macOS the controls are drawn on the left as colored circles, elsewhere on the right. |
| 68 | +- If you need the native frame behaviour back with a custom look, JetBrains Runtime exposes a custom title bar |
| 69 | + API that keeps native controls and snapping, at the cost of depending on JBR internals. |
| 70 | + |
| 71 | +## Packaging notes |
| 72 | + |
| 73 | +- `modules(...)` in `build.gradle.kts` lists the JDK modules jlink keeps. Add what your dependencies need |
| 74 | + (for example `java.sql`). If something works with `run` but fails in the installed app, a missing module is the usual cause. |
| 75 | +- Installer size is mostly the bundled runtime. Expect tens of megabytes. |
| 76 | +- `jpackage` rejects `0.x.y` versions for `.dmg`/`.pkg`, so the template starts at `1.0.0`. |
| 77 | + |
| 78 | +## Lucide icons |
| 79 | + |
| 80 | +All [Lucide](https://lucide.dev) icons as a font, drawn with Skia. |
| 81 | + |
| 82 | +```kotlin |
| 83 | +LucideIcon(Lucide.Settings) // 16.dp, theme text color |
| 84 | +LucideIcon(Lucide.FolderOpen, size = 24.dp, tint = AppTheme.colors.accent, contentDescription = "Open") |
| 85 | +LucideIcon(Lucide.byName("arrow-up-right") ?: Lucide.CircleHelp) // by kebab-case name at runtime |
| 86 | +BasicText(Lucide.Heart.text, style = TextStyle(fontFamily = LucideFont.family, fontSize = 18.sp)) |
| 87 | +``` |
| 88 | + |
| 89 | +How it works: `lucide.ttf` has a 1000 unit em square with the baseline at the bottom edge, and every icon sits on |
| 90 | +Lucide's 24x24 grid inside it. `LucideIcon` loads the TTF into a Skia `Typeface` once and draws the icon's code |
| 91 | +point with `Canvas.drawString`, placing the baseline at the bottom of the icon box. No font metrics are involved, |
| 92 | +so the result is identical on every OS. Prefer `LucideIcon` over `Text` with the font family, which adds line |
| 93 | +height and can shift the glyph. |
| 94 | + |
| 95 | +Files: |
| 96 | + |
| 97 | +``` |
| 98 | +scripts/generate-lucide.groovy installs the font and generates the mapping |
| 99 | +src/main/resources/fonts/lucide.ttf the icon font |
| 100 | +src/main/resources/fonts/lucide-codepoints.json name -> code point (also contains renamed/alias names) |
| 101 | +src/main/resources/licenses/lucide.txt ISC + MIT notices, they must ship with the font |
| 102 | +src/main/kotlin/.../ui/icons/Lucide.kt generated: one typed constant per icon (Lucide.ArrowDown, ...) |
| 103 | +src/main/kotlin/.../ui/icons/Lucide*.kt LucideGlyph, LucideFont, LucideIcon, lookup, gallery |
| 104 | +``` |
| 105 | + |
| 106 | +- `groovy scripts/generate-lucide.groovy` downloads the newest `lucide-static` from npm (needs `npm` and `tar`), |
| 107 | + `--version 1.62.0` pins a release, `--lucide-dir <path>` uses an unpacked copy. Rerun it to update the icons. |
| 108 | + The Kotlin package defaults to `<app.group>.ui.icons`, override it with `--package`. |
| 109 | +- Constant names are the kebab-case name in PascalCase (`folder-open` -> `FolderOpen`). Aliases of renamed icons |
| 110 | + are included, so old names keep working. Icons that Lucide removed from the font (brand logos such as GitHub) |
| 111 | + are skipped by the script. |
| 112 | +- `LucideGallery.kt` has a `main()` with a searchable grid of every icon: run it from the IDE to browse names. |
| 113 | +- To remove it: delete `ui/icons/`, `scripts/generate-lucide.groovy` and the three resource files above. |
| 114 | + |
| 115 | +## Alternatives worth knowing |
| 116 | + |
| 117 | +- [Nucleus](https://github.com/kdroidfilter/Nucleus): Gradle plugin and runtime libraries for Compose Desktop |
| 118 | + (packaging, an updater, decorated window helpers, native notifications). It tracks the newest Kotlin and Compose |
| 119 | + releases closely, so check its requirements before adopting it. |
| 120 | +- `compose-native` (community fork): Kotlin/Native builds of Compose without a JVM for Linux and Windows. Alpha. |
0 commit comments