2011-09-14 12:47:09 -04:00
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const Params = imports.misc.params;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2012-05-27 13:06:08 -04:00
|
|
|
const EntryMenu = new Lang.Class({
|
2011-11-20 08:10:48 -05:00
|
|
|
Name: 'ShellEntryMenu',
|
|
|
|
Extends: PopupMenu.PopupMenu,
|
2011-09-14 12:47:09 -04:00
|
|
|
|
|
|
|
_init: function(entry, params) {
|
|
|
|
params = Params.parse (params, { isPassword: false });
|
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent(entry, 0, St.Side.TOP);
|
2011-09-14 12:47:09 -04:00
|
|
|
|
|
|
|
this.actor.add_style_class_name('entry-context-menu');
|
|
|
|
|
|
|
|
this._entry = entry;
|
|
|
|
this._clipboard = St.Clipboard.get_default();
|
|
|
|
|
|
|
|
// Populate menu
|
|
|
|
let item;
|
|
|
|
item = new PopupMenu.PopupMenuItem(_("Copy"));
|
|
|
|
item.connect('activate', Lang.bind(this, this._onCopyActivated));
|
|
|
|
this.addMenuItem(item);
|
|
|
|
this._copyItem = item;
|
|
|
|
|
|
|
|
item = new PopupMenu.PopupMenuItem(_("Paste"));
|
|
|
|
item.connect('activate', Lang.bind(this, this._onPasteActivated));
|
|
|
|
this.addMenuItem(item);
|
|
|
|
this._pasteItem = item;
|
|
|
|
|
|
|
|
this._passwordItem = null;
|
2012-05-27 13:06:08 -04:00
|
|
|
if (params.isPassword)
|
|
|
|
this._makePasswordItem();
|
2011-09-14 12:47:09 -04:00
|
|
|
|
|
|
|
Main.uiGroup.add_actor(this.actor);
|
|
|
|
this.actor.hide();
|
|
|
|
},
|
|
|
|
|
2012-05-27 13:06:08 -04:00
|
|
|
_makePasswordItem: function() {
|
|
|
|
let item = new PopupMenu.PopupMenuItem('');
|
|
|
|
item.connect('activate', Lang.bind(this,
|
|
|
|
this._onPasswordActivated));
|
|
|
|
this.addMenuItem(item);
|
|
|
|
this._passwordItem = item;
|
|
|
|
},
|
|
|
|
|
|
|
|
get isPassword() {
|
|
|
|
return this._passwordItem != null;
|
|
|
|
},
|
|
|
|
|
|
|
|
set isPassword(v) {
|
|
|
|
if (v == this.isPassword)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (v)
|
|
|
|
this._makePasswordItem();
|
|
|
|
else {
|
|
|
|
this._passwordItem.destroy();
|
|
|
|
this._passwordItem = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-09-14 12:47:09 -04:00
|
|
|
open: function() {
|
|
|
|
this._updatePasteItem();
|
|
|
|
this._updateCopyItem();
|
|
|
|
if (this._passwordItem)
|
|
|
|
this._updatePasswordItem();
|
|
|
|
|
|
|
|
let direction = Gtk.DirectionType.TAB_FORWARD;
|
|
|
|
if (!this.actor.navigate_focus(null, direction, false))
|
|
|
|
this.actor.grab_key_focus();
|
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent();
|
2012-09-06 15:35:54 -04:00
|
|
|
this._entry.add_style_pseudo_class('focus');
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
this._entry.grab_key_focus();
|
|
|
|
this.parent();
|
2011-09-14 12:47:09 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_updateCopyItem: function() {
|
|
|
|
let selection = this._entry.clutter_text.get_selection();
|
|
|
|
this._copyItem.setSensitive(selection && selection != '');
|
|
|
|
},
|
|
|
|
|
|
|
|
_updatePasteItem: function() {
|
2011-11-09 11:19:47 -05:00
|
|
|
this._clipboard.get_text(St.ClipboardType.CLIPBOARD, Lang.bind(this,
|
2011-09-14 12:47:09 -04:00
|
|
|
function(clipboard, text) {
|
|
|
|
this._pasteItem.setSensitive(text && text != '');
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_updatePasswordItem: function() {
|
|
|
|
let textHidden = (this._entry.clutter_text.password_char);
|
|
|
|
if (textHidden)
|
|
|
|
this._passwordItem.label.set_text(_("Show Text"));
|
|
|
|
else
|
|
|
|
this._passwordItem.label.set_text(_("Hide Text"));
|
|
|
|
},
|
|
|
|
|
|
|
|
_onCopyActivated: function() {
|
|
|
|
let selection = this._entry.clutter_text.get_selection();
|
2011-11-09 11:19:47 -05:00
|
|
|
this._clipboard.set_text(St.ClipboardType.CLIPBOARD, selection);
|
2011-09-14 12:47:09 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_onPasteActivated: function() {
|
2011-11-09 11:19:47 -05:00
|
|
|
this._clipboard.get_text(St.ClipboardType.CLIPBOARD, Lang.bind(this,
|
2011-09-14 12:47:09 -04:00
|
|
|
function(clipboard, text) {
|
|
|
|
if (!text)
|
|
|
|
return;
|
|
|
|
this._entry.clutter_text.delete_selection();
|
|
|
|
let pos = this._entry.clutter_text.get_cursor_position();
|
|
|
|
this._entry.clutter_text.insert_text(text, pos);
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_onPasswordActivated: function() {
|
|
|
|
let visible = !!(this._entry.clutter_text.password_char);
|
|
|
|
this._entry.clutter_text.set_password_char(visible ? '' : '\u25cf');
|
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|
2011-09-14 12:47:09 -04:00
|
|
|
|
|
|
|
function _setMenuAlignment(entry, stageX) {
|
|
|
|
let [success, entryX, entryY] = entry.transform_stage_point(stageX, 0);
|
|
|
|
if (success)
|
2012-05-27 13:06:08 -04:00
|
|
|
entry.menu.setSourceAlignment(entryX / entry.width);
|
2011-09-14 12:47:09 -04:00
|
|
|
};
|
|
|
|
|
2012-09-06 15:14:32 -04:00
|
|
|
function _onButtonPressEvent(actor, event, entry) {
|
2012-05-27 13:06:08 -04:00
|
|
|
if (entry.menu.isOpen) {
|
|
|
|
entry.menu.close();
|
2012-09-06 15:14:32 -04:00
|
|
|
return true;
|
|
|
|
} else if (event.get_button() == 3) {
|
|
|
|
let [stageX, stageY] = event.get_coords();
|
2011-09-14 12:47:09 -04:00
|
|
|
_setMenuAlignment(entry, stageX);
|
2012-05-27 13:06:08 -04:00
|
|
|
entry.menu.open();
|
2012-09-06 15:14:32 -04:00
|
|
|
return true;
|
2011-09-14 12:47:09 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2012-09-06 15:14:32 -04:00
|
|
|
function _onPopup(actor, entry) {
|
2011-09-14 12:47:09 -04:00
|
|
|
let [success, textX, textY, lineHeight] = entry.clutter_text.position_to_coords(-1);
|
|
|
|
if (success)
|
2012-05-27 13:06:08 -04:00
|
|
|
entry.menu.setSourceAlignment(textX / entry.width);
|
|
|
|
entry.menu.open();
|
2011-09-14 12:47:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
function addContextMenu(entry, params) {
|
2012-05-27 13:06:08 -04:00
|
|
|
if (entry.menu)
|
2011-09-14 12:47:09 -04:00
|
|
|
return;
|
|
|
|
|
2012-05-27 13:06:08 -04:00
|
|
|
entry.menu = new EntryMenu(entry, params);
|
2011-09-14 12:47:09 -04:00
|
|
|
entry._menuManager = new PopupMenu.PopupMenuManager({ actor: entry });
|
2012-05-27 13:06:08 -04:00
|
|
|
entry._menuManager.addMenu(entry.menu);
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2012-09-06 15:14:32 -04:00
|
|
|
// Add an event handler to both the entry and its clutter_text; the former
|
2011-09-14 12:47:09 -04:00
|
|
|
// so padding is included in the clickable area, the latter because the
|
|
|
|
// event processing of ClutterText prevents event-bubbling.
|
2012-09-06 15:14:32 -04:00
|
|
|
entry.clutter_text.connect('button-press-event', Lang.bind(null, _onButtonPressEvent, entry));
|
|
|
|
entry.connect('button-press-event', Lang.bind(null, _onButtonPressEvent, entry));
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2012-09-06 15:14:32 -04:00
|
|
|
entry.connect('popup-menu', Lang.bind(null, _onPopup, entry));
|
2011-09-14 12:47:09 -04:00
|
|
|
}
|