Skip to content

Commit

Permalink
fix: There isn't context menu in TextField
Browse files Browse the repository at this point in the history
add context menu in TextField

Issue: linuxdeepin/developer-center#6521
  • Loading branch information
FeiWang1119 committed Dec 21, 2023
1 parent 2b5497d commit 26f929f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
59 changes: 59 additions & 0 deletions qt6/src/qml/TextField.qml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,63 @@ T.TextField {
implicitWidth: DS.Style.edit.width
implicitHeight: DS.Style.edit.textFieldHeight
}

MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton

onClicked: {
contextMenu.popup(mouse.x, mouse.y)
}
}

Menu {
id: contextMenu

MenuItem
{
text: "Copy"
onTriggered: control.copy()
enabled: control.selectedText.length
}

MenuItem
{
text: "Cut"
onTriggered: control.cut()
enabled: control.selectedText.length
}

MenuItem
{
text: "Paste"
onTriggered:
{
var text = control.paste()
control.insert(control.cursorPosition, text)
}
}

MenuItem
{
text: "Select All"
onTriggered: control.selectAll()
enabled: control.text.length
}

MenuItem
{
text: "Undo"
onTriggered: control.undo()
enabled: control.canUndo
}

MenuItem
{
text: "Redo"
onTriggered: control.redo()
enabled: control.canRedo
}
}

}
59 changes: 59 additions & 0 deletions src/qml/TextField.qml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,63 @@ T.TextField {
implicitWidth: DS.Style.edit.width
implicitHeight: DS.Style.edit.textFieldHeight
}

MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton

onClicked: {
contextMenu.popup(mouse.x, mouse.y)
}
}

Menu {
id: contextMenu

MenuItem
{
text: "Copy"
onTriggered: control.copy()
enabled: control.selectedText.length
}

MenuItem
{
text: "Cut"
onTriggered: control.cut()
enabled: control.selectedText.length
}

MenuItem
{
text: "Paste"
onTriggered:
{
var text = control.paste()
control.insert(control.cursorPosition, text)
}
}

MenuItem
{
text: "Select All"
onTriggered: control.selectAll()
enabled: control.text.length
}

MenuItem
{
text: "Undo"
onTriggered: control.undo()
enabled: control.canUndo
}

MenuItem
{
text: "Redo"
onTriggered: control.redo()
enabled: control.canRedo
}
}

}

0 comments on commit 26f929f

Please sign in to comment.