~ubuntu-branches/ubuntu/trusty/gnome-shell/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/30-remoteMenu-Prevent-the-shell-from-becoming-unrespons.patch/js/ui/popupMenu.js

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-06-16 19:14:01 UTC
  • mfrom: (1.1.50) (19.1.37 experimental)
  • Revision ID: package-import@ubuntu.com-20130616191401-kef9vj3obfuvusrn
Tags: 3.8.3-1ubuntu1
* Merge with Debian. Remaining changes:
  - debian/control.in:
    + Build-depend on libsystemd-login-dev & libsystemd-daemon-dev
    + Depend on gdm instead of gdm3
  - debian/patches/40_change-pam-name-to-match-gdm.patch:
  - debian/patches/revert-suspend-break.patch:
    + Disabled, not needed on Ubuntu
  - debian/patches/ubuntu-lightdm-user-switching.patch:
    + Allow user switching when using LightDM.
  - debian/patches/ubuntu_lock_on_suspend.patch
    + Respect Ubuntu's lock-on-suspend setting.
      Disabled until it can be rewritten.
  - debian/patches/git_relock_screen_after_crash.patch:
    + Backport fix to ensure session is locked after crash
* debian/patches/revert-input-source-changes.patch:
  - Temporarily revert input source changes that need gnome-settings-daemon
    and gnome-control-center 3.8.3 to be useful
* debian/patches/revert-notification-settings-link.patch:
  - Don't link to Notification Settings since that's only available
    with gnome-control-center >= 3.8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
2
 
 
3
 
const Clutter = imports.gi.Clutter;
4
 
const GLib = imports.gi.GLib;
5
 
const Gtk = imports.gi.Gtk;
6
 
const Gio = imports.gi.Gio;
7
 
const Lang = imports.lang;
8
 
const Shell = imports.gi.Shell;
9
 
const Signals = imports.signals;
10
 
const St = imports.gi.St;
11
 
const Atk = imports.gi.Atk;
12
 
 
13
 
const BoxPointer = imports.ui.boxpointer;
14
 
const GrabHelper = imports.ui.grabHelper;
15
 
const Main = imports.ui.main;
16
 
const Params = imports.misc.params;
17
 
const Separator = imports.ui.separator;
18
 
const Tweener = imports.ui.tweener;
19
 
 
20
 
const SLIDER_SCROLL_STEP = 0.05; /* Slider scrolling step in % */
21
 
 
22
 
function _ensureStyle(actor) {
23
 
    if (actor.get_children) {
24
 
        let children = actor.get_children();
25
 
        for (let i = 0; i < children.length; i++)
26
 
            _ensureStyle(children[i]);
27
 
    }
28
 
 
29
 
    if (actor instanceof St.Widget)
30
 
        actor.ensure_style();
31
 
}
32
 
 
33
 
const PopupBaseMenuItem = new Lang.Class({
34
 
    Name: 'PopupBaseMenuItem',
35
 
 
36
 
    _init: function (params) {
37
 
        params = Params.parse (params, { reactive: true,
38
 
                                         activate: true,
39
 
                                         hover: true,
40
 
                                         sensitive: true,
41
 
                                         style_class: null,
42
 
                                         can_focus: true
43
 
                                       });
44
 
        this.actor = new Shell.GenericContainer({ style_class: 'popup-menu-item',
45
 
                                                  reactive: params.reactive,
46
 
                                                  track_hover: params.reactive,
47
 
                                                  can_focus: params.can_focus,
48
 
                                                  accessible_role: Atk.Role.MENU_ITEM});
49
 
        this.actor.connect('get-preferred-width', Lang.bind(this, this._getPreferredWidth));
50
 
        this.actor.connect('get-preferred-height', Lang.bind(this, this._getPreferredHeight));
51
 
        this.actor.connect('allocate', Lang.bind(this, this._allocate));
52
 
        this.actor.connect('style-changed', Lang.bind(this, this._onStyleChanged));
53
 
        this.actor._delegate = this;
54
 
 
55
 
        this._children = [];
56
 
        this._dot = null;
57
 
        this._columnWidths = null;
58
 
        this._spacing = 0;
59
 
        this.active = false;
60
 
        this._activatable = params.reactive && params.activate;
61
 
        this.sensitive = this._activatable && params.sensitive;
62
 
 
63
 
        this.setSensitive(this.sensitive);
64
 
 
65
 
        if (!this._activatable)
66
 
            this.actor.add_style_class_name('popup-inactive-menu-item');
67
 
 
68
 
        if (params.style_class)
69
 
            this.actor.add_style_class_name(params.style_class);
70
 
 
71
 
        if (this._activatable) {
72
 
            this.actor.connect('button-release-event', Lang.bind(this, this._onButtonReleaseEvent));
73
 
            this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
74
 
        }
75
 
        if (params.reactive && params.hover)
76
 
            this.actor.connect('notify::hover', Lang.bind(this, this._onHoverChanged));
77
 
 
78
 
        this.actor.connect('key-focus-in', Lang.bind(this, this._onKeyFocusIn));
79
 
        this.actor.connect('key-focus-out', Lang.bind(this, this._onKeyFocusOut));
80
 
    },
81
 
 
82
 
    _onStyleChanged: function (actor) {
83
 
        this._spacing = Math.round(actor.get_theme_node().get_length('spacing'));
84
 
    },
85
 
 
86
 
    _onButtonReleaseEvent: function (actor, event) {
87
 
        this.activate(event);
88
 
        return true;
89
 
    },
90
 
 
91
 
    _onKeyPressEvent: function (actor, event) {
92
 
        let symbol = event.get_key_symbol();
93
 
 
94
 
        if (symbol == Clutter.KEY_space || symbol == Clutter.KEY_Return) {
95
 
            this.activate(event);
96
 
            return true;
97
 
        }
98
 
        return false;
99
 
    },
100
 
 
101
 
    _onKeyFocusIn: function (actor) {
102
 
        this.setActive(true);
103
 
    },
104
 
 
105
 
    _onKeyFocusOut: function (actor) {
106
 
        this.setActive(false);
107
 
    },
108
 
 
109
 
    _onHoverChanged: function (actor) {
110
 
        this.setActive(actor.hover);
111
 
    },
112
 
 
113
 
    activate: function (event) {
114
 
        this.emit('activate', event);
115
 
    },
116
 
 
117
 
    setActive: function (active, params) {
118
 
        let activeChanged = active != this.active;
119
 
        params = Params.parse (params, { grabKeyboard: true });
120
 
 
121
 
        if (activeChanged) {
122
 
            this.active = active;
123
 
            if (active) {
124
 
                this.actor.add_style_pseudo_class('active');
125
 
                if (params.grabKeyboard)
126
 
                    this.actor.grab_key_focus();
127
 
            } else
128
 
                this.actor.remove_style_pseudo_class('active');
129
 
            this.emit('active-changed', active);
130
 
        }
131
 
    },
132
 
 
133
 
    setSensitive: function(sensitive) {
134
 
        if (!this._activatable)
135
 
            return;
136
 
        if (this.sensitive == sensitive)
137
 
            return;
138
 
 
139
 
        this.sensitive = sensitive;
140
 
        this.actor.reactive = sensitive;
141
 
        this.actor.can_focus = sensitive;
142
 
 
143
 
        this.emit('sensitive-changed', sensitive);
144
 
    },
145
 
 
146
 
    destroy: function() {
147
 
        this.actor.destroy();
148
 
        this.emit('destroy');
149
 
    },
150
 
 
151
 
    // adds an actor to the menu item; @params can contain %span
152
 
    // (column span; defaults to 1, -1 means "all the remaining width"),
153
 
    // %expand (defaults to #false), and %align (defaults to
154
 
    // #St.Align.START)
155
 
    addActor: function(child, params) {
156
 
        params = Params.parse(params, { span: 1,
157
 
                                        expand: false,
158
 
                                        align: St.Align.START });
159
 
        params.actor = child;
160
 
        this._children.push(params);
161
 
        this.actor.connect('destroy', Lang.bind(this, function () { this._removeChild(child); }));
162
 
        this.actor.add_actor(child);
163
 
    },
164
 
 
165
 
    _removeChild: function(child) {
166
 
        for (let i = 0; i < this._children.length; i++) {
167
 
            if (this._children[i].actor == child) {
168
 
                this._children.splice(i, 1);
169
 
                return;
170
 
            }
171
 
        }
172
 
    },
173
 
 
174
 
    removeActor: function(child) {
175
 
        this.actor.remove_actor(child);
176
 
        this._removeChild(child);
177
 
    },
178
 
 
179
 
    setShowDot: function(show) {
180
 
        if (show) {
181
 
            if (this._dot)
182
 
                return;
183
 
 
184
 
            this._dot = new St.DrawingArea({ style_class: 'popup-menu-item-dot' });
185
 
            this._dot.connect('repaint', Lang.bind(this, this._onRepaintDot));
186
 
            this.actor.add_actor(this._dot);
187
 
            this.actor.add_accessible_state (Atk.StateType.CHECKED);
188
 
        } else {
189
 
            if (!this._dot)
190
 
                return;
191
 
 
192
 
            this._dot.destroy();
193
 
            this._dot = null;
194
 
            this.actor.remove_accessible_state (Atk.StateType.CHECKED);
195
 
        }
196
 
    },
197
 
 
198
 
    _onRepaintDot: function(area) {
199
 
        let cr = area.get_context();
200
 
        let [width, height] = area.get_surface_size();
201
 
        let color = area.get_theme_node().get_foreground_color();
202
 
 
203
 
        cr.setSourceRGBA (
204
 
            color.red / 255,
205
 
            color.green / 255,
206
 
            color.blue / 255,
207
 
            color.alpha / 255);
208
 
        cr.arc(width / 2, height / 2, width / 3, 0, 2 * Math.PI);
209
 
        cr.fill();
210
 
        cr.$dispose();
211
 
    },
212
 
 
213
 
    // This returns column widths in logical order (i.e. from the dot
214
 
    // to the image), not in visual order (left to right)
215
 
    getColumnWidths: function() {
216
 
        let widths = [];
217
 
        for (let i = 0, col = 0; i < this._children.length; i++) {
218
 
            let child = this._children[i];
219
 
            let [min, natural] = child.actor.get_preferred_width(-1);
220
 
            widths[col++] = natural;
221
 
            if (child.span > 1) {
222
 
                for (let j = 1; j < child.span; j++)
223
 
                    widths[col++] = 0;
224
 
            }
225
 
        }
226
 
        return widths;
227
 
    },
228
 
 
229
 
    setColumnWidths: function(widths) {
230
 
        this._columnWidths = widths;
231
 
    },
232
 
 
233
 
    _getPreferredWidth: function(actor, forHeight, alloc) {
234
 
        let width = 0;
235
 
        if (this._columnWidths) {
236
 
            for (let i = 0; i < this._columnWidths.length; i++) {
237
 
                if (i > 0)
238
 
                    width += this._spacing;
239
 
                width += this._columnWidths[i];
240
 
            }
241
 
        } else {
242
 
            for (let i = 0; i < this._children.length; i++) {
243
 
                let child = this._children[i];
244
 
                if (i > 0)
245
 
                    width += this._spacing;
246
 
                let [min, natural] = child.actor.get_preferred_width(-1);
247
 
                width += natural;
248
 
            }
249
 
        }
250
 
        alloc.min_size = alloc.natural_size = width;
251
 
    },
252
 
 
253
 
    _getPreferredHeight: function(actor, forWidth, alloc) {
254
 
        let height = 0, x = 0, minWidth, childWidth;
255
 
        for (let i = 0; i < this._children.length; i++) {
256
 
            let child = this._children[i];
257
 
            if (this._columnWidths) {
258
 
                if (child.span == -1) {
259
 
                    childWidth = 0;
260
 
                    for (let j = i; j < this._columnWidths.length; j++)
261
 
                        childWidth += this._columnWidths[j]
262
 
                } else
263
 
                    childWidth = this._columnWidths[i];
264
 
            } else {
265
 
                if (child.span == -1)
266
 
                    childWidth = forWidth - x;
267
 
                else
268
 
                    [minWidth, childWidth] = child.actor.get_preferred_width(-1);
269
 
            }
270
 
            x += childWidth;
271
 
 
272
 
            let [min, natural] = child.actor.get_preferred_height(childWidth);
273
 
            if (natural > height)
274
 
                height = natural;
275
 
        }
276
 
        alloc.min_size = alloc.natural_size = height;
277
 
    },
278
 
 
279
 
    _allocate: function(actor, box, flags) {
280
 
        let height = box.y2 - box.y1;
281
 
        let direction = this.actor.get_text_direction();
282
 
 
283
 
        if (this._dot) {
284
 
            // The dot is placed outside box
285
 
            // one quarter of padding from the border of the container
286
 
            // (so 3/4 from the inner border)
287
 
            // (padding is box.x1)
288
 
            let dotBox = new Clutter.ActorBox();
289
 
            let dotWidth = Math.round(box.x1 / 2);
290
 
 
291
 
            if (direction == Clutter.TextDirection.LTR) {
292
 
                dotBox.x1 = Math.round(box.x1 / 4);
293
 
                dotBox.x2 = dotBox.x1 + dotWidth;
294
 
            } else {
295
 
                dotBox.x2 = box.x2 + 3 * Math.round(box.x1 / 4);
296
 
                dotBox.x1 = dotBox.x2 - dotWidth;
297
 
            }
298
 
            dotBox.y1 = Math.round(box.y1 + (height - dotWidth) / 2);
299
 
            dotBox.y2 = dotBox.y1 + dotWidth;
300
 
            this._dot.allocate(dotBox, flags);
301
 
        }
302
 
 
303
 
        let x;
304
 
        if (direction == Clutter.TextDirection.LTR)
305
 
            x = box.x1;
306
 
        else
307
 
            x = box.x2;
308
 
        // if direction is ltr, x is the right edge of the last added
309
 
        // actor, and it's constantly increasing, whereas if rtl, x is
310
 
        // the left edge and it decreases
311
 
        for (let i = 0, col = 0; i < this._children.length; i++) {
312
 
            let child = this._children[i];
313
 
            let childBox = new Clutter.ActorBox();
314
 
 
315
 
            let [minWidth, naturalWidth] = child.actor.get_preferred_width(-1);
316
 
            let availWidth, extraWidth;
317
 
            if (this._columnWidths) {
318
 
                if (child.span == -1) {
319
 
                    if (direction == Clutter.TextDirection.LTR)
320
 
                        availWidth = box.x2 - x;
321
 
                    else
322
 
                        availWidth = x - box.x1;
323
 
                } else {
324
 
                    availWidth = 0;
325
 
                    for (let j = 0; j < child.span; j++)
326
 
                        availWidth += this._columnWidths[col++];
327
 
                }
328
 
                extraWidth = availWidth - naturalWidth;
329
 
            } else {
330
 
                if (child.span == -1) {
331
 
                    if (direction == Clutter.TextDirection.LTR)
332
 
                        availWidth = box.x2 - x;
333
 
                    else
334
 
                        availWidth = x - box.x1;
335
 
                } else {
336
 
                    availWidth = naturalWidth;
337
 
                }
338
 
                extraWidth = 0;
339
 
            }
340
 
 
341
 
            if (direction == Clutter.TextDirection.LTR) {
342
 
                if (child.expand) {
343
 
                    childBox.x1 = x;
344
 
                    childBox.x2 = x + availWidth;
345
 
                } else if (child.align === St.Align.MIDDLE) {
346
 
                    childBox.x1 = x + Math.round(extraWidth / 2);
347
 
                    childBox.x2 = childBox.x1 + naturalWidth;
348
 
                } else if (child.align === St.Align.END) {
349
 
                    childBox.x2 = x + availWidth;
350
 
                    childBox.x1 = childBox.x2 - naturalWidth;
351
 
                } else {
352
 
                    childBox.x1 = x;
353
 
                    childBox.x2 = x + naturalWidth;
354
 
                }
355
 
            } else {
356
 
                if (child.expand) {
357
 
                    childBox.x1 = x - availWidth;
358
 
                    childBox.x2 = x;
359
 
                } else if (child.align === St.Align.MIDDLE) {
360
 
                    childBox.x1 = x - Math.round(extraWidth / 2);
361
 
                    childBox.x2 = childBox.x1 + naturalWidth;
362
 
                } else if (child.align === St.Align.END) {
363
 
                    // align to the left
364
 
                    childBox.x1 = x - availWidth;
365
 
                    childBox.x2 = childBox.x1 + naturalWidth;
366
 
                } else {
367
 
                    // align to the right
368
 
                    childBox.x2 = x;
369
 
                    childBox.x1 = x - naturalWidth;
370
 
                }
371
 
            }
372
 
 
373
 
            let [minHeight, naturalHeight] = child.actor.get_preferred_height(childBox.x2 - childBox.x1);
374
 
            childBox.y1 = Math.round(box.y1 + (height - naturalHeight) / 2);
375
 
            childBox.y2 = childBox.y1 + naturalHeight;
376
 
 
377
 
            child.actor.allocate(childBox, flags);
378
 
 
379
 
            if (direction == Clutter.TextDirection.LTR)
380
 
                x += availWidth + this._spacing;
381
 
            else
382
 
                x -= availWidth + this._spacing;
383
 
        }
384
 
    }
385
 
});
386
 
