Kirigami-addons

ConvergentContextMenu.qml
1// SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4pragma ComponentBehavior: Bound
5
6import QtQuick
7import QtQuick.Controls as QQC2
8import QtQuick.Controls as T
9import QtQuick.Layouts
10import Qt.labs.qmlmodels
11
12import org.kde.kirigami as Kirigami
13import org.kde.kirigamiaddons.components as KirigamiComponents
14import org.kde.kirigamiaddons.formcard as FormCard
15
16import './private' as P
17
18/**
19 * Menu popup that appears as a tradional menu on desktop and as a bottom
20 * drawer mobile.
21 *
22 * \code{qml}
23 * import QtQuick
24 * import QtQuick.Controls as Controls
25 * import org.kde.kirigamiaddons.components as Addons
26 *
27 * ListView {
28 * model: 10
29 * delegate: Controls.ItemDelegate {
30 * text: index
31 * onPressAndHold: contextMenu.popup();
32 * }
33 *
34 * Addons.ConvergentContextMenu {
35 * id: menu
36 * Controls.Action {
37 * text: i18nc("@action:inmenu", "Action 1")
38 * }
39 *
40 * Kirigami.Action {
41 * text: i18nc("@action:inmenu", "Action 2")
42 *
43 * Controls.Action {
44 * text: i18nc("@action:inmenu", "Sub-action")
45 * }
46 * }
47 * }
48 * }
49 * \endcode{qml}
50 *
51 * \since 1.7.0.
52 */
53Item {
54 id: root
55
56 /**
57 * This property holds the list of actions. This can be either tradional
58 * action from QtQuick.Controls or a Kirigami.Action with sub actions.
59 */
60 default property list<T.Action> actions
61
62 /**
63 * Optional item which will be displayed as header of the internal ButtonDrawer.
64 *
65 * Note: This is only displayed on the first level of the ContextMenu mobile mode.
66 */
67 property Item headerContentItem
68
69 signal closed
70
71 function popup(position = null): void {
72 if (Kirigami.Settings.isMobile) {
73 const item = mobileMenu.createObject(root);
74 item.open();
75 } else {
76 const item = desktopMenu.createObject(root);
77 if (position) {
78 item.popup(position);
79 } else {
80 item.popup();
81 }
82 }
83 }
84
85 Component {
86 id: desktopMenu
87
88 P.ActionsMenu {
89 actions: root.actions
90 submenuComponent: P.ActionsMenu { }
91 onClosed: {
92 root.closed();
93 destroy();
94 }
95 }
96 }
97
98 Component {
99 id: mobileMenu
100
101 KirigamiComponents.BottomDrawer {
102 id: drawer
103
104 parent: root.QQC2.Overlay.overlay
105 onClosed: {
106 root.closed();
107 destroy();
108 }
109
110 headerContentItem: ColumnLayout {
111 children: if (stackViewMenu.depth > 1 || root.headerContentItem === null) {
112 return nestedHeader;
113 } else {
114 return root.headerContentItem;
115 }
116 }
117
118 property Item nestedHeader: RowLayout {
119 Layout.fillWidth: true
120 spacing: Kirigami.Units.smallSpacing
121
122 enabled: stackViewMenu.currentItem?.title.length > 0
123
124 QQC2.ToolButton {
125 icon.name: 'draw-arrow-back-symbolic'
126 text: i18ndc("kirigami-addons6", "@action:button", "Go Back")
127 display: QQC2.ToolButton.IconOnly
128 onClicked: stackViewMenu.pop();
129
130 QQC2.ToolTip.visible: hovered
131 QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
132 QQC2.ToolTip.text: text
133 }
134
136 level: 2
137 text: stackViewMenu.currentItem?.title
138 elide: Text.ElideRight
139 Layout.fillWidth: true
140 }
141 }
142
143 QQC2.StackView {
144 id: stackViewMenu
145
146 implicitHeight: currentItem?.implicitHeight
147 implicitWidth: currentItem?.implicitWidth
148
149 initialItem: P.ContextMenuPage {
150 stackView: stackViewMenu
151 actions: root.actions
152 drawer: drawer
153 }
154 }
155 }
156 }
157}
QString i18ndc(const char *domain, const char *context, const char *text, const TYPE &arg...)
QStringView level(QStringView ifopt)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:46:31 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.