Kirigami-addons

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