Signals.addSignalMethods(PopupBaseMenuItem.prototype);
387
 
 
388
 
const PopupMenuItem = new Lang.Class({
389
 
    Name: 'PopupMenuItem',
390
 
    Extends: PopupBaseMenuItem,
391
 
 
392
 
    _init: function (text, params) {
393
 
        this.parent(params);
394
 
 
395
 
        this.label = new St.Label({ text: text });
396
 
        this.addActor(this.label);
397
 
        this.actor.label_actor = this.label
398
 
    }
399
 
});
400
 
 
401
 
const PopupSeparatorMenuItem = new Lang.Class({
402
 
    Name: 'PopupSeparatorMenuItem',
403
 
    Extends: PopupBaseMenuItem,
404
 
 
405
 
    _init: function () {
406
 
        this.parent({ reactive: false,
407
 
                      can_focus: false});
408
 
 
409
 
        this._separator = new Separator.HorizontalSeparator({ style_class: 'popup-separator-menu-item' });
410
 
        this.addActor(this._separator.actor, { span: -1, expand: true });
411
 
    }
412
 
});
413
 
 
414
 
const PopupAlternatingMenuItemState = {
415
 
    DEFAULT: 0,
416
 
    ALTERNATIVE: 1
417
 
}
418
 
 
419
 
const PopupAlternatingMenuItem = new Lang.Class({
420
 
    Name: 'PopupAlternatingMenuItem',
421
 
    Extends: PopupBaseMenuItem,
422
 
 
423
 
    _init: function(text, alternateText, params) {
424
 
        this.parent(params);
425
 
        this.actor.add_style_class_name('popup-alternating-menu-item');
426
 
 
427
 
        this._text = text;
428
 
        this._alternateText = alternateText;
429
 
        this.label = new St.Label({ text: text });
430
 
        this.state = PopupAlternatingMenuItemState.DEFAULT;
431
 
        this.addActor(this.label);
432
 
        this.actor.label_actor = this.label;
433
 
 
434
 
        this.actor.connect('notify::mapped', Lang.bind(this, this._onMapped));
435
 
    },
436
 
 
437
 
    _onMapped: function() {
438
 
        if (this.actor.mapped) {
439
 
            this._capturedEventId = global.stage.connect('captured-event',
440
 
                                                         Lang.bind(this, this._onCapturedEvent));
441
 
            this._updateStateFromModifiers();
442
 
        } else {
443
 
            if (this._capturedEventId != 0) {
444
 
                global.stage.disconnect(this._capturedEventId);
445
 
                this._capturedEventId = 0;
446
 
            }
447
 
        }
448
 
    },
449
 
 
450
 
    _setState: function(state) {
451
 
        if (this.state != state) {
452
 
            if (state == PopupAlternatingMenuItemState.ALTERNATIVE && !this._canAlternate())
453
 
                return;
454
 
 
455
 
            this.state = state;
456
 
            this._updateLabel();
457
 
        }
458
 
    },
459
 
 
460
 
    _updateStateFromModifiers: function() {
461
 
        let [x, y, mods] = global.get_pointer();
462
 
        let state;
463
 
 
464
 
        if ((mods & Clutter.ModifierType.MOD1_MASK) == 0) {
465
 
            state = PopupAlternatingMenuItemState.DEFAULT;
466
 
        } else {
467
 
            state = PopupAlternatingMenuItemState.ALTERNATIVE;
468
 
        }
469
 
 
470
 
        this._setState(state);
471
 
    },
472
 
 
473
 
    _onCapturedEvent: function(actor, event) {
474
 
        if (event.type() != Clutter.EventType.KEY_PRESS &&
475
 
            event.type() != Clutter.EventType.KEY_RELEASE)
476
 
            return false;
477
 
 
478
 
        let key = event.get_key_symbol();
479
 
 
480
 
        if (key == Clutter.KEY_Alt_L || key == Clutter.KEY_Alt_R)
481
 
            this._updateStateFromModifiers();
482
 
 
483
 
        return false;
484
 
    },
485
 
 
486
 
    _updateLabel: function() {
487
 
        if (this.state == PopupAlternatingMenuItemState.ALTERNATIVE) {
488
 
            this.actor.add_style_pseudo_class('alternate');
489
 
            this.label.set_text(this._alternateText);
490
 
        } else {
491
 
            this.actor.remove_style_pseudo_class('alternate');
492
 
            this.label.set_text(this._text);
493
 
        }
494
 
    },
495
 
 
496
 
    _canAlternate: function() {
497
 
        if (this.state == PopupAlternatingMenuItemState.DEFAULT && !this._alternateText)
498
 
            return false;
499
 
        return true;
500
 
    },
501
 
 
502
 
    updateText: function(text, alternateText) {
503
 
        this._text = text;
504
 
        this._alternateText = alternateText;
505
 
 
506
 
        if (!this._canAlternate())
507
 
            this._setState(PopupAlternatingMenuItemState.DEFAULT);
508
 
 
509
 
        this._updateLabel();
510
 
    }
511
 
});
512
 
 
513
 
