2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-05-20 11:18:46 -04:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2011-05-15 12:55:23 -04:00
|
|
|
const GLib = imports.gi.GLib;
|
2010-05-20 11:18:46 -04:00
|
|
|
const Gtk = imports.gi.Gtk;
|
2011-05-15 12:55:23 -04:00
|
|
|
const Gio = imports.gi.Gio;
|
2010-05-20 11:18:46 -04:00
|
|
|
const Lang = imports.lang;
|
2010-07-22 08:34:02 -04:00
|
|
|
const Shell = imports.gi.Shell;
|
2010-05-20 11:18:46 -04:00
|
|
|
const Signals = imports.signals;
|
2010-07-22 08:34:02 -04:00
|
|
|
const St = imports.gi.St;
|
2012-02-27 11:31:10 -05:00
|
|
|
const Atk = imports.gi.Atk;
|
2010-05-20 11:18:46 -04:00
|
|
|
|
|
|
|
const BoxPointer = imports.ui.boxpointer;
|
2012-02-29 19:09:41 -05:00
|
|
|
const GrabHelper = imports.ui.grabHelper;
|
2010-07-03 19:47:31 -04:00
|
|
|
const Main = imports.ui.main;
|
|
|
|
const Params = imports.misc.params;
|
2012-08-15 21:28:49 -04:00
|
|
|
const Separator = imports.ui.separator;
|
2010-05-20 11:18:46 -04:00
|
|
|
const Tweener = imports.ui.tweener;
|
|
|
|
|
2010-10-20 16:41:54 -04:00
|
|
|
const SLIDER_SCROLL_STEP = 0.05; /* Slider scrolling step in % */
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
function _ensureStyle(actor) {
|
|
|
|
if (actor.get_children) {
|
|
|
|
let children = actor.get_children();
|
|
|
|
for (let i = 0; i < children.length; i++)
|
|
|
|
_ensureStyle(children[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actor instanceof St.Widget)
|
|
|
|
actor.ensure_style();
|
|
|
|
}
|
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupBaseMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupBaseMenuItem',
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-07-03 19:47:31 -04:00
|
|
|
_init: function (params) {
|
|
|
|
params = Params.parse (params, { reactive: true,
|
|
|
|
activate: true,
|
2011-01-25 16:06:40 -05:00
|
|
|
hover: true,
|
2011-09-15 10:51:19 -04:00
|
|
|
sensitive: true,
|
2012-08-17 05:03:23 -04:00
|
|
|
style_class: null,
|
|
|
|
can_focus: true
|
2011-01-25 16:06:40 -05:00
|
|
|
});
|
2010-10-19 13:41:41 -04:00
|
|
|
this.actor = new Shell.GenericContainer({ style_class: 'popup-menu-item',
|
|
|
|
reactive: params.reactive,
|
2010-10-07 14:15:51 -04:00
|
|
|
track_hover: params.reactive,
|
2012-08-17 05:03:23 -04:00
|
|
|
can_focus: params.can_focus,
|
2012-02-27 11:31:10 -05:00
|
|
|
accessible_role: Atk.Role.MENU_ITEM});
|
2010-10-19 13:41:41 -04:00
|
|
|
this.actor.connect('get-preferred-width', Lang.bind(this, this._getPreferredWidth));
|
|
|
|
this.actor.connect('get-preferred-height', Lang.bind(this, this._getPreferredHeight));
|
|
|
|
this.actor.connect('allocate', Lang.bind(this, this._allocate));
|
|
|
|
this.actor.connect('style-changed', Lang.bind(this, this._onStyleChanged));
|
2010-05-20 11:18:46 -04:00
|
|
|
this.actor._delegate = this;
|
2010-10-19 13:41:41 -04:00
|
|
|
|
|
|
|
this._children = [];
|
2010-10-20 12:43:22 -04:00
|
|
|
this._dot = null;
|
2010-10-19 13:41:41 -04:00
|
|
|
this._columnWidths = null;
|
|
|
|
this._spacing = 0;
|
2010-05-20 11:18:46 -04:00
|
|
|
this.active = false;
|
2011-09-15 10:51:19 -04:00
|
|
|
this._activatable = params.reactive && params.activate;
|
|
|
|
this.sensitive = this._activatable && params.sensitive;
|
|
|
|
|
|
|
|
this.setSensitive(this.sensitive);
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2012-09-11 08:00:22 -04:00
|
|
|
if (!this._activatable)
|
|
|
|
this.actor.add_style_class_name('popup-inactive-menu-item');
|
|
|
|
|
2011-01-25 16:06:40 -05:00
|
|
|
if (params.style_class)
|
|
|
|
this.actor.add_style_class_name(params.style_class);
|
|
|
|
|
2011-09-15 10:51:19 -04:00
|
|
|
if (this._activatable) {
|
2010-10-07 14:15:51 -04:00
|
|
|
this.actor.connect('button-release-event', Lang.bind(this, this._onButtonReleaseEvent));
|
|
|
|
this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2010-07-03 19:47:31 -04:00
|
|
|
if (params.reactive && params.hover)
|
2010-10-07 14:15:51 -04:00
|
|
|
this.actor.connect('notify::hover', Lang.bind(this, this._onHoverChanged));
|
2012-08-17 05:03:23 -04:00
|
|
|
|
|
|
|
this.actor.connect('key-focus-in', Lang.bind(this, this._onKeyFocusIn));
|
|
|
|
this.actor.connect('key-focus-out', Lang.bind(this, this._onKeyFocusOut));
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2010-10-19 13:41:41 -04:00
|
|
|
_onStyleChanged: function (actor) {
|
2011-03-25 18:40:38 -04:00
|
|
|
this._spacing = Math.round(actor.get_theme_node().get_length('spacing'));
|
2010-10-19 13:41:41 -04:00
|
|
|
},
|
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
_onButtonReleaseEvent: function (actor, event) {
|
2010-11-01 11:03:28 -04:00
|
|
|
this.activate(event);
|
2010-10-07 14:15:51 -04:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onKeyPressEvent: function (actor, event) {
|
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
|
|
|
|
if (symbol == Clutter.KEY_space || symbol == Clutter.KEY_Return) {
|
2010-11-01 11:03:28 -04:00
|
|
|
this.activate(event);
|
2010-10-07 14:15:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onKeyFocusIn: function (actor) {
|
|
|
|
this.setActive(true);
|
|
|
|
},
|
|
|
|
|
2011-02-11 14:43:01 -05:00
|
|
|
_onKeyFocusOut: function (actor) {
|
|
|
|
this.setActive(false);
|
|
|
|
},
|
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
_onHoverChanged: function (actor) {
|
2010-05-20 11:18:46 -04:00
|
|
|
this.setActive(actor.hover);
|
|
|
|
},
|
|
|
|
|
|
|
|
activate: function (event) {
|
|
|
|
this.emit('activate', event);
|
|
|
|
},
|
|
|
|
|
2011-10-26 13:24:36 -04:00
|
|
|
setActive: function (active, params) {
|
2010-05-20 11:18:46 -04:00
|
|
|
let activeChanged = active != this.active;
|
2011-10-26 13:24:36 -04:00
|
|
|
params = Params.parse (params, { grabKeyboard: true });
|
2010-05-20 11:18:46 -04:00
|
|
|
|
|
|
|
if (activeChanged) {
|
|
|
|
this.active = active;
|
2010-10-07 14:15:51 -04:00
|
|
|
if (active) {
|
2010-05-20 11:18:46 -04:00
|
|
|
this.actor.add_style_pseudo_class('active');
|
2011-10-26 13:24:36 -04:00
|
|
|
if (params.grabKeyboard)
|
|
|
|
this.actor.grab_key_focus();
|
2010-10-07 14:15:51 -04:00
|
|
|
} else
|
2010-05-20 11:18:46 -04:00
|
|
|
this.actor.remove_style_pseudo_class('active');
|
|
|
|
this.emit('active-changed', active);
|
|
|
|
}
|
2010-07-03 19:47:31 -04:00
|
|
|
},
|
|
|
|
|
2011-09-15 10:51:19 -04:00
|
|
|
setSensitive: function(sensitive) {
|
|
|
|
if (!this._activatable)
|
|
|
|
return;
|
|
|
|
if (this.sensitive == sensitive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.sensitive = sensitive;
|
|
|
|
this.actor.reactive = sensitive;
|
2012-08-19 21:40:09 -04:00
|
|
|
this.actor.can_focus = sensitive;
|
2011-09-15 10:51:19 -04:00
|
|
|
|
|
|
|
this.emit('sensitive-changed', sensitive);
|
|
|
|
},
|
|
|
|
|
2010-07-03 19:47:31 -04:00
|
|
|
destroy: function() {
|
|
|
|
this.actor.destroy();
|
|
|
|
this.emit('destroy');
|
|
|
|
},
|
|
|
|
|
2010-11-17 14:10:12 -05:00
|
|
|
// adds an actor to the menu item; @params can contain %span
|
|
|
|
// (column span; defaults to 1, -1 means "all the remaining width"),
|
|
|
|
// %expand (defaults to #false), and %align (defaults to
|
|
|
|
// #St.Align.START)
|
|
|
|
addActor: function(child, params) {
|
|
|
|
params = Params.parse(params, { span: 1,
|
|
|
|
expand: false,
|
|
|
|
align: St.Align.START });
|
|
|
|
params.actor = child;
|
|
|
|
this._children.push(params);
|
2010-10-28 16:28:17 -04:00
|
|
|
this.actor.connect('destroy', Lang.bind(this, function () { this._removeChild(child); }));
|
2010-10-19 13:41:41 -04:00
|
|
|
this.actor.add_actor(child);
|
|
|
|
},
|
|
|
|
|
2010-10-28 16:28:17 -04:00
|
|
|
_removeChild: function(child) {
|
2010-10-19 13:41:41 -04:00
|
|
|
for (let i = 0; i < this._children.length; i++) {
|
|
|
|
if (this._children[i].actor == child) {
|
|
|
|
this._children.splice(i, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-10-28 16:28:17 -04:00
|
|
|
removeActor: function(child) {
|
|
|
|
this.actor.remove_actor(child);
|
|
|
|
this._removeChild(child);
|
|
|
|
},
|
|
|
|
|
2010-10-20 12:43:22 -04:00
|
|
|
setShowDot: function(show) {
|
|
|
|
if (show) {
|
|
|
|
if (this._dot)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._dot = new St.DrawingArea({ style_class: 'popup-menu-item-dot' });
|
|
|
|
this._dot.connect('repaint', Lang.bind(this, this._onRepaintDot));
|
|
|
|
this.actor.add_actor(this._dot);
|
2012-07-31 11:53:37 -04:00
|
|
|
this.actor.add_accessible_state (Atk.StateType.CHECKED);
|
2010-10-20 12:43:22 -04:00
|
|
|
} else {
|
|
|
|
if (!this._dot)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._dot.destroy();
|
|
|
|
this._dot = null;
|
2012-07-31 11:53:37 -04:00
|
|
|
this.actor.remove_accessible_state (Atk.StateType.CHECKED);
|
2010-10-20 12:43:22 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onRepaintDot: function(area) {
|
|
|
|
let cr = area.get_context();
|
|
|
|
let [width, height] = area.get_surface_size();
|
2011-02-14 09:20:22 -05:00
|
|
|
let color = area.get_theme_node().get_foreground_color();
|
2010-10-20 12:43:22 -04:00
|
|
|
|
|
|
|
cr.setSourceRGBA (
|
|
|
|
color.red / 255,
|
|
|
|
color.green / 255,
|
|
|
|
color.blue / 255,
|
|
|
|
color.alpha / 255);
|
|
|
|
cr.arc(width / 2, height / 2, width / 3, 0, 2 * Math.PI);
|
|
|
|
cr.fill();
|
2013-01-07 15:07:40 -05:00
|
|
|
cr.$dispose();
|
2010-10-20 12:43:22 -04:00
|
|
|
},
|
|
|
|
|
2011-03-22 12:57:48 -04:00
|
|
|
// This returns column widths in logical order (i.e. from the dot
|
|
|
|
// to the image), not in visual order (left to right)
|
2010-10-19 13:41:41 -04:00
|
|
|
getColumnWidths: function() {
|
|
|
|
let widths = [];
|
|
|
|
for (let i = 0, col = 0; i < this._children.length; i++) {
|
|
|
|
let child = this._children[i];
|
|
|
|
let [min, natural] = child.actor.get_preferred_width(-1);
|
|
|
|
widths[col++] = natural;
|
|
|
|
if (child.span > 1) {
|
|
|
|
for (let j = 1; j < child.span; j++)
|
|
|
|
widths[col++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return widths;
|
|
|
|
},
|
|
|
|
|
|
|
|
setColumnWidths: function(widths) {
|
|
|
|
this._columnWidths = widths;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPreferredWidth: function(actor, forHeight, alloc) {
|
|
|
|
let width = 0;
|
|
|
|
if (this._columnWidths) {
|
|
|
|
for (let i = 0; i < this._columnWidths.length; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
width += this._spacing;
|
|
|
|
width += this._columnWidths[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let i = 0; i < this._children.length; i++) {
|
|
|
|
let child = this._children[i];
|
|
|
|
if (i > 0)
|
|
|
|
width += this._spacing;
|
2011-08-22 14:29:22 -04:00
|
|
|
let [min, natural] = child.actor.get_preferred_width(-1);
|
2010-10-19 13:41:41 -04:00
|
|
|
width += natural;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alloc.min_size = alloc.natural_size = width;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPreferredHeight: function(actor, forWidth, alloc) {
|
2011-09-19 16:46:38 -04:00
|
|
|
let height = 0, x = 0, minWidth, childWidth;
|
2010-10-19 13:41:41 -04:00
|
|
|
for (let i = 0; i < this._children.length; i++) {
|
|
|
|
let child = this._children[i];
|
2011-09-19 16:46:38 -04:00
|
|
|
if (this._columnWidths) {
|
|
|
|
if (child.span == -1) {
|
|
|
|
childWidth = 0;
|
|
|
|
for (let j = i; j < this._columnWidths.length; j++)
|
|
|
|
childWidth += this._columnWidths[j]
|
|
|
|
} else
|
|
|
|
childWidth = this._columnWidths[i];
|
|
|
|
} else {
|
|
|
|
if (child.span == -1)
|
|
|
|
childWidth = forWidth - x;
|
|
|
|
else
|
|
|
|
[minWidth, childWidth] = child.actor.get_preferred_width(-1);
|
|
|
|
}
|
|
|
|
x += childWidth;
|
|
|
|
|
|
|
|
let [min, natural] = child.actor.get_preferred_height(childWidth);
|
2010-10-19 13:41:41 -04:00
|
|
|
if (natural > height)
|
|
|
|
height = natural;
|
|
|
|
}
|
|
|
|
alloc.min_size = alloc.natural_size = height;
|
|
|
|
},
|
|
|
|
|
|
|
|
_allocate: function(actor, box, flags) {
|
2010-10-20 12:43:22 -04:00
|
|
|
let height = box.y2 - box.y1;
|
2012-02-13 20:37:28 -05:00
|
|
|
let direction = this.actor.get_text_direction();
|
2010-10-20 12:43:22 -04:00
|
|
|
|
|
|
|
if (this._dot) {
|
2011-03-22 12:57:48 -04:00
|
|
|
// The dot is placed outside box
|
|
|
|
// one quarter of padding from the border of the container
|
|
|
|
// (so 3/4 from the inner border)
|
|
|
|
// (padding is box.x1)
|
2010-10-20 12:43:22 -04:00
|
|
|
let dotBox = new Clutter.ActorBox();
|
|
|
|
let dotWidth = Math.round(box.x1 / 2);
|
|
|
|
|
2012-02-13 20:37:28 -05:00
|
|
|
if (direction == Clutter.TextDirection.LTR) {
|
2011-03-22 12:57:48 -04:00
|
|
|
dotBox.x1 = Math.round(box.x1 / 4);
|
|
|
|
dotBox.x2 = dotBox.x1 + dotWidth;
|
|
|
|
} else {
|
|
|
|
dotBox.x2 = box.x2 + 3 * Math.round(box.x1 / 4);
|
|
|
|
dotBox.x1 = dotBox.x2 - dotWidth;
|
|
|
|
}
|
2010-10-20 12:43:22 -04:00
|
|
|
dotBox.y1 = Math.round(box.y1 + (height - dotWidth) / 2);
|
|
|
|
dotBox.y2 = dotBox.y1 + dotWidth;
|
|
|
|
this._dot.allocate(dotBox, flags);
|
|
|
|
}
|
|
|
|
|
2011-03-22 12:57:48 -04:00
|
|
|
let x;
|
2012-02-13 20:37:28 -05:00
|
|
|
if (direction == Clutter.TextDirection.LTR)
|
2011-03-22 12:57:48 -04:00
|
|
|
x = box.x1;
|
|
|
|
else
|
|
|
|
x = box.x2;
|
|
|
|
// if direction is ltr, x is the right edge of the last added
|
|
|
|
// actor, and it's constantly increasing, whereas if rtl, x is
|
|
|
|
// the left edge and it decreases
|
2010-10-19 13:41:41 -04:00
|
|
|
for (let i = 0, col = 0; i < this._children.length; i++) {
|
|
|
|
let child = this._children[i];
|
|
|
|
let childBox = new Clutter.ActorBox();
|
2010-11-17 14:10:12 -05:00
|
|
|
|
|
|
|
let [minWidth, naturalWidth] = child.actor.get_preferred_width(-1);
|
|
|
|
let availWidth, extraWidth;
|
2010-10-19 13:41:41 -04:00
|
|
|
if (this._columnWidths) {
|
2011-03-22 12:57:48 -04:00
|
|
|
if (child.span == -1) {
|
2012-02-13 20:37:28 -05:00
|
|
|
if (direction == Clutter.TextDirection.LTR)
|
2011-03-22 12:57:48 -04:00
|
|
|
availWidth = box.x2 - x;
|
|
|
|
else
|
|
|
|
availWidth = x - box.x1;
|
|
|
|
} else {
|
2010-11-17 14:10:12 -05:00
|
|
|
availWidth = 0;
|
2010-10-19 13:41:41 -04:00
|
|
|
for (let j = 0; j < child.span; j++)
|
2010-11-17 14:10:12 -05:00
|
|
|
availWidth += this._columnWidths[col++];
|
2010-10-19 13:41:41 -04:00
|
|
|
}
|
2010-11-17 14:10:12 -05:00
|
|
|
extraWidth = availWidth - naturalWidth;
|
2010-10-19 13:41:41 -04:00
|
|
|
} else {
|
2011-09-22 09:33:43 -04:00
|
|
|
if (child.span == -1) {
|
2012-02-13 20:37:28 -05:00
|
|
|
if (direction == Clutter.TextDirection.LTR)
|
2011-09-22 09:33:43 -04:00
|
|
|
availWidth = box.x2 - x;
|
|
|
|
else
|
|
|
|
availWidth = x - box.x1;
|
|
|
|
} else {
|
2011-09-19 16:46:38 -04:00
|
|
|
availWidth = naturalWidth;
|
2011-09-22 09:33:43 -04:00
|
|
|
}
|
2010-11-17 14:10:12 -05:00
|
|
|
extraWidth = 0;
|
2010-10-19 13:41:41 -04:00
|
|
|
}
|
2010-11-17 14:10:12 -05:00
|
|
|
|
2012-02-13 20:37:28 -05:00
|
|
|
if (direction == Clutter.TextDirection.LTR) {
|
2011-03-22 12:57:48 -04:00
|
|
|
if (child.expand) {
|
|
|
|
childBox.x1 = x;
|
|
|
|
childBox.x2 = x + availWidth;
|
2011-09-19 15:56:49 -04:00
|
|
|
} else if (child.align === St.Align.MIDDLE) {
|
2011-03-22 12:57:48 -04:00
|
|
|
childBox.x1 = x + Math.round(extraWidth / 2);
|
|
|
|
childBox.x2 = childBox.x1 + naturalWidth;
|
|
|
|
} else if (child.align === St.Align.END) {
|
|
|
|
childBox.x2 = x + availWidth;
|
|
|
|
childBox.x1 = childBox.x2 - naturalWidth;
|
|
|
|
} else {
|
|
|
|
childBox.x1 = x;
|
|
|
|
childBox.x2 = x + naturalWidth;
|
|
|
|
}
|
2010-11-17 14:10:12 -05:00
|
|
|
} else {
|
2011-03-22 12:57:48 -04:00
|
|
|
if (child.expand) {
|
|
|
|
childBox.x1 = x - availWidth;
|
|
|
|
childBox.x2 = x;
|
2011-09-19 15:56:49 -04:00
|
|
|
} else if (child.align === St.Align.MIDDLE) {
|
2011-03-22 12:57:48 -04:00
|
|
|
childBox.x1 = x - Math.round(extraWidth / 2);
|
|
|
|
childBox.x2 = childBox.x1 + naturalWidth;
|
|
|
|
} else if (child.align === St.Align.END) {
|
|
|
|
// align to the left
|
|
|
|
childBox.x1 = x - availWidth;
|
|
|
|
childBox.x2 = childBox.x1 + naturalWidth;
|
|
|
|
} else {
|
|
|
|
// align to the right
|
|
|
|
childBox.x2 = x;
|
|
|
|
childBox.x1 = x - naturalWidth;
|
|
|
|
}
|
2010-11-17 14:10:12 -05:00
|
|
|
}
|
|
|
|
|
2011-08-22 14:29:22 -04:00
|
|
|
let [minHeight, naturalHeight] = child.actor.get_preferred_height(childBox.x2 - childBox.x1);
|
2010-11-17 14:10:12 -05:00
|
|
|
childBox.y1 = Math.round(box.y1 + (height - naturalHeight) / 2);
|
|
|
|
childBox.y2 = childBox.y1 + naturalHeight;
|
|
|
|
|
2010-10-19 13:41:41 -04:00
|
|
|
child.actor.allocate(childBox, flags);
|
|
|
|
|
2012-02-13 20:37:28 -05:00
|
|
|
if (direction == Clutter.TextDirection.LTR)
|
2011-03-22 12:57:48 -04:00
|
|
|
x += availWidth + this._spacing;
|
|
|
|
else
|
|
|
|
x -= availWidth + this._spacing;
|
2010-10-19 13:41:41 -04:00
|
|
|
}
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-05-20 11:18:46 -04:00
|
|
|
Signals.addSignalMethods(PopupBaseMenuItem.prototype);
|
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-11-16 09:22:38 -05:00
|
|
|
_init: function (text, params) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(params);
|
2010-05-20 11:18:46 -04:00
|
|
|
|
|
|
|
this.label = new St.Label({ text: text });
|
2010-10-19 13:41:41 -04:00
|
|
|
this.addActor(this.label);
|
2012-01-05 13:00:06 -05:00
|
|
|
this.actor.label_actor = this.label
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupSeparatorMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupSeparatorMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2010-05-20 11:18:46 -04:00
|
|
|
|
|
|
|
_init: function () {
|
2012-08-17 05:03:23 -04:00
|
|
|
this.parent({ reactive: false,
|
|
|
|
can_focus: false});
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2012-08-15 21:28:49 -04:00
|
|
|
this._separator = new Separator.HorizontalSeparator({ style_class: 'popup-separator-menu-item' });
|
|
|
|
this.addActor(this._separator.actor, { span: -1, expand: true });
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-02-21 15:25:56 -05:00
|
|
|
const PopupAlternatingMenuItemState = {
|
|
|
|
DEFAULT: 0,
|
|
|
|
ALTERNATIVE: 1
|
|
|
|
}
|
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupAlternatingMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupAlternatingMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2011-02-21 15:25:56 -05:00
|
|
|
|
|
|
|
_init: function(text, alternateText, params) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(params);
|
2011-02-21 15:25:56 -05:00
|
|
|
this.actor.add_style_class_name('popup-alternating-menu-item');
|
|
|
|
|
|
|
|
this._text = text;
|
|
|
|
this._alternateText = alternateText;
|
|
|
|
this.label = new St.Label({ text: text });
|
|
|
|
this.state = PopupAlternatingMenuItemState.DEFAULT;
|
|
|
|
this.addActor(this.label);
|
2012-03-15 14:04:54 -04:00
|
|
|
this.actor.label_actor = this.label;
|
2011-02-21 15:25:56 -05:00
|
|
|
|
|
|
|
this.actor.connect('notify::mapped', Lang.bind(this, this._onMapped));
|
|
|
|
},
|
|
|
|
|
|
|
|
_onMapped: function() {
|
|
|
|
if (this.actor.mapped) {
|
|
|
|
this._capturedEventId = global.stage.connect('captured-event',
|
|
|
|
Lang.bind(this, this._onCapturedEvent));
|
|
|
|
this._updateStateFromModifiers();
|
|
|
|
} else {
|
|
|
|
if (this._capturedEventId != 0) {
|
|
|
|
global.stage.disconnect(this._capturedEventId);
|
|
|
|
this._capturedEventId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_setState: function(state) {
|
|
|
|
if (this.state != state) {
|
|
|
|
if (state == PopupAlternatingMenuItemState.ALTERNATIVE && !this._canAlternate())
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.state = state;
|
|
|
|
this._updateLabel();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateStateFromModifiers: function() {
|
|
|
|
let [x, y, mods] = global.get_pointer();
|
|
|
|
let state;
|
|
|
|
|
|
|
|
if ((mods & Clutter.ModifierType.MOD1_MASK) == 0) {
|
|
|
|
state = PopupAlternatingMenuItemState.DEFAULT;
|
|
|
|
} else {
|
|
|
|
state = PopupAlternatingMenuItemState.ALTERNATIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._setState(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onCapturedEvent: function(actor, event) {
|
|
|
|
if (event.type() != Clutter.EventType.KEY_PRESS &&
|
|
|
|
event.type() != Clutter.EventType.KEY_RELEASE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let key = event.get_key_symbol();
|
|
|
|
|
|
|
|
if (key == Clutter.KEY_Alt_L || key == Clutter.KEY_Alt_R)
|
|
|
|
this._updateStateFromModifiers();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateLabel: function() {
|
|
|
|
if (this.state == PopupAlternatingMenuItemState.ALTERNATIVE) {
|
|
|
|
this.actor.add_style_pseudo_class('alternate');
|
|
|
|
this.label.set_text(this._alternateText);
|
|
|
|
} else {
|
|
|
|
this.actor.remove_style_pseudo_class('alternate');
|
|
|
|
this.label.set_text(this._text);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_canAlternate: function() {
|
|
|
|
if (this.state == PopupAlternatingMenuItemState.DEFAULT && !this._alternateText)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
updateText: function(text, alternateText) {
|
|
|
|
this._text = text;
|
|
|
|
this._alternateText = alternateText;
|
|
|
|
|
|
|
|
if (!this._canAlternate())
|
|
|
|
this._setState(PopupAlternatingMenuItemState.DEFAULT);
|
|
|
|
|
|
|
|
this._updateLabel();
|
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-07-22 08:34:02 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupSliderMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupSliderMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2010-07-22 08:34:02 -04:00
|
|
|
|
2011-07-19 19:45:29 -04:00
|
|
|
_init: function(value) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent({ activate: false });
|
2010-07-22 08:34:02 -04:00
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
|
|
|
|
|
2010-07-22 08:34:02 -04:00
|
|
|
if (isNaN(value))
|
|
|
|
// Avoid spreading NaNs around
|
|
|
|
throw TypeError('The slider value must be a number');
|
2010-07-22 20:39:44 -04:00
|
|
|
this._value = Math.max(Math.min(value, 1), 0);
|
2010-07-22 08:34:02 -04:00
|
|
|
|
|
|
|
this._slider = new St.DrawingArea({ style_class: 'popup-slider-menu-item', reactive: true });
|
2010-11-17 14:10:12 -05:00
|
|
|
this.addActor(this._slider, { span: -1, expand: true });
|
2010-07-22 08:34:02 -04:00
|
|
|
this._slider.connect('repaint', Lang.bind(this, this._sliderRepaint));
|
2011-04-21 11:49:41 -04:00
|
|
|
this.actor.connect('button-press-event', Lang.bind(this, this._startDragging));
|
2010-11-01 08:26:49 -04:00
|
|
|
this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
|
2012-03-23 14:32:17 -04:00
|
|
|
this.actor.connect('notify::mapped', Lang.bind(this, function() {
|
|
|
|
if (!this.actor.mapped)
|
|
|
|
this._endDragging();
|
|
|
|
}));
|
2010-07-22 08:34:02 -04:00
|
|
|
|
|
|
|
this._releaseId = this._motionId = 0;
|
|
|
|
this._dragging = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
setValue: function(value) {
|
|
|
|
if (isNaN(value))
|
|
|
|
throw TypeError('The slider value must be a number');
|
|
|
|
|
2010-07-22 20:39:44 -04:00
|
|
|
this._value = Math.max(Math.min(value, 1), 0);
|
2010-07-22 08:34:02 -04:00
|
|
|
this._slider.queue_repaint();
|
|
|
|
},
|
|
|
|
|
|
|
|
_sliderRepaint: function(area) {
|
|
|
|
let cr = area.get_context();
|
|
|
|
let themeNode = area.get_theme_node();
|
|
|
|
let [width, height] = area.get_surface_size();
|
|
|
|
|
StThemeNode: simplify use of get_color/get_double/get_length
Although within St itself there are situations where the semantics of
these functions (return TRUE or FALSE and return the actual value in
an out parameter) is useful, it's mostly just annoying at the
application level, where you generally know that the CSS property is
going to specified, and there is no especially sane fallback if it's
not.
So rename the current methods to lookup_color, lookup_double, and
lookup_length, and add new get_color, get_double, and get_length
methods that don't take an "inherit" parameter, and return their
values directly. (Well, except for get_color, due to the lack of (out
caller-allocates) in gjs.)
And update the code to use either the old or new methods as appropriate.
https://bugzilla.gnome.org/show_bug.cgi?id=632590
2010-09-26 17:38:36 -04:00
|
|
|
let handleRadius = themeNode.get_length('-slider-handle-radius');
|
2010-07-22 08:34:02 -04:00
|
|
|
|
|
|
|
let sliderWidth = width - 2 * handleRadius;
|
StThemeNode: simplify use of get_color/get_double/get_length
Although within St itself there are situations where the semantics of
these functions (return TRUE or FALSE and return the actual value in
an out parameter) is useful, it's mostly just annoying at the
application level, where you generally know that the CSS property is
going to specified, and there is no especially sane fallback if it's
not.
So rename the current methods to lookup_color, lookup_double, and
lookup_length, and add new get_color, get_double, and get_length
methods that don't take an "inherit" parameter, and return their
values directly. (Well, except for get_color, due to the lack of (out
caller-allocates) in gjs.)
And update the code to use either the old or new methods as appropriate.
https://bugzilla.gnome.org/show_bug.cgi?id=632590
2010-09-26 17:38:36 -04:00
|
|
|
let sliderHeight = themeNode.get_length('-slider-height');
|
2010-07-22 08:34:02 -04:00
|
|
|
|
StThemeNode: simplify use of get_color/get_double/get_length
Although within St itself there are situations where the semantics of
these functions (return TRUE or FALSE and return the actual value in
an out parameter) is useful, it's mostly just annoying at the
application level, where you generally know that the CSS property is
going to specified, and there is no especially sane fallback if it's
not.
So rename the current methods to lookup_color, lookup_double, and
lookup_length, and add new get_color, get_double, and get_length
methods that don't take an "inherit" parameter, and return their
values directly. (Well, except for get_color, due to the lack of (out
caller-allocates) in gjs.)
And update the code to use either the old or new methods as appropriate.
https://bugzilla.gnome.org/show_bug.cgi?id=632590
2010-09-26 17:38:36 -04:00
|
|
|
let sliderBorderWidth = themeNode.get_length('-slider-border-width');
|
2010-07-22 08:34:02 -04:00
|
|
|
|
2011-02-14 09:20:22 -05:00
|
|
|
let sliderBorderColor = themeNode.get_color('-slider-border-color');
|
|
|
|
let sliderColor = themeNode.get_color('-slider-background-color');
|
2010-07-22 08:34:02 -04:00
|
|
|
|
2011-03-12 17:49:03 -05:00
|
|
|
let sliderActiveBorderColor = themeNode.get_color('-slider-active-border-color');
|
|
|
|
let sliderActiveColor = themeNode.get_color('-slider-active-background-color');
|
|
|
|
|
|
|
|
cr.setSourceRGBA (
|
|
|
|
sliderActiveColor.red / 255,
|
|
|
|
sliderActiveColor.green / 255,
|
|
|
|
sliderActiveColor.blue / 255,
|
|
|
|
sliderActiveColor.alpha / 255);
|
|
|
|
cr.rectangle(handleRadius, (height - sliderHeight) / 2, sliderWidth * this._value, sliderHeight);
|
|
|
|
cr.fillPreserve();
|
|
|
|
cr.setSourceRGBA (
|
|
|
|
sliderActiveBorderColor.red / 255,
|
|
|
|
sliderActiveBorderColor.green / 255,
|
|
|
|
sliderActiveBorderColor.blue / 255,
|
|
|
|
sliderActiveBorderColor.alpha / 255);
|
|
|
|
cr.setLineWidth(sliderBorderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
|
2010-07-22 08:34:02 -04:00
|
|
|
cr.setSourceRGBA (
|
|
|
|
sliderColor.red / 255,
|
|
|
|
sliderColor.green / 255,
|
|
|
|
sliderColor.blue / 255,
|
|
|
|
sliderColor.alpha / 255);
|
2011-03-12 17:49:03 -05:00
|
|
|
cr.rectangle(handleRadius + sliderWidth * this._value, (height - sliderHeight) / 2, sliderWidth * (1 - this._value), sliderHeight);
|
2010-07-22 08:34:02 -04:00
|
|
|
cr.fillPreserve();
|
|
|
|
cr.setSourceRGBA (
|
|
|
|
sliderBorderColor.red / 255,
|
|
|
|
sliderBorderColor.green / 255,
|
|
|
|
sliderBorderColor.blue / 255,
|
|
|
|
sliderBorderColor.alpha / 255);
|
|
|
|
cr.setLineWidth(sliderBorderWidth);
|
|
|
|
cr.stroke();
|
|
|
|
|
|
|
|
let handleY = height / 2;
|
2010-07-22 20:39:44 -04:00
|
|
|
let handleX = handleRadius + (width - 2 * handleRadius) * this._value;
|
2010-07-22 08:34:02 -04:00
|
|
|
|
2011-02-14 09:20:22 -05:00
|
|
|
let color = themeNode.get_foreground_color();
|
2010-07-22 08:34:02 -04:00
|
|
|
cr.setSourceRGBA (
|
|
|
|
color.red / 255,
|
|
|
|
color.green / 255,
|
|
|
|
color.blue / 255,
|
|
|
|
color.alpha / 255);
|
|
|
|
cr.arc(handleX, handleY, handleRadius, 0, 2 * Math.PI);
|
|
|
|
cr.fill();
|
2013-01-07 15:07:40 -05:00
|
|
|
cr.$dispose();
|
2010-07-22 08:34:02 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_startDragging: function(actor, event) {
|
|
|
|
if (this._dragging) // don't allow two drags at the same time
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._dragging = true;
|
|
|
|
|
|
|
|
// FIXME: we should only grab the specific device that originated
|
|
|
|
// the event, but for some weird reason events are still delivered
|
|
|
|
// outside the slider if using clutter_grab_pointer_for_device
|
|
|
|
Clutter.grab_pointer(this._slider);
|
|
|
|
this._releaseId = this._slider.connect('button-release-event', Lang.bind(this, this._endDragging));
|
|
|
|
this._motionId = this._slider.connect('motion-event', Lang.bind(this, this._motionEvent));
|
|
|
|
let absX, absY;
|
|
|
|
[absX, absY] = event.get_coords();
|
|
|
|
this._moveHandle(absX, absY);
|
|
|
|
},
|
|
|
|
|
|
|
|
_endDragging: function() {
|
|
|
|
if (this._dragging) {
|
|
|
|
this._slider.disconnect(this._releaseId);
|
|
|
|
this._slider.disconnect(this._motionId);
|
|
|
|
|
|
|
|
Clutter.ungrab_pointer();
|
|
|
|
this._dragging = false;
|
|
|
|
|
2010-07-22 20:39:44 -04:00
|
|
|
this.emit('drag-end');
|
2010-07-22 08:34:02 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2012-11-04 10:53:23 -05:00
|
|
|
scroll: function(event) {
|
2010-10-20 16:41:54 -04:00
|
|
|
let direction = event.get_scroll_direction();
|
2012-11-04 10:50:11 -05:00
|
|
|
let delta;
|
|
|
|
|
|
|
|
if (event.is_pointer_emulated())
|
|
|
|
return;
|
2010-10-20 16:41:54 -04:00
|
|
|
|
|
|
|
if (direction == Clutter.ScrollDirection.DOWN) {
|
2012-11-04 10:50:11 -05:00
|
|
|
delta = -SLIDER_SCROLL_STEP;
|
|
|
|
} else if (direction == Clutter.ScrollDirection.UP) {
|
|
|
|
delta = +SLIDER_SCROLL_STEP;
|
|
|
|
} else if (direction == Clutter.ScrollDirection.SMOOTH) {
|
|
|
|
let [dx, dy] = event.get_scroll_delta();
|
|
|
|
// Even though the slider is horizontal, use dy to match
|
|
|
|
// the UP/DOWN above.
|
|
|
|
delta = -dy / 10;
|
2010-10-20 16:41:54 -04:00
|
|
|
}
|
|
|
|
|
2012-11-04 10:50:11 -05:00
|
|
|
this._value = Math.min(Math.max(0, this._value + delta), 1);
|
2012-11-04 10:53:23 -05:00
|
|
|
|
2010-10-20 16:41:54 -04:00
|
|
|
this._slider.queue_repaint();
|
|
|
|
this.emit('value-changed', this._value);
|
|
|
|
},
|
|
|
|
|
2012-11-04 10:53:23 -05:00
|
|
|
_onScrollEvent: function(actor, event) {
|
|
|
|
this.scroll(event);
|
|
|
|
},
|
|
|
|
|
2010-07-22 08:34:02 -04:00
|
|
|
_motionEvent: function(actor, event) {
|
|
|
|
let absX, absY;
|
|
|
|
[absX, absY] = event.get_coords();
|
2011-02-08 14:19:04 -05:00
|
|
|
this._moveHandle(absX, absY);
|
2010-07-22 08:34:02 -04:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_moveHandle: function(absX, absY) {
|
|
|
|
let relX, relY, sliderX, sliderY;
|
|
|
|
[sliderX, sliderY] = this._slider.get_transformed_position();
|
|
|
|
relX = absX - sliderX;
|
|
|
|
relY = absY - sliderY;
|
|
|
|
|
|
|
|
let width = this._slider.width;
|
StThemeNode: simplify use of get_color/get_double/get_length
Although within St itself there are situations where the semantics of
these functions (return TRUE or FALSE and return the actual value in
an out parameter) is useful, it's mostly just annoying at the
application level, where you generally know that the CSS property is
going to specified, and there is no especially sane fallback if it's
not.
So rename the current methods to lookup_color, lookup_double, and
lookup_length, and add new get_color, get_double, and get_length
methods that don't take an "inherit" parameter, and return their
values directly. (Well, except for get_color, due to the lack of (out
caller-allocates) in gjs.)
And update the code to use either the old or new methods as appropriate.
https://bugzilla.gnome.org/show_bug.cgi?id=632590
2010-09-26 17:38:36 -04:00
|
|
|
let handleRadius = this._slider.get_theme_node().get_length('-slider-handle-radius');
|
2010-07-22 08:34:02 -04:00
|
|
|
|
|
|
|
let newvalue;
|
|
|
|
if (relX < handleRadius)
|
|
|
|
newvalue = 0;
|
|
|
|
else if (relX > width - handleRadius)
|
|
|
|
newvalue = 1;
|
|
|
|
else
|
|
|
|
newvalue = (relX - handleRadius) / (width - 2 * handleRadius);
|
2010-07-22 20:39:44 -04:00
|
|
|
this._value = newvalue;
|
2010-07-22 08:34:02 -04:00
|
|
|
this._slider.queue_repaint();
|
2010-07-22 20:39:44 -04:00
|
|
|
this.emit('value-changed', this._value);
|
2010-07-22 08:34:02 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this._value;
|
|
|
|
},
|
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
_onKeyPressEvent: function (actor, event) {
|
2010-07-22 08:34:02 -04:00
|
|
|
let key = event.get_key_symbol();
|
2010-10-07 14:15:51 -04:00
|
|
|
if (key == Clutter.KEY_Right || key == Clutter.KEY_Left) {
|
|
|
|
let delta = key == Clutter.KEY_Right ? 0.1 : -0.1;
|
2010-07-22 20:39:44 -04:00
|
|
|
this._value = Math.max(0, Math.min(this._value + delta, 1));
|
2010-07-22 08:34:02 -04:00
|
|
|
this._slider.queue_repaint();
|
|
|
|
this.emit('value-changed', this._value);
|
2010-07-22 20:39:44 -04:00
|
|
|
this.emit('drag-end');
|
2010-07-22 08:34:02 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2011-02-08 14:19:04 -05:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const Switch = new Lang.Class({
|
|
|
|
Name: 'Switch',
|
2010-07-22 08:34:02 -04:00
|
|
|
|
2011-02-08 14:19:04 -05:00
|
|
|
_init: function(state) {
|
2012-02-27 11:31:10 -05:00
|
|
|
this.actor = new St.Bin({ style_class: 'toggle-switch',
|
2012-08-19 21:40:09 -04:00
|
|
|
accessible_role: Atk.Role.CHECK_BOX,
|
|
|
|
can_focus: true });
|
2011-02-08 14:19:04 -05:00
|
|
|
// Translators: this MUST be either "toggle-switch-us"
|
|
|
|
// (for toggle switches containing the English words
|
|
|
|
// "ON" and "OFF") or "toggle-switch-intl" (for toggle
|
|
|
|
// switches containing "◯" and "|"). Other values will
|
|
|
|
// simply result in invisible toggle switches.
|
|
|
|
this.actor.add_style_class_name(_("toggle-switch-us"));
|
|
|
|
this.setToggleState(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
setToggleState: function(state) {
|
|
|
|
if (state)
|
|
|
|
this.actor.add_style_pseudo_class('checked');
|
|
|
|
else
|
|
|
|
this.actor.remove_style_pseudo_class('checked');
|
|
|
|
this.state = state;
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle: function() {
|
|
|
|
this.setToggleState(!this.state);
|
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-06-17 08:17:01 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupSwitchMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupSwitchMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2010-06-17 08:17:01 -04:00
|
|
|
|
2011-01-25 16:06:40 -05:00
|
|
|
_init: function(text, active, params) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(params);
|
2010-06-17 08:17:01 -04:00
|
|
|
|
|
|
|
this.label = new St.Label({ text: text });
|
2010-10-13 14:10:38 -04:00
|
|
|
this._switch = new Switch(active);
|
2010-06-17 08:17:01 -04:00
|
|
|
|
2012-02-27 11:31:10 -05:00
|
|
|
this.actor.accessible_role = Atk.Role.CHECK_MENU_ITEM;
|
2012-01-20 12:10:45 -05:00
|
|
|
this.checkAccessibleState();
|
2012-02-27 11:31:10 -05:00
|
|
|
this.actor.label_actor = this.label;
|
|
|
|
|
2010-10-19 13:41:41 -04:00
|
|
|
this.addActor(this.label);
|
2010-06-17 08:17:01 -04:00
|
|
|
|
2011-04-21 10:44:28 -04:00
|
|
|
this._statusBin = new St.Bin({ x_align: St.Align.END });
|
2011-07-28 10:59:10 -04:00
|
|
|
this.addActor(this._statusBin,
|
|
|
|
{ expand: true, span: -1, align: St.Align.END });
|
2011-04-21 10:44:28 -04:00
|
|
|
|
|
|
|
this._statusLabel = new St.Label({ text: '',
|
2012-09-11 08:00:22 -04:00
|
|
|
style_class: 'popup-status-menu-item'
|
2011-04-21 10:44:28 -04:00
|
|
|
});
|
|
|
|
this._statusBin.child = this._switch.actor;
|
|
|
|
},
|
|
|
|
|
|
|
|
setStatus: function(text) {
|
|
|
|
if (text != null) {
|
|
|
|
this._statusLabel.text = text;
|
|
|
|
this._statusBin.child = this._statusLabel;
|
|
|
|
this.actor.reactive = false;
|
2012-02-27 11:31:10 -05:00
|
|
|
this.actor.accessible_role = Atk.Role.MENU_ITEM;
|
2011-04-21 10:44:28 -04:00
|
|
|
} else {
|
|
|
|
this._statusBin.child = this._switch.actor;
|
|
|
|
this.actor.reactive = true;
|
2012-02-27 11:31:10 -05:00
|
|
|
this.actor.accessible_role = Atk.Role.CHECK_MENU_ITEM;
|
2011-04-21 10:44:28 -04:00
|
|
|
}
|
2012-01-20 12:10:45 -05:00
|
|
|
this.checkAccessibleState();
|
2011-04-21 10:44:28 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
activate: function(event) {
|
|
|
|
if (this._switch.actor.mapped) {
|
2010-06-17 08:17:01 -04:00
|
|
|
this.toggle();
|
2011-04-21 10:44:28 -04:00
|
|
|
}
|
|
|
|
|
2011-11-21 08:16:51 -05:00
|
|
|
// we allow pressing space to toggle the switch
|
|
|
|
// without closing the menu
|
|
|
|
if (event.type() == Clutter.EventType.KEY_PRESS &&
|
|
|
|
event.get_key_symbol() == Clutter.KEY_space)
|
|
|
|
return;
|
|
|
|
|
2011-11-20 12:56:27 -05:00
|
|
|
this.parent(event);
|
2010-06-17 08:17:01 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
toggle: function() {
|
|
|
|
this._switch.toggle();
|
|
|
|
this.emit('toggled', this._switch.state);
|
2012-01-20 12:10:45 -05:00
|
|
|
this.checkAccessibleState();
|
2010-06-17 08:17:01 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
get state() {
|
|
|
|
return this._switch.state;
|
|
|
|
},
|
|
|
|
|
|
|
|
setToggleState: function(state) {
|
|
|
|
this._switch.setToggleState(state);
|
2012-01-20 12:10:45 -05:00
|
|
|
this.checkAccessibleState();
|
|
|
|
},
|
|
|
|
|
|
|
|
checkAccessibleState: function() {
|
|
|
|
switch (this.actor.accessible_role) {
|
|
|
|
case Atk.Role.CHECK_MENU_ITEM:
|
|
|
|
if (this._switch.state)
|
|
|
|
this.actor.add_accessible_state (Atk.StateType.CHECKED);
|
|
|
|
else
|
|
|
|
this.actor.remove_accessible_state (Atk.StateType.CHECKED);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.actor.remove_accessible_state (Atk.StateType.CHECKED);
|
|
|
|
}
|
2010-06-17 08:17:01 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-06-17 08:17:01 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupImageMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupImageMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-01-25 16:06:40 -05:00
|
|
|
_init: function (text, iconName, params) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(params);
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-07-06 03:51:53 -04:00
|
|
|
this.label = new St.Label({ text: text });
|
2010-10-19 13:41:41 -04:00
|
|
|
this.addActor(this.label);
|
2010-11-02 18:33:22 -04:00
|
|
|
this._icon = new St.Icon({ style_class: 'popup-menu-icon' });
|
2010-11-17 14:10:12 -05:00
|
|
|
this.addActor(this._icon, { align: St.Align.END });
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-10-19 11:59:23 -04:00
|
|
|
this.setIcon(iconName);
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2010-07-06 03:51:53 -04:00
|
|
|
setIcon: function(name) {
|
2010-11-02 18:33:22 -04:00
|
|
|
this._icon.icon_name = name;
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupMenuBase = new Lang.Class({
|
|
|
|
Name: 'PopupMenuBase',
|
|
|
|
Abstract: true,
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
_init: function(sourceActor, styleClass) {
|
2010-05-20 11:18:46 -04:00
|
|
|
this.sourceActor = sourceActor;
|
|
|
|
|
2011-01-25 16:06:40 -05:00
|
|
|
if (styleClass !== undefined) {
|
|
|
|
this.box = new St.BoxLayout({ style_class: styleClass,
|
|
|
|
vertical: true });
|
|
|
|
} else {
|
|
|
|
this.box = new St.BoxLayout({ vertical: true });
|
|
|
|
}
|
2011-03-25 10:14:55 -04:00
|
|
|
this.box.connect_after('queue-relayout', Lang.bind(this, this._menuQueueRelayout));
|
2011-03-28 15:27:03 -04:00
|
|
|
this.length = 0;
|
2010-10-07 14:15:51 -04:00
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
this.isOpen = false;
|
2011-03-22 10:29:32 -04:00
|
|
|
|
|
|
|
// If set, we don't send events (including crossing events) to the source actor
|
|
|
|
// for the menu which causes its prelight state to freeze
|
|
|
|
this.blockSourceEvents = false;
|
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
this._activeMenuItem = null;
|
2011-07-12 15:47:37 -04:00
|
|
|
this._childMenus = [];
|
2012-05-22 18:27:06 -04:00
|
|
|
this._settingsActions = { };
|
2012-09-01 08:42:53 -04:00
|
|
|
|
|
|
|
this._sessionUpdatedId = Main.sessionMode.connect('updated', Lang.bind(this, this._sessionUpdated));
|
|
|
|
},
|
|
|
|
|
|
|
|
_sessionUpdated: function() {
|
|
|
|
this._setSettingsVisibility(Main.sessionMode.allowSettings);
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
addAction: function(title, callback) {
|
2011-08-23 10:14:55 -04:00
|
|
|
let menuItem = new PopupMenuItem(title);
|
2010-05-20 11:18:46 -04:00
|
|
|
this.addMenuItem(menuItem);
|
|
|
|
menuItem.connect('activate', Lang.bind(this, function (menuItem, event) {
|
|
|
|
callback(event);
|
|
|
|
}));
|
2011-08-23 10:14:55 -04:00
|
|
|
|
|
|
|
return menuItem;
|
|
|
|
},
|
|
|
|
|
|
|
|
addSettingsAction: function(title, desktopFile) {
|
|
|
|
let menuItem = this.addAction(title, function() {
|
|
|
|
let app = Shell.AppSystem.get_default().lookup_setting(desktopFile);
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
log('Settings panel for desktop file ' + desktopFile + ' could not be loaded!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Main.overview.hide();
|
|
|
|
app.activate();
|
|
|
|
});
|
2012-05-22 18:27:06 -04:00
|
|
|
|
2012-09-20 09:58:08 -04:00
|
|
|
menuItem.actor.visible = Main.sessionMode.allowSettings;
|
2012-05-22 18:27:06 -04:00
|
|
|
this._settingsActions[desktopFile] = menuItem;
|
|
|
|
|
2011-08-23 10:14:55 -04:00
|
|
|
return menuItem;
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2012-09-01 08:42:53 -04:00
|
|
|
_setSettingsVisibility: function(visible) {
|
2012-05-22 18:27:06 -04:00
|
|
|
for (let id in this._settingsActions) {
|
|
|
|
let item = this._settingsActions[id];
|
|
|
|
item.actor.visible = visible;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-11-15 08:58:55 -05:00
|
|
|
isEmpty: function() {
|
2012-05-29 20:36:45 -04:00
|
|
|
return this.box.get_n_children() == 0;
|
2011-11-15 08:58:55 -05:00
|
|
|
},
|
|
|
|
|
2011-07-12 15:47:37 -04:00
|
|
|
isChildMenu: function(menu) {
|
|
|
|
return this._childMenus.indexOf(menu) != -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
addChildMenu: function(menu) {
|
|
|
|
if (this.isChildMenu(menu))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._childMenus.push(menu);
|
|
|
|
this.emit('child-menu-added', menu);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeChildMenu: function(menu) {
|
|
|
|
let index = this._childMenus.indexOf(menu);
|
|
|
|
|
|
|
|
if (index == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._childMenus.splice(index, 1);
|
|
|
|
this.emit('child-menu-removed', menu);
|
|
|
|
},
|
|
|
|
|
2011-01-25 16:04:57 -05:00
|
|
|
/**
|
|
|
|
* _connectSubMenuSignals:
|
|
|
|
* @object: a menu item, or a menu section
|
|
|
|
* @menu: a sub menu, or a menu section
|
|
|
|
*
|
|
|
|
* Connects to signals on @menu that are necessary for
|
|
|
|
* operating the submenu, and stores the ids on @object.
|
|
|
|
*/
|
|
|
|
_connectSubMenuSignals: function(object, menu) {
|
|
|
|
object._subMenuActivateId = menu.connect('activate', Lang.bind(this, function() {
|
|
|
|
this.emit('activate');
|
2012-06-15 13:16:10 -04:00
|
|
|
this.close(BoxPointer.PopupAnimation.FULL);
|
2011-01-25 16:04:57 -05:00
|
|
|
}));
|
|
|
|
object._subMenuActiveChangeId = menu.connect('active-changed', Lang.bind(this, function(submenu, submenuItem) {
|
|
|
|
if (this._activeMenuItem && this._activeMenuItem != submenuItem)
|
|
|
|
this._activeMenuItem.setActive(false);
|
|
|
|
this._activeMenuItem = submenuItem;
|
|
|
|
this.emit('active-changed', submenuItem);
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_connectItemSignals: function(menuItem) {
|
2010-05-20 11:18:46 -04:00
|
|
|
menuItem._activeChangeId = menuItem.connect('active-changed', Lang.bind(this, function (menuItem, active) {
|
|
|
|
if (active && this._activeMenuItem != menuItem) {
|
|
|
|
if (this._activeMenuItem)
|
|
|
|
this._activeMenuItem.setActive(false);
|
|
|
|
this._activeMenuItem = menuItem;
|
|
|
|
this.emit('active-changed', menuItem);
|
|
|
|
} else if (!active && this._activeMenuItem == menuItem) {
|
|
|
|
this._activeMenuItem = null;
|
|
|
|
this.emit('active-changed', null);
|
|
|
|
}
|
|
|
|
}));
|
2011-09-15 10:51:19 -04:00
|
|
|
menuItem._sensitiveChangeId = menuItem.connect('sensitive-changed', Lang.bind(this, function(menuItem, sensitive) {
|
|
|
|
if (!sensitive && this._activeMenuItem == menuItem) {
|
|
|
|
if (!this.actor.navigate_focus(menuItem.actor,
|
|
|
|
Gtk.DirectionType.TAB_FORWARD,
|
|
|
|
true))
|
|
|
|
this.actor.grab_key_focus();
|
|
|
|
} else if (sensitive && this._activeMenuItem == null) {
|
|
|
|
if (global.stage.get_key_focus() == this.actor)
|
|
|
|
menuItem.actor.grab_key_focus();
|
|
|
|
}
|
|
|
|
}));
|
2010-05-20 11:18:46 -04:00
|
|
|
menuItem._activateId = menuItem.connect('activate', Lang.bind(this, function (menuItem, event) {
|
|
|
|
this.emit('activate', menuItem);
|
2012-06-15 13:16:10 -04:00
|
|
|
this.close(BoxPointer.PopupAnimation.FULL);
|
2010-05-20 11:18:46 -04:00
|
|
|
}));
|
2011-12-12 14:36:00 -05:00
|
|
|
// the weird name is to avoid a conflict with some random property
|
|
|
|
// the menuItem may have, called destroyId
|
|
|
|
// (FIXME: in the future it may make sense to have container objects
|
|
|
|
// like PopupMenuManager does)
|
|
|
|
menuItem._popupMenuDestroyId = menuItem.connect('destroy', Lang.bind(this, function(menuItem) {
|
|
|
|
menuItem.disconnect(menuItem._popupMenuDestroyId);
|
2010-07-03 19:47:31 -04:00
|
|
|
menuItem.disconnect(menuItem._activateId);
|
|
|
|
menuItem.disconnect(menuItem._activeChangeId);
|
2011-09-15 10:51:19 -04:00
|
|
|
menuItem.disconnect(menuItem._sensitiveChangeId);
|
2010-11-01 11:03:28 -04:00
|
|
|
if (menuItem.menu) {
|
|
|
|
menuItem.menu.disconnect(menuItem._subMenuActivateId);
|
|
|
|
menuItem.menu.disconnect(menuItem._subMenuActiveChangeId);
|
|
|
|
this.disconnect(menuItem._closingId);
|
|
|
|
}
|
2010-07-03 19:47:31 -04:00
|
|
|
if (menuItem == this._activeMenuItem)
|
|
|
|
this._activeMenuItem = null;
|
|
|
|
}));
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2011-08-19 14:42:20 -04:00
|
|
|
_updateSeparatorVisibility: function(menuItem) {
|
|
|
|
let children = this.box.get_children();
|
|
|
|
|
|
|
|
let index = children.indexOf(menuItem.actor);
|
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let childBeforeIndex = index - 1;
|
|
|
|
|
|
|
|
while (childBeforeIndex >= 0 && !children[childBeforeIndex].visible)
|
|
|
|
childBeforeIndex--;
|
|
|
|
|
|
|
|
if (childBeforeIndex < 0
|
|
|
|
|| children[childBeforeIndex]._delegate instanceof PopupSeparatorMenuItem) {
|
|
|
|
menuItem.actor.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let childAfterIndex = index + 1;
|
|
|
|
|
|
|
|
while (childAfterIndex < children.length && !children[childAfterIndex].visible)
|
|
|
|
childAfterIndex++;
|
|
|
|
|
|
|
|
if (childAfterIndex >= children.length
|
|
|
|
|| children[childAfterIndex]._delegate instanceof PopupSeparatorMenuItem) {
|
|
|
|
menuItem.actor.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItem.actor.show();
|
|
|
|
},
|
|
|
|
|
2011-01-25 16:04:57 -05:00
|
|
|
addMenuItem: function(menuItem, position) {
|
|
|
|
let before_item = null;
|
|
|
|
if (position == undefined) {
|
|
|
|
this.box.add(menuItem.actor);
|
|
|
|
} else {
|
|
|
|
let items = this._getMenuItems();
|
|
|
|
if (position < items.length) {
|
|
|
|
before_item = items[position].actor;
|
2012-02-13 15:27:16 -05:00
|
|
|
this.box.insert_child_below(menuItem.actor, before_item);
|
|
|
|
} else {
|
2011-01-25 16:04:57 -05:00
|
|
|
this.box.add(menuItem.actor);
|
2012-02-13 15:27:16 -05:00
|
|
|
}
|
2011-01-25 16:04:57 -05:00
|
|
|
}
|
2012-02-13 15:27:16 -05:00
|
|
|
|
2011-01-25 16:04:57 -05:00
|
|
|
if (menuItem instanceof PopupMenuSection) {
|
|
|
|
this._connectSubMenuSignals(menuItem, menuItem);
|
2012-08-29 18:11:05 -04:00
|
|
|
menuItem._parentOpenStateChangedId = this.connect('open-state-changed',
|
2011-10-07 14:17:44 -04:00
|
|
|
function(self, open) {
|
2012-08-29 18:11:05 -04:00
|
|
|
if (open)
|
|
|
|
menuItem.open();
|
|
|
|
else
|
|
|
|
menuItem.close();
|
2011-10-07 14:17:44 -04:00
|
|
|
});
|
2011-01-25 16:04:57 -05:00
|
|
|
menuItem.connect('destroy', Lang.bind(this, function() {
|
|
|
|
menuItem.disconnect(menuItem._subMenuActivateId);
|
|
|
|
menuItem.disconnect(menuItem._subMenuActiveChangeId);
|
2012-08-29 18:11:05 -04:00
|
|
|
this.disconnect(menuItem._parentOpenStateChangedId);
|
2011-03-28 15:27:03 -04:00
|
|
|
|
|
|
|
this.length--;
|
2011-01-25 16:04:57 -05:00
|
|
|
}));
|
|
|
|
} else if (menuItem instanceof PopupSubMenuMenuItem) {
|
|
|
|
if (before_item == null)
|
|
|
|
this.box.add(menuItem.menu.actor);
|
|
|
|
else
|
2012-02-13 15:27:16 -05:00
|
|
|
this.box.insert_child_below(menuItem.menu.actor, before_item);
|
2011-01-25 16:04:57 -05:00
|
|
|
this._connectSubMenuSignals(menuItem, menuItem.menu);
|
|
|
|
this._connectItemSignals(menuItem);
|
|
|
|
menuItem._closingId = this.connect('open-state-changed', function(self, open) {
|
|
|
|
if (!open)
|
2012-06-15 13:16:10 -04:00
|
|
|
menuItem.menu.close(BoxPointer.PopupAnimation.FADE);
|
2011-01-25 16:04:57 -05:00
|
|
|
});
|
2011-08-19 14:42:20 -04:00
|
|
|
} else if (menuItem instanceof PopupSeparatorMenuItem) {
|
|
|
|
this._connectItemSignals(menuItem);
|
|
|
|
|
|
|
|
// updateSeparatorVisibility needs to get called any time the
|
|
|
|
// separator's adjacent siblings change visibility or position.
|
|
|
|
// open-state-changed isn't exactly that, but doing it in more
|
|
|
|
// precise ways would require a lot more bookkeeping.
|
|
|
|
this.connect('open-state-changed', Lang.bind(this, function() { this._updateSeparatorVisibility(menuItem); }));
|
2011-01-25 16:04:57 -05:00
|
|
|
} else if (menuItem instanceof PopupBaseMenuItem)
|
|
|
|
this._connectItemSignals(menuItem);
|
|
|
|
else
|
|
|
|
throw TypeError("Invalid argument to PopupMenuBase.addMenuItem()");
|
2011-03-28 15:27:03 -04:00
|
|
|
|
|
|
|
this.length++;
|
2011-01-25 16:04:57 -05:00
|
|
|
},
|
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
getColumnWidths: function() {
|
|
|
|
let columnWidths = [];
|
|
|
|
let items = this.box.get_children();
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2012-09-06 05:43:02 -04:00
|
|
|
if (!items[i].visible &&
|
|
|
|
!(items[i]._delegate instanceof PopupSubMenu && items[i-1].visible))
|
2011-03-10 11:59:50 -05:00
|
|
|
continue;
|
2010-11-01 11:03:28 -04:00
|
|
|
if (items[i]._delegate instanceof PopupBaseMenuItem || items[i]._delegate instanceof PopupMenuBase) {
|
|
|
|
let itemColumnWidths = items[i]._delegate.getColumnWidths();
|
|
|
|
for (let j = 0; j < itemColumnWidths.length; j++) {
|
|
|
|
if (j >= columnWidths.length || itemColumnWidths[j] > columnWidths[j])
|
|
|
|
columnWidths[j] = itemColumnWidths[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return columnWidths;
|
|
|
|
},
|
|
|
|
|
|
|
|
setColumnWidths: function(widths) {
|
|
|
|
let items = this.box.get_children();
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
if (items[i]._delegate instanceof PopupBaseMenuItem || items[i]._delegate instanceof PopupMenuBase)
|
|
|
|
items[i]._delegate.setColumnWidths(widths);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-03-25 10:14:55 -04:00
|
|
|
// Because of the above column-width funniness, we need to do a
|
|
|
|
// queue-relayout on every item whenever the menu itself changes
|
|
|
|
// size, to force clutter to drop its cached size requests. (The
|
|
|
|
// menuitems will in turn call queue_relayout on their parent, the
|
|
|
|
// menu, but that call will be a no-op since the menu already
|
|
|
|
// has a relayout queued, so we won't get stuck in a loop.
|
|
|
|
_menuQueueRelayout: function() {
|
|
|
|
this.box.get_children().map(function (actor) { actor.queue_relayout(); });
|
|
|
|
},
|
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
addActor: function(actor) {
|
2010-11-01 11:03:28 -04:00
|
|
|
this.box.add(actor);
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2011-01-25 16:04:57 -05:00
|
|
|
_getMenuItems: function() {
|
|
|
|
return this.box.get_children().map(function (actor) {
|
|
|
|
return actor._delegate;
|
|
|
|
}).filter(function(item) {
|
|
|
|
return item instanceof PopupBaseMenuItem || item instanceof PopupMenuSection;
|
|
|
|
});
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2011-06-27 11:45:24 -04:00
|
|
|
get firstMenuItem() {
|
|
|
|
let items = this._getMenuItems();
|
|
|
|
if (items.length)
|
|
|
|
return items[0];
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
get numMenuItems() {
|
|
|
|
return this._getMenuItems().length;
|
|
|
|
},
|
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
removeAll: function() {
|
2011-01-25 16:04:57 -05:00
|
|
|
let children = this._getMenuItems();
|
2010-05-20 11:18:46 -04:00
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
let item = children[i];
|
2010-07-03 19:47:31 -04:00
|
|
|
item.destroy();
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
toggle: function() {
|
2010-05-20 11:18:46 -04:00
|
|
|
if (this.isOpen)
|
2012-06-15 13:16:10 -04:00
|
|
|
this.close(BoxPointer.PopupAnimation.FULL);
|
2010-11-01 11:03:28 -04:00
|
|
|
else
|
2012-06-15 13:16:10 -04:00
|
|
|
this.open(BoxPointer.PopupAnimation.FULL);
|
2010-11-01 11:03:28 -04:00
|
|
|
},
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
destroy: function() {
|
|
|
|
this.removeAll();
|
|
|
|
this.actor.destroy();
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
this.emit('destroy');
|
2012-09-01 08:42:53 -04:00
|
|
|
|
|
|
|
Main.sessionMode.disconnect(this._sessionUpdatedId);
|
|
|
|
this._sessionUpdatedId = 0;
|
2010-11-01 11:03:28 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-11-01 11:03:28 -04:00
|
|
|
Signals.addSignalMethods(PopupMenuBase.prototype);
|
2010-06-12 12:13:04 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupMenu = new Lang.Class({
|
|
|
|
Name: 'PopupMenu',
|
|
|
|
Extends: PopupMenuBase,
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-09-14 18:14:03 -04:00
|
|
|
_init: function(sourceActor, arrowAlignment, arrowSide) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(sourceActor, 'popup-menu-content');
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-09-14 18:14:03 -04:00
|
|
|
this._arrowAlignment = arrowAlignment;
|
2010-11-01 11:03:28 -04:00
|
|
|
this._arrowSide = arrowSide;
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
this._boxPointer = new BoxPointer.BoxPointer(arrowSide,
|
|
|
|
{ x_fill: true,
|
|
|
|
y_fill: true,
|
|
|
|
x_align: St.Align.START });
|
|
|
|
this.actor = this._boxPointer.actor;
|
|
|
|
this.actor._delegate = this;
|
|
|
|
this.actor.style_class = 'popup-menu-boxpointer';
|
2011-02-08 14:53:43 -05:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
this._boxWrapper = new Shell.GenericContainer();
|
|
|
|
this._boxWrapper.connect('get-preferred-width', Lang.bind(this, this._boxGetPreferredWidth));
|
|
|
|
this._boxWrapper.connect('get-preferred-height', Lang.bind(this, this._boxGetPreferredHeight));
|
|
|
|
this._boxWrapper.connect('allocate', Lang.bind(this, this._boxAllocate));
|
|
|
|
this._boxPointer.bin.set_child(this._boxWrapper);
|
|
|
|
this._boxWrapper.add_actor(this.box);
|
|
|
|
this.actor.add_style_class_name('popup-menu');
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
global.focus_manager.add_group(this.actor);
|
2010-05-20 11:18:46 -04:00
|
|
|
this.actor.reactive = true;
|
2010-11-01 11:03:28 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_boxGetPreferredWidth: function (actor, forHeight, alloc) {
|
|
|
|
let columnWidths = this.getColumnWidths();
|
|
|
|
this.setColumnWidths(columnWidths);
|
|
|
|
|
|
|
|
// Now they will request the right sizes
|
|
|
|
[alloc.min_size, alloc.natural_size] = this.box.get_preferred_width(forHeight);
|
|
|
|
},
|
|
|
|
|
|
|
|
_boxGetPreferredHeight: function (actor, forWidth, alloc) {
|
|
|
|
[alloc.min_size, alloc.natural_size] = this.box.get_preferred_height(forWidth);
|
|
|
|
},
|
|
|
|
|
|
|
|
_boxAllocate: function (actor, box, flags) {
|
|
|
|
this.box.allocate(box, flags);
|
|
|
|
},
|
|
|
|
|
|
|
|
setArrowOrigin: function(origin) {
|
|
|
|
this._boxPointer.setArrowOrigin(origin);
|
|
|
|
},
|
|
|
|
|
2011-09-14 18:14:03 -04:00
|
|
|
setSourceAlignment: function(alignment) {
|
|
|
|
this._boxPointer.setSourceAlignment(alignment);
|
|
|
|
},
|
|
|
|
|
2010-11-18 16:18:54 -05:00
|
|
|
open: function(animate) {
|
2010-11-01 11:03:28 -04:00
|
|
|
if (this.isOpen)
|
|
|
|
return;
|
|
|
|
|
2011-11-15 08:58:55 -05:00
|
|
|
if (this.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
this.isOpen = true;
|
2010-11-01 11:03:28 -04:00
|
|
|
|
2011-09-14 18:14:03 -04:00
|
|
|
this._boxPointer.setPosition(this.sourceActor, this._arrowAlignment);
|
2010-11-18 16:18:54 -05:00
|
|
|
this._boxPointer.show(animate);
|
2010-11-01 11:03:28 -04:00
|
|
|
|
2011-08-29 12:04:17 -04:00
|
|
|
this.actor.raise_top();
|
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
this.emit('open-state-changed', true);
|
|
|
|
},
|
|
|
|
|
2010-11-18 16:18:54 -05:00
|
|
|
close: function(animate) {
|
2010-07-03 19:47:31 -04:00
|
|
|
if (this._activeMenuItem)
|
|
|
|
this._activeMenuItem.setActive(false);
|
2010-11-01 11:03:28 -04:00
|
|
|
|
2012-10-19 17:13:16 -04:00
|
|
|
if (this._boxPointer.actor.visible)
|
|
|
|
this._boxPointer.hide(animate);
|
|
|
|
|
|
|
|
if (!this.isOpen)
|
|
|
|
return;
|
2010-11-01 11:03:28 -04:00
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
this.isOpen = false;
|
|
|
|
this.emit('open-state-changed', false);
|
2010-11-01 11:03:28 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-11-01 11:03:28 -04:00
|
|
|
|
2012-12-04 14:52:34 -05:00
|
|
|
const PopupDummyMenu = new Lang.Class({
|
|
|
|
Name: 'PopupDummyMenu',
|
|
|
|
|
|
|
|
_init: function(sourceActor) {
|
|
|
|
this.sourceActor = sourceActor;
|
|
|
|
this.actor = sourceActor;
|
|
|
|
this.actor._delegate = this;
|
|
|
|
},
|
|
|
|
|
|
|
|
isChildMenu: function() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
open: function() { this.emit('open-state-changed', true); },
|
|
|
|
close: function() { this.emit('open-state-changed', false); },
|
2012-12-10 03:42:53 -05:00
|
|
|
toggle: function() {},
|
2012-12-04 14:52:34 -05:00
|
|
|
destroy: function() {
|
|
|
|
this.emit('destroy');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Signals.addSignalMethods(PopupDummyMenu.prototype);
|
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupSubMenu = new Lang.Class({
|
|
|
|
Name: 'PopupSubMenu',
|
|
|
|
Extends: PopupMenuBase,
|
2010-11-01 11:03:28 -04:00
|
|
|
|
|
|
|
_init: function(sourceActor, sourceArrow) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(sourceActor);
|
2010-11-01 11:03:28 -04:00
|
|
|
|
|
|
|
this._arrow = sourceArrow;
|
|
|
|
this._arrow.rotation_center_z_gravity = Clutter.Gravity.CENTER;
|
|
|
|
|
2011-04-02 12:35:03 -04:00
|
|
|
// Since a function of a submenu might be to provide a "More.." expander
|
|
|
|
// with long content, we make it scrollable - the scrollbar will only take
|
|
|
|
// effect if a CSS max-height is set on the top menu.
|
2011-04-04 08:55:15 -04:00
|
|
|
this.actor = new St.ScrollView({ style_class: 'popup-sub-menu',
|
|
|
|
hscrollbar_policy: Gtk.PolicyType.NEVER,
|
2011-04-02 12:35:03 -04:00
|
|
|
vscrollbar_policy: Gtk.PolicyType.NEVER });
|
|
|
|
|
|
|
|
this.actor.add_actor(this.box);
|
2010-11-01 11:03:28 -04:00
|
|
|
this.actor._delegate = this;
|
|
|
|
this.actor.clip_to_allocation = true;
|
|
|
|
this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
|
|
|
|
this.actor.hide();
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2011-04-02 12:35:03 -04:00
|
|
|
_getTopMenu: function() {
|
|
|
|
let actor = this.actor.get_parent();
|
|
|
|
while (actor) {
|
|
|
|
if (actor._delegate && actor._delegate instanceof PopupMenu)
|
|
|
|
return actor._delegate;
|
|
|
|
|
|
|
|
actor = actor.get_parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_needsScrollbar: function() {
|
|
|
|
let topMenu = this._getTopMenu();
|
|
|
|
let [topMinHeight, topNaturalHeight] = topMenu.actor.get_preferred_height(-1);
|
|
|
|
let topThemeNode = topMenu.actor.get_theme_node();
|
|
|
|
|
|
|
|
let topMaxHeight = topThemeNode.get_max_height();
|
|
|
|
return topMaxHeight >= 0 && topNaturalHeight >= topMaxHeight;
|
|
|
|
},
|
|
|
|
|
2010-11-18 16:18:54 -05:00
|
|
|
open: function(animate) {
|
2010-05-20 11:18:46 -04:00
|
|
|
if (this.isOpen)
|
2010-11-01 11:03:28 -04:00
|
|
|
return;
|
|
|
|
|
2011-11-15 08:58:55 -05:00
|
|
|
if (this.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
this.isOpen = true;
|
|
|
|
|
2011-03-31 15:51:38 -04:00
|
|
|
this.actor.show();
|
2011-04-02 12:35:03 -04:00
|
|
|
|
|
|
|
let needsScrollbar = this._needsScrollbar();
|
|
|
|
|
|
|
|
// St.ScrollView always requests space horizontally for a possible vertical
|
|
|
|
// scrollbar if in AUTOMATIC mode. Doing better would require implementation
|
|
|
|
// of width-for-height in St.BoxLayout and St.ScrollView. This looks bad
|
|
|
|
// when we *don't* need it, so turn off the scrollbar when that's true.
|
|
|
|
// Dynamic changes in whether we need it aren't handled properly.
|
|
|
|
this.actor.vscrollbar_policy =
|
|
|
|
needsScrollbar ? Gtk.PolicyType.AUTOMATIC : Gtk.PolicyType.NEVER;
|
|
|
|
|
2012-09-06 05:41:23 -04:00
|
|
|
if (needsScrollbar)
|
|
|
|
this.actor.add_style_pseudo_class('scrolled');
|
|
|
|
else
|
|
|
|
this.actor.remove_style_pseudo_class('scrolled');
|
|
|
|
|
2011-04-02 12:35:03 -04:00
|
|
|
// It looks funny if we animate with a scrollbar (at what point is
|
|
|
|
// the scrollbar added?) so just skip that case
|
|
|
|
if (animate && needsScrollbar)
|
|
|
|
animate = false;
|
|
|
|
|
|
|
|
if (animate) {
|
|
|
|
let [minHeight, naturalHeight] = this.actor.get_preferred_height(-1);
|
|
|
|
this.actor.height = 0;
|
|
|
|
this.actor._arrow_rotation = this._arrow.rotation_angle_z;
|
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ _arrow_rotation: 90,
|
|
|
|
height: naturalHeight,
|
|
|
|
time: 0.25,
|
|
|
|
onUpdateScope: this,
|
|
|
|
onUpdate: function() {
|
|
|
|
this._arrow.rotation_angle_z = this.actor._arrow_rotation;
|
|
|
|
},
|
|
|
|
onCompleteScope: this,
|
|
|
|
onComplete: function() {
|
|
|
|
this.actor.set_height(-1);
|
|
|
|
this.emit('open-state-changed', true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._arrow.rotation_angle_z = 90;
|
|
|
|
this.emit('open-state-changed', true);
|
|
|
|
}
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2010-11-18 16:18:54 -05:00
|
|
|
close: function(animate) {
|
2010-11-01 11:03:28 -04:00
|
|
|
if (!this.isOpen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.isOpen = false;
|
|
|
|
|
|
|
|
if (this._activeMenuItem)
|
2010-10-07 14:15:51 -04:00
|
|
|
this._activeMenuItem.setActive(false);
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-04-02 12:35:03 -04:00
|
|
|
if (animate && this._needsScrollbar())
|
|
|
|
animate = false;
|
|
|
|
|
2010-11-18 16:18:54 -05:00
|
|
|
if (animate) {
|
|
|
|
this.actor._arrow_rotation = this._arrow.rotation_angle_z;
|
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ _arrow_rotation: 0,
|
|
|
|
height: 0,
|
|
|
|
time: 0.25,
|
|
|
|
onCompleteScope: this,
|
|
|
|
onComplete: function() {
|
|
|
|
this.actor.hide();
|
|
|
|
this.actor.set_height(-1);
|
|
|
|
|
|
|
|
this.emit('open-state-changed', false);
|
|
|
|
},
|
|
|
|
onUpdateScope: this,
|
|
|
|
onUpdate: function() {
|
|
|
|
this._arrow.rotation_angle_z = this.actor._arrow_rotation;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._arrow.rotation_angle_z = 0;
|
|
|
|
this.actor.hide();
|
2010-11-01 11:03:28 -04:00
|
|
|
|
2010-11-18 16:18:54 -05:00
|
|
|
this.isOpen = false;
|
|
|
|
this.emit('open-state-changed', false);
|
|
|
|
}
|
2010-07-03 19:47:31 -04:00
|
|
|
},
|
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
_onKeyPressEvent: function(actor, event) {
|
|
|
|
// Move focus back to parent menu if the user types Left.
|
2010-07-03 19:47:31 -04:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
if (this.isOpen && event.get_key_symbol() == Clutter.KEY_Left) {
|
2012-06-15 13:16:10 -04:00
|
|
|
this.close(BoxPointer.PopupAnimation.FULL);
|
2010-11-01 11:03:28 -04:00
|
|
|
this.sourceActor._delegate.setActive(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-05-20 11:18:46 -04:00
|
|
|
|
2011-01-25 16:04:57 -05:00
|
|
|
/**
|
|
|
|
* PopupMenuSection:
|
|
|
|
*
|
|
|
|
* A section of a PopupMenu which is handled like a submenu
|
|
|
|
* (you can add and remove items, you can destroy it, you
|
|
|
|
* can add it to another menu), but is completely transparent
|
|
|
|
* to the user
|
|
|
|
*/
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupMenuSection = new Lang.Class({
|
|
|
|
Name: 'PopupMenuSection',
|
|
|
|
Extends: PopupMenuBase,
|
2011-01-25 16:04:57 -05:00
|
|
|
|
|
|
|
_init: function() {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent();
|
2011-01-25 16:04:57 -05:00
|
|
|
|
|
|
|
this.actor = this.box;
|
|
|
|
this.actor._delegate = this;
|
|
|
|
this.isOpen = true;
|
2011-12-21 17:11:40 -05:00
|
|
|
|
|
|
|
// an array of externally managed separators
|
|
|
|
this.separators = [];
|
2011-01-25 16:04:57 -05:00
|
|
|
},
|
|
|
|
|
2011-10-07 14:17:44 -04:00
|
|
|
// deliberately ignore any attempt to open() or close(), but emit the
|
|
|
|
// corresponding signal so children can still pick it up
|
2012-08-29 18:11:05 -04:00
|
|
|
open: function() { this.emit('open-state-changed', true); },
|
2011-10-07 14:17:44 -04:00
|
|
|
close: function() { this.emit('open-state-changed', false); },
|
2011-12-21 17:11:40 -05:00
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
for (let i = 0; i < this.separators.length; i++)
|
|
|
|
this.separators[i].destroy();
|
|
|
|
this.separators = [];
|
|
|
|
|
|
|
|
this.parent();
|
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-07-03 19:47:31 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupSubMenuMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupSubMenuMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2010-07-03 19:47:31 -04:00
|
|
|
|
|
|
|
_init: function(text) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent();
|
2010-11-01 11:03:28 -04:00
|
|
|
|
|
|
|
this.actor.add_style_class_name('popup-submenu-menu-item');
|
2010-07-03 19:47:31 -04:00
|
|
|
|
|
|
|
this.label = new St.Label({ text: text });
|
2010-10-19 13:41:41 -04:00
|
|
|
this.addActor(this.label);
|
2012-01-05 13:00:06 -05:00
|
|
|
this.actor.label_actor = this.label;
|
2010-11-01 11:03:28 -04:00
|
|
|
this._triangle = new St.Label({ text: '\u25B8' });
|
|
|
|
this.addActor(this._triangle, { align: St.Align.END });
|
2010-07-03 19:47:31 -04:00
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
this.menu = new PopupSubMenu(this.actor, this._triangle);
|
|
|
|
this.menu.connect('open-state-changed', Lang.bind(this, this._subMenuOpenStateChanged));
|
2010-07-03 19:47:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_subMenuOpenStateChanged: function(menu, open) {
|
2010-11-01 11:03:28 -04:00
|
|
|
if (open)
|
|
|
|
this.actor.add_style_pseudo_class('open');
|
|
|
|
else
|
|
|
|
this.actor.remove_style_pseudo_class('open');
|
2010-07-03 19:47:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
2010-11-01 11:03:28 -04:00
|
|
|
this.menu.destroy();
|
2011-11-20 08:10:48 -05:00
|
|
|
|
|
|
|
this.parent();
|
2010-07-03 19:47:31 -04:00
|
|
|
},
|
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
_onKeyPressEvent: function(actor, event) {
|
2011-02-08 14:53:43 -05:00
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
|
|
|
|
if (symbol == Clutter.KEY_Right) {
|
2012-06-15 13:16:10 -04:00
|
|
|
this.menu.open(BoxPointer.PopupAnimation.FULL);
|
2011-02-08 14:53:43 -05:00
|
|
|
this.menu.actor.navigate_focus(null, Gtk.DirectionType.DOWN, false);
|
|
|
|
return true;
|
|
|
|
} else if (symbol == Clutter.KEY_Left && this.menu.isOpen) {
|
|
|
|
this.menu.close();
|
2010-07-03 19:47:31 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-08 14:53:43 -05:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
return this.parent(actor, event);
|
2010-07-03 19:47:31 -04:00
|
|
|
},
|
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
activate: function(event) {
|
2012-06-15 13:16:10 -04:00
|
|
|
this.menu.open(BoxPointer.PopupAnimation.FULL);
|
2010-07-03 19:47:31 -04:00
|
|
|
},
|
|
|
|
|
2010-11-01 11:03:28 -04:00
|
|
|
_onButtonReleaseEvent: function(actor) {
|
|
|
|
this.menu.toggle();
|
2010-07-03 19:47:31 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2011-07-20 23:17:05 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupComboMenu = new Lang.Class({
|
|
|
|
Name: 'PopupComboMenu',
|
|
|
|
Extends: PopupMenuBase,
|
2011-07-20 23:17:05 -04:00
|
|
|
|
|
|
|
_init: function(sourceActor) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(sourceActor, 'popup-combo-menu');
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
this.actor = this.box;
|
|
|
|
this.actor._delegate = this;
|
|
|
|
this.actor.connect('key-focus-in', Lang.bind(this, this._onKeyFocusIn));
|
2011-10-26 12:29:59 -04:00
|
|
|
sourceActor.connect('style-changed',
|
|
|
|
Lang.bind(this, this._onSourceActorStyleChanged));
|
2011-07-20 23:17:05 -04:00
|
|
|
this._activeItemPos = -1;
|
|
|
|
global.focus_manager.add_group(this.actor);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onKeyFocusIn: function(actor) {
|
|
|
|
let items = this._getMenuItems();
|
|
|
|
let activeItem = items[this._activeItemPos];
|
|
|
|
activeItem.actor.grab_key_focus();
|
|
|
|
},
|
|
|
|
|
2011-10-26 12:29:59 -04:00
|
|
|
_onSourceActorStyleChanged: function() {
|
2011-10-26 13:24:36 -04:00
|
|
|
// PopupComboBoxMenuItem clones the active item's actors
|
|
|
|
// to work with arbitrary items in the menu; this means
|
|
|
|
// that we need to propagate some style information and
|
|
|
|
// enforce style updates even when the menu is closed
|
|
|
|
let activeItem = this._getMenuItems()[this._activeItemPos];
|
|
|
|
if (this.sourceActor.has_style_pseudo_class('insensitive'))
|
|
|
|
activeItem.actor.add_style_pseudo_class('insensitive');
|
|
|
|
else
|
|
|
|
activeItem.actor.remove_style_pseudo_class('insensitive');
|
|
|
|
|
|
|
|
// To propagate the :active style, we need to make sure that the
|
|
|
|
// internal state of the PopupComboMenu is updated as well, but
|
|
|
|
// we must not move the keyboard grab
|
|
|
|
activeItem.setActive(this.sourceActor.has_style_pseudo_class('active'),
|
|
|
|
{ grabKeyboard: false });
|
|
|
|
|
2011-10-26 12:29:59 -04:00
|
|
|
_ensureStyle(this.actor);
|
|
|
|
},
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
open: function() {
|
|
|
|
if (this.isOpen)
|
|
|
|
return;
|
|
|
|
|
2011-11-15 08:58:55 -05:00
|
|
|
if (this.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
this.isOpen = true;
|
|
|
|
|
|
|
|
let [sourceX, sourceY] = this.sourceActor.get_transformed_position();
|
|
|
|
let items = this._getMenuItems();
|
|
|
|
let activeItem = items[this._activeItemPos];
|
|
|
|
|
|
|
|
this.actor.set_position(sourceX, sourceY - activeItem.actor.y);
|
|
|
|
this.actor.width = Math.max(this.actor.width, this.sourceActor.width);
|
|
|
|
this.actor.raise_top();
|
|
|
|
|
|
|
|
this.actor.opacity = 0;
|
|
|
|
this.actor.show();
|
|
|
|
|
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ opacity: 255,
|
|
|
|
transition: 'linear',
|
|
|
|
time: BoxPointer.POPUP_ANIMATION_TIME });
|
|
|
|
|
|
|
|
this.emit('open-state-changed', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
if (!this.isOpen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.isOpen = false;
|
|
|
|
Tweener.addTween(this.actor,
|
|
|
|
{ opacity: 0,
|
|
|
|
transition: 'linear',
|
|
|
|
time: BoxPointer.POPUP_ANIMATION_TIME,
|
|
|
|
onComplete: Lang.bind(this,
|
|
|
|
function() {
|
|
|
|
this.actor.hide();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
this.emit('open-state-changed', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
setActiveItem: function(position) {
|
|
|
|
this._activeItemPos = position;
|
|
|
|
},
|
|
|
|
|
2012-03-15 14:04:54 -04:00
|
|
|
getActiveItem: function() {
|
|
|
|
return this._getMenuItems()[this._activeItemPos];
|
|
|
|
},
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
setItemVisible: function(position, visible) {
|
|
|
|
if (!visible && position == this._activeItemPos) {
|
|
|
|
log('Trying to hide the active menu item.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._getMenuItems()[position].actor.visible = visible;
|
2011-09-01 13:33:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getItemVisible: function(position) {
|
|
|
|
return this._getMenuItems()[position].actor.visible;
|
2011-07-20 23:17:05 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2011-07-20 23:17:05 -04:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupComboBoxMenuItem = new Lang.Class({
|
|
|
|
Name: 'PopupComboBoxMenuItem',
|
|
|
|
Extends: PopupBaseMenuItem,
|
2011-07-20 23:17:05 -04:00
|
|
|
|
|
|
|
_init: function (params) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(params);
|
2011-07-20 23:17:05 -04:00
|
|
|
|
2012-03-15 14:04:54 -04:00
|
|
|
this.actor.accessible_role = Atk.Role.COMBO_BOX;
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
this._itemBox = new Shell.Stack();
|
|
|
|
this.addActor(this._itemBox);
|
|
|
|
|
|
|
|
let expander = new St.Label({ text: '\u2304' });
|
2011-08-23 13:15:31 -04:00
|
|
|
this.addActor(expander, { align: St.Align.END,
|
|
|
|
span: -1 });
|
2011-07-20 23:17:05 -04:00
|
|
|
|
|
|
|
this._menu = new PopupComboMenu(this.actor);
|
|
|
|
Main.uiGroup.add_actor(this._menu.actor);
|
|
|
|
this._menu.actor.hide();
|
|
|
|
|
|
|
|
if (params.style_class)
|
|
|
|
this._menu.actor.add_style_class_name(params.style_class);
|
|
|
|
|
2011-09-01 13:33:31 -04:00
|
|
|
this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
this._activeItemPos = -1;
|
|
|
|
this._items = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
_getTopMenu: function() {
|
|
|
|
let actor = this.actor.get_parent();
|
|
|
|
while (actor) {
|
|
|
|
if (actor._delegate &&
|
|
|
|
(actor._delegate instanceof PopupMenu ||
|
|
|
|
actor._delegate instanceof PopupComboMenu))
|
|
|
|
return actor._delegate;
|
|
|
|
|
|
|
|
actor = actor.get_parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2011-09-01 13:33:31 -04:00
|
|
|
_onScrollEvent: function(actor, event) {
|
|
|
|
if (this._activeItemPos == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let position = this._activeItemPos;
|
|
|
|
let direction = event.get_scroll_direction();
|
|
|
|
if (direction == Clutter.ScrollDirection.DOWN) {
|
|
|
|
while (position < this._items.length - 1) {
|
|
|
|
position++;
|
|
|
|
if (this._menu.getItemVisible(position))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (direction == Clutter.ScrollDirection.UP) {
|
|
|
|
while (position > 0) {
|
|
|
|
position--;
|
|
|
|
if (this._menu.getItemVisible(position))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position == this._activeItemPos)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.setActiveItem(position);
|
|
|
|
this.emit('active-item-changed', position);
|
|
|
|
},
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
activate: function(event) {
|
|
|
|
let topMenu = this._getTopMenu();
|
|
|
|
if (!topMenu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
topMenu.addChildMenu(this._menu);
|
|
|
|
this._menu.toggle();
|
|
|
|
},
|
|
|
|
|
|
|
|
addMenuItem: function(menuItem, position) {
|
|
|
|
if (position === undefined)
|
|
|
|
position = this._menu.numMenuItems;
|
|
|
|
|
|
|
|
this._menu.addMenuItem(menuItem, position);
|
|
|
|
_ensureStyle(this._menu.actor);
|
|
|
|
|
|
|
|
let item = new St.BoxLayout({ style_class: 'popup-combobox-item' });
|
|
|
|
|
|
|
|
let children = menuItem.actor.get_children();
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
let clone = new Clutter.Clone({ source: children[i] });
|
|
|
|
item.add(clone, { y_fill: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
let oldItem = this._items[position];
|
|
|
|
if (oldItem)
|
|
|
|
this._itemBox.remove_actor(oldItem);
|
|
|
|
|
|
|
|
this._items[position] = item;
|
|
|
|
this._itemBox.add_actor(item);
|
|
|
|
|
|
|
|
menuItem.connect('activate',
|
|
|
|
Lang.bind(this, this._itemActivated, position));
|
|
|
|
},
|
|
|
|
|
2012-03-15 14:04:54 -04:00
|
|
|
checkAccessibleLabel: function() {
|
|
|
|
let activeItem = this._menu.getActiveItem();
|
|
|
|
this.actor.label_actor = activeItem.label;
|
|
|
|
},
|
|
|
|
|
2011-07-20 23:17:05 -04:00
|
|
|
setActiveItem: function(position) {
|
|
|
|
let item = this._items[position];
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
if (this._activeItemPos == position)
|
|
|
|
return;
|
|
|
|
this._menu.setActiveItem(position);
|
|
|
|
this._activeItemPos = position;
|
|
|
|
for (let i = 0; i < this._items.length; i++)
|
|
|
|
this._items[i].visible = (i == this._activeItemPos);
|
2012-03-15 14:04:54 -04:00
|
|
|
|
|
|
|
this.checkAccessibleLabel();
|
2011-07-20 23:17:05 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setItemVisible: function(position, visible) {
|
|
|
|
this._menu.setItemVisible(position, visible);
|
|
|
|
},
|
|
|
|
|
|
|
|
_itemActivated: function(menuItem, event, position) {
|
|
|
|
this.setActiveItem(position);
|
|
|
|
this.emit('active-item-changed', position);
|
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2010-07-03 19:47:31 -04:00
|
|
|
|
2011-05-15 12:55:23 -04:00
|
|
|
/**
|
|
|
|
* RemoteMenu:
|
|
|
|
*
|
|
|
|
* A PopupMenu that tracks a GMenuModel and shows its actions
|
|
|
|
* (exposed by GApplication/GActionGroup)
|
|
|
|
*/
|
|
|
|
const RemoteMenu = new Lang.Class({
|
|
|
|
Name: 'RemoteMenu',
|
|
|
|
Extends: PopupMenu,
|
|
|
|
|
|
|
|
_init: function(sourceActor, model, actionGroup) {
|
|
|
|
this.parent(sourceActor, 0.0, St.Side.TOP);
|
|
|
|
|
|
|
|
this.model = model;
|
|
|
|
this.actionGroup = actionGroup;
|
|
|
|
|
|
|
|
this._actions = { };
|
|
|
|
this._modelChanged(this.model, 0, 0, this.model.get_n_items(), this);
|
|
|
|
|
|
|
|
this._actionStateChangeId = this.actionGroup.connect('action-state-changed', Lang.bind(this, this._actionStateChanged));
|
|
|
|
this._actionEnableChangeId = this.actionGroup.connect('action-enabled-changed', Lang.bind(this, this._actionEnabledChanged));
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
if (this._actionStateChangeId) {
|
|
|
|
this.actionGroup.disconnect(this._actionStateChangeId);
|
|
|
|
this._actionStateChangeId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._actionEnableChangeId) {
|
|
|
|
this.actionGroup.disconnect(this._actionEnableChangeId);
|
|
|
|
this._actionEnableChangeId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parent();
|
|
|
|
},
|
|
|
|
|
|
|
|
_createMenuItem: function(model, index) {
|
2011-12-21 17:11:40 -05:00
|
|
|
let labelValue = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_LABEL, null);
|
|
|
|
let label = labelValue ? labelValue.deep_unpack() : '';
|
|
|
|
// remove all underscores that are not followed by another underscore
|
|
|
|
label = label.replace(/_([^_])/, '$1');
|
|
|
|
|
2011-05-15 12:55:23 -04:00
|
|
|
let section_link = model.get_item_link(index, Gio.MENU_LINK_SECTION);
|
|
|
|
if (section_link) {
|
|
|
|
let item = new PopupMenuSection();
|
2011-12-21 17:11:40 -05:00
|
|
|
if (label) {
|
|
|
|
let title = new PopupMenuItem(label, { reactive: false,
|
|
|
|
style_class: 'popup-subtitle-menu-item' });
|
|
|
|
item._titleMenuItem = title;
|
|
|
|
title._ignored = true;
|
|
|
|
item.addMenuItem(title);
|
|
|
|
}
|
2011-05-15 12:55:23 -04:00
|
|
|
this._modelChanged(section_link, 0, 0, section_link.get_n_items(), item);
|
|
|
|
return [item, true, ''];
|
|
|
|
}
|
|
|
|
|
|
|
|
let submenu_link = model.get_item_link(index, Gio.MENU_LINK_SUBMENU);
|
|
|
|
|
|
|
|
if (submenu_link) {
|
|
|
|
let item = new PopupSubMenuMenuItem(label);
|
|
|
|
this._modelChanged(submenu_link, 0, 0, submenu_link.get_n_items(), item.menu);
|
|
|
|
return [item, false, ''];
|
|
|
|
}
|
|
|
|
|
|
|
|
let action_id = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_ACTION, null).deep_unpack();
|
|
|
|
if (!this.actionGroup.has_action(action_id)) {
|
|
|
|
// the action may not be there yet, wait for action-added
|
|
|
|
return [null, false, 'action-added'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._actions[action_id])
|
|
|
|
this._actions[action_id] = { enabled: this.actionGroup.get_action_enabled(action_id),
|
|
|
|
state: this.actionGroup.get_action_state(action_id),
|
|
|
|
items: [ ],
|
|
|
|
};
|
|
|
|
let action = this._actions[action_id];
|
|
|
|
let item, target, destroyId, specificSignalId;
|
|
|
|
|
|
|
|
if (action.state) {
|
|
|
|
// Docs have get_state_hint(), except that the DBus protocol
|
|
|
|
// has no provision for it (so ShellApp does not implement it,
|
|
|
|
// and neither GApplication), and g_action_get_state_hint()
|
|
|
|
// always returns null
|
|
|
|
// Funny :)
|
|
|
|
|
|
|
|
switch (String.fromCharCode(action.state.classify())) {
|
|
|
|
case 'b':
|
|
|
|
item = new PopupSwitchMenuItem(label, action.state.get_boolean());
|
|
|
|
action.items.push(item);
|
|
|
|
specificSignalId = item.connect('toggled', Lang.bind(this, function(item) {
|
2012-04-27 15:40:34 -04:00
|
|
|
this.actionGroup.activate_action(action_id, null);
|
2011-05-15 12:55:23 -04:00
|
|
|
}));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
item = new PopupMenuItem(label);
|
|
|
|
item._remoteTarget = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_TARGET, null).deep_unpack();
|
|
|
|
action.items.push(item);
|
|
|
|
item.setShowDot(action.state.deep_unpack() == item._remoteTarget);
|
|
|
|
specificSignalId = item.connect('activate', Lang.bind(this, function(item) {
|
|
|
|
this.actionGroup.activate_action(action_id, GLib.Variant.new_string(item._remoteTarget));
|
|
|
|
}));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log('Action "%s" has state of type %s, which is not supported'.format(action_id, action.state.get_type_string()));
|
|
|
|
return [null, false, 'action-state-changed'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target = model.get_item_attribute_value(index, Gio.MENU_ATTRIBUTE_TARGET, null);
|
|
|
|
item = new PopupMenuItem(label);
|
|
|
|
action.items.push(item);
|
|
|
|
specificSignalId = item.connect('activate', Lang.bind(this, function() {
|
|
|
|
this.actionGroup.activate_action(action_id, target);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2012-08-19 21:40:09 -04:00
|
|
|
item.actor.reactive = item.actor.can_focus = action.enabled;
|
2011-05-15 12:55:23 -04:00
|
|
|
|
|
|
|
destroyId = item.connect('destroy', Lang.bind(this, function() {
|
|
|
|
item.disconnect(destroyId);
|
|
|
|
item.disconnect(specificSignalId);
|
|
|
|
|
|
|
|
let pos = action.items.indexOf(item);
|
|
|
|
if (pos != -1)
|
|
|
|
action.items.splice(pos, 1);
|
|
|
|
}));
|
|
|
|
|
|
|
|
return [item, false, ''];
|
|
|
|
},
|
|
|
|
|
|
|
|
_modelChanged: function(model, position, removed, added, target) {
|
|
|
|
let j, k;
|
|
|
|
let j0, k0;
|
|
|
|
|
|
|
|
let currentItems = target._getMenuItems();
|
|
|
|
|
2011-12-21 17:11:40 -05:00
|
|
|
k0 = 0;
|
|
|
|
// skip ignored items at the beginning
|
|
|
|
while (k0 < currentItems.length && currentItems[k0]._ignored)
|
|
|
|
k0++;
|
|
|
|
// find the right menu item matching the model item
|
2012-05-20 17:36:27 -04:00
|
|
|
for (j0 = 0; k0 < currentItems.length && j0 < position; j0++, k0++) {
|
2011-12-21 17:11:40 -05:00
|
|
|
if (currentItems[k0]._ignored)
|
2011-05-15 12:55:23 -04:00
|
|
|
k0++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removed == -1) {
|
|
|
|
// special flag to indicate we should destroy everything
|
|
|
|
for (k = k0; k < currentItems.length; k++)
|
|
|
|
currentItems[k].destroy();
|
|
|
|
} else {
|
2012-05-20 17:36:27 -04:00
|
|
|
for (j = j0, k = k0; k < currentItems.length && j < j0 + removed; j++, k++) {
|
2011-05-15 12:55:23 -04:00
|
|
|
currentItems[k].destroy();
|
|
|
|
|
2011-12-21 17:11:40 -05:00
|
|
|
if (currentItems[k]._ignored)
|
2011-05-15 12:55:23 -04:00
|
|
|
j--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = j0, k = k0; j < j0 + added; j++, k++) {
|
|
|
|
let [item, addSeparator, changeSignal] = this._createMenuItem(model, j);
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
// separators must be added in the parent to make autohiding work
|
|
|
|
if (addSeparator) {
|
2011-12-21 17:11:40 -05:00
|
|
|
let separator = new PopupSeparatorMenuItem();
|
|
|
|
item.separators.push(separator);
|
|
|
|
separator._ignored = true;
|
|
|
|
target.addMenuItem(separator, k+1);
|
2011-05-15 12:55:23 -04:00
|
|
|
k++;
|
|
|
|
}
|
|
|
|
|
|
|
|
target.addMenuItem(item, k);
|
|
|
|
|
|
|
|
if (addSeparator) {
|
2011-12-21 17:11:40 -05:00
|
|
|
let separator = new PopupSeparatorMenuItem();
|
|
|
|
item.separators.push(separator);
|
|
|
|
separator._ignored = true;
|
|
|
|
target.addMenuItem(separator, k+1);
|
2011-05-15 12:55:23 -04:00
|
|
|
k++;
|
|
|
|
}
|
|
|
|
} else if (changeSignal) {
|
2012-05-20 17:37:22 -04:00
|
|
|
let signalId = this.actionGroup.connect(changeSignal, Lang.bind(this, function(actionGroup, actionName) {
|
|
|
|
actionGroup.disconnect(signalId);
|
|
|
|
if (this._actions[actionName]) return;
|
2011-05-15 12:55:23 -04:00
|
|
|
|
|
|
|
// force a full update
|
|
|
|
this._modelChanged(model, 0, -1, model.get_n_items(), target);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!model._changedId) {
|
|
|
|
model._changedId = model.connect('items-changed', Lang.bind(this, this._modelChanged, target));
|
|
|
|
model._destroyId = target.connect('destroy', function() {
|
|
|
|
if (model._changedId)
|
|
|
|
model.disconnect(model._changedId);
|
|
|
|
if (model._destroyId)
|
|
|
|
target.disconnect(model._destroyId);
|
|
|
|
model._changedId = 0;
|
|
|
|
model._destroyId = 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target instanceof PopupMenuSection) {
|
2011-12-21 17:11:40 -05:00
|
|
|
if (target._titleMenuItem)
|
|
|
|
target.actor.visible = target.numMenuItems != 1;
|
|
|
|
else
|
|
|
|
target.actor.visible = target.numMenuItems != 0;
|
2011-05-15 12:55:23 -04:00
|
|
|
} else {
|
|
|
|
let sourceItem = target.sourceActor._delegate;
|
|
|
|
if (sourceItem instanceof PopupSubMenuMenuItem)
|
|
|
|
sourceItem.actor.visible = target.numMenuItems != 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_actionStateChanged: function(actionGroup, action_id) {
|
|
|
|
let action = this._actions[action_id];
|
|
|
|
if (!action)
|
|
|
|
return;
|
|
|
|
|
|
|
|
action.state = actionGroup.get_action_state(action_id);
|
|
|
|
if (action.items.length) {
|
|
|
|
switch (String.fromCharCode(action.state.classify())) {
|
|
|
|
case 'b':
|
|
|
|
for (let i = 0; i < action.items.length; i++)
|
|
|
|
action.items[i].setToggleState(action.state.get_boolean());
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
for (let i = 0; i < action.items.length; i++)
|
|
|
|
action.items[i].setValue(action.state.get_double());
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
for (let i = 0; i < action.items.length; i++)
|
|
|
|
action.items[i].setShowDot(action.items[i]._remoteTarget == action.state.deep_unpack());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_actionEnabledChanged: function(actionGroup, action_id) {
|
|
|
|
let action = this._actions[action_id];
|
|
|
|
if (!action)
|
|
|
|
return;
|
|
|
|
|
|
|
|
action.enabled = actionGroup.get_action_enabled(action_id);
|
|
|
|
if (action.items.length) {
|
|
|
|
for (let i = 0; i < action.items.length; i++) {
|
|
|
|
let item = action.items[i];
|
2012-08-19 21:40:09 -04:00
|
|
|
item.actor.reactive = item.actor.can_focus = action.enabled;
|
2011-05-15 12:55:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-05-20 11:18:46 -04:00
|
|
|
/* Basic implementation of a menu manager.
|
|
|
|
* Call addMenu to add menus
|
|
|
|
*/
|
2011-11-20 08:10:48 -05:00
|
|
|
const PopupMenuManager = new Lang.Class({
|
|
|
|
Name: 'PopupMenuManager',
|
2010-05-20 11:18:46 -04:00
|
|
|
|
|
|
|
_init: function(owner) {
|
|
|
|
this._owner = owner;
|
2012-02-29 19:09:41 -05:00
|
|
|
this._grabHelper = new GrabHelper.GrabHelper(owner.actor);
|
2010-05-20 11:18:46 -04:00
|
|
|
this._menus = [];
|
|
|
|
},
|
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
addMenu: function(menu, position) {
|
2012-09-01 08:42:53 -04:00
|
|
|
if (this._findMenu(menu) > -1)
|
|
|
|
return;
|
|
|
|
|
2010-06-25 08:55:03 -04:00
|
|
|
let menudata = {
|
|
|
|
menu: menu,
|
|
|
|
openStateChangeId: menu.connect('open-state-changed', Lang.bind(this, this._onMenuOpenState)),
|
2011-07-12 15:47:37 -04:00
|
|
|
childMenuAddedId: menu.connect('child-menu-added', Lang.bind(this, this._onChildMenuAdded)),
|
|
|
|
childMenuRemovedId: menu.connect('child-menu-removed', Lang.bind(this, this._onChildMenuRemoved)),
|
2010-09-29 12:12:38 -04:00
|
|
|
destroyId: menu.connect('destroy', Lang.bind(this, this._onMenuDestroy)),
|
2010-06-25 08:55:03 -04:00
|
|
|
enterId: 0,
|
2011-02-08 14:53:43 -05:00
|
|
|
focusInId: 0
|
2010-06-25 08:55:03 -04:00
|
|
|
};
|
2010-05-20 11:18:46 -04:00
|
|
|
|
|
|
|
let source = menu.sourceActor;
|
|
|
|
if (source) {
|
2012-02-29 19:09:41 -05:00
|
|
|
if (!menu.blockSourceEvents)
|
|
|
|
this._grabHelper.addActor(source);
|
2010-10-07 14:15:51 -04:00
|
|
|
menudata.enterId = source.connect('enter-event', Lang.bind(this, function() { this._onMenuSourceEnter(menu); }));
|
2010-11-03 13:30:08 -04:00
|
|
|
menudata.focusInId = source.connect('key-focus-in', Lang.bind(this, function() { this._onMenuSourceEnter(menu); }));
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2010-06-25 08:55:03 -04:00
|
|
|
|
|
|
|
if (position == undefined)
|
|
|
|
this._menus.push(menudata);
|
|
|
|
else
|
|
|
|
this._menus.splice(position, 0, menudata);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeMenu: function(menu) {
|
|
|
|
if (menu == this._activeMenu)
|
2012-02-29 19:09:41 -05:00
|
|
|
this._closeMenu(menu);
|
2010-07-03 19:47:31 -04:00
|
|
|
|
2010-06-25 08:55:03 -04:00
|
|
|
let position = this._findMenu(menu);
|
|
|
|
if (position == -1) // not a menu we manage
|
|
|
|
return;
|
|
|
|
|
|
|
|
let menudata = this._menus[position];
|
|
|
|
menu.disconnect(menudata.openStateChangeId);
|
2011-07-12 15:47:37 -04:00
|
|
|
menu.disconnect(menudata.childMenuAddedId);
|
|
|
|
menu.disconnect(menudata.childMenuRemovedId);
|
2010-07-03 19:47:31 -04:00
|
|
|
menu.disconnect(menudata.destroyId);
|
2010-06-25 08:55:03 -04:00
|
|
|
|
|
|
|
if (menudata.enterId)
|
|
|
|
menu.sourceActor.disconnect(menudata.enterId);
|
2010-11-03 13:30:08 -04:00
|
|
|
if (menudata.focusInId)
|
|
|
|
menu.sourceActor.disconnect(menudata.focusInId);
|
2010-06-25 08:55:03 -04:00
|
|
|
|
2012-02-29 19:09:41 -05:00
|
|
|
if (menu.sourceActor)
|
|
|
|
this._grabHelper.removeActor(menu.sourceActor);
|
2010-06-25 08:55:03 -04:00
|
|
|
this._menus.splice(position, 1);
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2012-02-29 19:09:41 -05:00
|
|
|
get activeMenu() {
|
|
|
|
let actor = this._grabHelper.currentGrab.actor;
|
|
|
|
if (actor)
|
|
|
|
return actor._delegate;
|
|
|
|
else
|
|
|
|
return null;
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2012-02-29 19:09:41 -05:00
|
|
|
ignoreRelease: function() {
|
|
|
|
return this._grabHelper.ignoreRelease();
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_onMenuOpenState: function(menu, open) {
|
2011-07-12 15:47:37 -04:00
|
|
|
if (open) {
|
2012-02-29 19:09:41 -05:00
|
|
|
this._grabHelper.grab({ actor: menu.actor, modal: true, focus: menu.sourceActor,
|
|
|
|
onUngrab: Lang.bind(this, this._closeMenu, menu) });
|
2011-07-12 15:47:37 -04:00
|
|
|
} else {
|
2012-02-29 19:09:41 -05:00
|
|
|
this._grabHelper.ungrab({ actor: menu.actor });
|
2010-11-03 13:30:08 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-07-12 15:47:37 -04:00
|
|
|
_onChildMenuAdded: function(menu, childMenu) {
|
|
|
|
this.addMenu(childMenu);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onChildMenuRemoved: function(menu, childMenu) {
|
|
|
|
this.removeMenu(childMenu);
|
|
|
|
},
|
|
|
|
|
2010-11-03 13:30:08 -04:00
|
|
|
_changeMenu: function(newMenu) {
|
2012-12-10 03:47:32 -05:00
|
|
|
let oldMenu = this.activeMenu;
|
|
|
|
if (oldMenu) {
|
|
|
|
oldMenu.close(BoxPointer.PopupAnimation.FADE);
|
|
|
|
newMenu.open(BoxPointer.PopupAnimation.FADE);
|
2012-02-29 19:09:41 -05:00
|
|
|
} else {
|
2012-12-10 03:47:32 -05:00
|
|
|
newMenu.open(BoxPointer.PopupAnimation.FULL);
|
2012-02-29 19:09:41 -05:00
|
|
|
}
|
2010-05-20 11:18:46 -04:00
|
|
|
},
|
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
_onMenuSourceEnter: function(menu) {
|
2012-02-29 19:09:41 -05:00
|
|
|
if (!this._grabHelper.grabbed)
|
2010-05-20 11:18:46 -04:00
|
|
|
return false;
|
|
|
|
|
2012-02-29 19:09:41 -05:00
|
|
|
if (this._grabHelper.isActorGrabbed(menu.actor))
|
2011-07-12 15:47:37 -04:00
|
|
|
return false;
|
|
|
|
|
2012-02-29 19:09:41 -05:00
|
|
|
let isChildMenu = this._grabHelper.grabStack.some(function(grab) {
|
|
|
|
let existingMenu = grab.actor._delegate;
|
|
|
|
return existingMenu.isChildMenu(menu);
|
|
|
|
});
|
|
|
|
if (isChildMenu)
|
2011-07-12 15:47:37 -04:00
|
|
|
return false;
|
|
|
|
|
2010-11-03 13:30:08 -04:00
|
|
|
this._changeMenu(menu);
|
2010-05-20 11:18:46 -04:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2010-07-03 19:47:31 -04:00
|
|
|
_onMenuDestroy: function(menu) {
|
|
|
|
this.removeMenu(menu);
|
|
|
|
},
|
|
|
|
|
2010-06-25 08:55:03 -04:00
|
|
|
_findMenu: function(item) {
|
|
|
|
for (let i = 0; i < this._menus.length; i++) {
|
|
|
|
let menudata = this._menus[i];
|
|
|
|
if (item == menudata.menu)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
|
2012-12-10 03:55:14 -05:00
|
|
|
_closeMenu: function(isUser, menu) {
|
|
|
|
// If this isn't a user action, we called close()
|
|
|
|
// on the BoxPointer ourselves, so we shouldn't
|
|
|
|
// reanimate.
|
|
|
|
if (isUser)
|
|
|
|
menu.close(BoxPointer.PopupAnimation.FULL);
|
2010-05-20 11:18:46 -04:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|