aboutsummaryrefslogtreecommitdiff
path: root/example/simple-client-vala.vala
blob: 8d661cede57774fe652ee3d5535e7de90e1504ef (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
A small piece of sample code demonstrating a very simple application
with an indicator.

Copyright 2011 Canonical Ltd.

Authors:
    Marco Trevisan <mail@3v1n0.net>

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License version 3, as published 
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranties of 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using Gtk;
using AppIndicator;

static int main(string[] args) {
	var sc = new SimpleClient(args);
	sc.run();
	return 0;
}

class SimpleClient {
	Menu menu;
	Indicator ci;
	int percentage;
	bool active;
	bool can_haz_label;

	public SimpleClient(string[] args) {
		Gtk.init(ref args);

		ci = new Indicator("example-simple-client",
                           "indicator-messages",
                           IndicatorCategory.APPLICATION_STATUS);

		ci.set_status(IndicatorStatus.ACTIVE);
		ci.set_attention_icon("indicator-messages-new");
		ci.set_label("1%", "100%");

		active = true;
		can_haz_label = true;
	}

	private void toggle_sensitivity(Widget widget) {
		widget.set_sensitive(!widget.is_sensitive());
	}

	private void append_submenu(MenuItem item) {
		var menu = new Menu();
		MenuItem mi;

		mi = new MenuItem.with_label("Sub 1");
		menu.append(mi);
		mi.activate.connect(() => { print("Sub1\n"); });

		MenuItem prev_mi = mi;
		mi = new MenuItem.with_label("Sub 2");
		menu.append(mi);
		mi.activate.connect(() => { toggle_sensitivity(prev_mi); });

		mi = new MenuItem.with_label("Sub 3");
		menu.append(mi);
		mi.activate.connect(() => { print("Sub3\n"); });

		mi = new MenuItem.with_label("Toggle Attention");
		menu.append(mi);
		mi.activate.connect(() => {
			if (ci.get_status() == IndicatorStatus.ATTENTION)
				ci.set_status(IndicatorStatus.ACTIVE);
			else
				ci.set_status(IndicatorStatus.ATTENTION);
		});

		ci.set_secondary_activate_target(mi);

		menu.show_all();
		item.set_submenu(menu);
	}

	private void label_toggle(MenuItem item) {
		can_haz_label = !can_haz_label;

		if (can_haz_label) {
			item.set_label("Hide label");
		} else {
			item.set_label("Show label");
		}
	}

	public void run() {

		ci.scroll_event.connect((delta, direction) => {
			print(@"Got scroll event! delta: $delta, direction: $direction\n");
		});

		Timeout.add_seconds(1, () => {
			percentage = (percentage + 1) % 100;
			if (can_haz_label) {
				ci.set_label(@"$(percentage+1)%", "");
			} else {
				ci.set_label("", "");
			}
			return true;
		});

		menu = new Menu();
		var chk = new CheckMenuItem.with_label("1");
		chk.activate.connect(() => { print("1\n"); });
		menu.append(chk);
		chk.show();

		var radio = new RadioMenuItem.with_label(new SList<RadioMenuItem>(), "2");
		radio.activate.connect(() => { print("2\n"); });
		menu.append(radio);
		radio.show();

		var submenu = new MenuItem.with_label("3");
		menu.append(submenu);
		append_submenu(submenu);
		submenu.show();

		var toggle_item = new MenuItem.with_label("Toggle 3");
		toggle_item.activate.connect(() => { toggle_sensitivity(submenu); });
		menu.append(toggle_item);
		toggle_item.show();

		var imgitem = new ImageMenuItem.from_stock(Stock.NEW, null);
		imgitem.activate.connect(() => {
			Image img = (Image) imgitem.get_image();
			img.set_from_stock(Stock.OPEN, IconSize.MENU);
		});
		menu.append(imgitem);
		imgitem.show();

		var att = new MenuItem.with_label("Get Attention");
		att.activate.connect(() => {
			if (active) {
				ci.set_status(IndicatorStatus.ATTENTION);
				att.set_label("I'm okay now");
				active = false;
			} else {
				ci.set_status(IndicatorStatus.ACTIVE);
				att.set_label("Get Attention");
				active = false;
			}
		});
		menu.append(att);
		att.show();

		var show = new MenuItem.with_label("Show Label");
		label_toggle(show);
		show.activate.connect(() => { label_toggle(show); });
		menu.append(show);
		show.show();

		var icon = new CheckMenuItem.with_label("Set Local Icon");
		icon.activate.connect(() => {
			if (icon.get_active()) {
				ci.set_icon("simple-client-test-icon.png");
			} else {
				ci.set_icon("indicator-messages");
			}
		});
		menu.append(icon);
		icon.show();

		ci.set_menu(menu);

		Gtk.main();
	}
}