const PopupSliderMenuItem = new Lang.Class({
514
 
    Name: 'PopupSliderMenuItem',
515
 
    Extends: PopupBaseMenuItem,
516
 
 
517
 
    _init: function(value) {
518
 
        this.parent({ activate: false });
519
 
 
520
 
        this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
521
 
 
522
 
        if (isNaN(value))
523
 
            // Avoid spreading NaNs around
524
 
            throw TypeError('The slider value must be a number');
525
 
        this._value = Math.max(Math.min(value, 1), 0);
526
 
 
527
 
        this._slider = new St.DrawingArea({ style_class: 'popup-slider-menu-item', reactive: true });
528
 
        this.addActor(this._slider, { span: -1, expand: true });
529
 
        this._slider.connect('repaint', Lang.bind(this, this._sliderRepaint));
530
 
        this.actor.connect('button-press-event', Lang.bind(this, this._startDragging));
531
 
        this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
532
 
        this.actor.connect('notify::mapped', Lang.bind(this, function() {
533
 
            if (!this.actor.mapped)
534
 
                this._endDragging();
535
 
        }));
536
 
 
537
 
        this._releaseId = this._motionId = 0;
538
 
        this._dragging = false;
539
 
    },
540
 
 
541
 
    setValue: function(value) {
542
 
        if (isNaN(value))
543
 
            throw TypeError('The slider value must be a number');
544
 
 
545
 
        this._value = Math.max(Math.min(value, 1), 0);
546
 
        this._slider.queue_repaint();
547
 
    },
548
 
 
549
 
    _sliderRepaint: function(area) {
550
 
        let cr = area.get_context();
551
 
        let themeNode = area.get_theme_node();
552
 
        let [width, height] = area.get_surface_size();
553
 
 
554
 
        let handleRadius = themeNode.get_length('-slider-handle-radius');
555
 
 
556
 
        let sliderWidth = width - 2 * handleRadius;
557
 
        let sliderHeight = themeNode.get_length('-slider-height');
558
 
 
559
 
        let sliderBorderWidth = themeNode.get_length('-slider-border-width');
560
 
 
561
 
        let sliderBorderColor = themeNode.get_color('-slider-border-color');
562
 
        let sliderColor = themeNode.get_color('-slider-background-color');
563
 
 
564
 
        let sliderActiveBorderColor = themeNode.get_color('-slider-active-border-color');
565
 
        let sliderActiveColor = themeNode.get_color('-slider-active-background-color');
566
 
 
567
 
        cr.setSourceRGBA (
568
 
            sliderActiveColor.red / 255,
569
 
            sliderActiveColor.green / 255,
570
 
            sliderActiveColor.blue / 255,
571
 
            sliderActiveColor.alpha / 255);
572
 
        cr.rectangle(handleRadius, (height - sliderHeight) / 2, sliderWidth * this._value, sliderHeight);
573
 
        cr.fillPreserve();
574
 
        cr.setSourceRGBA (
575
 
            sliderActiveBorderColor.red / 255,
576
 
            sliderActiveBorderColor.green / 255,
577
 
            sliderActiveBorderColor.blue / 255,
578
 
            sliderActiveBorderColor.alpha / 255);
579
 
        cr.setLineWidth(sliderBorderWidth);
580
 
        cr.stroke();
581
 
 
582
 
        cr.setSourceRGBA (
583
 
            sliderColor.red / 255,
584
 
            sliderColor.green / 255,
585
 
            sliderColor.blue / 255,
586
 
            sliderColor.alpha / 255);
587
 
        cr.rectangle(handleRadius + sliderWidth * this._value, (height - sliderHeight) / 2, sliderWidth * (1 - this._value), sliderHeight);
588
 
        cr.fillPreserve();
589
 
        cr.setSourceRGBA (
590
 
            sliderBorderColor.red / 255,
591
 
            sliderBorderColor.green / 255,
592
 
            sliderBorderColor.blue / 255,
593
 
            sliderBorderColor.alpha / 255);
594
 
        cr.setLineWidth(sliderBorderWidth);
595
 
        cr.stroke();
596
 
 
597
 
        let handleY = height / 2;
598
 
        let handleX = handleRadius + (width - 2 * handleRadius) * this._value;
599
 
 
600
 
        let color = themeNode.get_foreground_color();
601
 
        cr.setSourceRGBA (
602
 
            color.red / 255,
603
 
            color.green / 255,
604
 
            color.blue / 255,
605
 
            color.alpha / 255);
606
 
        cr.arc(handleX, handleY, handleRadius, 0, 2 * Math.PI);
607
 
        cr.fill();
608
 
        cr.$dispose();
609
 
    },
610
 
 
611
 
    _startDragging: function(actor, event) {
612
 
        if (this._dragging) // don't allow two drags at the same time
613
 
            return;
614
 
 
615
 
        this._dragging = true;
616
 
 
617
 
        // FIXME: we should only grab the specific device that originated
618
 
        // the event, but for some weird reason events are still delivered
619
 
        // outside the slider if using clutter_grab_pointer_for_device
620
 
        Clutter.grab_pointer(this._slider);
621
 
        this._releaseId = this._slider.connect('button-release-event', Lang.bind(this, this._endDragging));
622
 
        this._motionId = this._slider.connect('motion-event', Lang.bind(this, this._motionEvent));
623
 
        let absX, absY;
624
 
        [absX, absY] = event.get_coords();
625
 
        this._moveHandle(absX, absY);
626
 
    },
627
 
 
628
 
    _endDragging: function() {
629
 
        if (this._dragging) {
630
 
            this._slider.disconnect(this._releaseId);
631
 
            this._slider.disconnect(this._motionId);
632
 
 
633
 
            Clutter.ungrab_pointer();
634
 
            this._dragging = false;
635
 
 
636
 
            this.emit('drag-end');
637
 
        }
638
 
        return true;
639
 
    },
640
 
 
641
 
    scroll: function(event) {
642
 
        let direction = event.get_scroll_direction();
643
 
        let delta;
644
 
 
645
 
        if (event.is_pointer_emulated())
646
 
            return;
647
 
 
648
 
        if (direction == Clutter.ScrollDirection.DOWN) {
649
 
            delta = -SLIDER_SCROLL_STEP;
650
 
        } else if (direction == Clutter.ScrollDirection.UP) {
651
 
            delta = +SLIDER_SCROLL_STEP;
652
 
        } else if (direction == Clutter.ScrollDirection.SMOOTH) {
653
 
            let [dx, dy] = event.get_scroll_delta();
654
 
            // Even though the slider is horizontal, use dy to match
655
 
            // the UP/DOWN above.
656
 
            delta = -dy / 10;
657
 
        }
658
 
 
659
 
        this._value = Math.min(Math.max(0, this._value + delta), 1);
660
 
 
661
 
        this._slider.queue_repaint();
662
 
        this.emit('value-changed', this._value);
663
 
    },
664
 
 
665
 
    _onScrollEvent: function(actor, event) {
666
 
        this.scroll(event);
667
 
    },
668
 
 
669
 
    _motionEvent: function(actor, event) {
670
 
        let absX, absY;
671
 
        [absX, absY] = event.get_coords();
672
 
        this._moveHandle(absX, absY);
673
 
        return true;
674
 
    },
675
 
 
676
 
    _moveHandle: function(absX, absY) {
677
 
        let relX, relY, sliderX, sliderY;
678
 
        [sliderX, sliderY] = this._slider.get_transformed_position();
679
 
        relX = absX - sliderX;
680
 
        relY = absY - sliderY;
681
 
 
682
 
        let width = this._slider.width;
683
 
        let handleRadius = this._slider.get_theme_node().get_length('-slider-handle-radius');
684
 
 
685
 
        let newvalue;
686
 
        if (relX < handleRadius)
687
 
            newvalue = 0;
688
 
        else if (relX > width - handleRadius)
689
 
            newvalue = 1;
690
 
        else
691
 
            newvalue = (relX - handleRadius) / (width - 2 * handleRadius);
692
 
        this._value = newvalue;
693
 
        this._slider.queue_repaint();
694
 
        this.emit('value-changed', this._value);
695
 
    },
696
 
 
697
 
    get value() {
698
 
        return this._value;
699
 
    },
700
 
 
701
 
    _onKeyPressEvent: function (actor, event) {
702
 
        let key = event.get_key_symbol();
703
 
        if (key == Clutter.KEY_Right || key == Clutter.KEY_Left) {
704
 
            let delta = key == Clutter.KEY_Right ? 0.1 : -0.1;
705
 
            this._value = Math.max(0, Math.min(this._value + delta, 1));
706
 
            this._slider.queue_repaint();
707
 
            this.emit('value-changed', this._value);
708
 
            this.emit('drag-end');
709
 
            return true;
710
 
        }
711
 
        return false;
712
 
    }
713
 
});
714
 
 
715
 
const Switch = new Lang.Class({
716
 
    Name: 'Switch',
717
 
 
718
 
    _init: function(state) {
719
 
        this.actor = new St.Bin({ style_class: 'toggle-switch',
720
 
                                  accessible_role: Atk.Role.CHECK_BOX,
721
 
                                  can_focus: true });
722
 
        // Translators: this MUST be either "toggle-switch-us"
723
 
        // (for toggle switches containing the English words
724
 
        // "ON" and "OFF") or "toggle-switch-intl" (for toggle
725
 
        // switches containing "◯" and "|"). Other values will
726
 
        // simply result in invisible toggle switches.
727
 
        this.actor.add_style_class_name(_("toggle-switch-us"));
728
 
        this.setToggleState(state);
729
 
    },
730
 
 
731
 
    setToggleState: function(state) {
732
 
        if (state)
733
 
            this.actor.add_style_pseudo_class('checked');
734
 
        else
735
 
            this.actor.remove_style_pseudo_class('checked');
736
 
        this.state = state;
737
 
    },
738
 
 
739
 
    toggle: function() {
740
 
        this.setToggleState(!this.state);
741
 
    }
742
 
});
743
 
 
744
 
const PopupSwitchMenuItem = new Lang.Class({
745
 
    Name: 'PopupSwitchMenuItem',
746
 
    Extends: PopupBaseMenuItem,
747
 
 
748
 
    _init: function(text, active, params) {
749
 
        this.parent(params);
750
 
 
751
 
        this.label = new St.Label({ text: text });
752
 
        this._switch = new Switch(active);
753
 
 
754
 
        this.actor.accessible_role = Atk.Role.CHECK_MENU_ITEM;
755
 
        this.checkAccessibleState();
756
 
        this.actor.label_actor = this.label;
757
 
 
758
 
        this.addActor(this.label);
759
 
 
760
 
        this._statusBin = new St.Bin({ x_align: St.Align.END });
761
 
        this.addActor(this._statusBin,
762
 
                      { expand: true, span: -1, align: St.Align.END });
763
 
 
764
 
        this._statusLabel = new St.Label({ text: '',
765
 
                                           style_class: 'popup-status-menu-item'
766
 
                                         });
767
 
        this._statusBin.child = this._switch.actor;
768
 
    },
769
 
 
770
 
    setStatus: function(text) {
771
 
        if (text != null) {
772
 
            this._statusLabel.text = text;
773
 
            this._statusBin.child = this._statusLabel;
774
 
            this.actor.reactive = false;
775
 
            this.actor.accessible_role = Atk.Role.MENU_ITEM;
776
 
        } else {
777
 
            this._statusBin.child = this._switch.actor;
778
 
            this.actor.reactive = true;
779
 
            this.actor.accessible_role = Atk.Role.CHECK_MENU_ITEM;
780
 
        }
781
 
        this.checkAccessibleState();
782
 
    },
783
 
 
784
 
    activate: function(event) {
785
 
        if (this._switch.actor.mapped) {
786
 
            this.toggle();
787
 
        }
788
 
 
789
 
        // we allow pressing space to toggle the switch
790
 
        // without closing the menu
791
 
        if (event.type() == Clutter.EventType.KEY_PRESS &&
792
 
            event.get_key_symbol() == Clutter.KEY_space)
793
 
            return;
794
 
 
795
 
        this.parent(event);
796
 
    },
797
 
 
798
 
    toggle: function() {
799
 
        this._switch.toggle();
800
 
        this.emit('toggled', this._switch.state);
801
 
        this.checkAccessibleState();
802
 
    },
803
 
 
804
 
    get state() {
805
 
        return this._switch.state;
806
 
    },
807
 
 
808
 
    setToggleState: function(state) {
809
 
        this._switch.setToggleState(state);
810
 
        this.checkAccessibleState();
811
 
    },
812
 
 
813
 
    checkAccessibleState: function() {
814
 
        switch (this.actor.accessible_role) {
815
 
        case Atk.Role.CHECK_MENU_ITEM:
816
 
            if (this._switch.state)
817
 
                this.actor.add_accessible_state (Atk.StateType.CHECKED);
818
 
            else
819
 
                this.actor.remove_accessible_state (Atk.StateType.CHECKED);
820
 
            break;
821
 
        default:
822
 
            this.actor.remove_accessible_state (Atk.StateType.CHECKED);
823
 
        }
824
 
    }
825
 
});
826
 
 
827
 
