Compose - Desktop

Compose allows creating desktop applications for macOS, Linux, and Windows.

Introduction

Compose has a desktop-only API: Desktop-only API

This API provides access to desktop-specific features, such as window management, system tray integration, file dialogs, and more.

Top-level windows

Compose offers various functions for managing windows. You can hide them in the tray, make them draggable, adapt their size, change their position, etc.

Top-level windows management

Scrollbars

You can apply scrollbars to scrollable components. The scrollbar and scrollable components share a common state to synchronize with each other.

Scrollbars

Tooltips

You can add tooltips to any composable. A tooltip is a small pop-up window that displays a brief description of the component when the user hovers over it with the mouse pointer.

Tooltips

Context menus

You can add context menus to any composable. A context menu is a pop-up menu that appears when the user right-clicks on a component.

Contexts menus

Mouse events

In your desktop project, you can listen to various mouse events, like clicking, moving, scrolling, or entering and exiting the input region.

Mouse Events

Keyboard events

You can manage keyboard events by setting up event handlers in two different scopes:

  • Event handlers based on the focused element.
  • Event handlers in the window scope.

Keyboard events

Accessibility support

Compose Multiplatform make most accessibility features available in common code across all platforms.

Accessibility

Testing UI with JUnit

Compose Multiplatform for desktop provides a testing API based on JUnit and the Jetpack Compose testing API.

Testing UI with JUnit

Native distribution

Native distributions

All available tasks are shown in the Gradle tool window.

In the composeApp\build.gradle.kts file is the application configuration unit:

compose.desktop {
application {
mainClass = "dev.xtec.desktop.MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "Desktop"
packageVersion = "1.0.0"
windows {
console = true
perUserInstall = true
}
}
}
}

You can see that we have modified the application configuration with Platform-specific options for Windows.

Generate an installation package for Windows:

Terminal window
.\gradlew packageMsi
...
> Task :composeApp:packageMsi
The distribution is written to C:\Users\david\Workspace\desktop\composeApp\build\compose\binaries\main\msi\dev.xtec.desktop-1.0.0.msi

After running a task, Gradle generates the output binaries in the ${project.buildDir}/compose/binaries directory.

Install the application:

ProGuard

ProGuard is an open-source tool for code shrinking and obfuscation.

You can run the application with the same configuration as a distribution (with ProGuard):

Terminal window
.\gradlew runRelease

TODO We have an error Unsupported version number [66.0] (maximum 62.65535, Java 18). Does ProGuard only work with Java 17 to 18?

For each packaging task, the Gradle plugin provides a “release” task (with ProGuard).

Terminal window
> .\gradlew packageReleaseMsi

Or let Gradle select automatically depending on the operating system:

Terminal window
> .\gradlew packageDistributionForCurrentOS

Pending