Kirigami2

ContextDrawer.qml
1/*
2 * SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
3 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8import QtQuick
9import QtQuick.Controls as QQC2
10import QtQuick.Templates as T
11import org.kde.kirigami as Kirigami
12import "private" as KP
13
14/**
15 * A specialized type of drawer that will show a list of actions
16 * relevant to the application's current page.
17 *
18 * Example usage:
19 *
20 * @code
21 * import org.kde.kirigami as Kirigami
22 *
23 * Kirigami.ApplicationWindow {
24 * contextDrawer: Kirigami.ContextDrawer {
25 * enabled: true
26 * actions: [
27 * Kirigami.Action {
28 * icon.name: "edit"
29 * text: "Action text"
30 * onTriggered: {
31 * // do stuff
32 * }
33 * },
34 * Kirigami.Action {
35 * icon.name: "edit"
36 * text: "Action text"
37 * onTriggered: {
38 * // do stuff
39 * }
40 * }
41 * ]
42 * }
43 * }
44 * @endcode
45 *
46 * @inherit OverlayDrawer
47 */
48Kirigami.OverlayDrawer {
49 id: root
50
51 handleClosedIcon.source: null
52 handleOpenIcon.source: null
53
54 /**
55 * @brief A title for the action list that will be shown to the user when opens the drawer
56 *
57 * default: ``qsTr("Actions")``
58 */
59 property string title: qsTr("Actions")
60
61 /**
62 * List of contextual actions to be displayed in a ListView.
63 *
64 * @see QtQuick.Action
65 * @see org::kde::kirigami::Action
66 * @property list<T.Action> actions
67 */
68 property list<T.Action> actions
69
70 /**
71 * @brief Arbitrary content to show above the list view.
72 *
73 * default: `an Item containing a Kirigami.Heading that displays a title whose text is
74 * controlled by the title property.`
75 *
76 * @property Component header
77 * @since 2.7
78 */
79 property alias header: menu.header
80
81 /**
82 * @brief Arbitrary content to show below the list view.
83 * @property Component footer
84 * @since 2.7
85 */
86 property alias footer: menu.footer
87
88 // Not stored in a property, so we don't have to waste memory on an extra list.
89 function visibleActions() {
90 return actions.filter(
91 action => !(action instanceof Kirigami.Action) || action.visible
92 );
93 }
94
95 // Disable for empty menus or when we have a global toolbar
96 enabled: {
97 const pageStack = typeof applicationWindow !== "undefined" ? applicationWindow().pageStack : null;
98 const itemExistsButStyleIsNotToolBar = item => item && item.globalToolBarStyle !== Kirigami.ApplicationHeaderStyle.ToolBar;
99 return menu.count > 0
100 && (!pageStack
101 || !pageStack.globalToolBar
102 || (pageStack.layers.depth > 1
103 && itemExistsButStyleIsNotToolBar(pageStack.layers.currentItem))
104 || itemExistsButStyleIsNotToolBar(pageStack.trailingVisibleItem));
105 }
106
107 edge: Qt.application.layoutDirection === Qt.RightToLeft ? Qt.LeftEdge : Qt.RightEdge
108 drawerOpen: false
109
110 // list items go to edges, have their own padding
111 topPadding: 0
112 leftPadding: 0
113 rightPadding: 0
114 bottomPadding: 0
115
116 property bool handleVisible: {
117 if (typeof applicationWindow === "function") {
118 const w = applicationWindow();
119 if (w) {
120 return w.controlsVisible;
121 }
122 }
123 // For a ContextDrawer its handle is hidden by default
124 return false;
125 }
126
127 contentItem: QQC2.ScrollView {
128 // this just to create the attached property
129 Kirigami.Theme.inherit: true
130 implicitWidth: Kirigami.Units.gridUnit * 20
131 ListView {
132 id: menu
133 interactive: contentHeight > height
134
135 model: root.visibleActions()
136
137 topMargin: root.handle.y > 0 ? menu.height - menu.contentHeight : 0
138 header: QQC2.ToolBar {
139 height: pageStack.globalToolBar.preferredHeight
140 width: parent.width
141
142 Kirigami.Heading {
143 id: heading
144 elide: Text.ElideRight
145 text: root.title
146
147 anchors {
148 verticalCenter: parent.verticalCenter
149 left: parent.left
150 right: parent.right
151 leftMargin: Kirigami.Units.largeSpacing
152 rightMargin: Kirigami.Units.largeSpacing
153 }
154 }
155 }
156
157 delegate: Column {
158 id: delegate
159
160 required property T.Action modelData
161
162 width: parent.width
163
164 KP.ContextDrawerActionItem {
165 tAction: delegate.modelData
166 width: parent.width
167 }
168
169 Repeater {
170 model: delegate.modelData instanceof Kirigami.Action && delegate.modelData.expandible
171 ? delegate.modelData.children : null
172
173 delegate: KP.ContextDrawerActionItem {
174 width: parent.width
175 leftPadding: Kirigami.Units.gridUnit
176 opacity: !root.collapsed
177 }
178 }
179 }
180 }
181 }
182}
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.