const PopupImageMenuItem = new Lang.Class({
828
 
    Name: 'PopupImageMenuItem',
829
 
    Extends: PopupBaseMenuItem,
830
 
 
831
 
    _init: function (text, iconName, params) {
832
 
        this.parent(params);
833
 
 
834
 
        this.label = new St.Label({ text: text });
835
 
        this.addActor(this.label);
836
 
        this._icon = new St.Icon({ style_class: 'popup-menu-icon' });
837
 
        this.addActor(this._icon, { align: St.Align.END });
838
 
 
839
 
        this.setIcon(iconName);
840
 
    },
841
 
 
842
 
    setIcon: function(name) {
843
 
        this._icon.icon_name = name;
844
 
    }
845
 
});
846
 
 
847
 
const PopupMenuBase = new Lang.Class({
848
 
    Name: 'PopupMenuBase',
849
 
    Abstract: true,
850
 
 
851
 
    _init: function(sourceActor, styleClass) {
852
 
        this.sourceActor = sourceActor;
853
 
 
854
 
        if (styleClass !== undefined) {
855
 
            this.box = new St.BoxLayout({ style_class: styleClass,
856
 
                                          vertical: true });
857
 
        } else {
858
 
            this.box = new St.BoxLayout({ vertical: true });
859
 
        }
860
 
        this.box.connect_after('queue-relayout', Lang.bind(this, this._menuQueueRelayout));
861
 
        this.length = 0;
862
 
 
863
 
        this.isOpen = false;
864
 
 
865
 
        // If set, we don't send events (including crossing events) to the source actor
866
 
        // for the menu which causes its prelight state to freeze
867
 
        this.blockSourceEvents = false;
868
 
 
869
 
        this._activeMenuItem = null;
870
 
        this._settingsActions = { };
871
 
 
872
 
        this._sessionUpdatedId = Main.sessionMode.connect('updated', Lang.bind(this, this._sessionUpdated));
873
 
    },
874
 
 
875
 
    _sessionUpdated: function() {
876
 
        this._setSettingsVisibility(Main.sessionMode.allowSettings);
877
 
    },
878
 
 
879
 
    addAction: function(title, callback) {
880
 
        let menuItem = new PopupMenuItem(title);
881
 
        this.addMenuItem(menuItem);
882
 
        menuItem.connect('activate', Lang.bind(this, function (menuItem, event) {
883
 
            callback(event);
884
 
        }));
885
 
 
886
 
        return menuItem;
887
 
    },
888
 
 
889
 
    addSettingsAction: function(title, desktopFile) {
890
 
        let menuItem = this.addAction(title, function() {
891
 
                           let app = Shell.AppSystem.get_default().lookup_app(desktopFile);
892
 
 
893
 
                           if (!app) {
894
 
                               log('Settings panel for desktop file ' + desktopFile + ' could not be loaded!');
895
 
                               return;
896
 
                           }
897
 
 
898
 
                           Main.overview.hide();
899
 
                           app.activate();
900
 
                       });
901
 
 
902
 
        menuItem.actor.visible = Main.sessionMode.allowSettings;
903
 
        this._settingsActions[desktopFile] = menuItem;
904
 
 
905
 
        return menuItem;
906
 
    },
907
 
 
908
 
    _setSettingsVisibility: function(visible) {
909
 
        for (let id in this._settingsActions) {
910
 
            let item = this._settingsActions[id];
911
 
            item.actor.visible = visible;
912
 
        }
913
 
    },
914
 
 
915
 
    isEmpty: function() {
916
 
        let hasVisibleChildren = this.box.get_children().some(function(child) {
917
 
            return child.visible;
918
 
        });
919
 
 
920
 
        return !hasVisibleChildren;
921
 
    },
922
 
 
923
 
    isChildMenu: function() {
924
 
        return false;
925
 
    },
926
 
 
927
 
    /**
928
 
     * _connectSubMenuSignals:
929
 
     * @object: a menu item, or a menu section
930
 
     * @menu: a sub menu, or a menu section
931
 
     *
932
 
     * Connects to signals on @menu that are necessary for
933
 
     * operating the submenu, and stores the ids on @object.
934
 
     */
935
 
    _connectSubMenuSignals: function(object, menu) {
936
 
        object._subMenuActivateId = menu.connect('activate', Lang.bind(this, function() {
937
 
            this.emit('activate');
938
 
            this.close(BoxPointer.PopupAnimation.FULL);
939
 
        }));
940
 
        object._subMenuActiveChangeId = menu.connect('active-changed', Lang.bind(this, function(submenu, submenuItem) {
941
 
            if (this._activeMenuItem && this._activeMenuItem != submenuItem)
942
 
                this._activeMenuItem.setActive(false);
943
 
            this._activeMenuItem = submenuItem;
944
 
            this.emit('active-changed', submenuItem);
945
 
        }));
946
 
    },
947
 
 
948
 
    _connectItemSignals: function(menuItem) {
949
 
        menuItem._activeChangeId = menuItem.connect('active-changed', Lang.bind(this, function (menuItem, active) {
950
 
            if (active && this._activeMenuItem != menuItem) {
951
 
                if (this._activeMenuItem)
952
 
                    this._activeMenuItem.setActive(false);
953
 
                this._activeMenuItem = menuItem;
954
 
                this.emit('active-changed', menuItem);
955
 
            } else if (!active && this._activeMenuItem == menuItem) {
956
 
                this._activeMenuItem = null;
957
 
                this.emit('active-changed', null);
958
 
            }
959
 
        }));
960
 
        menuItem._sensitiveChangeId = menuItem.connect('sensitive-changed', Lang.bind(this, function(menuItem, sensitive) {
961
 
            if (!sensitive && this._activeMenuItem == menuItem) {
962
 
                if (!this.actor.navigate_focus(menuItem.actor,
963
 
                                               Gtk.DirectionType.TAB_FORWARD,
964
 
                                               true))
965
 
                    this.actor.grab_key_focus();
966
 
            } else if (sensitive && this._activeMenuItem == null) {
967
 
                if (global.stage.get_key_focus() == this.actor)
968
 
                    menuItem.actor.grab_key_focus();
969
 
            }
970
 
        }));
971
 
        menuItem._activateId = menuItem.connect('activate', Lang.bind(this, function (menuItem, event) {
972
 
            this.emit('activate', menuItem);
973
 
            this.close(BoxPointer.PopupAnimation.FULL);
974
 
        }));
975
 
        // the weird name is to avoid a conflict with some random property
976
 
        // the menuItem may have, called destroyId
977
 
        // (FIXME: in the future it may make sense to have container objects
978
 
        // like PopupMenuManager does)
979
 
        menuItem._popupMenuDestroyId = menuItem.connect('destroy', Lang.bind(this, function(menuItem) {
980
 
            menuItem.disconnect(menuItem._popupMenuDestroyId);
981
 
            menuItem.disconnect(menuItem._activateId);
982
 
            menuItem.disconnect(menuItem._activeChangeId);
983
 
            menuItem.disconnect(menuItem._sensitiveChangeId);
984
 
            if (menuItem.menu) {
985
 
                menuItem.menu.disconnect(menuItem._subMenuActivateId);
986
 
                menuItem.menu.disconnect(menuItem._subMenuActiveChangeId);
987
 
                this.disconnect(menuItem._closingId);
988
 
            }
989
 
            if (menuItem == this._activeMenuItem)
990
 
                this._activeMenuItem = null;
991
 
        }));
992
 
    },
993
 
 
994
 
    _updateSeparatorVisibility: function(menuItem) {
995
 
        let children = this.box.get_children();
996
 
 
997
 
        let index = children.indexOf(menuItem.actor);
998
 
 
999
 
        if (index < 0)
1000
 
            return;
1001
 
 
1002
 
        let childBeforeIndex = index - 1;
1003
 
 
1004
 
        while (childBeforeIndex >= 0 && !children[childBeforeIndex].visible)
1005
 
            childBeforeIndex--;
1006
 
 
1007
 
        if (childBeforeIndex < 0
1008
 
            || children[childBeforeIndex]._delegate instanceof PopupSeparatorMenuItem) {
1009
 
            menuItem.actor.hide();
1010
 
            return;
1011
 
        }
1012
 
 
1013
 
        let childAfterIndex = index + 1;
1014
 
 
1015
 
        while (childAfterIndex < children.length && !children[childAfterIndex].visible)
1016
 
            childAfterIndex++;
1017
 
 
1018
 
        if (childAfterIndex >= children.length
1019
 
            || children[childAfterIndex]._delegate instanceof PopupSeparatorMenuItem) {
1020
 
            menuItem.actor.hide();
1021
 
            return;
1022
 
        }
1023
 
 
1024
 
        menuItem.actor.show();
1025
 
    },
1026
 
 
1027
 
    addMenuItem: function(menuItem, position) {
1028
 
        let before_item = null;
1029
 
        if (position == undefined) {
1030
 
            this.box.add(menuItem.actor);
1031
 
        } else {
1032
 
            let items = this._getMenuItems();
1033
 
            if (position < items.length) {
1034
 
                before_item = items[position].actor;
1035
 
                this.box.insert_child_below(menuItem.actor, before_item);
1036
 
            } else {
1037
 
                this.box.add(menuItem.actor);
1038
 
            }
1039
 
        }
1040
 
 
1041
 
        if (menuItem instanceof PopupMenuSection) {
1042
 
            this._connectSubMenuSignals(menuItem, menuItem);
1043
 
            menuItem._parentOpenStateChangedId = this.connect('open-state-changed',
1044
 
                function(self, open) {
1045
 
                    if (open)
1046
 
                        menuItem.open();
1047
 
                    else
1048
 
                        menuItem.close();
1049
 
                });
1050
 
            menuItem.connect('destroy', Lang.bind(this, function() {
1051
 
                menuItem.disconnect(menuItem._subMenuActivateId);
1052
 
                menuItem.disconnect(menuItem._subMenuActiveChangeId);
1053
 
                this.disconnect(menuItem._parentOpenStateChangedId);
1054
 
 
1055
 
                this.length--;
1056
 
            }));
1057
 
        } else if (menuItem instanceof PopupSubMenuMenuItem) {
1058
 
            if (before_item == null)
1059
 
                this.box.add(menuItem.menu.actor);
1060
 
            else
1061
 
                this.box.insert_child_below(menuItem.menu.actor, before_item);
1062
 
            this._connectSubMenuSignals(menuItem, menuItem.menu);
1063
 
            this._connectItemSignals(menuItem);
1064
 
            menuItem._closingId = this.connect('open-state-changed', function(self, open) {
1065
 
                if (!open)
1066
 
                    menuItem.menu.close(BoxPointer.PopupAnimation.FADE);
1067
 
            });
1068
 
        } else if (menuItem instanceof PopupSeparatorMenuItem) {
1069
 
            this._connectItemSignals(menuItem);
1070
 
 
1071
 
            // updateSeparatorVisibility needs to get called any time the
1072
 
            // separator's adjacent siblings change visibility or position.
1073
 
            // open-state-changed isn't exactly that, but doing it in more
1074
 
            // precise ways would require a lot more bookkeeping.
1075
 
            this.connect('open-state-changed', Lang.bind(this, function() { this._updateSeparatorVisibility(menuItem); }));
1076
 
        } else if (menuItem instanceof PopupBaseMenuItem)
1077
 
            this._connectItemSignals(menuItem);
1078
 
        else
1079
 
            throw TypeError("Invalid argument to PopupMenuBase.addMenuItem()");
1080
 
 
1081
 
        this.length++;
1082
 
    },
1083
 
 
1084
 
    getColumnWidths: function() {
1085
 
        let columnWidths = [];
1086
 
        let items = this.box.get_children();
1087
 
        for (let i = 0; i < items.length; i++) {
1088
 
            if (!items[i].visible &&
1089
 
                !(items[i]._delegate instanceof PopupSubMenu && items[i-1].visible))
1090
 
                continue;
1091
 
            if (items[i]._delegate instanceof PopupBaseMenuItem || items[i]._delegate instanceof PopupMenuBase) {
1092
 
                let itemColumnWidths = items[i]._delegate.getColumnWidths();
1093
 
                for (let j = 0; j < itemColumnWidths.length; j++) {
1094
 
                    if (j >= columnWidths.length || itemColumnWidths[j] > columnWidths[j])
1095
 
                        columnWidths[j] = itemColumnWidths[j];
1096
 
                }
1097
 
            }
1098
 
        }
1099
 
        return columnWidths;
1100
 
    },
1101
 
 
1102
 
    setColumnWidths: function(widths) {
1103
 
        let items = this.box.get_children();
1104
 
        for (let i = 0; i < items.length; i++) {
1105
 
            if (items[i]._delegate instanceof PopupBaseMenuItem || items[i]._delegate instanceof PopupMenuBase)
1106
 
                items[i]._delegate.setColumnWidths(widths);
1107
 
        }
1108
 
    },
1109
 
 
1110
 
    // Because of the above column-width funniness, we need to do a
1111
 
    // queue-relayout on every item whenever the menu itself changes
1112
 
    // size, to force clutter to drop its cached size requests. (The
1113
 
    // menuitems will in turn call queue_relayout on their parent, the
1114
 
    // menu, but that call will be a no-op since the menu already
1115
 
    // has a relayout queued, so we won't get stuck in a loop.
1116
 
    _menuQueueRelayout: function() {
1117
 
        this.box.get_children().map(function (actor) { actor.queue_relayout(); });
1118
 
    },
1119
 
 
1120
 
    addActor: function(actor) {
1121
 
        this.box.add(actor);
1122
 
    },
1123
 
 
1124
 
    _getMenuItems: function() {
1125
 
        return this.box.get_children().map(function (actor) {
1126
 
            return actor._delegate;
1127
 
        }).filter(function(item) {
1128
 
            return item instanceof PopupBaseMenuItem || item instanceof PopupMenuSection;
1129
 
        });
1130
 
    },
1131
 
 
1132
 
    get firstMenuItem() {
1133
 
        let items = this._getMenuItems();
1134
 
        if (items.length)
1135
 
            return items[0];
1136
 
        else
1137
 
            return null;
1138
 
    },
1139
 
 
1140
 
    get numMenuItems() {
1141
 
        return this._getMenuItems().length;
1142
 
    },
1143
 
 
1144
 
    removeAll: function() {
1145
 
        let children = this._getMenuItems();
1146
 
        for (let i = 0; i < children.length; i++) {
1147
 
            let item = children[i];
1148
 
            item.destroy();
1149
 
        }
1150
 
    },
1151
 
 
1152
 
    toggle: function() {
1153
 
        if (this.isOpen)
1154
 
            this.close(BoxPointer.PopupAnimation.FULL);
1155
 
        else
1156
 
            this.open(BoxPointer.PopupAnimation.FULL);
1157
 
    },
1158
 
 
1159
 
    destroy: function() {
1160
 
        this.close();
1161
 
        this.removeAll();
1162
 
        this.actor.destroy();
1163
 
 
1164
 
        this.emit('destroy');
1165
 
 
1166
 
        Main.sessionMode.disconnect(this._sessionUpdatedId);
1167
 
        this._sessionUpdatedId = 0;
1168
 
    }
1169
 
});
1170
 
