aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/quickcontrols/menus/main.qml
blob: 59dfbac3fc2bc53479c52b017bb9ed8db12b4d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtCore
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

ApplicationWindow {
    id: window
    width: 800
    height: 600
    visible: true
    title: qsTr("Menus - style: %1").arg(currentStyle)

    required property string currentStyle

    Shortcut {
        sequence: "Ctrl+Q"
        onActivated: Qt.quit()
    }

    Settings {
        id: settings

        property alias windowX: window.x
        property alias windowY: window.y
        property alias windowWidth: window.width
        property alias windowHeight: window.height
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("Edit")

            Action {
                text: qsTr("Cut")
                enabled: textArea.selectedText.length > 0
            }
            Action {
                text: qsTr("Copy")
                enabled: textArea.selectedText.length > 0
            }
            Action {
                text: qsTr("Paste")
                enabled: textArea.activeFocus
            }

    //        MenuSeparator { }

    //        Menu {
    //            title: "Find/Replace"
    //            Action { text: "Find Next" }
    //            Action { text: "Find Previous" }
    //            Action { text: "Replace" }
    //        }
        }
    }

    ColumnLayout {
        anchors.fill: parent

        Label {
            text: qsTr("Right click on the window background to open a context menu. "
               + "Right click on the TextArea to access its edit context menu.\n\n"
               + "Things to check:\n\n"
               + "- Do the menu items trigger their actions (check console for output)?\n"
               + "- Do the Edit menu items (in the MenuBar menu and edit context menu)"
               + " work as expected with the TextArea?\n"
               + "  - Are they enabled/disabled as expected?\n"
               + "  - Does the TextArea keep focus after interacting with the Edit menu items?\n"
               + "- Does adding and removing menu items work?")
            verticalAlignment: Text.AlignVCenter
            wrapMode: Text.Wrap

            Layout.alignment: Qt.AlignHCenter
            Layout.preferredWidth: window.width * 0.5
            Layout.fillHeight: true
        }

        GroupBox {
            title: qsTr("Context menu")

            Layout.fillWidth: true

            RowLayout {
                anchors.fill: parent

                Button {
                    text: qsTr("Add action")
                    onClicked: backgroundContextMenu.appendAction()
                }
                Button {
                    text: qsTr("Remove action")
                    onClicked: backgroundContextMenu.removeLastAction()
                }

                Button {
                    text: qsTr("Add sub-menu action")
                    onClicked: subMenu.appendAction()
                }
                Button {
                    text: qsTr("Remove sub-menu action")
                    onClicked: subMenu.removeLastAction()
                }

                Item {
                    Layout.fillWidth: true
                }
            }
        }

        TextArea {
            id: textArea
            text: qsTr("Dummy TextArea to test disabled menu items")

            Layout.fillWidth: true
            Layout.minimumHeight: 100

            TapHandler {
                objectName: "textAreaTapHandler"
                acceptedButtons: Qt.RightButton
                onTapped: editContextMenu.open()
            }
        }
    }

    TapHandler {
        objectName: "backgroundTapHandler"
        acceptedButtons: Qt.RightButton
        onTapped: backgroundContextMenu.open()
    }

    Component {
        id: actionComponent

        Action {}
    }

    component ContextAction: Action {
        onTriggered: print("triggered", text)
    }

    Menu {
        id: backgroundContextMenu

        function appendAction() {
            let action = actionComponent.createObject(null, { text: qsTr("Extra context menu item") })
            backgroundContextMenu.addAction(action)
        }

        function removeLastAction() {
            // TODO: Can't use count here because it's 0: it uses contentModel->count(), but native menu items
            // are not Qt Quick items, so we either need to document that you should use contentData.count
            // or add an "actions" property. The problem with contentData is that it could contain
            // non-Action objects. Another potential issue is that "It is not re-ordered when items are inserted or moved",
            // making it unreliable as a general purpose container of actions if users add or remove them dynamically.
            backgroundContextMenu.removeAction(backgroundContextMenu.actionAt(backgroundContextMenu.contentData.length - 1))
        }

        ContextAction {
            text: qsTr("Context menu item 1")
        }
        ContextAction {
            text: qsTr("Context menu item 2")
        }
        ContextAction {
            text: qsTr("Context menu item 3")
        }

        // TODO: separator

        Menu {
            id: subMenu
            title: qsTr("Sub-menu")

            function appendAction() {
                let action = actionComponent.createObject(null, { text: qsTr("Extra sub-menu item") })
                subMenu.addAction(action)
            }

            function removeLastAction() {
                subMenu.removeAction(subMenu.actionAt(subMenu.contentData.length - 1))
            }

            ContextAction {
                text: qsTr("Sub-menu item 1")
            }
            ContextAction {
                text: qsTr("Sub-menu item 2")
            }
            ContextAction {
                text: qsTr("Sub-menu item 3")
            }
        }
    }

    Menu {
        id: editContextMenu

        ContextAction {
            text: qsTr("Cut")
            enabled: textArea.selectedText.length > 0
        }
        ContextAction {
            text: qsTr("Copy")
            enabled: textArea.selectedText.length > 0
        }
        ContextAction {
            text: qsTr("Paste")
            enabled: textArea.activeFocus
        }
    }
}