2013-02-18 15:55:30 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
import Clutter from 'gi://Clutter';
|
|
|
|
import GObject from 'gi://GObject';
|
|
|
|
import Pango from 'gi://Pango';
|
|
|
|
import Shell from 'gi://Shell';
|
|
|
|
import St from 'gi://St';
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
import * as BoxPointer from './boxpointer.js';
|
|
|
|
import * as Main from './main.js';
|
|
|
|
import * as Params from '../misc/params.js';
|
|
|
|
import * as PopupMenu from './popupMenu.js';
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
export class EntryMenu extends PopupMenu.PopupMenu {
|
2017-10-30 21:19:44 -04:00
|
|
|
constructor(entry) {
|
|
|
|
super(entry, 0, St.Side.TOP);
|
2011-09-14 12:47:09 -04:00
|
|
|
|
|
|
|
this._entry = entry;
|
|
|
|
this._clipboard = St.Clipboard.get_default();
|
|
|
|
|
|
|
|
// Populate menu
|
|
|
|
let item;
|
2023-08-06 18:34:20 -04:00
|
|
|
item = new PopupMenu.PopupMenuItem(_('Copy'));
|
2017-12-01 19:27:35 -05:00
|
|
|
item.connect('activate', this._onCopyActivated.bind(this));
|
2011-09-14 12:47:09 -04:00
|
|
|
this.addMenuItem(item);
|
|
|
|
this._copyItem = item;
|
|
|
|
|
2023-08-06 18:34:20 -04:00
|
|
|
item = new PopupMenu.PopupMenuItem(_('Paste'));
|
2017-12-01 19:27:35 -05:00
|
|
|
item.connect('activate', this._onPasteActivated.bind(this));
|
2011-09-14 12:47:09 -04:00
|
|
|
this.addMenuItem(item);
|
|
|
|
this._pasteItem = item;
|
|
|
|
|
2019-12-12 04:08:42 -05:00
|
|
|
if (entry instanceof St.PasswordEntry)
|
|
|
|
this._makePasswordItem();
|
2011-09-14 12:47:09 -04:00
|
|
|
|
|
|
|
Main.uiGroup.add_actor(this.actor);
|
|
|
|
this.actor.hide();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_makePasswordItem() {
|
2012-05-27 13:06:08 -04:00
|
|
|
let item = new PopupMenu.PopupMenuItem('');
|
2017-12-01 19:27:35 -05:00
|
|
|
item.connect('activate', this._onPasswordActivated.bind(this));
|
2012-05-27 13:06:08 -04:00
|
|
|
this.addMenuItem(item);
|
|
|
|
this._passwordItem = item;
|
2021-07-21 14:01:48 -04:00
|
|
|
|
|
|
|
this._entry.bind_property('show-peek-icon',
|
|
|
|
this._passwordItem, 'visible',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-27 13:06:08 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
open(animate) {
|
2011-09-14 12:47:09 -04:00
|
|
|
this._updatePasteItem();
|
|
|
|
this._updateCopyItem();
|
|
|
|
if (this._passwordItem)
|
|
|
|
this._updatePasswordItem();
|
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
super.open(animate);
|
2013-02-18 16:06:53 -05:00
|
|
|
this._entry.add_style_pseudo_class('focus');
|
|
|
|
|
2018-11-27 07:58:25 -05:00
|
|
|
let direction = St.DirectionType.TAB_FORWARD;
|
2011-09-14 12:47:09 -04:00
|
|
|
if (!this.actor.navigate_focus(null, direction, false))
|
|
|
|
this.actor.grab_key_focus();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-09-06 15:35:54 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateCopyItem() {
|
2011-09-14 12:47:09 -04:00
|
|
|
let selection = this._entry.clutter_text.get_selection();
|
2013-03-04 10:22:16 -05:00
|
|
|
this._copyItem.setSensitive(!this._entry.clutter_text.password_char &&
|
|
|
|
selection && selection != '');
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updatePasteItem() {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._clipboard.get_text(St.ClipboardType.CLIPBOARD,
|
|
|
|
(clipboard, text) => {
|
2011-09-14 12:47:09 -04:00
|
|
|
this._pasteItem.setSensitive(text && text != '');
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updatePasswordItem() {
|
2019-12-12 04:08:42 -05:00
|
|
|
if (!this._entry.password_visible)
|
2023-08-06 18:34:20 -04:00
|
|
|
this._passwordItem.label.set_text(_('Show Text'));
|
2011-09-14 12:47:09 -04:00
|
|
|
else
|
2023-08-06 18:34:20 -04:00
|
|
|
this._passwordItem.label.set_text(_('Hide Text'));
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onCopyActivated() {
|
2011-09-14 12:47:09 -04:00
|
|
|
let selection = this._entry.clutter_text.get_selection();
|
2011-11-09 11:19:47 -05:00
|
|
|
this._clipboard.set_text(St.ClipboardType.CLIPBOARD, selection);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onPasteActivated() {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._clipboard.get_text(St.ClipboardType.CLIPBOARD,
|
|
|
|
(clipboard, text) => {
|
2011-09-14 12:47:09 -04: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-30 20:38:18 -04:00
|
|
|
});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onPasswordActivated() {
|
2019-12-12 04:08:42 -05:00
|
|
|
this._entry.password_visible = !this._entry.password_visible;
|
2011-09-14 12:47:09 -04:00
|
|
|
}
|
2023-07-10 05:53:00 -04:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
|
|
|
function _setMenuAlignment(entry, stageX) {
|
2019-02-01 08:41:55 -05:00
|
|
|
let [success, entryX] = entry.transform_stage_point(stageX, 0);
|
2011-09-14 12:47:09 -04:00
|
|
|
if (success)
|
2012-05-27 13:06:08 -04:00
|
|
|
entry.menu.setSourceAlignment(entryX / entry.width);
|
2019-01-28 20:18:52 -05:00
|
|
|
}
|
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) {
|
2013-02-18 15:55:44 -05:00
|
|
|
entry.menu.close(BoxPointer.PopupAnimation.FULL);
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2012-09-06 15:14:32 -04:00
|
|
|
} else if (event.get_button() == 3) {
|
2019-02-01 08:41:55 -05:00
|
|
|
let [stageX] = event.get_coords();
|
2011-09-14 12:47:09 -04:00
|
|
|
_setMenuAlignment(entry, stageX);
|
2013-02-18 15:55:44 -05:00
|
|
|
entry.menu.open(BoxPointer.PopupAnimation.FULL);
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2011-09-14 12:47:09 -04:00
|
|
|
}
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2019-01-28 20:18:52 -05:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2012-09-06 15:14:32 -04:00
|
|
|
function _onPopup(actor, entry) {
|
2019-12-08 15:56:46 -05:00
|
|
|
let cursorPosition = entry.clutter_text.get_cursor_position();
|
|
|
|
let [success, textX, textY_, lineHeight_] = entry.clutter_text.position_to_coords(cursorPosition);
|
2011-09-14 12:47:09 -04:00
|
|
|
if (success)
|
2012-05-27 13:06:08 -04:00
|
|
|
entry.menu.setSourceAlignment(textX / entry.width);
|
2013-02-18 15:55:44 -05:00
|
|
|
entry.menu.open(BoxPointer.PopupAnimation.FULL);
|
2019-01-28 20:18:52 -05:00
|
|
|
}
|
2011-09-14 12:47:09 -04:00
|
|
|
|
2023-07-30 08:56:59 -04:00
|
|
|
/**
|
|
|
|
* @param {St.Entry} entry
|
|
|
|
* @param {*} params
|
|
|
|
*/
|
2023-07-10 05:53:00 -04:00
|
|
|
export function addContextMenu(entry, params) {
|
2012-05-27 13:06:08 -04:00
|
|
|
if (entry.menu)
|
2011-09-14 12:47:09 -04:00
|
|
|
return;
|
|
|
|
|
2023-08-06 18:40:20 -04:00
|
|
|
params = Params.parse(params, {actionMode: Shell.ActionMode.POPUP});
|
2013-05-09 21:35:05 -04:00
|
|
|
|
|
|
|
entry.menu = new EntryMenu(entry);
|
2023-08-06 19:45:22 -04:00
|
|
|
entry._menuManager = new PopupMenu.PopupMenuManager(entry, {
|
|
|
|
actionMode: params.actionMode,
|
|
|
|
});
|
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.
|
2017-12-01 19:27:35 -05: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 12:47:09 -04:00
|
|
|
|
2019-01-27 19:42:00 -05:00
|
|
|
entry.connect('popup-menu', actor => _onPopup(actor, entry));
|
2013-04-04 11:35:50 -04:00
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
entry.connect('destroy', () => {
|
2013-04-04 11:35:50 -04:00
|
|
|
entry.menu.destroy();
|
|
|
|
entry.menu = null;
|
|
|
|
entry._menuManager = null;
|
|
|
|
});
|
2011-09-14 12:47:09 -04:00
|
|
|
}
|
2019-12-13 03:06:14 -05:00
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
export const CapsLockWarning = GObject.registerClass(
|
2019-12-13 03:06:14 -05:00
|
|
|
class CapsLockWarning extends St.Label {
|
|
|
|
_init(params) {
|
2023-08-06 18:40:20 -04:00
|
|
|
let defaultParams = {style_class: 'caps-lock-warning-label'};
|
2019-12-13 03:06:14 -05:00
|
|
|
super._init(Object.assign(defaultParams, params));
|
|
|
|
|
|
|
|
this.text = _('Caps lock is on.');
|
|
|
|
|
2020-01-23 16:36:09 -05:00
|
|
|
this.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
|
|
|
|
this.clutter_text.line_wrap = true;
|
|
|
|
|
2020-01-30 13:57:12 -05:00
|
|
|
let seat = Clutter.get_default_backend().get_default_seat();
|
|
|
|
this._keymap = seat.get_keymap();
|
2019-12-13 03:06:14 -05:00
|
|
|
|
|
|
|
this.connect('notify::mapped', () => {
|
|
|
|
if (this.is_mapped()) {
|
2021-08-15 18:36:59 -04:00
|
|
|
this._keymap.connectObject(
|
|
|
|
'state-changed', () => this._sync(true), this);
|
2019-12-13 03:06:14 -05:00
|
|
|
} else {
|
2021-08-15 18:36:59 -04:00
|
|
|
this._keymap.disconnectObject(this);
|
2019-12-13 03:06:14 -05:00
|
|
|
}
|
|
|
|
|
2020-01-23 16:36:09 -05:00
|
|
|
this._sync(false);
|
2019-12-13 03:06:14 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:36:09 -05:00
|
|
|
_sync(animate) {
|
2019-12-13 03:06:14 -05:00
|
|
|
let capsLockOn = this._keymap.get_caps_lock_state();
|
2020-01-23 16:36:09 -05:00
|
|
|
|
|
|
|
this.remove_all_transitions();
|
|
|
|
|
2023-08-06 18:40:20 -04:00
|
|
|
const {naturalHeightSet} = this;
|
2020-01-23 16:36:09 -05:00
|
|
|
this.natural_height_set = false;
|
|
|
|
let [, height] = this.get_preferred_height(-1);
|
2020-02-21 13:38:53 -05:00
|
|
|
this.natural_height_set = naturalHeightSet;
|
2020-01-23 16:36:09 -05:00
|
|
|
|
|
|
|
this.ease({
|
|
|
|
height: capsLockOn ? height : 0,
|
|
|
|
opacity: capsLockOn ? 255 : 0,
|
|
|
|
duration: animate ? 200 : 0,
|
|
|
|
onComplete: () => {
|
|
|
|
if (capsLockOn)
|
|
|
|
this.height = -1;
|
|
|
|
},
|
|
|
|
});
|
2019-12-13 03:06:14 -05:00
|
|
|
}
|
|
|
|
});
|