Signals.addSignalMethods(PopupMenuBase.prototype);
1171
 
 
1172
 
const PopupMenu = new Lang.Class({
1173
 
    Name: 'PopupMenu',
1174
 
    Extends: PopupMenuBase,
1175
 
 
1176
 
    _init: function(sourceActor, arrowAlignment, arrowSide) {
1177
 
        this.parent(sourceActor, 'popup-menu-content');
1178
 
 
1179
 
        this._arrowAlignment = arrowAlignment;
1180
 
        this._arrowSide = arrowSide;
1181
 
 
1182
 
        this._boxPointer = new BoxPointer.BoxPointer(arrowSide,
1183
 
                                                     { x_fill: true,
1184
 
                                                       y_fill: true,
1185
 
                                                       x_align: St.Align.START });
1186
 
        this.actor = this._boxPointer.actor;
1187
 
        this.actor._delegate = this;
1188
 
        this.actor.style_class = 'popup-menu-boxpointer';
1189
 
 
1190
 
        this._boxWrapper = new Shell.GenericContainer();
1191
 
        this._boxWrapper.connect('get-preferred-width', Lang.bind(this, this._boxGetPreferredWidth));
1192
 
        this._boxWrapper.connect('get-preferred-height', Lang.bind(this, this._boxGetPreferredHeight));
1193
 
        this._boxWrapper.connect('allocate', Lang.bind(this, this._boxAllocate));
1194
 
        this._boxPointer.bin.set_child(this._boxWrapper);
1195
 
        this._boxWrapper.add_actor(this.box);
1196
 
        this.actor.add_style_class_name('popup-menu');
1197
 
 
1198
 
        global.focus_manager.add_group(this.actor);
1199
 
        this.actor.reactive = true;
1200
 
 
1201
 
        this._childMenus = [];
1202
 
    },
1203
 
 
1204
 
    _boxGetPreferredWidth: function (actor, forHeight, alloc) {
1205
 
        let columnWidths = this.getColumnWidths();
1206
 
        this.setColumnWidths(columnWidths);
1207
 
 
1208
 
        // Now they will request the right sizes
1209
 
        [alloc.min_size, alloc.natural_size] = this.box.get_preferred_width(forHeight);
1210
 
    },
1211
 
 
1212
 
    _boxGetPreferredHeight: function (actor, forWidth, alloc) {
1213
 
        [alloc.min_size, alloc.natural_size] = this.box.get_preferred_height(forWidth);
1214
 
    },
1215
 
 
1216
 
    _boxAllocate: function (actor, box, flags) {
1217
 
        this.box.allocate(box, flags);
1218
 
    },
1219
 
 
1220
 
    setArrowOrigin: function(origin) {
1221
 
        this._boxPointer.setArrowOrigin(origin);
1222
 
    },
1223
 
 
1224
 
    setSourceAlignment: function(alignment) {
1225
 
        this._boxPointer.setSourceAlignment(alignment);
1226
 
    },
1227
 
 
1228
 
    isChildMenu: function(menu) {
1229
 
        return this._childMenus.indexOf(menu) != -1;
1230
 
    },
1231
 
 
1232
 
    addChildMenu: function(menu) {
1233
 
        if (this.isChildMenu(menu))
1234
 
            return;
1235
 
 
1236
 
        this._childMenus.push(menu);
1237
 
        this.emit('child-menu-added', menu);
1238
 
    },
1239
 
 
1240
 
    removeChildMenu: function(menu) {
1241
 
        let index = this._childMenus.indexOf(menu);
1242
 
 
1243
 
        if (index == -1)
1244
 
            return;
1245
 
 
1246
 
        this._childMenus.splice(index, 1);
1247
 
        this.emit('child-menu-removed', menu);
1248
 
    },
1249
 
 
1250
 
    open: function(animate) {
1251
 
        if (this.isOpen)
1252
 
            return;
1253
 
 
1254
 
        if (this.isEmpty())
1255
 
            return;
1256
 
 
1257
 
        this.isOpen = true;
1258
 
 
1259
 
        this._boxPointer.setPosition(this.sourceActor, this._arrowAlignment);
1260
 
        this._boxPointer.show(animate);
1261
 
 
1262
 
        this.actor.raise_top();
1263
 
 
1264
 
        this.emit('open-state-changed', true);
1265
 
    },
1266
 
 
1267
 
    close: function(animate) {
1268
 
        if (this._activeMenuItem)
1269
 
            this._activeMenuItem.setActive(false);
1270
 
 
1271
 
        this._childMenus.forEach(function(childMenu) {
1272
 
            childMenu.close();
1273
 
        });
1274
 
 
1275
 
        if (this._boxPointer.actor.visible)
1276
 
            this._boxPointer.hide(animate);
1277
 
 
1278
 
        if (!this.isOpen)
1279
 
            return;
1280
 
 
1281
 
        this.isOpen = false;
1282
 
        this.emit('open-state-changed', false);
1283
 
    }
1284
 
});
1285
 
 
1286
 
const PopupDummyMenu = new Lang.Class({
1287
 
    Name: 'PopupDummyMenu',
1288
 
 
1289
 
    _init: function(sourceActor) {
1290
 
        this.sourceActor = sourceActor;
1291
 
        this.actor = sourceActor;
1292
 
        this.actor._delegate = this;
1293
 
    },
1294
 
 
1295
 
    isChildMenu: function() {
1296
 
        return false;
1297
 
    },
1298
 
 
1299
 
    open: function() { this.emit('open-state-changed', true); },
1300
 
    close: function() { this.emit('open-state-changed', false); },
1301
 
    toggle: function() {},
1302
 
    destroy: function() {
1303
 
        this.emit('destroy');
1304
 
    },
1305
 
});
1306
 
Signals.addSignalMethods(PopupDummyMenu.prototype);
1307
 
 
1308
 
