環境
- Compose for Desktop 1.0.0-alpha3
問題
Compose for Desktopのプロジェクトを作ると、Window関数がDeprecatedの警告が表示される。
対策
新しいWindow APIを使用する。
importするWindowを変更する。applicationもimportする。
//import androidx.compose.desktop.Window
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
Window関数の呼び出しを以下のように変更する。
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
...
}
}
全体のソースコードは次のようになる。
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
var text by remember { mutableStateOf("Hello, World!") }
MaterialTheme {
Button(onClick = {
text = "Hello, Desktop!"
}) {
Text(text)
}
}
}
}