File: gridNavigator.js

package info (click to toggle)
cinnamon 6.4.13-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,304 kB
  • sloc: javascript: 54,298; ansic: 51,499; python: 21,971; xml: 2,803; sh: 96; makefile: 27; perl: 13
file content (71 lines) | stat: -rw-r--r-- 2,497 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Lang = imports.lang;
const Clutter = imports.gi.Clutter;

function nextIndex(itemCount, numCols, currentIndex, symbol) {
    let result = -1;
    if (itemCount > 3 // grid navigation is not suited for a low item count
        && (symbol === Clutter.KEY_Down || symbol === Clutter.KEY_Up))
    {
        let numRows = Math.ceil(itemCount/numCols);

        let curRow = Math.floor(currentIndex/numCols);
        let curCol = currentIndex % numCols;

        let numColsLastRow = itemCount % numCols || numCols;
        let lastRowColStart = Math.floor((numCols - numColsLastRow) / 2);

        if (symbol === Clutter.KEY_Down) {
            if (curRow < numRows - 2) {
                return (curRow + 1) * numCols + curCol;
            }

            if (curRow === numRows - 1) {
                let actualCurCol = curCol + lastRowColStart;
                return (actualCurCol < numCols - 1) ? actualCurCol + 1 : 0;
            }

            if (curCol >= lastRowColStart && curCol < lastRowColStart + numColsLastRow) {
                return (curRow + 1) * numCols + curCol - lastRowColStart;
            }

            return (curCol < numCols - 1) ? curCol + 1 : 0;
        }
        else {
            if (numRows === 1) {
                return (curCol > 0) ? curCol - 1 : itemCount - 1;
            }

            if (curRow > 0 && curRow < numRows - 1) {
                return (curRow - 1) * numCols + curCol;
            }

            if (curRow === numRows - 1) {
                return (curRow - 1) * numCols + curCol + lastRowColStart;
            }

            if (curCol === 0) {
                return (numColsLastRow === numCols) ? itemCount - 1 : (numRows - 1) * numCols - 1;
            }

            if (curCol >= lastRowColStart + 1 && curCol < lastRowColStart + numColsLastRow + 1) {
                return (numRows - 1) * numCols + curCol - 1 - lastRowColStart;
            }

            return (numRows - 2) * numCols + curCol - 1;
        }
    }
    else if (symbol === Clutter.KEY_Left || symbol === Clutter.KEY_Down) {
        result = (currentIndex < 1 ? itemCount : currentIndex) - 1;
    }
    else if (symbol === Clutter.KEY_Right || symbol === Clutter.KEY_Up) {
        result = (currentIndex + 1) % itemCount;
    }
    else if (symbol === Clutter.KEY_Home) {
        result = 0;
    }
    else if (symbol === Clutter.KEY_End) {
        result = itemCount - 1;
    }
    return result;
}