Skip to content

Commit

Permalink
Extending tutorial with Tray-only application (#1063)
Browse files Browse the repository at this point in the history
* Extending tutorial with Tray-only application

* Update README.md

* Update README.md

* Update README.md

* Update README.md

Co-authored-by: Igor Demin <[email protected]>
  • Loading branch information
akurasov and igordmn authored Aug 16, 2021
1 parent d95c61a commit 8df79e5
Showing 1 changed file with 65 additions and 29 deletions.
94 changes: 65 additions & 29 deletions tutorials/Tray_Notifications_MenuBar_new/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,51 @@ import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.window.Notification
import androidx.compose.ui.window.Tray
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberNotification
import androidx.compose.ui.window.rememberTrayState

fun main() = application {
var count by remember { mutableStateOf(0) }
var isOpen by remember { mutableStateOf(true) }

if (isOpen) {
val trayState = rememberTrayState()
val notification = rememberNotification("Notification", "Message from MyApp!")

Tray(
state = trayState,
icon = TrayIcon,
menu = {
Item(
"Increment value",
onClick = {
count++
}
)
Item(
"Send notification",
onClick = {
trayState.sendNotification(notification)
}
)
Item(
"Exit",
onClick = {
isOpen = false
}
)
}
)

Window(
onCloseRequest = ::exitApplication,
onCloseRequest = {
isOpen = false
},
icon = MyAppIcon
) {
val trayState = rememberTrayState()
val notification = Notification("Notification", "Message from MyApp!")
Tray(
state = trayState,
icon = TrayIcon,
menu = {
Item(
"Increment value",
onClick = {
count++
}
)
Item(
"Send notification",
onClick = {
trayState.sendNotification(notification)
}
)
Item(
"Exit",
onClick = {
isOpen = false
}
)
}
)

// content
Box(
modifier = Modifier.fillMaxSize(),
Expand Down Expand Up @@ -101,6 +104,39 @@ object TrayIcon : Painter() {

<img alt="Tray" src="tray.gif" height="266" />

Please note, that it is possible to create a Tray application without Window:

```kotlin
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.window.Tray
import androidx.compose.ui.window.application

@OptIn(ExperimentalComposeUiApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class)
fun main() = application {
Tray(
icon = TrayIcon,
menu = {
Item(
"Exit",
onClick = ::exitApplication
)
}
)
}

object TrayIcon : Painter() {
override val intrinsicSize = Size(256f, 256f)

override fun DrawScope.onDraw() {
drawOval(Color(0xFFFFA500))
}
}
```

## MenuBar

MenuBar is used to create and customize the menu bar for a particular window.
Expand Down

0 comments on commit 8df79e5

Please sign in to comment.