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
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
30  * import org.kde.kirigami 2.4 as Kirigami
31  *
32  * Kirigami.Page {
33  * [...]
34  * 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  *
54  * @inherit OverlayDrawer
55  */
56 Kirigami.OverlayDrawer {
57  id: root
58  handleClosedIcon.source: null
59  handleOpenIcon.source: null
60 
61  /**
62  * @brief A title for the action list that will be shown to the user when opens the drawer
63  *
64  * default: ``qsTr("Actions")``
65  */
66  property string title: qsTr("Actions")
67 
68  /**
69  * This can be any type of object that a ListView can accept as model.
70  * It expects items compatible with either QtQuick.Action or Kirigami.Action
71  *
72  * @see QtQuick.Action
73  * @see org::kde::kirigami::Action
74  * @property list<Action> actions
75  */
76  property var actions: page ? page.contextualActions : []
77 
78  /**
79  * @brief Arbitrary content to show above the list view.
80  *
81  * default: `an Item containing a Kirigami.Heading that displays a title whose text is
82  * controlled by the title property.`
83  *
84  * @property Component header
85  * @since 2.7
86  */
87  property alias header: menu.header
88 
89  /**
90  * @brief Arbitrary content to show below the list view.
91  * @property Component footer
92  * @since 2.7
93  */
94  property alias footer: menu.footer
95 
96  property Page page: {
97  if (applicationWindow().pageStack.layers && applicationWindow().pageStack.layers.depth > 1 && applicationWindow().pageStack.layers.currentItem.hasOwnProperty("contextualActions")) {
98  return applicationWindow().pageStack.layers.currentItem;
99  }
100  else if ((applicationWindow().pageStack.currentItem || {}).hasOwnProperty("contextualActions")) {
101  return applicationWindow().pageStack.currentItem;
102  }
103  else {
104  return applicationWindow().pageStack.lastVisibleItem;
105  }
106  }
107 
108  // Disable for empty menus or when we have a global toolbar
109  enabled: menu.count > 0 &&
110  (typeof applicationWindow() === "undefined" || !applicationWindow().pageStack.globalToolBar ||
111  (applicationWindow().pageStack.lastVisibleItem && applicationWindow().pageStack.lastVisibleItem.globalToolBarStyle !== Kirigami.ApplicationHeaderStyle.ToolBar) ||
112  (applicationWindow().pageStack.layers && applicationWindow().pageStack.layers.depth > 1 && applicationWindow().pageStack.layers.currentItem && applicationWindow().pageStack.layers.currentItem.globalToolBarStyle !== Kirigami.ApplicationHeaderStyle.ToolBar))
113  edge: Qt.application.layoutDirection === Qt.RightToLeft ? Qt.LeftEdge : Qt.RightEdge
114  drawerOpen: false
115 
116  // list items go to edges, have their own padding
117  leftPadding: 0
118  rightPadding: 0
119  bottomPadding: 0
120 
121  handleVisible: applicationWindow === undefined ? false : applicationWindow().controlsVisible
122 
123  onPeekingChanged: {
124  if (page) {
125  page.contextualActionsAboutToShow();
126  }
127  }
128  contentItem: QQC2.ScrollView {
129  // this just to create the attached property
130  Kirigami.Theme.inherit: true
131  implicitWidth: Kirigami.Units.gridUnit * 20
132  ListView {
133  id: menu
134  interactive: contentHeight > height
135  model: {
136  if (typeof root.actions === "undefined") {
137  return null;
138  }
139  if (root.actions.length === 0) {
140  return null;
141  } else {
142 
143  // Check if at least one action is visible
144  let somethingVisible = false;
145  for (let i = 0; i < root.actions.length; i++) {
146  if (root.actions[i].visible) {
147  somethingVisible = true;
148  break;
149  }
150  }
151 
152  if (!somethingVisible) {
153  return null;
154  }
155 
156  return root.actions[0].text !== undefined &&
157  root.actions[0].trigger !== undefined ?
158  root.actions :
159  root.actions[0];
160  }
161  }
162  topMargin: root.handle.y > 0 ? menu.height - menu.contentHeight : 0
163  header: Item {
164  height: heading.height
165  width: menu.width
166  Kirigami.Heading {
167  id: heading
168  anchors {
169  left: parent.left
170  right: parent.right
171  margins: Kirigami.Units.largeSpacing
172  }
173  elide: Text.ElideRight
174  level: 2
175  text: root.title
176  }
177  }
178  delegate: Column {
179  width: parent.width
180  P.ContextDrawerActionItem {
181  width: parent.width
182  }
183  Repeater {
184  model: modelData.hasOwnProperty("expandible") && modelData.expandible ? modelData.children : null
185  delegate: P.ContextDrawerActionItem {
186  width: parent.width
187  leftPadding: Kirigami.Units.largeSpacing * 2
188  opacity: !root.collapsed
189  }
190  }
191  }
192  }
193  }
194 }
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 ApplicationWindow's pageStack sho...
Definition: Page.qml:20
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.