2012-05-29 13:30:46 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported CandidatePopup */
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2019-02-08 22:21:36 -05:00
|
|
|
const { Clutter, IBus, St } = imports.gi;
|
2013-02-14 17:36:32 -05:00
|
|
|
const Signals = imports.signals;
|
2012-05-29 13:30:46 -04:00
|
|
|
|
|
|
|
const BoxPointer = imports.ui.boxpointer;
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var MAX_CANDIDATES_PER_PAGE = 16;
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2019-01-28 20:27:05 -05:00
|
|
|
var DEFAULT_INDEX_LABELS = ['1', '2', '3', '4', '5', '6', '7', '8',
|
|
|
|
'9', '0', 'a', 'b', 'c', 'd', 'e', 'f'];
|
2014-07-07 08:30:40 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var CandidateArea = class CandidateArea {
|
|
|
|
constructor() {
|
2013-01-02 08:24:37 -05:00
|
|
|
this.actor = new St.BoxLayout({ vertical: true,
|
2016-12-12 03:02:32 -05:00
|
|
|
reactive: true,
|
2013-01-02 08:24:37 -05:00
|
|
|
visible: false });
|
|
|
|
this._candidateBoxes = [];
|
2012-05-29 13:30:46 -04:00
|
|
|
for (let i = 0; i < MAX_CANDIDATES_PER_PAGE; ++i) {
|
2013-01-16 18:21:14 -05:00
|
|
|
let box = new St.BoxLayout({ style_class: 'candidate-box',
|
|
|
|
reactive: true,
|
|
|
|
track_hover: true });
|
2013-01-02 08:24:37 -05:00
|
|
|
box._indexLabel = new St.Label({ style_class: 'candidate-index' });
|
|
|
|
box._candidateLabel = new St.Label({ style_class: 'candidate-label' });
|
|
|
|
box.add(box._indexLabel, { y_fill: false });
|
|
|
|
box.add(box._candidateLabel, { y_fill: false });
|
|
|
|
this._candidateBoxes.push(box);
|
|
|
|
this.actor.add(box);
|
2013-01-16 18:21:14 -05:00
|
|
|
|
|
|
|
let j = i;
|
2017-10-30 20:38:18 -04:00
|
|
|
box.connect('button-release-event', (actor, event) => {
|
2013-01-16 18:21:14 -05:00
|
|
|
this.emit('candidate-clicked', j, event.get_button(), event.get_state());
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2012-05-29 13:30:46 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this.actor.connect('scroll-event', (actor, event) => {
|
2016-12-12 03:02:32 -05:00
|
|
|
let direction = event.get_scroll_direction();
|
2019-01-28 20:27:05 -05:00
|
|
|
switch (direction) {
|
2016-12-12 03:02:32 -05:00
|
|
|
case Clutter.ScrollDirection.UP:
|
|
|
|
this.emit('cursor-up');
|
|
|
|
break;
|
|
|
|
case Clutter.ScrollDirection.DOWN:
|
|
|
|
this.emit('cursor-down');
|
|
|
|
break;
|
2019-01-28 20:18:52 -05:00
|
|
|
}
|
2016-12-12 03:02:32 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2016-12-12 03:02:32 -05:00
|
|
|
|
2013-02-14 17:36:32 -05:00
|
|
|
this._buttonBox = new St.BoxLayout({ style_class: 'candidate-page-button-box' });
|
|
|
|
|
2015-01-16 09:33:56 -05:00
|
|
|
this._previousButton = new St.Button({ style_class: 'candidate-page-button candidate-page-button-previous button' });
|
2013-02-14 17:36:32 -05:00
|
|
|
this._previousButton.child = new St.Icon({ style_class: 'candidate-page-button-icon' });
|
|
|
|
this._buttonBox.add(this._previousButton, { expand: true });
|
|
|
|
|
2015-01-16 09:33:56 -05:00
|
|
|
this._nextButton = new St.Button({ style_class: 'candidate-page-button candidate-page-button-next button' });
|
2013-02-14 17:36:32 -05:00
|
|
|
this._nextButton.child = new St.Icon({ style_class: 'candidate-page-button-icon' });
|
|
|
|
this._buttonBox.add(this._nextButton, { expand: true });
|
|
|
|
|
|
|
|
this.actor.add(this._buttonBox);
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this._previousButton.connect('clicked', () => {
|
2013-02-14 17:36:32 -05:00
|
|
|
this.emit('previous-page');
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
|
|
|
this._nextButton.connect('clicked', () => {
|
2013-02-14 17:36:32 -05:00
|
|
|
this.emit('next-page');
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2013-02-14 17:36:32 -05:00
|
|
|
|
2012-05-29 13:30:46 -04:00
|
|
|
this._orientation = -1;
|
|
|
|
this._cursorPosition = 0;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
setOrientation(orientation) {
|
2012-05-29 13:30:46 -04:00
|
|
|
if (this._orientation == orientation)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._orientation = orientation;
|
|
|
|
|
2013-02-14 17:36:32 -05:00
|
|
|
if (this._orientation == IBus.Orientation.HORIZONTAL) {
|
2013-01-02 08:24:37 -05:00
|
|
|
this.actor.vertical = false;
|
2013-02-14 17:36:32 -05:00
|
|
|
this.actor.remove_style_class_name('vertical');
|
|
|
|
this.actor.add_style_class_name('horizontal');
|
|
|
|
this._previousButton.child.icon_name = 'go-previous-symbolic';
|
|
|
|
this._nextButton.child.icon_name = 'go-next-symbolic';
|
|
|
|
} else { // VERTICAL || SYSTEM
|
2013-01-02 08:24:37 -05:00
|
|
|
this.actor.vertical = true;
|
2013-02-14 17:36:32 -05:00
|
|
|
this.actor.add_style_class_name('vertical');
|
|
|
|
this.actor.remove_style_class_name('horizontal');
|
|
|
|
this._previousButton.child.icon_name = 'go-up-symbolic';
|
|
|
|
this._nextButton.child.icon_name = 'go-down-symbolic';
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
setCandidates(indexes, candidates, cursorPosition, cursorVisible) {
|
2012-05-29 13:30:46 -04:00
|
|
|
for (let i = 0; i < MAX_CANDIDATES_PER_PAGE; ++i) {
|
|
|
|
let visible = i < candidates.length;
|
2013-01-02 08:24:37 -05:00
|
|
|
let box = this._candidateBoxes[i];
|
|
|
|
box.visible = visible;
|
2012-05-29 13:30:46 -04:00
|
|
|
|
|
|
|
if (!visible)
|
|
|
|
continue;
|
|
|
|
|
2014-07-07 08:30:40 -04:00
|
|
|
box._indexLabel.text = ((indexes && indexes[i]) ? indexes[i] : DEFAULT_INDEX_LABELS[i]);
|
2013-01-02 08:24:37 -05:00
|
|
|
box._candidateLabel.text = candidates[i];
|
2012-05-29 13:30:46 -04:00
|
|
|
}
|
|
|
|
|
2013-01-02 08:24:37 -05:00
|
|
|
this._candidateBoxes[this._cursorPosition].remove_style_pseudo_class('selected');
|
2012-05-29 13:30:46 -04:00
|
|
|
this._cursorPosition = cursorPosition;
|
|
|
|
if (cursorVisible)
|
2013-01-02 08:24:37 -05:00
|
|
|
this._candidateBoxes[cursorPosition].add_style_pseudo_class('selected');
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-02-14 17:36:32 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
updateButtons(wrapsAround, page, nPages) {
|
2013-02-14 17:36:32 -05:00
|
|
|
if (nPages < 2) {
|
|
|
|
this._buttonBox.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._buttonBox.show();
|
|
|
|
this._previousButton.reactive = wrapsAround || page > 0;
|
|
|
|
this._nextButton.reactive = wrapsAround || page < nPages - 1;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
|
|
|
};
|
2013-02-14 17:36:32 -05:00
|
|
|
Signals.addSignalMethods(CandidateArea.prototype);
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var CandidatePopup = class CandidatePopup {
|
|
|
|
constructor() {
|
2019-10-08 06:22:34 -04:00
|
|
|
this._dummyCursor = new St.Widget({ opacity: 0 });
|
|
|
|
Main.layoutManager.uiGroup.add_actor(this._dummyCursor);
|
|
|
|
|
2013-01-02 08:24:37 -05:00
|
|
|
this._boxPointer = new BoxPointer.BoxPointer(St.Side.TOP);
|
2018-08-21 06:38:23 -04:00
|
|
|
this._boxPointer.visible = false;
|
|
|
|
this._boxPointer.style_class = 'candidate-popup-boxpointer';
|
|
|
|
Main.layoutManager.addChrome(this._boxPointer);
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2013-01-02 08:24:37 -05:00
|
|
|
let box = new St.BoxLayout({ style_class: 'candidate-popup-content',
|
|
|
|
vertical: true });
|
|
|
|
this._boxPointer.bin.set_child(box);
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2013-01-02 08:24:37 -05:00
|
|
|
this._preeditText = new St.Label({ style_class: 'candidate-popup-text',
|
|
|
|
visible: false });
|
|
|
|
box.add(this._preeditText);
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2013-01-02 08:24:37 -05:00
|
|
|
this._auxText = new St.Label({ style_class: 'candidate-popup-text',
|
|
|
|
visible: false });
|
|
|
|
box.add(this._auxText);
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2013-01-02 08:24:37 -05:00
|
|
|
this._candidateArea = new CandidateArea();
|
|
|
|
box.add(this._candidateArea.actor);
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this._candidateArea.connect('previous-page', () => {
|
2013-02-14 17:36:32 -05:00
|
|
|
this._panelService.page_up();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
|
|
|
this._candidateArea.connect('next-page', () => {
|
2013-02-14 17:36:32 -05:00
|
|
|
this._panelService.page_down();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2016-12-12 03:02:32 -05:00
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this._candidateArea.connect('cursor-up', () => {
|
2016-12-12 03:02:32 -05:00
|
|
|
this._panelService.cursor_up();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
|
|
|
this._candidateArea.connect('cursor-down', () => {
|
2016-12-12 03:02:32 -05:00
|
|
|
this._panelService.cursor_down();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2016-12-12 03:02:32 -05:00
|
|
|
|
2018-04-18 00:21:55 -04:00
|
|
|
this._candidateArea.connect('candidate-clicked', (area, index, button, state) => {
|
2013-01-16 18:21:14 -05:00
|
|
|
this._panelService.candidate_clicked(index, button, state);
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2013-02-14 17:36:32 -05:00
|
|
|
|
2012-05-29 13:30:46 -04:00
|
|
|
this._panelService = null;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
setPanelService(panelService) {
|
2012-05-29 13:30:46 -04:00
|
|
|
this._panelService = panelService;
|
|
|
|
if (!panelService)
|
|
|
|
return;
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
panelService.connect('set-cursor-location', (ps, x, y, w, h) => {
|
|
|
|
this._setDummyCursorGeometry(x, y, w, h);
|
|
|
|
});
|
2016-02-17 11:54:52 -05:00
|
|
|
try {
|
2017-10-30 20:38:18 -04:00
|
|
|
panelService.connect('set-cursor-location-relative', (ps, x, y, w, h) => {
|
|
|
|
if (!global.display.focus_window)
|
|
|
|
return;
|
|
|
|
let window = global.display.focus_window.get_compositor_private();
|
|
|
|
this._setDummyCursorGeometry(window.x + x, window.y + y, w, h);
|
|
|
|
});
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2016-02-17 11:54:52 -05:00
|
|
|
// Only recent IBus versions have support for this signal
|
|
|
|
// which is used for wayland clients. In order to work
|
|
|
|
// with older IBus versions we can silently ignore the
|
|
|
|
// signal's absence.
|
|
|
|
}
|
2017-10-30 20:38:18 -04:00
|
|
|
panelService.connect('update-preedit-text', (ps, text, cursorPosition, visible) => {
|
|
|
|
this._preeditText.visible = visible;
|
|
|
|
this._updateVisibility();
|
|
|
|
|
|
|
|
this._preeditText.text = text.get_text();
|
|
|
|
|
|
|
|
let attrs = text.get_attributes();
|
|
|
|
if (attrs)
|
|
|
|
this._setTextAttributes(this._preeditText.clutter_text,
|
|
|
|
attrs);
|
|
|
|
});
|
2019-02-04 06:30:53 -05:00
|
|
|
panelService.connect('show-preedit-text', () => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._preeditText.show();
|
|
|
|
this._updateVisibility();
|
|
|
|
});
|
2019-02-04 06:30:53 -05:00
|
|
|
panelService.connect('hide-preedit-text', () => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._preeditText.hide();
|
|
|
|
this._updateVisibility();
|
|
|
|
});
|
2019-01-31 09:08:10 -05:00
|
|
|
panelService.connect('update-auxiliary-text', (_ps, text, visible) => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._auxText.visible = visible;
|
|
|
|
this._updateVisibility();
|
|
|
|
|
|
|
|
this._auxText.text = text.get_text();
|
|
|
|
});
|
2019-02-04 06:30:53 -05:00
|
|
|
panelService.connect('show-auxiliary-text', () => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._auxText.show();
|
|
|
|
this._updateVisibility();
|
|
|
|
});
|
2019-02-04 06:30:53 -05:00
|
|
|
panelService.connect('hide-auxiliary-text', () => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._auxText.hide();
|
|
|
|
this._updateVisibility();
|
|
|
|
});
|
2019-01-31 09:08:10 -05:00
|
|
|
panelService.connect('update-lookup-table', (_ps, lookupTable, visible) => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._candidateArea.actor.visible = visible;
|
|
|
|
this._updateVisibility();
|
|
|
|
|
|
|
|
let nCandidates = lookupTable.get_number_of_candidates();
|
|
|
|
let cursorPos = lookupTable.get_cursor_pos();
|
|
|
|
let pageSize = lookupTable.get_page_size();
|
|
|
|
let nPages = Math.ceil(nCandidates / pageSize);
|
|
|
|
let page = ((cursorPos == 0) ? 0 : Math.floor(cursorPos / pageSize));
|
|
|
|
let startIndex = page * pageSize;
|
|
|
|
let endIndex = Math.min((page + 1) * pageSize, nCandidates);
|
|
|
|
|
|
|
|
let indexes = [];
|
|
|
|
let indexLabel;
|
2019-01-29 16:56:03 -05:00
|
|
|
for (let i = 0; (indexLabel = lookupTable.get_label(i)); ++i)
|
2019-01-29 14:36:54 -05:00
|
|
|
indexes.push(indexLabel.get_text());
|
2017-10-30 20:38:18 -04:00
|
|
|
|
|
|
|
Main.keyboard.resetSuggestions();
|
|
|
|
|
|
|
|
let candidates = [];
|
|
|
|
for (let i = startIndex; i < endIndex; ++i) {
|
|
|
|
candidates.push(lookupTable.get_candidate(i).get_text());
|
|
|
|
|
|
|
|
Main.keyboard.addSuggestion(lookupTable.get_candidate(i).get_text(), () => {
|
|
|
|
let index = i;
|
|
|
|
this._panelService.candidate_clicked(index, 1, 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._candidateArea.setCandidates(indexes,
|
|
|
|
candidates,
|
|
|
|
cursorPos % pageSize,
|
|
|
|
lookupTable.is_cursor_visible());
|
|
|
|
this._candidateArea.setOrientation(lookupTable.get_orientation());
|
|
|
|
this._candidateArea.updateButtons(lookupTable.is_round(), page, nPages);
|
|
|
|
});
|
2019-02-04 06:30:53 -05:00
|
|
|
panelService.connect('show-lookup-table', () => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._candidateArea.actor.show();
|
|
|
|
this._updateVisibility();
|
|
|
|
});
|
2019-02-04 06:30:53 -05:00
|
|
|
panelService.connect('hide-lookup-table', () => {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._candidateArea.actor.hide();
|
|
|
|
this._updateVisibility();
|
|
|
|
});
|
2019-02-04 06:30:53 -05:00
|
|
|
panelService.connect('focus-out', () => {
|
2018-08-21 06:29:44 -04:00
|
|
|
this._boxPointer.close(BoxPointer.PopupAnimation.NONE);
|
2017-10-30 20:38:18 -04:00
|
|
|
Main.keyboard.resetSuggestions();
|
|
|
|
});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_setDummyCursorGeometry(x, y, w, h) {
|
2019-10-08 06:22:34 -04:00
|
|
|
this._dummyCursor.set_position(Math.round(x), Math.round(y));
|
|
|
|
this._dummyCursor.set_size(Math.round(w), Math.round(h));
|
|
|
|
|
2019-04-09 16:21:15 -04:00
|
|
|
if (this._boxPointer.visible)
|
2019-10-08 06:22:34 -04:00
|
|
|
this._boxPointer.setPosition(this._dummyCursor, 0);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2014-07-30 08:40:39 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateVisibility() {
|
2017-12-06 08:44:16 -05:00
|
|
|
let isVisible = (!Main.keyboard.visible &&
|
|
|
|
(this._preeditText.visible ||
|
|
|
|
this._auxText.visible ||
|
|
|
|
this._candidateArea.actor.visible));
|
2013-01-02 08:24:37 -05:00
|
|
|
|
|
|
|
if (isVisible) {
|
2019-10-08 06:22:34 -04:00
|
|
|
this._boxPointer.setPosition(this._dummyCursor, 0);
|
2018-08-21 06:29:44 -04:00
|
|
|
this._boxPointer.open(BoxPointer.PopupAnimation.NONE);
|
2019-04-09 16:21:15 -04:00
|
|
|
this._boxPointer.raise_top();
|
2013-01-02 08:24:37 -05:00
|
|
|
} else {
|
2018-08-21 06:29:44 -04:00
|
|
|
this._boxPointer.close(BoxPointer.PopupAnimation.NONE);
|
2013-01-02 08:24:37 -05:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-05-29 13:30:46 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_setTextAttributes(clutterText, ibusAttrList) {
|
2012-05-29 13:30:46 -04:00
|
|
|
let attr;
|
2019-01-29 16:56:03 -05:00
|
|
|
for (let i = 0; (attr = ibusAttrList.get(i)); ++i)
|
2012-05-29 13:30:46 -04:00
|
|
|
if (attr.get_attr_type() == IBus.AttrType.BACKGROUND)
|
|
|
|
clutterText.set_selection(attr.get_start_index(), attr.get_end_index());
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|