-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.vala
90 lines (73 loc) · 1.64 KB
/
menu.vala
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
using Curses;
using Posix;
public class Demo {
public MainLoop loop;
private IOChannel io_channel;
internal IOSource io;
public Demo() {
loop = new MainLoop();
io_channel = new IOChannel.unix_new(Posix.STDIN_FILENO);
io = new IOSource(io_channel, IOCondition.IN);
io.attach(loop.get_context());
}
public void start() {
initscr();
noecho();
stdscr.keypad(true);
}
public void stop() {
endwin();
}
private string[] choices = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
private Menu menu;
private MenuItem[] menu_items = {};
public void activate() {
// passing unowned choice here is critical,
// as vala will free(choice) makig menu render
// blank items
foreach (unowned string choice in choices) {
menu_items += new MenuItem(choice, choice);
}
// no need to add last null item to menu items (as new Menu() requires)
// in vala arrays are null-terminated by default
menu = new Menu(menu_items);
menu.post();
refresh();
io.set_callback(() => {
var c = getch();
switch (c) {
case Key.DOWN:
menu.driver(MenuRequest.DOWN_ITEM);
break;
case Key.UP:
menu.driver(MenuRequest.UP_ITEM);
break;
default:
break;
}
refresh();
return Source.CONTINUE;
});
}
public void run() {
loop.run();
}
public void redraw() {
refresh();
}
static int main(string[] args) {
var app = new Demo();
app.start();
app.activate();
app.redraw();
app.run();
app.stop();
return EXIT_SUCCESS;
}
}