Kirigami2

ActionsMenu.qml
1/*
2 * SPDX-FileCopyrightText: 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7import QtQuick
8import QtQuick.Controls as QQC2
9import QtQuick.Templates as T
10import org.kde.kirigami as Kirigami
11
12QQC2.Menu {
13 id: root
14
15 property alias actions: actionsInstantiator.model
16
17 property Component submenuComponent
18 property Component itemDelegate: ActionMenuItem {}
19 property Component separatorDelegate: QQC2.MenuSeparator {
20 property T.Action action
21 }
22 property Component loaderDelegate: Loader {
23 property T.Action action
24 }
25 property T.Action parentAction
26 property T.MenuItem parentItem
27
28 Instantiator {
29 id: actionsInstantiator
30
31 active: root.visible
32 delegate: QtObject {
33 readonly property T.Action action: modelData
34
35 property QtObject item: null
36 property bool isSubMenu: false
37
38 Component.onCompleted: {
39 const isKirigamiAction = action instanceof Kirigami.Action;
40 if (!isKirigamiAction || action.children.length === 0) {
41 if (isKirigamiAction && action.separator) {
42 item = root.separatorDelegate.createObject(null, { action });
43 } else if (action.displayComponent) {
44 item = root.loaderDelegate.createObject(null, {
45 action,
46 sourceComponent: action.displayComponent,
47 });
48 } else {
49 item = root.itemDelegate.createObject(null, { action });
50 }
51 root.addItem(item)
52 } else if (root.submenuComponent) {
53 item = root.submenuComponent.createObject(null, {
54 parentAction: action,
55 title: action.text,
56 actions: action.children,
57 });
58
59 root.insertMenu(root.count, item);
60 item.parentItem = root.contentData[root.contentData.length - 1];
61 item.parentItem.icon = action.icon;
62 isSubMenu = true;
63 }
64 }
65
66 Component.onDestruction: {
67 if (isSubMenu) {
68 root.removeMenu(item);
69 } else {
70 root.removeItem(item);
71 }
72 item.destroy();
73 }
74 }
75 }
76}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.