Skip to content

Commit

Permalink
Fixes for Context menu tutorial (#1059)
Browse files Browse the repository at this point in the history
* Small fixes for Context Menu tutorial

* Fixing Context Menu tutorial
  • Loading branch information
akurasov authored Aug 16, 2021
1 parent 00d0ee0 commit d95c61a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tutorials/Context_Menu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ There is out-of-the box context menu support for TextField and Selectable text.
To enable standard context menu for a TextField you just need to put it inside DesktopMaterialTheme:

```kotlin
import androidx.compose.desktop.DesktopMaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalComposeUiApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class)
fun main() = singleWindowApplication(title = "Context menu") {
DesktopMaterialTheme { //it is mandatory for Context Menu
Expand All @@ -29,6 +37,12 @@ Standard context menu for TextField contains the following items based on text s
Enabling standard context menu for a Text component is similar - you just need to make it selectable:

```kotlin
import androidx.compose.desktop.DesktopMaterialTheme
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.Text
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalComposeUiApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class)
fun main() = singleWindowApplication(title = "Context menu") {
DesktopMaterialTheme { //it is mandatory for Context Menu
Expand All @@ -44,6 +58,22 @@ Context menu for text contains just Copy action.
To enable additional context menu items for TextField and Text components, ContextMenuDataProvider and ContextMenuItem elements are used:

```kotlin
import androidx.compose.desktop.DesktopMaterialTheme
import androidx.compose.foundation.ContextMenuDataProvider
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ContextMenuItem
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalComposeUiApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class)
fun main() = singleWindowApplication(title = "Context menu") {
DesktopMaterialTheme { //it is mandatory for Context Menu
Expand Down Expand Up @@ -79,6 +109,20 @@ In this example Text/TextField context menus will be extended with two additiona
There is a possibility to create a context menu for an arbitary application window area. This is implemented using ContextMenuArea API that is
similar to ContextMenuDataProvider.
```kotlin

import androidx.compose.desktop.DesktopMaterialTheme
import androidx.compose.foundation.ContextMenuArea
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ContextMenuItem
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalComposeUiApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class)
fun main() = singleWindowApplication(title = "Context menu") {
DesktopMaterialTheme { //it is mandatory for Context Menu
Expand Down

0 comments on commit d95c61a

Please sign in to comment.