This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
forked from eth-p/wingpanel-indicator-ayatana
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAyatanaIndicator.vala
427 lines (355 loc) · 13.8 KB
/
AyatanaIndicator.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* Copyright (c) 2011-2015 Wingpanel Developers (http://launchpad.net/wingpanel)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
public class AyatanaCompatibility.Indicator : Wingpanel.Indicator {
private IndicatorButton icon;
private Gtk.Stack main_stack;
private Gtk.ListBox main_list;
private unowned IndicatorAyatana.ObjectEntry entry;
private unowned IndicatorAyatana.Object parent_object;
private IndicatorIface indicator;
private string entry_name_hint;
//maps to help dynamic changes in menus and submenus
private Gee.HashMap<Gtk.Widget, Gtk.Widget> menu_map;
private Gee.HashMap<Gtk.Widget, Gtk.Widget> submenu_map;
const int MAX_ICON_SIZE = 20;
//grouping radio buttons
private Gtk.RadioButton? group_radio=null ;
public Indicator (IndicatorAyatana.ObjectEntry entry, IndicatorAyatana.Object obj, IndicatorIface indicator) {
string name_hint = entry.name_hint;
if (name_hint == null) {
var current_time = new DateTime.now_local ();
name_hint = current_time.hash ().to_string ();
}
Object (code_name: "%s%s".printf ("ayatana-", name_hint));
this.entry = entry;
this.indicator = indicator;
this.parent_object = obj;
this.menu_map = new Gee.HashMap<Gtk.Widget, Gtk.Widget> ();
entry_name_hint = name_hint;
if (entry.menu == null) {
critical ("Indicator: %s has no menu widget.", entry_name_hint);
return;
}
/*
* Workaround for buggy indicators: this menu may still be part of
* another panel entry which hasn't been destroyed yet. Those indicators
* trigger entry-removed after entry-added, which means that the previous
* parent is still in the panel when the new one is added.
*/
if (entry.menu.get_attach_widget () != null) {
entry.menu.detach ();
}
this.visible = true;
}
public override Gtk.Widget get_display_widget () {
//show an icon in the panel
if (icon == null) {
icon = new IndicatorButton ();
var image = entry.image as Gtk.Image;
if (image != null) {
/*
* images holding pixbufs are quite frequently way too large, so we whenever a pixbuf
* is assigned to an image we need to check whether this pixbuf is within reasonable size
*/
if (image.storage_type == Gtk.ImageType.PIXBUF) {
image.notify["pixbuf"].connect (() => {
ensure_max_size (image);
});
ensure_max_size (image);
}
image.pixel_size = MAX_ICON_SIZE;
icon.set_widget (IndicatorButton.WidgetSlot.IMAGE, image);
}
var label = entry.label;
if (label != null && label is Gtk.Label) {
icon.set_widget (IndicatorButton.WidgetSlot.LABEL, label);
}
icon.scroll_event.connect (on_scroll);
icon.button_press_event.connect (on_button_press);
}
return icon;
}
public string name_hint () {
return entry_name_hint;
}
public bool on_button_press (Gdk.EventButton event) {
if (event.button == Gdk.BUTTON_MIDDLE) {
parent_object.secondary_activate (entry, event.time);
return Gdk.EVENT_STOP;
}
return Gdk.EVENT_PROPAGATE;
}
public bool on_scroll (Gdk.EventScroll event) {
parent_object.entry_scrolled (entry, 1, (IndicatorAyatana.ScrollDirection)event.direction);
return Gdk.EVENT_PROPAGATE;
}
public override Gtk.Widget? get_widget () {
if (main_stack == null) {
bool reloaded = false;
icon.parent.parent.enter_notify_event.connect ((w, e) => {
if (!reloaded && e.mode != Gdk.CrossingMode.TOUCH_BEGIN) {
/*
* workaround for indicators (e.g. dropbox) that only update menu children after
* the menu is popuped
*/
reloaded = true;
//show underlying menu (debug)
//entry.menu.popup_at_widget(icon.parent,0,0);
//entry.menu.popdown ();
}
return Gdk.EVENT_PROPAGATE;
});
main_stack = new Gtk.Stack ();
main_stack.map.connect (() => {
//reload: open first on main_list
main_stack.set_visible_child (main_list);
reloaded = false;
});
main_list = new Gtk.ListBox();
main_list.set_size_request(230,-1);
main_stack.add (main_list);
foreach (var item in entry.menu.get_children ()) {
on_menu_widget_insert (item);
}
entry.menu.insert.connect (on_menu_widget_insert);
entry.menu.remove.connect (on_menu_widget_remove);
}
return main_stack;
}
private void on_menu_widget_insert (Gtk.Widget item) {
var w = convert_menu_widget (item);
if (w != null) {
menu_map.set (item, w);
main_list.add (w);
/* menuitem not visible */
if (!item.get_visible ()) {
w.no_show_all = true;
w.hide ();
} else {
w.show ();
}
}
}
private void on_menu_widget_remove (Gtk.Widget item) {
var w = menu_map.get (item);
if (w != null) {
main_list.remove (w);
menu_map.unset (item);
}
}
private Gtk.Image? check_for_image (Gtk.Container container) {
foreach (var c in container.get_children ()) {
if (c is Gtk.Image) {
return (c as Gtk.Image);
} else if (c is Gtk.Container) {
return check_for_image ((c as Gtk.Container));
}
}
return null;
}
private void connect_signals (Gtk.Widget item, Gtk.Widget button) {
item.show.connect (() => {
button.no_show_all = false;
button.show ();
});
item.hide.connect (() => {
button.no_show_all = true;
button.hide ();
});
item.state_flags_changed.connect ((type) => {
button.set_state_flags (item.get_state_flags (),true);
});
}
/* convert the menuitems to widgets that can be shown in popovers */
private Gtk.Widget? convert_menu_widget (Gtk.Widget item) {
/* separator are GTK.SeparatorMenuItem, return a separator */
if (item is Gtk.SeparatorMenuItem) {
var separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
connect_signals (item, separator);
group_radio = null;
return separator;
}
/* all other items are genericmenuitems */
string label = ((Gtk.MenuItem)item).get_label ();
label = label.replace ("_", "");
/*
* get item type from atk accessibility
* 34 = MENU_ITEM 8 = CHECKBOX 32 = SUBMENU 44 = RADIO
*/
const int ATK_CHECKBOX =8;
const int ATK_RADIO =44;
var atk = item.get_accessible ();
Value val = Value (typeof (int));
atk.get_property ("accessible_role", ref val);
var item_type = val.get_int ();
var state = item.get_state_flags ();
//RAZ group_radio
group_radio = ( item_type == ATK_RADIO)? group_radio:null;
/* detect if it has a image */
Gtk.Image? image = null;
var child = ((Gtk.Bin)item).get_child ();
if (child != null) {
if (child is Gtk.Image) {
image = (child as Gtk.Image);
} else if (child is Gtk.Container) {
image = check_for_image ((child as Gtk.Container));
}
}
if (item_type == ATK_CHECKBOX) {
var box_switch = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 5);
var lbl = new Gtk.Label (label);
var button = new Gtk.Switch ();
box_switch.pack_start (lbl, true, true, 5);
box_switch.pack_end (button, false, false, 5);
lbl.set_halign (Gtk.Align.START);
lbl.margin_start = 6;
//init
var active= ((Gtk.CheckMenuItem)item).get_active ();
button.set_active(active);
button.state_set.connect ((b) => {
((Gtk.CheckMenuItem)item).set_active (b);
return(false);
});
connect_signals (item, button);
((Gtk.CheckMenuItem)item).toggled.connect (() => {
button.active = ((Gtk.CheckMenuItem)item).get_active ();
});
return box_switch;
}
//RADIO BUTTON
if (item_type == ATK_RADIO) {
var button= new Gtk.RadioButton.with_label_from_widget(group_radio,label);
if (group_radio==null) {group_radio=button;}
button.margin = 5;
button.set_margin_start(10);
var active = ((Gtk.CheckMenuItem)item).get_active ();
button.set_active(active);
button.toggled.connect (() => {
item.activate ();
});
//concern only visible underlying menu (debug)
/* if (item.get_visible ()) {
(item as Gtk.CheckMenuItem).toggled.connect (() => {
button.active = (item as Gtk.CheckMenuItem).get_active ();
});
} */
return button;
}
/* convert menuitem to a indicatorbutton */
if (item is Gtk.MenuItem) {
Gtk.ModelButton button;
if (image != null && image.pixbuf == null && image.icon_name != null) {
try {
Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default ();
image.pixbuf = icon_theme.load_icon (image.icon_name, 16, 0);
} catch (Error e) {
warning (e.message);
}
}
button = new Gtk.ModelButton();
button.text=label;
var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 3);
if (image != null && image.pixbuf != null) {
var img= new Gtk.Image.from_pixbuf(image.pixbuf);
hbox.spacing = 6;
hbox.add(button);
hbox.add(img);
//Modelbutton = text OR icon not both
//button.icon= (image.pixbuf);
}
if (item_type == ATK_RADIO) {
button.role=Gtk.ButtonRole.RADIO;
button.active = ((Gtk.RadioMenuItem)item).get_active ();
}
((Gtk.CheckMenuItem)item).notify["label"].connect (() => {
button.text= ((Gtk.MenuItem)item).get_label ().replace ("_", "");
});
button.set_state_flags(state,true);
var submenu = ((Gtk.MenuItem)item).submenu;
if (submenu != null) {
var scroll_sub = new Gtk.ScrolledWindow (null, null);
scroll_sub.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
var sub_list = new Gtk.ListBox ();
scroll_sub.add (sub_list);
//btn back
var btn_back = new Gtk.ModelButton();
btn_back.text= _("Back");
btn_back.inverted=true;
btn_back.menu_name="main_list";
btn_back.clicked.connect(()=>{
main_stack.set_visible_child (main_list);
});
sub_list.add(btn_back);
//convert
foreach (var sub_item in submenu.get_children ()) {
var sub_menu_item = convert_menu_widget (sub_item);
connect_signals (sub_item, sub_menu_item);
sub_list.add (sub_menu_item);
}
//dynamic change at run time
submenu.insert.connect ((item) => {
var w = convert_menu_widget (item);
if (w != null) {
submenu_map.set (item, w);
sub_list.add(w);
}
});
submenu.remove.connect ((item)=> {
var w = menu_map.get (item);
if (w != null) {
sub_list.remove (w);
submenu_map.unset (item);
}
});
main_stack.add (scroll_sub);
//modelbutton for popup
button = new Gtk.ModelButton();
button.text= label;
button.menu_name="submenu";
button.clicked.connect (() => {
main_stack.set_visible_child (scroll_sub);
main_stack.show_all ();
});
} else {
button.clicked.connect (() => {
close ();
item.activate ();
});
}
connect_signals (item, button);
if ((image != null && image.pixbuf != null)) {
return hbox;}
else {return button;}
}
return null;
}
public override void opened () {
}
public override void closed () {
}
private void ensure_max_size (Gtk.Image image) {
var pixbuf = image.pixbuf;
if (pixbuf != null && pixbuf.get_height () != MAX_ICON_SIZE) {
//scale_simple(dest_width,dest_height,interp)
image.pixbuf = pixbuf.scale_simple (
(int)((double)MAX_ICON_SIZE / pixbuf.get_height () * pixbuf.get_width ()),
MAX_ICON_SIZE, Gdk.InterpType.HYPER);
}
}
}