2 * Copyright 2013-2016 Canonical Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18import Lomiri.Components 1.3
19import Lomiri.Components.ListItems 1.3 as ListItems
20import Lomiri.Indicators 0.1 as Indicators
27 property var submenuIndex: undefined
28 property QtObject menuModel: null
29 property Component factory
33 target: root.menuModel
34 property bool ready: false
36 // fix async creation with signal from model before it's finished.
38 if (submenuIndex !== undefined && first <= submenuIndex) {
43 if (submenuIndex !== undefined && first <= submenuIndex) {
48 if (root.submenuIndex !== undefined) {
54 Component.onCompleted: {
56 dynamicChanges.ready = true;
59 function reset(clearModel) {
62 var model = root.submenuIndex == undefined ? menuModel : menuModel.submenu(root.submenuIndex)
64 push(pageComponent, { "menuModel": model });
66 } else if (root.currentPage) {
67 root.currentPage.reset();
76 property alias menuModel: listView.model
77 property alias title: backLabel.title
78 property bool isSubmenu: false
81 listView.positionViewAtBeginning();
84 property QtObject factory: root.factory.createObject(page, { menuModel: page.menuModel } )
88 visible: page.isSubmenu
89 leadingActionBar.actions: [
102 objectName: "listView"
105 top: page.isSubmenu ? backLabel.bottom : parent.top
108 bottom: parent.bottom
109 bottomMargin: Qt.inputMethod.visible ? (Qt.inputMethod.keyboardRectangle.height - root.anchors.bottomMargin) : 0
111 Behavior on bottomMargin {
114 easing.type: Easing.OutQuad
117 // TODO - does ever frame.
118 onBottomMarginChanged: {
119 listView.positionViewAtIndex(listView.currentIndex, ListView.End)
123 // Don't load all the delegates (only max of 3 pages worth -1/0/+1)
124 cacheBuffer: Math.max(height * 3, units.gu(70))
126 // Only allow flicking if the content doesn't fit on the page
127 interactive: contentHeight > height
129 property int selectedIndex: -1
130 property bool blockCurrentIndexChange: false
133 if (count == 0 && selectedIndex != -1) {
137 // for highlight following
138 onSelectedIndexChanged: {
139 if (currentIndex != selectedIndex) {
140 var blocked = blockCurrentIndexChange;
141 blockCurrentIndexChange = true;
143 currentIndex = selectedIndex;
145 blockCurrentIndexChange = blocked;
148 // for item addition/removal
149 onCurrentIndexChanged: {
150 if (!blockCurrentIndexChange) {
151 if (selectedIndex != -1 && selectedIndex != currentIndex) {
152 selectedIndex = currentIndex;
158 target: listView.model ? listView.model : null
159 onRowsAboutToBeRemoved: {
160 // track current item deletion.
161 if (listView.selectedIndex >= first && listView.selectedIndex <= last) {
162 listView.selectedIndex = -1;
169 objectName: "menuItem" + index
170 width: ListView.view.width
171 visible: status == Loader.Ready
173 property int modelIndex: index
174 sourceComponent: page.factory.load(model)
177 if (item.hasOwnProperty("selected")) {
178 item.selected = listView.selectedIndex == index;
180 if (item.hasOwnProperty("menuSelected")) {
181 item.menuSelected.connect(function() { listView.selectedIndex = index; });
183 if (item.hasOwnProperty("menuDeselected")) {
184 item.menuDeselected.connect(function() { listView.selectedIndex = -1; });
186 if (item.hasOwnProperty("menuData")) {
187 item.menuData = Qt.binding(function() { return model; });
189 if (item.hasOwnProperty("menuIndex")) {
190 item.menuIndex = Qt.binding(function() { return modelIndex; });
192 if (item.hasOwnProperty("clicked")) {
193 item.clicked.connect(function() {
194 if (model.hasSubmenu) {
195 page.menuModel.aboutToShow(modelIndex);
196 root.push(pageComponent, {
198 "title": model.label.replace(/_|&/, ""),
199 "menuModel": page.menuModel.submenu(modelIndex)
207 target: item ? item : null
208 property: "objectName"
212 // TODO: Fixes lp#1243146
213 // This is a workaround for a Qt bug. https://bugreports.qt-project.org/browse/QTBUG-34351
216 onSelectedIndexChanged: {
217 if (loader.item && loader.item.hasOwnProperty("selected")) {
218 loader.item.selected = listView.selectedIndex == index;