const PopupSubMenu = new Lang.Class({
1309
 
    Name: 'PopupSubMenu',
1310
 
    Extends: PopupMenuBase,
1311
 
 
1312
 
    _init: function(sourceActor, sourceArrow) {
1313
 
        this.parent(sourceActor);
1314
 
 
1315
 
        this._arrow = sourceArrow;
1316
 
        this._arrow.rotation_center_z_gravity = Clutter.Gravity.CENTER;
1317
 
 
1318
 
        // Since a function of a submenu might be to provide a "More.." expander
1319
 
        // with long content, we make it scrollable - the scrollbar will only take
1320
 
        // effect if a CSS max-height is set on the top menu.
1321
 
        this.actor = new St.ScrollView({ style_class: 'popup-sub-menu',
1322
 
                                         hscrollbar_policy: Gtk.PolicyType.NEVER,
1323
 
                                         vscrollbar_policy: Gtk.PolicyType.NEVER });
1324
 
 
1325
 
        this.actor.add_actor(this.box);
1326
 
        this.actor._delegate = this;
1327
 
        this.actor.clip_to_allocation = true;
1328
 
        this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
1329
 
        this.actor.hide();
1330
 
    },
1331
 
 
1332
 
    _getTopMenu: function() {
1333
 
        let actor = this.actor.get_parent();
1334
 
        while (actor) {
1335
 
            if (actor._delegate && actor._delegate instanceof PopupMenu)
1336
 
                return actor._delegate;
1337
 
 
1338
 
            actor = actor.get_parent();
1339
 
        }
1340
 
 
1341
 
        return null;
1342
 
    },
1343
 
 
1344
 
    _needsScrollbar: function() {
1345
 
        let topMenu = this._getTopMenu();
1346
 
        let [topMinHeight, topNaturalHeight] = topMenu.actor.get_preferred_height(-1);
1347
 
        let topThemeNode = topMenu.actor.get_theme_node();
1348
 
 
1349
 
        let topMaxHeight = topThemeNode.get_max_height();
1350
 
        return topMaxHeight >= 0 && topNaturalHeight >= topMaxHeight;
1351
 
    },
1352
 
 
1353
 
    open: function(animate) {
1354
 
        if (this.isOpen)
1355
 
            return;
1356
 
 
1357
 
        if (this.isEmpty())
1358
 
            return;
1359
 
 
1360
 
        this.isOpen = true;
1361
 
 
1362
 
        this.actor.show();
1363
 
 
1364
 
        let needsScrollbar = this._needsScrollbar();
1365
 
 
1366
 
        // St.ScrollView always requests space horizontally for a possible vertical
1367
 
        // scrollbar if in AUTOMATIC mode. Doing better would require implementation
1368
 
        // of width-for-height in St.BoxLayout and St.ScrollView. This looks bad
1369
 
        // when we *don't* need it, so turn off the scrollbar when that's true.
1370
 
        // Dynamic changes in whether we need it aren't handled properly.
1371
 
        this.actor.vscrollbar_policy =
1372
 
            needsScrollbar ? Gtk.PolicyType.AUTOMATIC : Gtk.PolicyType.NEVER;
1373
 
 
1374
 
        if (needsScrollbar)
1375
 
            this.actor.add_style_pseudo_class('scrolled');
1376
 
        else
1377
 
            this.actor.remove_style_pseudo_class('scrolled');
1378
 
 
1379
 
        // It looks funny if we animate with a scrollbar (at what point is
1380
 
        // the scrollbar added?) so just skip that case
1381
 
        if (animate && needsScrollbar)
1382
 
            animate = false;
1383
 
 
1384
 
        if (animate) {
1385
 
            let [minHeight, naturalHeight] = this.actor.get_preferred_height(-1);
1386
 
            this.actor.height = 0;
1387
 
            this.actor._arrow_rotation = this._arrow.rotation_angle_z;
1388
 
            Tweener.addTween(this.actor,
1389
 
                             { _arrow_rotation: 90,
1390
 
                               height: naturalHeight,
1391
 
                               time: 0.25,
1392
 
                               onUpdateScope: this,
1393
 
                               onUpdate: function() {
1394
 
                                   this._arrow.rotation_angle_z = this.actor._arrow_rotation;
1395
 
                               },
1396
 
                               onCompleteScope: this,
1397
 
                               onComplete: function() {
1398
 
                                   this.actor.set_height(-1);
1399
 
                                   this.emit('open-state-changed', true);
1400
 
                               }
1401
 
                             });
1402
 
        } else {
1403
 
            this._arrow.rotation_angle_z = 90;
1404
 
            this.emit('open-state-changed', true);
1405
 
        }
1406
 
    },
1407
 
 
1408
 
    close: function(animate) {
1409
 
        if (!this.isOpen)
1410
 
            return;
1411
 
 
1412
 
        this.isOpen = false;
1413
 
 
1414
 
        if (this._activeMenuItem)
1415
 
            this._activeMenuItem.setActive(false);
1416
 
 
1417
 
        if (animate && this._needsScrollbar())
1418
 
            animate = false;
1419
 
 
1420
 
        if (animate) {
1421
 
            this.actor._arrow_rotation = this._arrow.rotation_angle_z;
1422
 
            Tweener.addTween(this.actor,
1423
 
                             { _arrow_rotation: 0,
1424
 
                               height: 0,
1425
 
                               time: 0.25,
1426
 
                               onCompleteScope: this,
1427
 
                               onComplete: function() {
1428
 
                                   this.actor.hide();
1429
 
                                   this.actor.set_height(-1);
1430
 
 
1431
 
                                   this.emit('open-state-changed', false);
1432
 
                               },
1433
 
                               onUpdateScope: this,
1434
 
                               onUpdate: function() {
1435
 
                                   this._arrow.rotation_angle_z = this.actor._arrow_rotation;
1436
 
                               }
1437
 
                             });
1438
 
            } else {
1439
 
                this._arrow.rotation_angle_z = 0;
1440
 
                this.actor.hide();
1441
 
 
1442
 
                this.isOpen = false;
1443
 
                this.emit('open-state-changed', false);
1444
 
            }
1445
 
    },
1446
 
 
1447
 
    _onKeyPressEvent: function(actor, event) {
1448
 
        // Move focus back to parent menu if the user types Left.
1449
 
 
1450
 
        if (this.isOpen && event.get_key_symbol() == Clutter.KEY_Left) {
1451
 
            this.close(BoxPointer.PopupAnimation.FULL);
1452
 
            this.sourceActor._delegate.setActive(true);
1453
 
            return true;
1454
 
        }
1455
 
 
1456
 
        return false;
1457
 
    }
1458
 
});
1459
 
 
1460
 
/**
1461
 
 * PopupMenuSection:
1462
 
 *
1463
 
 * A section of a PopupMenu which is handled like a submenu
1464
 
 * (you can add and remove items, you can destroy it, you
1465
 
 * can add it to another menu), but is completely transparent
1466
 
 * to the user
1467
 
 */
1468
 
const PopupMenuSection = new Lang.Class({
1469
 
    Name: 'PopupMenuSection',
1470
 
    Extends: PopupMenuBase,
1471
 
 
1472
 
    _init: function() {
1473
 
        this.parent();
1474
 
 
1475
 
        this.actor = this.box;
1476
 
        this.actor._delegate = this;
1477
 
        this.isOpen = true;
1478
 
 
1479
 
        // an array of externally managed separators
1480
 
        this.separators = [];
1481
 
    },
1482
 
 
1483
 
    // deliberately ignore any attempt to open() or close(), but emit the
1484
 
    // corresponding signal so children can still pick it up
1485
 
    open: function() { this.emit('open-state-changed', true); },
1486
 
    close: function() { this.emit('open-state-changed', false); },
1487
 
 
1488
 
    destroy: function() {
1489
 
        for (let i = 0; i < this.separators.length; i++)
1490
 
            this.separators[i].destroy();
1491
 
        this.separators = [];
1492
 
 
1493
 
        this.parent();
1494
 
    }
1495
 
});
1496
 
 
1497
 
const PopupSubMenuMenuItem = new Lang.Class({
1498
 
    Name: 'PopupSubMenuMenuItem',
1499
 
    Extends: PopupBaseMenuItem,
1500
 
 
1501
 
    _init: function(text) {
1502
 
        this.parent();
1503
 
 
1504
 
        this.actor.add_style_class_name('popup-submenu-menu-item');
1505
 
 
1506
 
        this.label = new St.Label({ text: text });
1507
 
        this.addActor(this.label);
1508
 
        this.actor.label_actor = this.label;
1509
 
        this._triangle = new St.Label({ text: '\u25B8' });
1510
 
        this.addActor(this._triangle, { align: St.Align.END });
1511
 
 
1512
 
        this.menu = new PopupSubMenu(this.actor, this._triangle);
1513
 
        this.menu.connect('open-state-changed', Lang.bind(this, this._subMenuOpenStateChanged));
1514
 
    },
1515
 
 
1516
 
    _subMenuOpenStateChanged: function(menu, open) {
1517
 
        if (open)
1518
 
            this.actor.add_style_pseudo_class('open');
1519
 
        else
1520
 
            this.actor.remove_style_pseudo_class('open');
1521
 
    },
1522
 
 
1523
 
    destroy: function() {
1524
 
        this.menu.destroy();
1525
 
 
1526
 
        this.parent();
1527
 
    },
1528
 
 
1529
 
    _onKeyPressEvent: function(actor, event) {
1530
 
        let symbol = event.get_key_symbol();
1531
 
 
1532
 
        if (symbol == Clutter.KEY_Right) {
1533
 
            this.menu.open(BoxPointer.PopupAnimation.FULL);
1534
 
            this.menu.actor.navigate_focus(null, Gtk.DirectionType.DOWN, false);
1535
 
            return true;
1536
 
        } else if (symbol == Clutter.KEY_Left && this.menu.isOpen) {
1537
 
            this.menu.close();
1538
 
            return true;
1539
 
        }
1540
 
 
1541
 
        return this.parent(actor, event);
1542
 
    },
1543
 
 
1544
 
    activate: function(event) {
1545
 
        this.menu.open(BoxPointer.PopupAnimation.FULL);
1546
 
    },
1547
 
 
1548
 
    _onButtonReleaseEvent: function(actor) {
1549
 
        this.menu.toggle();
1550
 
    }
1551
 
});
1552
 
 
1553
 
const PopupComboMenu = new Lang.Class({
1554
 
    Name: 'PopupComboMenu',
1555
 
    Extends: PopupMenuBase,
1556
 
 
1557
 
    _init: function(sourceActor) {
1558
 
        this.parent(sourceActor, 'popup-combo-menu');
1559
 
 
1560
 
        this.actor = this.box;
1561
 
        this.actor._delegate = this;
1562
 
        this.actor.connect('key-focus-in', Lang.bind(this, this._onKeyFocusIn));
1563
 
        sourceActor.connect('style-changed',
1564
 
                            Lang.bind(this, this._onSourceActorStyleChanged));
1565
 
        this._activeItemPos = -1;
1566
 
        global.focus_manager.add_group(this.actor);
1567
 
    },
1568
 
 
1569
 
    _onKeyFocusIn: function(actor) {
1570
 
        let items = this._getMenuItems();
1571
 
        let activeItem = items[this._activeItemPos];
1572
 
        activeItem.actor.grab_key_focus();
1573
 
    },
1574
 
 
1575
 
    _onSourceActorStyleChanged: function() {
1576
 
        // PopupComboBoxMenuItem clones the active item's actors
1577
 
        // to work with arbitrary items in the menu; this means
1578
 
        // that we need to propagate some style information and
1579
 
        // enforce style updates even when the menu is closed
1580
 
        let activeItem = this._getMenuItems()[this._activeItemPos];
1581
 
        if (this.sourceActor.has_style_pseudo_class('insensitive'))
1582
 
            activeItem.actor.add_style_pseudo_class('insensitive');
1583
 
        else
1584
 
            activeItem.actor.remove_style_pseudo_class('insensitive');
1585
 
 
1586
 
        // To propagate the :active style, we need to make sure that the
1587
 
        // internal state of the PopupComboMenu is updated as well, but
1588
 
        // we must not move the keyboard grab
1589
 
        activeItem.setActive(this.sourceActor.has_style_pseudo_class('active'),
1590
 
                             { grabKeyboard: false });
1591
 
 
1592
 
        _ensureStyle(this.actor);
1593
 
    },
1594
 
 
1595
 
    open: function() {
1596
 
        if (this.isOpen)
1597
 
            return;
1598
 
 
1599
 
        if (this.isEmpty())
1600
 
            return;
1601
 
 
1602
 
        this.isOpen = true;
1603
 
 
1604
 
        let [sourceX, sourceY] = this.sourceActor.get_transformed_position();
1605
 
        let items = this._getMenuItems();
1606
 
        let activeItem = items[this._activeItemPos];
1607
 
 
1608
 
        this.actor.set_position(sourceX, sourceY - activeItem.actor.y);
1609
 
        this.actor.width = Math.max(this.actor.width, this.sourceActor.width);
1610
 
        this.actor.raise_top();
1611
 
 
1612
 
        this.actor.opacity = 0;
1613
 
        this.actor.show();
1614
 
 
1615
 
        Tweener.addTween(this.actor,
1616
 
                         { opacity: 255,
1617
 
                           transition: 'linear',
1618
 
                           time: BoxPointer.POPUP_ANIMATION_TIME });
1619
 
 
1620
 
        this.emit('open-state-changed', true);
1621
 
    },
1622
 
 
1623
 
    close: function() {
1624
 
        if (!this.isOpen)
1625
 
            return;
1626
 
 
1627
 
        this.isOpen = false;
1628
 
        Tweener.addTween(this.actor,
1629
 
                         { opacity: 0,
1630
 
                           transition: 'linear',
1631
 
                           time: BoxPointer.POPUP_ANIMATION_TIME,
1632
 
                           onComplete: Lang.bind(this,
1633
 
                               function() {
1634
 
                                   this.actor.hide();
1635
 
                               })
1636
 
                         });
1637
 
 
1638
 
        this.emit('open-state-changed', false);
1639
 
    },
1640
 
 
1641
 
    setActiveItem: function(position) {
1642
 
        this._activeItemPos = position;
1643
 
    },
1644
 
 
1645
 
    getActiveItem: function() {
1646
 
        return this._getMenuItems()[this._activeItemPos];
1647
 
    },
1648
 
 
1649
 
    setItemVisible: function(position, visible) {
1650
 
        if (!visible && position == this._activeItemPos) {
1651
 
            log('Trying to hide the active menu item.');
1652
 
            return;
1653
 
        }
1654
 
 
1655
 
        this._getMenuItems()[position].actor.visible = visible;
1656
 
    },
1657
 
 
1658
 
    getItemVisible: function(position) {
1659
 
        return this._getMenuItems()[position].actor.visible;
1660
 
    }
1661
 
});
1662
 
 
1663
 
