~nick-dedekind/ubuntu-ui-toolkit/menus.ui

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/Private/1.3/MenuNavigator.qml

  • Committer: Nick Dedekind
  • Date: 2016-08-25 14:43:02 UTC
  • Revision ID: nick.dedekind@canonical.com-20160825144302-9gzc6c8y0ne0f9j7
menusĀ ui

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * Author: Nick Dedekind <nick.dedekind@canonical.com>
 
17
 */
 
18
 
 
19
import QtQuick 2.4
 
20
 
 
21
QtObject {
 
22
    id: root
 
23
    // items to navigate through
 
24
    property QtObject model
 
25
 
 
26
    signal select(int index)
 
27
 
 
28
    function selectNext(currentIndex) {
 
29
        var delegate;
 
30
        var newIndex = 0;
 
31
        if (currentIndex === -1 && model.count > 0) {
 
32
            while (model.count > newIndex) {
 
33
                delegate = model.get(newIndex);
 
34
                if (!!delegate["enabled"]) {
 
35
                    select(newIndex);
 
36
                    break;
 
37
                }
 
38
                newIndex++;
 
39
            }
 
40
        } else if (currentIndex !== -1 && model.count > 1) {
 
41
            var startIndex = (currentIndex + 1) % model.count;
 
42
            newIndex = startIndex;
 
43
            do {
 
44
                delegate = model.get(newIndex);
 
45
                if (!!delegate["enabled"]) {
 
46
                    select(newIndex);
 
47
                    break;
 
48
                }
 
49
                newIndex = (newIndex + 1) % model.count;
 
50
            } while (newIndex !== startIndex)
 
51
        }
 
52
    }
 
53
 
 
54
    function selectPrevious(currentIndex) {
 
55
        var delegate;
 
56
        var newIndex = model.count-1;
 
57
        if (currentIndex === -1 && model.count > 0) {
 
58
            while (model.count > newIndex) {
 
59
                delegate = model.get(newIndex);
 
60
                if (!!delegate["enabled"]) {
 
61
                    select(newIndex);
 
62
                    break;
 
63
                }
 
64
                newIndex--;
 
65
            }
 
66
        } else if (currentIndex !== -1 && model.count > 1) {
 
67
            var startIndex = currentIndex - 1;
 
68
            newIndex = startIndex;
 
69
            do {
 
70
                if (newIndex < 0) {
 
71
                    newIndex = model.count - 1;
 
72
                }
 
73
                delegate = model.get(newIndex);
 
74
                if (!!delegate["enabled"]) {
 
75
                    select(newIndex);
 
76
                    break;
 
77
                }
 
78
                newIndex--;
 
79
            } while (newIndex !== startIndex)
 
80
        }
 
81
    }
 
82
}
 
83