2013-02-18 20:55:30 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2011-09-14 16:47:09 +00:00
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
2013-02-18 20:55:44 +00:00
|
|
|
const BoxPointer = imports.ui.boxpointer;
|
2011-09-14 16:47:09 +00:00
|
|
|
const Main = imports.ui.main;
|
|
|
|
const Params = imports.misc.params;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var EntryMenu = class extends PopupMenu.PopupMenu {
|
|
|
|
constructor(entry) {
|
|
|
|
super(entry, 0, St.Side.TOP);
|
2011-09-14 16:47:09 +00:00
|
|
|
|
|
|
|
this._entry = entry;
|
|
|
|
this._clipboard = St.Clipboard.get_default();
|
|
|
|
|
|
|
|
// Populate menu
|
|
|
|
let item;
|
|
|
|
item = new PopupMenu.PopupMenuItem(_("Copy"));
|
2017-12-02 00:27:35 +00:00
|
|
|
item.connect('activate', this._onCopyActivated.bind(this));
|
2011-09-14 16:47:09 +00:00
|
|
|
this.addMenuItem(item);
|
|
|
|
this._copyItem = item;
|
|
|
|
|
|
|
|
item = new PopupMenu.PopupMenuItem(_("Paste"));
|
2017-12-02 00:27:35 +00:00
|
|
|
item.connect('activate', this._onPasteActivated.bind(this));
|
2011-09-14 16:47:09 +00:00
|
|
|
this.addMenuItem(item);
|
|
|
|
this._pasteItem = item;
|
|
|
|
|
|
|
|
this._passwordItem = null;
|
|
|
|
|
|
|
|
Main.uiGroup.add_actor(this.actor);
|
|
|
|
this.actor.hide();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_makePasswordItem() {
|
2012-05-27 17:06:08 +00:00
|
|
|
let item = new PopupMenu.PopupMenuItem('');
|
2017-12-02 00:27:35 +00:00
|
|
|
item.connect('activate', this._onPasswordActivated.bind(this));
|
2012-05-27 17:06:08 +00:00
|
|
|
this.addMenuItem(item);
|
|
|
|
this._passwordItem = item;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-05-27 17:06:08 +00:00
|
|
|
|
|
|
|
get isPassword() {
|
2013-05-10 01:35:05 +00:00
|
|
|
return this._passwordItem != null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-05-27 17:06:08 +00:00
|
|
|
|
|
|
|
set isPassword(v) {
|
2013-05-10 01:35:05 +00:00
|
|
|
if (v == this.isPassword)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (v) {
|
|
|
|
this._makePasswordItem();
|
2018-03-13 14:22:15 +00:00
|
|
|
this._entry.input_purpose = Clutter.InputContentPurpose.PASSWORD;
|
2013-05-10 01:35:05 +00:00
|
|
|
} else {
|
|
|
|
this._passwordItem.destroy();
|
|
|
|
this._passwordItem = null;
|
2018-03-13 14:22:15 +00:00
|
|
|
this._entry.input_purpose = Clutter.InputContentPurpose.NORMAL;
|
2013-05-10 01:35:05 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-05-27 17:06:08 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
open(animate) {
|
2011-09-14 16:47:09 +00:00
|
|
|
this._updatePasteItem();
|
|
|
|
this._updateCopyItem();
|
|
|
|
if (this._passwordItem)
|
|
|
|
this._updatePasswordItem();
|
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
super.open(animate);
|
2013-02-18 21:06:53 +00:00
|
|
|
this._entry.add_style_pseudo_class('focus');
|
|
|
|
|
2018-11-27 12:58:25 +00:00
|
|
|
let direction = St.DirectionType.TAB_FORWARD;
|
2011-09-14 16:47:09 +00:00
|
|
|
if (!this.actor.navigate_focus(null, direction, false))
|
|
|
|
this.actor.grab_key_focus();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-06 19:35:54 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateCopyItem() {
|
2011-09-14 16:47:09 +00:00
|
|
|
let selection = this._entry.clutter_text.get_selection();
|
2013-03-04 15:22:16 +00:00
|
|
|
this._copyItem.setSensitive(!this._entry.clutter_text.password_char &&
|
|
|
|
selection && selection != '');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updatePasteItem() {
|
2017-10-31 00:38:18 +00:00
|
|
|
this._clipboard.get_text(St.ClipboardType.CLIPBOARD,
|
|
|
|
(clipboard, text) => {
|
2011-09-14 16:47:09 +00:00
|
|
|
this._pasteItem.setSensitive(text && text != '');
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updatePasswordItem() {
|
2011-09-14 16:47:09 +00:00
|
|
|
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"));
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onCopyActivated() {
|
2011-09-14 16:47:09 +00:00
|
|
|
let selection = this._entry.clutter_text.get_selection();
|
2011-11-09 16:19:47 +00:00
|
|
|
this._clipboard.set_text(St.ClipboardType.CLIPBOARD, selection);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onPasteActivated() {
|
2017-10-31 00:38:18 +00:00
|
|
|
this._clipboard.get_text(St.ClipboardType.CLIPBOARD,
|
|
|
|
(clipboard, text) => {
|
2011-09-14 16:47:09 +00:00
|
|
|
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);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onPasswordActivated() {
|
2011-09-14 16:47:09 +00:00
|
|
|
let visible = !!(this._entry.clutter_text.password_char);
|
|
|
|
this._entry.clutter_text.set_password_char(visible ? '' : '\u25cf');
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2011-09-14 16:47:09 +00:00
|
|
|
|
|
|
|
function _setMenuAlignment(entry, stageX) {
|
|
|
|
let [success, entryX, entryY] = entry.transform_stage_point(stageX, 0);
|
|
|
|
if (success)
|
2012-05-27 17:06:08 +00:00
|
|
|
entry.menu.setSourceAlignment(entryX / entry.width);
|
2011-09-14 16:47:09 +00:00
|
|
|
};
|
|
|
|
|
2012-09-06 19:14:32 +00:00
|
|
|
function _onButtonPressEvent(actor, event, entry) {
|
2012-05-27 17:06:08 +00:00
|
|
|
if (entry.menu.isOpen) {
|
2013-02-18 20:55:44 +00:00
|
|
|
entry.menu.close(BoxPointer.PopupAnimation.FULL);
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2012-09-06 19:14:32 +00:00
|
|
|
} else if (event.get_button() == 3) {
|
|
|
|
let [stageX, stageY] = event.get_coords();
|
2011-09-14 16:47:09 +00:00
|
|
|
_setMenuAlignment(entry, stageX);
|
2013-02-18 20:55:44 +00:00
|
|
|
entry.menu.open(BoxPointer.PopupAnimation.FULL);
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-09-14 16:47:09 +00:00
|
|
|
}
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2011-09-14 16:47:09 +00:00
|
|
|
};
|
|
|
|
|
2012-09-06 19:14:32 +00:00
|
|
|
function _onPopup(actor, entry) {
|
2011-09-14 16:47:09 +00:00
|
|
|
let [success, textX, textY, lineHeight] = entry.clutter_text.position_to_coords(-1);
|
|
|
|
if (success)
|
2012-05-27 17:06:08 +00:00
|
|
|
entry.menu.setSourceAlignment(textX / entry.width);
|
2013-02-18 20:55:44 +00:00
|
|
|
entry.menu.open(BoxPointer.PopupAnimation.FULL);
|
2011-09-14 16:47:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function addContextMenu(entry, params) {
|
2012-05-27 17:06:08 +00:00
|
|
|
if (entry.menu)
|
2011-09-14 16:47:09 +00:00
|
|
|
return;
|
|
|
|
|
2013-05-10 01:35:05 +00:00
|
|
|
params = Params.parse (params, { isPassword: false });
|
|
|
|
|
|
|
|
entry.menu = new EntryMenu(entry);
|
|
|
|
entry.menu.isPassword = params.isPassword;
|
2011-09-14 16:47:09 +00:00
|
|
|
entry._menuManager = new PopupMenu.PopupMenuManager({ actor: entry });
|
2012-05-27 17:06:08 +00:00
|
|
|
entry._menuManager.addMenu(entry.menu);
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2012-09-06 19:14:32 +00:00
|
|
|
// Add an event handler to both the entry and its clutter_text; the former
|
2011-09-14 16:47:09 +00:00
|
|
|
// so padding is included in the clickable area, the latter because the
|
|
|
|
// event processing of ClutterText prevents event-bubbling.
|
2017-12-02 00:27:35 +00:00
|
|
|
entry.clutter_text.connect('button-press-event', (actor, event) => {
|
|
|
|
_onButtonPressEvent(actor, event, entry);
|
|
|
|
});
|
|
|
|
entry.connect('button-press-event', (actor, event) => {
|
|
|
|
_onButtonPressEvent(actor, event, entry);
|
|
|
|
});
|
2011-09-14 16:47:09 +00:00
|
|
|
|
2017-12-02 00:27:35 +00:00
|
|
|
entry.connect('popup-menu', actor => { _onPopup(actor, entry); });
|
2013-04-04 15:35:50 +00:00
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
entry.connect('destroy', () => {
|
2013-04-04 15:35:50 +00:00
|
|
|
entry.menu.destroy();
|
|
|
|
entry.menu = null;
|
|
|
|
entry._menuManager = null;
|
|
|
|
});
|
2011-09-14 16:47:09 +00:00
|
|
|
}
|