Browse Source

Migrate desktop templates/tutorials to 0.5.0-build226

pull/807/head
Igor Demin 4 years ago
parent
commit
e68baff90d
  1. 2
      templates/desktop-template/build.gradle.kts
  2. 2
      templates/multiplatform-template/build.gradle.kts
  3. 98
      tutorials/Window_API_new/README.md

2
templates/desktop-template/build.gradle.kts

@ -5,7 +5,7 @@ plugins {
// __KOTLIN_COMPOSE_VERSION__ // __KOTLIN_COMPOSE_VERSION__
kotlin("jvm") version "1.5.10" kotlin("jvm") version "1.5.10"
// __LATEST_COMPOSE_RELEASE_VERSION__ // __LATEST_COMPOSE_RELEASE_VERSION__
id("org.jetbrains.compose") version (System.getenv("COMPOSE_TEMPLATE_COMPOSE_VERSION") ?: "0.5.0-build225") id("org.jetbrains.compose") version (System.getenv("COMPOSE_TEMPLATE_COMPOSE_VERSION") ?: "0.5.0-build226")
} }
repositories { repositories {

2
templates/multiplatform-template/build.gradle.kts

@ -1,6 +1,6 @@
buildscript { buildscript {
// __LATEST_COMPOSE_RELEASE_VERSION__ // __LATEST_COMPOSE_RELEASE_VERSION__
val composeVersion = System.getenv("COMPOSE_TEMPLATE_COMPOSE_VERSION") ?: "0.5.0-build225" val composeVersion = System.getenv("COMPOSE_TEMPLATE_COMPOSE_VERSION") ?: "0.5.0-build226"
repositories { repositories {
google() google()

98
tutorials/Window_API_new/README.md

@ -19,7 +19,7 @@ import androidx.compose.ui.window.application
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
fun main() = application { fun main() = application {
Window { Window(onCloseRequest = ::exitApplication) {
// Content // Content
} }
} }
@ -41,7 +41,7 @@ import androidx.compose.ui.window.application
fun main() = application { fun main() = application {
var fileName by remember { mutableStateOf("Untitled") } var fileName by remember { mutableStateOf("Untitled") }
Window(title = "$fileName - Editor") { Window(onCloseRequest = ::exitApplication, title = "$fileName - Editor") {
Button(onClick = { fileName = "note.txt" }) { Button(onClick = { fileName = "note.txt" }) {
Text("Save") Text("Save")
} }
@ -74,11 +74,11 @@ fun main() = application {
isPerformingTask = false isPerformingTask = false
} }
if (isPerformingTask) { if (isPerformingTask) {
Window { Window(onCloseRequest = ::exitApplication) {
Text("Performing some tasks. Please wait!") Text("Performing some tasks. Please wait!")
} }
} else { } else {
Window { Window(onCloseRequest = ::exitApplication) {
Text("Hello, World!") Text("Hello, World!")
} }
} }
@ -112,8 +112,8 @@ fun main() = application {
) { ) {
if (isAskingToClose) { if (isAskingToClose) {
Dialog( Dialog(
onCloseRequest = { isAskingToClose = false },
title = "Close the document without saving?", title = "Close the document without saving?",
onCloseRequest = { isAskingToClose = false }
) { ) {
Button( Button(
onClick = { isOpen = false } onClick = { isOpen = false }
@ -150,9 +150,9 @@ fun main() = application {
val state = rememberWindowState() val state = rememberWindowState()
Window( Window(
onCloseRequest = { state.isVisible = false },
state, state,
title = "Counter", title = "Counter",
onCloseRequest = { state.isVisible = false }
) { ) {
var counter by remember { mutableStateOf(0) } var counter by remember { mutableStateOf(0) }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
@ -211,7 +211,7 @@ fun main() = application {
@Composable @Composable
private fun MyWindow( private fun MyWindow(
state: MyWindowState state: MyWindowState
) = Window(title = state.title, onCloseRequest = state::close) { ) = Window(onCloseRequest = state::close, title = state.title) {
MenuBar { MenuBar {
Menu("File") { Menu("File") {
Item("New window", onClick = state.openNewWindow) Item("New window", onClick = state.openNewWindow)
@ -272,22 +272,41 @@ import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.application import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState import androidx.compose.ui.window.rememberWindowState
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
fun main() = application { fun main() = application {
val state = rememberWindowState(isMaximized = true) val state = rememberWindowState(placement = WindowPlacement.Maximized)
Window(state) { Window(onCloseRequest = ::exitApplication, state) {
Column { Column {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(state.isFullscreen, { state.isFullscreen = !state.isFullscreen }) Checkbox(
state.placement == WindowPlacement.Fullscreen,
{
state.placement = if (it) {
WindowPlacement.Fullscreen
} else {
WindowPlacement.Floating
}
}
)
Text("isFullscreen") Text("isFullscreen")
} }
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(state.isMaximized, { state.isMaximized = !state.isMaximized }) Checkbox(
state.placement == WindowPlacement.Fullscreen,
{
state.placement = if (it) {
WindowPlacement.Maximized
} else {
WindowPlacement.Floating
}
}
)
Text("isMaximized") Text("isMaximized")
} }
@ -299,7 +318,10 @@ fun main() = application {
Text( Text(
"Position ${state.position}", "Position ${state.position}",
Modifier.clickable { Modifier.clickable {
state.position = state.position.copy(x = state.position.x + 10.dp) val position = state.position
if (position is WindowPosition.Absolute) {
state.position = position.copy(x = state.position.x + 10.dp)
}
} }
) )
@ -362,52 +384,28 @@ private fun onWindowRelocate(position: WindowPosition) {
## Handle window-level shortcuts ## Handle window-level shortcuts
```kotlin ```kotlin
import androidx.compose.foundation.layout.Box
import androidx.compose.material.TextField import androidx.compose.material.TextField
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.focusTarget
import androidx.compose.ui.input.key.Key import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.window.Window import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application import androidx.compose.ui.window.application
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
fun main() = application { fun main() = application {
var isOpen by remember { mutableStateOf(true) } Window(
onCloseRequest = ::exitApplication,
if (isOpen) { onPreviewKeyEvent = {
Window { when (it.key) {
val focusRequester = remember(::FocusRequester) Key.Escape -> {
LaunchedEffect(Unit) { exitApplication()
focusRequester.requestFocus() true
} }
else -> false
Box(
Modifier
.focusRequester(focusRequester)
.focusTarget()
.onPreviewKeyEvent {
when (it.key) {
Key.Escape -> {
isOpen = false
true
}
else -> false
}
}
) {
TextField("Text", {})
} }
} }
) {
TextField("Text", {})
} }
} }
``` ```
@ -445,8 +443,8 @@ fun main() = application {
if (isDialogOpen) { if (isDialogOpen) {
Dialog( Dialog(
initialAlignment = Alignment.Center, onCloseRequest = { isDialogOpen = false },
onCloseRequest = { isDialogOpen = false } initialAlignment = Alignment.Center
) { ) {
// Dialog's content // Dialog's content
} }
@ -487,7 +485,7 @@ import java.awt.Cursor
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
fun main() = application { fun main() = application {
Window { Window(onCloseRequest = ::exitApplication) {
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
window.cursor = Cursor(Cursor.CROSSHAIR_CURSOR) window.cursor = Cursor(Cursor.CROSSHAIR_CURSOR)
} }

Loading…
Cancel
Save