const PopupComboBoxMenuItem = new Lang.Class({
1664
 
    Name: 'PopupComboBoxMenuItem',
1665
 
    Extends: PopupBaseMenuItem,
1666
 
 
1667
 
    _init: function (params) {
1668
 
        this.parent(params);
1669
 
 
1670
 
        this.actor.accessible_role = Atk.Role.COMBO_BOX;
1671
 
 
1672
 
        this._itemBox = new Shell.Stack();
1673
 
        this.addActor(this._itemBox);
1674
 
 
1675
 
        let expander = new St.Label({ text: '\u2304' });
1676
 
        this.addActor(expander, { align: St.Align.END,
1677
 
                                  span: -1 });
1678
 
 
1679
 
        this._menu = new PopupComboMenu(this.actor);
1680
 
        Main.uiGroup.add_actor(this._menu.actor);
1681
 
        this._menu.actor.hide();
1682
 
 
1683
 
        if (params.style_class)
1684
 
            this._menu.actor.add_style_class_name(params.style_class);
1685
 
 
1686
 
        this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
1687
 
 
1688
 
        this._activeItemPos = -1;
1689
 
        this._items = [];
1690
 
    },
1691
 
 
1692
 
    _getTopMenu: function() {
1693
 
        let actor = this.actor.get_parent();
1694
 
        while (actor) {
1695
 
            if (actor._delegate && actor._delegate instanceof PopupMenu)
1696
 
                return actor._delegate;
1697
 
 
1698
 
            actor = actor.get_parent();
1699
 
        }
1700
 
 
1701
 
        return null;
1702
 
    },
1703
 
 
1704
 
    _onScrollEvent: function(actor, event) {
1705
 
        if (this._activeItemPos == -1)
1706
 
            return;
1707
 
 
1708
 
        let position = this._activeItemPos;
1709
 
        let direction = event.get_scroll_direction();
1710
 
        if (direction == Clutter.ScrollDirection.DOWN) {
1711
 
            while (position < this._items.length - 1) {
1712
 
                position++;
1713
 
                if (this._menu.getItemVisible(position))
1714
 
                    break;
1715
 
            }
1716
 
        } else if (direction == Clutter.ScrollDirection.UP) {
1717
 
            while (position > 0) {
1718
 
                position--;
1719
 
                if (this._menu.getItemVisible(position))
1720
 
                    break;
1721
 
            }
1722
 
        }
1723
 
 
1724
 
        if (position == this._activeItemPos)
1725
 
            return;
1726
 
 
1727
 
        this.setActiveItem(position);
1728
 
        this.emit('active-item-changed', position);
1729
 
    },
1730
 
 
1731
 
    activate: function(event) {
1732
 
        let topMenu = this._getTopMenu();
1733
 
        if (!topMenu)
1734
 
            return;
1735
 
 
1736
 
        topMenu.addChildMenu(this._menu);
1737
 
        this._menu.toggle();
1738
 
    },
1739
 
 
1740
 
    addMenuItem: function(menuItem, position) {
1741
 
        if (position === undefined)
1742
 
            position = this._menu.numMenuItems;
1743
 
 
1744
 
        this._menu.addMenuItem(menuItem, position);
1745
 
        _ensureStyle(this._menu.actor);
1746
 
 
1747
 
        let item = new St.BoxLayout({ style_class: 'popup-combobox-item' });
1748
 
 
1749
 
        let children = menuItem.actor.get_children();
1750
 
        for (let i = 0; i < children.length; i++) {
1751
 
            let clone = new Clutter.Clone({ source: children[i] });
1752
 
            item.add(clone, { y_fill: false });
1753
 
        }
1754
 
 
1755
 
        let oldItem = this._items[position];
1756
 
        if (oldItem)
1757
 
            this._itemBox.remove_actor(oldItem);
1758
 
 
1759
 
        this._items[position] = item;
1760
 
        this._itemBox.add_actor(item);
1761
 
 
1762
 
        menuItem.connect('activate',
1763
 
                         Lang.bind(this, this._itemActivated, position));
1764
 
    },
1765
 
 
1766
 
    checkAccessibleLabel: function() {
1767
 
        let activeItem = this._menu.getActiveItem();
1768
 
        this.actor.label_actor = activeItem.label;
1769
 
    },
1770
 
 
1771
 
    setActiveItem: function(position) {
1772
 
        let item = this._items[position];
1773
 
        if (!item)
1774
 
            return;
1775
 
        if (this._activeItemPos == position)
1776
 
            return;
1777
 
        this._menu.setActiveItem(position);
1778
 
        this._activeItemPos = position;
1779
 
        for (let i = 0; i < this._items.length; i++)
1780
 
            this._items[i].visible = (i == this._activeItemPos);
1781
 
 
1782
 
        this.checkAccessibleLabel();
1783
 
    },
1784
 
 
1785
 
    setItemVisible: function(position, visible) {
1786
 
        this._menu.setItemVisible(position, visible);
1787
 
    },
1788
 
 
1789
 
    _itemActivated: function(menuItem, event, position) {
1790
 
        this.setActiveItem(position);
1791
 
        this.emit('active-item-changed', position);
1792
 
    }
1793
 
});
1794
 
 
1795
 
/**
1796
 
 * RemoteMenu:
1797
 
 *
1798
 
 * A PopupMenu that tracks a GMenuModel and shows its actions
1799
 
 * (exposed by GApplication/GActionGroup)
1800
 
 */
1801
 
const RemoteMenu = new Lang.Class({
1802
 
    Name: 'RemoteMenu',
1803
 
    Extends: PopupMenu,
1804
 
 
1805
 
    _init: function(sourceActor, model, actionGroup) {
1806
 
        this.parent(sourceActor, 0.0, St.Side.TOP);
1807
 
 
1808
 
        this.model = model;
1809
 
        this.actionGroup = actionGroup;
1810
 
 
1811
 
        this._actions = { };
1812
 
        this._modelChanged(this.model, 0, 0, this.model.get_n_items(), this);
1813
 
 
1814
 
        this._actionStateChangeId = this.actionGroup.connect('action-state-changed', Lang.bind(this, this._actionStateChanged));
1815
 
        this._actionEnableChangeId = this.actionGroup.connect('action-enabled-changed', Lang.bind(this, this._actionEnabledChanged));
1816
 
    },
1817
 
 
1818
 
    destroy: function() {
1819
 
        if (this._actionStateChangeId) {
1820
 
            this.actionGroup.disconnect(this._actionStateChangeId);
1821
 
            this._actionStateChangeId = 0;
1822
 
        }
1823
 
 
1824
 
        if (this._actionEnableChangeId) {
1825
 
            this.actionGroup.disconnect(this._actionEnableChangeId);
1826
 
            this._actionEnableChangeId = 0;
1827
 
        }
1828
 
 
1829
 
        this.parent();
1830
 
    },
1831
 
 
1832
 
    _createMenuItem: function(model, index) {
1833
 
        let labelValue = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_LABEL, null);
1834
 
        let label = labelValue ? labelValue.deep_unpack() : '';
1835
 
        // remove all underscores that are not followed by another underscore
1836
 
        label = label.replace(/_([^_])/, '$1');
1837
 
 
1838
 
        let section_link = model.get_item_link(index, Gio.MENU_LINK_SECTION);
1839
 
        if (section_link) {
1840
 
            let item = new PopupMenuSection();
1841
 
            if (label) {
1842
 
                let title = new PopupMenuItem(label, { reactive: false,
1843
 
                                                       style_class: 'popup-subtitle-menu-item' });
1844
 
                item._titleMenuItem = title;
1845
 
                title._ignored = true;
1846
 
                item.addMenuItem(title);
1847
 
            }
1848
 
            this._modelChanged(section_link, 0, 0, section_link.get_n_items(), item);
1849
 
            return [item, true, ''];
1850
 
        }
1851
 
 
1852
 
        let submenu_link = model.get_item_link(index, Gio.MENU_LINK_SUBMENU);
1853
 
 
1854
 
        if (submenu_link) {
1855
 
            let item = new PopupSubMenuMenuItem(label);
1856
 
            this._modelChanged(submenu_link, 0, 0, submenu_link.get_n_items(), item.menu);
1857
 
            return [item, false, ''];
1858
 
        }
1859
 
 
1860
 
        let action_id = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_ACTION, null).deep_unpack();
1861
 
        if (!this.actionGroup.has_action(action_id)) {
1862
 
            // the action may not be there yet, wait for action-added
1863
 
            return [null, false, 'action-added'];
1864
 
        }
1865
 
 
1866
 
        if (!this._actions[action_id])
1867
 
            this._actions[action_id] = { enabled: this.actionGroup.get_action_enabled(action_id),
1868
 
                                         state: this.actionGroup.get_action_state(action_id),
1869
 
                                         items: [ ],
1870
 
                                       };
1871
 
        let action = this._actions[action_id];
1872
 
        let item, target, destroyId, specificSignalId;
1873
 
 
1874
 
        if (action.state) {
1875
 
            // Docs have get_state_hint(), except that the DBus protocol
1876
 
            // has no provision for it (so ShellApp does not implement it,
1877
 
            // and neither GApplication), and g_action_get_state_hint()
1878
 
            // always returns null
1879
 
            // Funny :)
1880
 
 
1881
 
            switch (String.fromCharCode(action.state.classify())) {
1882
 
            case 'b':
1883
 
                item = new PopupSwitchMenuItem(label, action.state.get_boolean());
1884
 
                action.items.push(item);
1885
 
                specificSignalId = item.connect('toggled', Lang.bind(this, function(item) {
1886
 
                    this.actionGroup.activate_action(action_id, null);
1887
 
                }));
1888
 
                break;
1889
 
            case 's':
1890
 
                item = new PopupMenuItem(label);
1891
 
                item._remoteTarget = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_TARGET, null).deep_unpack();
1892
 
                action.items.push(item);
1893
 
                item.setShowDot(action.state.deep_unpack() == item._remoteTarget);
1894
 
                specificSignalId = item.connect('activate', Lang.bind(this, function(item) {
1895
 
                    this.actionGroup.activate_action(action_id, GLib.Variant.new_string(item._remoteTarget));
1896
 
                }));
1897
 
                break;
1898
 
            default:
1899
 
                log('Action "%s" has state of type %s, which is not supported'.format(action_id, action.state.get_type_string()));
1900
 
                return [null, false, 'action-state-changed'];
1901
 
            }
1902
 
        } else {
1903
 
            target = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_TARGET, null);
1904
 
            item = new PopupMenuItem(label);
1905
 
            action.items.push(item);
1906
 
            specificSignalId = item.connect('activate', Lang.bind(this, function() {
1907
 
                this.actionGroup.activate_action(action_id, target);
1908
 
            }));
1909
 
        }
1910
 
 
1911
 
        item.actor.reactive = item.actor.can_focus = action.enabled;
1912
 
 
1913
 
        destroyId = item.connect('destroy', Lang.bind(this, function() {
1914
 
            item.disconnect(destroyId);
1915
 
            item.disconnect(specificSignalId);
1916
 
 
1917
 
            let pos = action.items.indexOf(item);
1918
 
            if (pos != -1)
1919
 
                action.items.splice(pos, 1);
1920
 
        }));
