How to prevent copy/paste/cut context menu when right click on textfield [Jetpack Compose desktop]?

huangapple go评论46阅读模式
英文:

How to prevent copy/paste/cut context menu when right click on textfield [Jetpack Compose desktop]?

问题

我正在开发一个Compose桌面应用程序。在这个平台上如何做同样的事情,即在右键单击桌面时防止复制/粘贴/剪切上下文菜单。

我尝试为LocalTextToolbar提供EmptyTextToolbar,但它不起作用。

英文:

Reference solution

i'm developing a compose desktop application.how to do same thing in this platform.i mean prevent copy/paste/cut context menu when right click on desktop.

i try to provide EmptyTextToolbar to LocalTextToolbar.it's doesn't work.

答案1

得分: 0

emm,我找到了一个解决方案。

当组合弹出上下文菜单时,它们从LocalTextContextMenu获取菜单项。所以,我尝试提供EmptyContextMenu给LocalTextContextMenu,它有效!

EmptyContextMenu:

@OptIn(ExperimentalFoundationApi::class)
object EmptyContextMenu: TextContextMenu {
    @Composable
    override fun Area(textManager: TextContextMenu.TextManager, state: ContextMenuState, content:@Composable () -> Unit) {
        ContextMenuArea({
            emptyList()
        }, state, content = content)
    }
}

显示的代码:

@OptIn(ExperimentalFoundationApi::class)
fun main() = application {
    Window(
        onCloseRequest = ::exitApplication
    ) {
        var textValue by remember { mutableStateOf(TextFieldValue("")) }
        CompositionLocalProvider(
            LocalTextContextMenu provides EmptyContextMenu
        ) {
            TextField(
                value = textValue,
                onValueChange = { newValue ->
                    textValue = if (newValue.selection.length > 0) {
                        newValue.copy(selection = textValue.selection)
                    } else {
                        newValue
                    }
                }
            )
        }
    }
}

这里有一些参考链接:

TextContextMenu接口定义

接口默认版本

接口用法

英文:

emm,i find a solution

when compose popup context menu,they get menu item from LocalTextContextMenu.
so,i try to provides EmptyContextMenu to LocalTextContextMenu.it's working!

EmptyContextMenu:

@OptIn(ExperimentalFoundationApi::class)
object EmptyContextMenu: TextContextMenu {
    @Composable
    override fun Area(textManager: TextContextMenu.TextManager, state: ContextMenuState, content:@Composable () -> Unit) {
        ContextMenuArea({
            emptyList()
        }, state, content = content)
    }
}

display code

@OptIn(ExperimentalFoundationApi::class)
fun main() = application {
    Window(
        onCloseRequest = ::exitApplication
    ) {
        var textValue by remember { mutableStateOf(TextFieldValue("")) }
        CompositionLocalProvider(
            LocalTextContextMenu provides EmptyContextMenu
        ) {
            TextField(
                value = textValue,
                onValueChange = { newValue ->
                    textValue = if (newValue.selection.length > 0) {
                        newValue.copy(selection = textValue.selection)
                    } else {
                        newValue
                    }
                }
            )
        }
    }
}

there is some reference link

TextContextMenu interface define,

interface default version,

interface usage

huangapple
  • 本文由 发表于 2023年6月25日 20:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定