You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.3 KiB
114 lines
3.3 KiB
4 years ago
|
# Getting Started with Compose for Desktop
|
||
|
|
||
|
## What is covered
|
||
|
|
||
4 years ago
|
In this tutorial we will create a simple desktop UI application
|
||
|
using the Compose UI framework.
|
||
4 years ago
|
|
||
|
## Prerequisites
|
||
|
|
||
|
Compose for Desktop can produce applications for macOS, Linux and Windows platforms,
|
||
4 years ago
|
and so any of these platforms can be used for this tutorial.
|
||
4 years ago
|
|
||
4 years ago
|
The following software has to be preinstalled:
|
||
4 years ago
|
* JDK 11 or later
|
||
4 years ago
|
* IntelliJ IDEA Community Edition or Ultimate Edition 20.2 or later (other editors could be used, but we assume you are using IntelliJ IDEA in this tutorial)
|
||
4 years ago
|
|
||
|
## Creating a new project
|
||
|
|
||
|
*TBD: new project wizard*
|
||
|
|
||
4 years ago
|
The recommended way of building Compose for Desktop projects is by using Gradle.
|
||
|
JetBrains provides a simple way of building Compose for Desktop projects
|
||
|
using a special Gradle plugin.
|
||
4 years ago
|
First create a new directory, named `sample`.
|
||
|
```shell script
|
||
|
mkdir sample
|
||
|
cd sample
|
||
|
```
|
||
|
|
||
4 years ago
|
Create `settings.gradle.kts` as follows:
|
||
4 years ago
|
```kotlin
|
||
|
pluginManagement {
|
||
|
repositories {
|
||
|
gradlePluginPortal()
|
||
|
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
Then create `build.gradle.kts` with the following content:
|
||
|
```kotlin
|
||
|
import org.jetbrains.compose.compose
|
||
|
|
||
|
plugins {
|
||
|
kotlin("jvm") version "1.4.0"
|
||
4 years ago
|
id("org.jetbrains.compose") version "0.1.0-dev97"
|
||
4 years ago
|
application
|
||
|
}
|
||
|
|
||
|
repositories {
|
||
|
jcenter()
|
||
|
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
|
||
|
}
|
||
|
|
||
|
dependencies {
|
||
4 years ago
|
implementation(compose.desktop.currentOs)
|
||
4 years ago
|
}
|
||
|
|
||
|
application {
|
||
|
mainClassName = "MainKt"
|
||
|
}
|
||
|
```
|
||
|
Then create file `src/main/kotlin/main.kt` and put there:
|
||
|
```kotlin
|
||
|
import androidx.compose.desktop.Window
|
||
|
import androidx.compose.foundation.Text
|
||
|
import androidx.compose.foundation.layout.Arrangement
|
||
|
import androidx.compose.foundation.layout.Column
|
||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||
|
import androidx.compose.material.Button
|
||
|
import androidx.compose.material.MaterialTheme
|
||
|
import androidx.compose.runtime.mutableStateOf
|
||
|
import androidx.compose.runtime.remember
|
||
|
import androidx.compose.ui.Alignment
|
||
|
import androidx.compose.ui.Modifier
|
||
4 years ago
|
import androidx.compose.ui.unit.IntSize
|
||
4 years ago
|
import androidx.compose.ui.unit.dp
|
||
|
|
||
4 years ago
|
fun main() = Window(title = "Compose for Desktop", size = IntSize(300, 300)) {
|
||
4 years ago
|
var count = remember { mutableStateOf(0) }
|
||
|
MaterialTheme {
|
||
|
Column(Modifier.fillMaxSize(), Arrangement.spacedBy(5.dp)) {
|
||
|
Button(modifier = Modifier.align(Alignment.CenterHorizontally),
|
||
|
onClick = {
|
||
|
count.value++
|
||
|
}) {
|
||
|
Text(if (count.value == 0) "Hello World" else "Clicked ${count.value}!")
|
||
|
}
|
||
|
Button(modifier = Modifier.align(Alignment.CenterHorizontally),
|
||
|
onClick = {
|
||
|
count.value = 0
|
||
|
}) {
|
||
|
Text("Reset")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
```
|
||
4 years ago
|
## Running the project
|
||
|
|
||
4 years ago
|
Open `build.gradle.kts` as a project in IntelliJ IDEA.
|
||
4 years ago
|
|
||
|
![New project](screen1.png)
|
||
|
|
||
4 years ago
|
|
||
4 years ago
|
After you download the Compose for Desktop dependencies from the Maven repositories your new project is ready
|
||
|
to go. Open the Gradle toolbar on the right, and select `sample/Tasks/applications/run`.
|
||
|
The first run may take some time, but afterwards following dialog will be shown:
|
||
4 years ago
|
|
||
|
![Application running](screen2.gif)
|
||
|
|
||
4 years ago
|
You can click on the button several times and see that application reacts and
|
||
|
updates the UI.
|