1921
 
 
1922
 
        return [item, false, ''];
1923
 
    }, 
1924
 
 
1925
 
    _modelChanged: function(model, position, removed, added, target) {
1926
 
        let j, k;
1927
 
        let j0, k0;
1928
 
 
1929
 
        let currentItems = target._getMenuItems();
1930
 
 
1931
 
        k0 = 0;
1932
 
        // skip ignored items at the beginning
1933
 
        while (k0 < currentItems.length && currentItems[k0]._ignored)
1934
 
            k0++;
1935
 
        // find the right menu item matching the model item
1936
 
        for (j0 = 0; k0 < currentItems.length && j0 < position; j0++, k0++) {
1937
 
            if (currentItems[k0]._ignored)
1938
 
                k0++;
1939
 
        }
1940
 
 
1941
 
        if (removed == -1) {
1942
 
            // special flag to indicate we should destroy everything
1943
 
            for (k = k0; k < currentItems.length; k++)
1944
 
                currentItems[k].destroy();
1945
 
        } else {
1946
 
            for (j = j0, k = k0; k < currentItems.length && j < j0 + removed; j++, k++) {
1947
 
                currentItems[k].destroy();
1948
 
 
1949
 
                if (currentItems[k]._ignored)
1950
 
                    j--;
1951
 
            }
1952
 
        }
1953
 
 
1954
 
        for (j = j0, k = k0; j < j0 + added; j++, k++) {
1955
 
            let [item, addSeparator, changeSignal] = this._createMenuItem(model, j);
1956
 
 
1957
 
            if (item) {
1958
 
                // separators must be added in the parent to make autohiding work
1959
 
                if (addSeparator) {
1960
 
                    let separator = new PopupSeparatorMenuItem();
1961
 
                    item.separators.push(separator);
1962
 
                    separator._ignored = true;
1963
 
                    target.addMenuItem(separator, k+1);
1964
 
                    k++;
1965
 
                }
1966
 
 
1967
 
                target.addMenuItem(item, k);
1968
 
 
1969
 
                if (addSeparator) {
1970
 
                    let separator = new PopupSeparatorMenuItem();
1971
 
                    item.separators.push(separator);
1972
 
                    separator._ignored = true;
1973
 
                    target.addMenuItem(separator, k+1);
1974
 
                    k++;
1975
 
                }
1976
 
            } else if (changeSignal) {
1977
 
                let signalId = this.actionGroup.connect(changeSignal, Lang.bind(this, function(actionGroup, actionName) {
1978
 
                    actionGroup.disconnect(signalId);
1979
 
                    if (this._actions[actionName]) return;
1980
 
 
1981
 
                    // force a full update
1982
 
                    this._modelChanged(model, 0, -1, model.get_n_items(), target);
1983
 
                }));
1984
 
            }
1985
 
        }
1986
 
 
1987
 
        if (!model._changedId) {
1988
 
            model._changedId = model.connect('items-changed', Lang.bind(this, this._modelChanged, target));
1989
 
            model._destroyId = target.connect('destroy', function() {
1990
 
                if (model._changedId)
1991
 
                    model.disconnect(model._changedId);
1992
 
                if (model._destroyId)
1993
 
                    target.disconnect(model._destroyId);
1994
 
                model._changedId = 0;
1995
 
                model._destroyId = 0;
1996
 
            });
1997
 
        }
1998
 
 
1999
 
        if (target instanceof PopupMenuSection) {
2000
 
            if (target._titleMenuItem)
2001
 
                target.actor.visible = target.numMenuItems != 1;
2002
 
            else
2003
 
                target.actor.visible = target.numMenuItems != 0;
2004
 
        } else {
2005
 
            let sourceItem = target.sourceActor._delegate;
2006
 
            if (sourceItem instanceof PopupSubMenuMenuItem)
2007
 
                sourceItem.actor.visible = target.numMenuItems != 0;
2008
 
        }
2009
 
    },
2010
 
 
2011
 
    _actionStateChanged: function(actionGroup, action_id) {
2012
 
        let action = this._actions[action_id];
2013
 
        if (!action)
2014
 
            return;
2015
 
 
2016
 
        action.state = actionGroup.get_action_state(action_id);
2017
 
        if (action.items.length) {
2018
 
            switch (String.fromCharCode(action.state.classify())) {
2019
 
            case 'b':
2020
 
                for (let i = 0; i < action.items.length; i++)
2021
 
                    action.items[i].setToggleState(action.state.get_boolean());
2022
 
                break;
2023
 
            case 'd':
2024
 
                for (let i = 0; i < action.items.length; i++)
2025
 
                    action.items[i].setValue(action.state.get_double());
2026
 
                break;
2027
 
            case 's':
2028
 
                for (let i = 0; i < action.items.length; i++)
2029
 
                    action.items[i].setShowDot(action.items[i]._remoteTarget == action.state.deep_unpack());
2030
 
            }
2031
 
        }
2032
 
    },
2033
 
 
2034
 
    _actionEnabledChanged: function(actionGroup, action_id) {
2035
 
        let action = this._actions[action_id];
2036
 
        if (!action)
2037
 
            return;
2038
 
 
2039
 
        action.enabled = actionGroup.get_action_enabled(action_id);
2040
 
        if (action.items.length) {
2041
 
            for (let i = 0; i < action.items.length; i++) {
2042
 
                let item = action.items[i];
2043
 
                item.actor.reactive = item.actor.can_focus = action.enabled;
2044
 
            }
2045
 
        }
2046
 
    }
2047
 
});
2048
 
 
2049
 
/* Basic implementation of a menu manager.
2050
 
 * Call addMenu to add menus
2051
 
 */
2052
 
const PopupMenuManager = new Lang.Class({
2053
 
    Name: 'PopupMenuManager',
2054
 
 
2055
 
    _init: function(owner, grabParams) {
2056
 
        this._owner = owner;
2057
 
        this._grabHelper = new GrabHelper.GrabHelper(owner.actor, grabParams);
2058
 
        this._menus = [];
2059
 
    },
2060
 
 
2061
 
    addMenu: function(menu, position) {
2062
 
        if (this._findMenu(menu) > -1)
2063
 
            return;
2064
 
 
2065
 
        let menudata = {
2066
 
            menu:              menu,
2067
 
            openStateChangeId: menu.connect('open-state-changed', Lang.bind(this, this._onMenuOpenState)),
2068
 
            childMenuAddedId:  menu.connect('child-menu-added', Lang.bind(this, this._onChildMenuAdded)),
2069
 
            childMenuRemovedId: menu.connect('child-menu-removed', Lang.bind(this, this._onChildMenuRemoved)),
2070
 
            destroyId:         menu.connect('destroy', Lang.bind(this, this._onMenuDestroy)),
2071
 
            enterId:           0,
2072
 
            focusInId:         0
2073
 
        };
2074
 
 
2075
 
        let source = menu.sourceActor;
2076
 
        if (source) {
2077
 
            if (!menu.blockSourceEvents)
2078
 
                this._grabHelper.addActor(source);
2079
 
            menudata.enterId = source.connect('enter-event', Lang.bind(this, function() { this._onMenuSourceEnter(menu); }));
2080
 
            menudata.focusInId = source.connect('key-focus-in', Lang.bind(this, function() { this._onMenuSourceEnter(menu); }));
2081
 
        }
2082
 
 
2083
 
        if (position == undefined)
2084
 
            this._menus.push(menudata);
2085
 
        else
2086
 
            this._menus.splice(position, 0, menudata);
2087
 
    },
2088
 
 
2089
 
    removeMenu: function(menu) {
2090
 
        if (menu == this.activeMenu)
2091
 
            this._closeMenu(menu);
2092
 
 
2093
 
        let position = this._findMenu(menu);
2094
 
        if (position == -1) // not a menu we manage
2095
 
            return;
2096
 
 
2097
 
        let menudata = this._menus[position];
2098
 
        menu.disconnect(menudata.openStateChangeId);
2099
 
        menu.disconnect(menudata.childMenuAddedId);
2100
 
        menu.disconnect(menudata.childMenuRemovedId);
2101
 
        menu.disconnect(menudata.destroyId);
2102
 
 
2103
 
        if (menudata.enterId)
2104
 
            menu.sourceActor.disconnect(menudata.enterId);
2105
 
        if (menudata.focusInId)
2106
 
            menu.sourceActor.disconnect(menudata.focusInId);
2107
 
 
2108
 
        if (menu.sourceActor)
2109
 
            this._grabHelper.removeActor(menu.sourceActor);
2110
 
        this._menus.splice(position, 1);
2111
 
    },
2112
 
 
2113
 
    get activeMenu() {
2114
 
        let firstGrab = this._grabHelper.grabStack[0];
2115
 
        if (firstGrab)
2116
 
            return firstGrab.actor._delegate;
2117
 
        else
2118
 
            return null;
2119
 
    },
2120
 
 
2121
 
    ignoreRelease: function() {
2122
 
        return this._grabHelper.ignoreRelease();
2123
 
    },
2124
 
 
2125
 
    _onMenuOpenState: function(menu, open) {
2126
 
        if (open) {
2127
 
            if (this.activeMenu)
2128
 
                this.activeMenu.close(BoxPointer.PopupAnimation.FADE);
2129
 
            this._grabHelper.grab({ actor: menu.actor, modal: true, focus: menu.sourceActor,
2130
 
                                    onUngrab: Lang.bind(this, this._closeMenu, menu) });
2131
 
        } else {
2132
 
            this._grabHelper.ungrab({ actor: menu.actor });
2133
 
        }
2134
 
    },
2135
 
 
2136
 
    _onChildMenuAdded: function(menu, childMenu) {
2137
 
        this.addMenu(childMenu);
2138
 
    },
2139
 
 
2140
 
    _onChildMenuRemoved: function(menu, childMenu) {
2141
 
        this.removeMenu(childMenu);
2142
 
    },
2143
 
 
2144
 
    _changeMenu: function(newMenu) {
2145
 
        newMenu.open(this.activeMenu ? BoxPointer.PopupAnimation.FADE
2146
 
                                     : BoxPointer.PopupAnimation.FULL);
2147
 
    },
2148
 
 
2149
 
    _onMenuSourceEnter: function(menu) {
2150
 
        if (!this._grabHelper.grabbed)
2151
 
            return false;
2152
 
 
2153
 
        if (this._grabHelper.isActorGrabbed(menu.actor))
2154
 
            return false;
2155
 
 
2156
 
        let isChildMenu = this._grabHelper.grabStack.some(function(grab) {
2157
 
            let existingMenu = grab.actor._delegate;
2158
 
            return existingMenu.isChildMenu(menu);
2159
 
        });
2160
 
        if (isChildMenu)
2161
 
            return false;
2162
 
 
2163
 
        this._changeMenu(menu);
2164
 
        return false;
2165
 
    },
2166
 
 
2167
 
    _onMenuDestroy: function(menu) {
2168
 
        this.removeMenu(menu);
2169
 
    },
2170
 
 
2171
 
    _findMenu: function(item) {
2172
 
        for (let i = 0; i < this._menus.length; i++) {
2173
 
            let menudata = this._menus[i];
2174
 
            if (item == menudata.menu)
2175
 
                return i;
2176
 
        }
2177
 
        return -1;
2178
 
    },
2179
 
 
2180
 
    _closeMenu: function(isUser, menu) {
2181
 
        // If this isn't a user action, we called close()
2182
 
        // on the BoxPointer ourselves, so we shouldn't
2183
 
        // reanimate.
2184
 
        if (isUser)
2185
 
            menu.close(BoxPointer.PopupAnimation.FULL);
2186
 
    }
2187
 
});