Kirigami2

ContextDrawer.qml
1 /*
2  * SPDX-FileCopyrightText: 2015 Marco Martin <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 import QtQuick 2.1
8 import QtQuick.Controls 2.2 as QQC2
9 import org.kde.kirigami 2.4 as Kirigami
10 import "private" as P
11 
12 /**
13  * A specialized type of drawer that will show a list of actions
14  * relevant to the application's current page.
15  *
16  * Example usage:
17  * @code{.qml}
18  * import org.kde.kirigami 2.4 as Kirigami
19  *
20  * Kirigami.ApplicationWindow {
21  * [...]
22  * contextDrawer: Kirigami.ContextDrawer {
23  * id: contextDrawer
24  * }
25  * [...]
26  * }
27  * @endcode
28  *
29  * @code{.qml}
30  * import org.kde.kirigami 2.4 as Kirigami
31  *
32  * Kirigami.Page {
33  * [...]
34  * actions.contextualActions: [
35  * Kirigami.Action {
36  * icon.name: "edit"
37  * text: "Action text"
38  * onTriggered: {
39  * // do stuff
40  * }
41  * },
42  * Kirigami.Action {
43  * icon.name: "edit"
44  * text: "Action text"
45  * onTriggered: {
46  * // do stuff
47  * }
48  * }
49  * ]
50  * [...]
51  * }
52  * @endcode
53  * @see <a href="https://develop.kde.org/hig/components/navigation/contextdrawer">Human Interface Guidelines on Context Drawers</a>
54  * @see <a href="https://develop.kde.org/hig/patterns-command/drawer/#context-drawer">KDE Human Interface Guidelines' Short Introduction of Context Drawers</a>
55  * @inherit OverlayDrawer
56  */
57 Kirigami.OverlayDrawer {
58  id: root
59  handleClosedIcon.source: null
60  handleOpenIcon.source: null
61 
62  /**
63  * @brief A title for the action list that will be shown to the user when opens the drawer
64  *
65  * default: ``qsTr("Actions")``
66  */
67  property string title: qsTr("Actions")
68 
69  /**
70  * This can be any type of object that a ListView can accept as model.
71  * It expects items compatible with either QtQuick.Controls.Action or
72  * Kirigami.Action.
73  *
74  * @see QtQuick.Controls.Action
75  * @see kirigami::Action
76  * @property list<Action> actions
77  */
78  property var actions: page ? page.contextualActions : []
79 
80  /**
81  * @brief Arbitrary content to show above the list view.
82  *
83  * default: `an Item containing a Kirigami.Heading that displays a title whose text is
84  * controlled by the title property.`
85  *
86  * @property Component header
87  * @since org.kde.kirigami 2.7
88  */
89  property alias header: menu.header
90 
91  /**
92  * @brief Arbitrary content to show below the list view.
93  * @property Component footer
94  * @since org.kde.kirigami 2.7
95  */
96  property alias footer: menu.footer
97 
98  property Page page: {
99  if (applicationWindow().pageStack.layers && applicationWindow().pageStack.layers.depth > 1 && applicationWindow().pageStack.layers.currentItem.hasOwnProperty("contextualActions")) {
100  return applicationWindow().pageStack.layers.currentItem;
101  }
102  else if ((applicationWindow().pageStack.currentItem || {}).hasOwnProperty("contextualActions")) {
103  return applicationWindow().pageStack.currentItem;
104  }
105  else {
106  return applicationWindow().pageStack.lastVisibleItem;
107  }
108  }
109 
110  // Disable for empty menus or when we have a global toolbar
111  enabled: menu.count > 0 &&
112  (typeof applicationWindow() === "undefined" || !applicationWindow().pageStack.globalToolBar ||
113  (applicationWindow().pageStack.lastVisibleItem && applicationWindow().pageStack.lastVisibleItem.globalToolBarStyle !== Kirigami.ApplicationHeaderStyle.ToolBar) ||
114  (applicationWindow().pageStack.layers && applicationWindow().pageStack.layers.depth > 1 && applicationWindow().pageStack.layers.currentItem && applicationWindow().pageStack.layers.currentItem.globalToolBarStyle !== Kirigami.ApplicationHeaderStyle.ToolBar))
115  edge: Qt.application.layoutDirection === Qt.RightToLeft ? Qt.LeftEdge : Qt.RightEdge
116  drawerOpen: false
117 
118  // list items go to edges, have their own padding
119  topPadding: 0
120  leftPadding: 0
121  rightPadding: 0
122  bottomPadding: 0
123 
124  handleVisible: applicationWindow === undefined ? false : applicationWindow().controlsVisible
125 
126  onPeekingChanged: {
127  if (page) {
128  page.contextualActionsAboutToShow();
129  }
130  }
131  contentItem: QQC2.ScrollView {
132  // this just to create the attached property
133  Kirigami.Theme.inherit: true
134  implicitWidth: Kirigami.Units.gridUnit * 20
135  ListView {
136  id: menu
137  interactive: contentHeight > height
138  model: {
139  if (typeof root.actions === "undefined") {
140  return null;
141  }
142  if (root.actions.length === 0) {
143  return null;
144  } else {
145 
146  // Check if at least one action is visible
147  let somethingVisible = false;
148  for (let i = 0; i < root.actions.length; i++) {
149  if (root.actions[i].visible) {
150  somethingVisible = true;
151  break;
152  }
153  }
154 
155  if (!somethingVisible) {
156  return null;
157  }
158 
159  return root.actions[0].text !== undefined &&
160  root.actions[0].trigger !== undefined ?
161  root.actions :
162  root.actions[0];
163  }
164  }
165  topMargin: root.handle.y > 0 ? menu.height - menu.contentHeight : 0
166  header: Item {
167  height: heading.height
168  width: menu.width
169  Kirigami.Heading {
170  id: heading
171  anchors {
172  left: parent.left
173  right: parent.right
174  margins: Kirigami.Units.largeSpacing
175  }
176  elide: Text.ElideRight
177  level: 2
178  text: root.title
179  }
180  }
181  delegate: Column {
182  width: parent.width
183  P.ContextDrawerActionItem {
184  width: parent.width
185  }
186  Repeater {
187  model: modelData.hasOwnProperty("expandible") && modelData.expandible ? modelData.children : null
188  delegate: P.ContextDrawerActionItem {
189  width: parent.width
190  leftPadding: Kirigami.Units.largeSpacing * 2
191  opacity: !root.collapsed
192  }
193  }
194  }
195  }
196  }
197 }
QTextStream & right(QTextStream &stream)
QTextStream & left(QTextStream &stream)
QStringView level(QStringView ifopt)
QTextStream & left(QTextStream &s)
QTextStream & right(QTextStream &s)
Page is a container for all the app pages: everything pushed to the pageStack should be a Page.
Definition: Page.qml:19
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Nov 28 2023 04:08:25 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.