forked from ch1bo/garmin-otp-authenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.mc
114 lines (94 loc) · 2.54 KB
/
Menu.mc
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Classes to provide a compatibility layer with a Menu2-like API also on
// ConnectIQ 2.x, but with the size limitation of 16 items, no support for
// sublabels etc. and the "old" callback API in the delegates.
using Toybox.WatchUi;
module Menu {
(:connectiq2)
function switchTo(menu, delegate, transition) {
// On CIQ 2.x native menus can't be switchViewTo'ed
WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
WatchUi.pushView(menu, delegate, transition);
}
(:connectiq3)
function switchTo(menu, delegate, transition) {
WatchUi.switchToView(menu, delegate, transition);
}
(:connectiq2)
class MenuView extends WatchUi.Menu {
private var nItems;
function initialize(options) {
WatchUi.Menu.initialize();
if (options != null) {
var title = options.get(:title);
if (title != null) {
self.setTitle(title);
}
// :focus is ignored
}
self.nItems = 0;
}
function addItem(item) {
self.nItems++;
if (self.nItems > WatchUi.Menu.MAX_SIZE) {
logf(WARN, "Menu item overflow ($1$ > $2$), truncating items", [self.nItems, WatchUi.Menu.MAX_SIZE]);
} else {
WatchUi.Menu.addItem(item.getLabel(), item.getId());
}
}
}
(:connectiq3)
class MenuView extends WatchUi.Menu2 {
function initialize(options) {
WatchUi.Menu2.initialize(options);
}
}
(:connectiq2)
// Polyfilled menu item which does ignore sublabel and options
class MenuItem {
private var label, identifier;
function initialize(label, sublabel, identifier, options) {
self.label = label;
self.identifier = identifier;
}
function getId() {
return self.identifier;
}
function getLabel() {
return self.label;
}
}
(:connectiq3)
class MenuItem extends WatchUi.MenuItem {
function initialize(label, sublabel, identifier, options) {
WatchUi.MenuItem.initialize(label, sublabel, identifier, options);
}
}
(:connectiq2)
class MenuDelegate extends WatchUi.MenuInputDelegate {
function initialize() {
WatchUi.MenuInputDelegate.initialize();
}
function onMenuItem(id) {
// Keep the "old" onMenuItem interface as we can't easily resolve id -> item
// on ConnectIQ 2.x
}
}
(:connectiq3)
class MenuDelegate extends WatchUi.Menu2InputDelegate {
function initialize() {
WatchUi.Menu2InputDelegate.initialize();
}
// Never wrap
function onWrap(key) {
return false;
}
function onSelect(item) {
if (self.onMenuItem(item.getId()) != true) {
WatchUi.popView(WatchUi.SLIDE_IMMEDIATE);
}
}
function onMenuItem(identifier) {
// Provide the "old" onMenuItem interface
}
}
}