aboutsummaryrefslogtreecommitdiff
path: root/source/ui/ListDelegate.mc
blob: a7c8bf0dfaae57c5bccb2e06f0a0bb1045c25124 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Author:  cirakg & claude ai
// Summary: Input on the list (README 13.4): scroll with UP/DOWN or swipes, open with START or a tap, menu (MENU or long press), back exits.

import Toybox.Lang;
import Toybox.WatchUi;

class ListDelegate extends WatchUi.BehaviorDelegate {

    private var _view as ListView;

    function initialize(view as ListView) {
        BehaviorDelegate.initialize();
        _view = view;
    }

    function onNextPage() as Boolean {
        AppState.activity();
        _view.move(1);
        return true;
    }

    function onPreviousPage() as Boolean {
        AppState.activity();
        _view.move(-1);
        return true;
    }

    // On touch screens a tap arrives as the select behaviour first; returning
    // false passes it on to onTap, which opens the row under the finger. The
    // START button then comes through onKey.
    function onSelect() as Boolean {
        if (Ui.isTouch()) {
            return false;
        }
        openFocused();
        return true;
    }

    function onKey(event as WatchUi.KeyEvent) as Boolean {
        if (Ui.isTouch() && event.getKey() == WatchUi.KEY_ENTER) {
            openFocused();
            return true;
        }
        return false;
    }

    private function openFocused() as Void {
        AppState.activity();
        var id = _view.focusedId();
        if (id != null) {
            Nav.openEntry(id);
        }
    }

    // A tap opens the row under the finger.
    function onTap(event as WatchUi.ClickEvent) as Boolean {
        AppState.activity();
        var index = _view.rowAt(event.getCoordinates()[1]);
        if (index < 0) {
            return true;
        }
        _view.focus = index;
        var id = _view.focusedId();
        if (id != null) {
            Nav.openEntry(id);
        }
        return true;
    }

    // A long press opens the menu too: watches without a MENU button
    // (Venu X1) have no other way to reach it (README 13.4).
    function onHold(event as WatchUi.ClickEvent) as Boolean {
        AppState.activity();
        Nav.openMenu();
        return true;
    }

    function onMenu() as Boolean {
        AppState.activity();
        Nav.openMenu();
        return true;
    }

    // BACK leaves the app; onStop locks everything.
    function onBack() as Boolean {
        return false;
    }
}