2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-06-22 17:02:26 -04:00
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
const Clutter = imports.gi.Clutter;
|
2012-08-26 09:49:18 -04:00
|
|
|
const Gio = imports.gi.Gio;
|
2011-02-08 14:53:43 -05:00
|
|
|
const Gtk = imports.gi.Gtk;
|
2011-06-09 11:50:24 -04:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const Shell = imports.gi.Shell;
|
2011-08-22 17:19:13 -04:00
|
|
|
const Signals = imports.signals;
|
2010-06-22 17:02:26 -04:00
|
|
|
const St = imports.gi.St;
|
2012-02-27 11:31:10 -05:00
|
|
|
const Atk = imports.gi.Atk;
|
2011-02-08 14:53:43 -05:00
|
|
|
|
2010-06-22 17:02:26 -04:00
|
|
|
const Main = imports.ui.main;
|
2011-06-09 11:50:24 -04:00
|
|
|
const Params = imports.misc.params;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2011-11-20 09:38:48 -05:00
|
|
|
const ButtonBox = new Lang.Class({
|
|
|
|
Name: 'ButtonBox',
|
2011-06-09 11:50:24 -04:00
|
|
|
|
|
|
|
_init: function(params) {
|
|
|
|
params = Params.parse(params, { style_class: 'panel-button' }, true);
|
|
|
|
this.actor = new Shell.GenericContainer(params);
|
|
|
|
this.actor._delegate = this;
|
|
|
|
|
2012-09-04 12:27:50 -04:00
|
|
|
this.container = new St.Bin({ y_fill: true,
|
|
|
|
x_fill: true,
|
|
|
|
child: this.actor });
|
|
|
|
|
2011-06-09 11:50:24 -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));
|
|
|
|
this._minHPadding = this._natHPadding = 0.0;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onStyleChanged: function(actor) {
|
|
|
|
let themeNode = actor.get_theme_node();
|
|
|
|
|
|
|
|
this._minHPadding = themeNode.get_length('-minimum-hpadding');
|
|
|
|
this._natHPadding = themeNode.get_length('-natural-hpadding');
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPreferredWidth: function(actor, forHeight, alloc) {
|
|
|
|
let children = actor.get_children();
|
|
|
|
let child = children.length > 0 ? children[0] : null;
|
|
|
|
|
|
|
|
if (child) {
|
|
|
|
[alloc.min_size, alloc.natural_size] = child.get_preferred_width(-1);
|
|
|
|
} else {
|
|
|
|
alloc.min_size = alloc.natural_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc.min_size += 2 * this._minHPadding;
|
|
|
|
alloc.natural_size += 2 * this._natHPadding;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPreferredHeight: function(actor, forWidth, alloc) {
|
|
|
|
let children = actor.get_children();
|
|
|
|
let child = children.length > 0 ? children[0] : null;
|
|
|
|
|
|
|
|
if (child) {
|
|
|
|
[alloc.min_size, alloc.natural_size] = child.get_preferred_height(-1);
|
|
|
|
} else {
|
|
|
|
alloc.min_size = alloc.natural_size = 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_allocate: function(actor, box, flags) {
|
|
|
|
let children = actor.get_children();
|
|
|
|
if (children.length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let child = children[0];
|
|
|
|
let [minWidth, natWidth] = child.get_preferred_width(-1);
|
|
|
|
let [minHeight, natHeight] = child.get_preferred_height(-1);
|
|
|
|
|
|
|
|
let availWidth = box.x2 - box.x1;
|
|
|
|
let availHeight = box.y2 - box.y1;
|
|
|
|
|
|
|
|
let childBox = new Clutter.ActorBox();
|
|
|
|
if (natWidth + 2 * this._natHPadding <= availWidth) {
|
|
|
|
childBox.x1 = this._natHPadding;
|
|
|
|
childBox.x2 = availWidth - this._natHPadding;
|
|
|
|
} else {
|
|
|
|
childBox.x1 = this._minHPadding;
|
|
|
|
childBox.x2 = availWidth - this._minHPadding;
|
|
|
|
}
|
|
|
|
|
2013-08-03 08:30:46 -04:00
|
|
|
childBox.y1 = 0;
|
|
|
|
childBox.y2 = availHeight;
|
2011-06-09 11:50:24 -04:00
|
|
|
|
|
|
|
child.allocate(childBox, flags);
|
|
|
|
},
|
2011-11-20 09:38:48 -05:00
|
|
|
});
|
2010-06-22 17:02:26 -04:00
|
|
|
|
2011-11-20 09:38:48 -05:00
|
|
|
const Button = new Lang.Class({
|
|
|
|
Name: 'PanelMenuButton',
|
|
|
|
Extends: ButtonBox,
|
2011-06-09 11:50:24 -04:00
|
|
|
|
2012-01-05 13:00:06 -05:00
|
|
|
_init: function(menuAlignment, nameText, dontCreateMenu) {
|
2011-11-20 09:38:48 -05:00
|
|
|
this.parent({ reactive: true,
|
|
|
|
can_focus: true,
|
2012-02-27 11:31:10 -05:00
|
|
|
track_hover: true,
|
|
|
|
accessible_role: Atk.Role.MENU });
|
2011-06-09 11:50:24 -04:00
|
|
|
|
2010-06-22 17:02:26 -04:00
|
|
|
this.actor.connect('button-press-event', Lang.bind(this, this._onButtonPress));
|
2011-02-08 14:53:43 -05:00
|
|
|
this.actor.connect('key-press-event', Lang.bind(this, this._onSourceKeyPress));
|
2013-07-03 09:55:35 -04:00
|
|
|
this.actor.connect('notify::visible', Lang.bind(this, this._onVisibilityChanged));
|
2011-05-15 12:55:23 -04:00
|
|
|
|
|
|
|
if (dontCreateMenu)
|
2012-12-04 14:52:34 -05:00
|
|
|
this.menu = new PopupMenu.PopupDummyMenu(this.actor);
|
2011-05-15 12:55:23 -04:00
|
|
|
else
|
|
|
|
this.setMenu(new PopupMenu.PopupMenu(this.actor, menuAlignment, St.Side.TOP, 0));
|
2012-01-05 13:00:06 -05:00
|
|
|
|
|
|
|
this.setName(nameText);
|
|
|
|
},
|
|
|
|
|
2012-09-01 17:44:46 -04:00
|
|
|
setSensitive: function(sensitive) {
|
|
|
|
this.actor.reactive = sensitive;
|
|
|
|
this.actor.can_focus = sensitive;
|
|
|
|
this.actor.track_hover = sensitive;
|
|
|
|
},
|
|
|
|
|
2012-01-05 13:00:06 -05:00
|
|
|
setName: function(text) {
|
|
|
|
if (text != null) {
|
|
|
|
// This is the easiest way to provide a accessible name to
|
|
|
|
// this widget. The label could be also used for other
|
|
|
|
// purposes in the future.
|
|
|
|
if (!this.label) {
|
|
|
|
this.label = new St.Label({ text: text });
|
|
|
|
this.actor.label_actor = this.label;
|
|
|
|
} else
|
|
|
|
this.label.text = text;
|
|
|
|
} else {
|
|
|
|
this.label = null;
|
|
|
|
this.actor.label_actor = null;
|
|
|
|
}
|
2011-05-15 12:55:23 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setMenu: function(menu) {
|
|
|
|
if (this.menu)
|
|
|
|
this.menu.destroy();
|
|
|
|
|
|
|
|
this.menu = menu;
|
|
|
|
if (this.menu) {
|
|
|
|
this.menu.actor.add_style_class_name('panel-menu');
|
|
|
|
this.menu.connect('open-state-changed', Lang.bind(this, this._onOpenStateChanged));
|
|
|
|
this.menu.actor.connect('key-press-event', Lang.bind(this, this._onMenuKeyPress));
|
|
|
|
|
|
|
|
Main.uiGroup.add_actor(this.menu.actor);
|
|
|
|
this.menu.actor.hide();
|
|
|
|
}
|
2010-06-22 17:02:26 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_onButtonPress: function(actor, event) {
|
2011-05-15 12:55:23 -04:00
|
|
|
if (!this.menu)
|
|
|
|
return;
|
|
|
|
|
2010-06-22 17:02:26 -04:00
|
|
|
this.menu.toggle();
|
|
|
|
},
|
|
|
|
|
2011-02-08 14:53:43 -05:00
|
|
|
_onSourceKeyPress: function(actor, event) {
|
2011-05-15 12:55:23 -04:00
|
|
|
if (!this.menu)
|
|
|
|
return false;
|
|
|
|
|
2010-10-07 14:15:51 -04:00
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
if (symbol == Clutter.KEY_space || symbol == Clutter.KEY_Return) {
|
|
|
|
this.menu.toggle();
|
|
|
|
return true;
|
2011-02-08 14:53:43 -05:00
|
|
|
} else if (symbol == Clutter.KEY_Escape && this.menu.isOpen) {
|
|
|
|
this.menu.close();
|
|
|
|
return true;
|
2010-10-07 14:15:51 -04:00
|
|
|
} else if (symbol == Clutter.KEY_Down) {
|
|
|
|
if (!this.menu.isOpen)
|
|
|
|
this.menu.toggle();
|
2011-02-08 14:53:43 -05:00
|
|
|
this.menu.actor.navigate_focus(this.actor, Gtk.DirectionType.DOWN, false);
|
2010-10-07 14:15:51 -04:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2013-07-03 09:55:35 -04:00
|
|
|
_onVisibilityChanged: function() {
|
|
|
|
if (!this.menu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!this.actor.visible)
|
|
|
|
this.menu.close();
|
|
|
|
},
|
|
|
|
|
2011-02-08 14:53:43 -05:00
|
|
|
_onMenuKeyPress: function(actor, event) {
|
2013-05-03 21:33:05 -04:00
|
|
|
if (global.focus_manager.navigate_from_event(event))
|
|
|
|
return true;
|
|
|
|
|
2011-02-08 14:53:43 -05:00
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
if (symbol == Clutter.KEY_Left || symbol == Clutter.KEY_Right) {
|
2012-08-15 12:22:01 -04:00
|
|
|
let group = global.focus_manager.get_group(this.actor);
|
2011-02-08 14:53:43 -05:00
|
|
|
if (group) {
|
|
|
|
let direction = (symbol == Clutter.KEY_Left) ? Gtk.DirectionType.LEFT : Gtk.DirectionType.RIGHT;
|
|
|
|
group.navigate_focus(this.actor, direction, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2010-06-22 17:02:26 -04:00
|
|
|
_onOpenStateChanged: function(menu, open) {
|
2010-11-03 13:30:08 -04:00
|
|
|
if (open)
|
2011-02-08 13:46:05 -05:00
|
|
|
this.actor.add_style_pseudo_class('active');
|
2010-11-03 13:30:08 -04:00
|
|
|
else
|
2011-02-08 13:46:05 -05:00
|
|
|
this.actor.remove_style_pseudo_class('active');
|
2012-02-28 13:09:56 -05:00
|
|
|
|
|
|
|
// Setting the max-height won't do any good if the minimum height of the
|
|
|
|
// menu is higher then the screen; it's useful if part of the menu is
|
|
|
|
// scrollable so the minimum height is smaller than the natural height
|
2013-01-28 00:09:12 -05:00
|
|
|
let workArea = Main.layoutManager.getWorkAreaForMonitor(Main.layoutManager.primaryIndex);
|
|
|
|
this.menu.actor.style = ('max-height: ' + Math.round(workArea.height) + 'px;');
|
2011-08-22 17:19:13 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
this.actor._delegate = null;
|
|
|
|
|
2012-10-26 06:34:45 -04:00
|
|
|
if (this.menu)
|
|
|
|
this.menu.destroy();
|
2011-08-22 17:19:13 -04:00
|
|
|
this.actor.destroy();
|
|
|
|
|
|
|
|
this.emit('destroy');
|
2010-06-22 17:02:26 -04:00
|
|
|
}
|
2011-11-20 09:38:48 -05:00
|
|
|
});
|
2011-08-22 17:19:13 -04:00
|
|
|
Signals.addSignalMethods(Button.prototype);
|
2010-06-22 17:06:17 -04:00
|
|
|
|
2013-06-06 17:27:25 -04:00
|
|
|
/* SystemIndicator:
|
2010-06-22 17:06:17 -04:00
|
|
|
*
|
2013-06-06 17:27:25 -04:00
|
|
|
* This class manages one system indicator, which are the icons
|
|
|
|
* that you see at the top right. A system indicator is composed
|
|
|
|
* of an icon and a menu section, which will be composed into the
|
|
|
|
* aggregate menu.
|
2010-06-22 17:06:17 -04:00
|
|
|
*/
|
2013-06-06 17:27:25 -04:00
|
|
|
const SystemIndicator = new Lang.Class({
|
|
|
|
Name: 'SystemIndicator',
|
2010-06-22 17:06:17 -04:00
|
|
|
|
2013-06-06 17:27:25 -04:00
|
|
|
_init: function() {
|
2013-06-06 17:27:32 -04:00
|
|
|
this.indicators = new St.BoxLayout({ style_class: 'panel-status-indicators-box',
|
|
|
|
reactive: true });
|
2013-07-15 19:46:25 -04:00
|
|
|
this.indicators.hide();
|
2013-06-06 17:27:32 -04:00
|
|
|
this.menu = new PopupMenu.PopupMenuSection();
|
2012-12-20 20:25:54 -05:00
|
|
|
},
|
|
|
|
|
2013-07-15 19:46:25 -04:00
|
|
|
_syncIndicatorsVisible: function() {
|
|
|
|
this.indicators.visible = this.indicators.get_children().some(function(actor) {
|
|
|
|
return actor.visible;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-07-19 06:05:47 -04:00
|
|
|
_addIndicator: function() {
|
|
|
|
let icon = new St.Icon({ style_class: 'system-status-icon' });
|
2013-06-06 17:27:25 -04:00
|
|
|
this.indicators.add_actor(icon);
|
2013-07-15 19:46:25 -04:00
|
|
|
icon.connect('notify::visible', Lang.bind(this, this._syncIndicatorsVisible));
|
|
|
|
this._syncIndicatorsVisible();
|
2012-08-26 09:49:18 -04:00
|
|
|
return icon;
|
2010-06-22 17:06:17 -04:00
|
|
|
}
|
2011-11-20 09:38:48 -05:00
|
|
|
});
|
2013-06-06 17:27:32 -04:00
|
|
|
Signals.addSignalMethods(SystemIndicator.prototype);
|