Unity 8
MenuNavigator.qml
1 /*
2  * Copyright 2016 Canonical Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16 
17 import QtQuick 2.12
18 
19 QtObject {
20  property Item itemView: null
21  property bool hasOverflow: false
22 
23  signal select(int index)
24  signal overflow()
25 
26  function selectNext(currentIndex) {
27  var menu;
28  var newIndex = 0;
29  if (currentIndex === -1 && itemView.count > 0) {
30  while (itemView.count > newIndex) {
31  menu = itemView.itemAt(newIndex);
32  if (!!menu["enabled"]) {
33  select(newIndex);
34  break;
35  }
36  newIndex++;
37 
38  if (hasOverflow && newIndex === itemView.count) {
39  overflow()
40  break;
41  }
42  }
43  } else if (currentIndex !== -1 && itemView.count > 1) {
44  var startIndex = (currentIndex + 1) % itemView.count;
45  newIndex = startIndex;
46  do {
47  menu = itemView.itemAt(newIndex);
48  if (!!menu["enabled"]) {
49  select(newIndex);
50  break;
51  }
52 
53  if (hasOverflow && newIndex + 1 === itemView.count) {
54  overflow()
55  break;
56  }
57 
58  newIndex = (newIndex + 1) % itemView.count;
59  } while (newIndex !== startIndex)
60  }
61  }
62 
63  function selectPrevious(currentIndex) {
64  var menu;
65  var newIndex = itemView.count-1;
66  if (currentIndex === -1 && itemView.count > 0) {
67  while (itemView.count > newIndex) {
68  menu = itemView.itemAt(newIndex);
69  if (!!menu["enabled"]) {
70  select(newIndex);
71  break;
72  }
73  newIndex--;
74 
75  if (hasOverflow && newIndex < 0 ) {
76  overflow();
77  break;
78  }
79  }
80  } else if (currentIndex !== -1 && itemView.count > 1) {
81  var startIndex = currentIndex - 1;
82  newIndex = startIndex;
83  do {
84  if (newIndex < 0) {
85  if (hasOverflow) {
86  overflow();
87  break;
88  }
89  newIndex = itemView.count - 1;
90  }
91  menu = itemView.itemAt(newIndex);
92  if (!!menu["enabled"]) {
93  select(newIndex);
94  break;
95  }
96  newIndex--;
97 
98  } while (newIndex !== startIndex)
99  }
100  }
101 }