|
|
|
@ -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 |
|
|
|
@ -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 |
|
|
|
@ -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 |
|
|
|
@ -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 |
|
|
|
|