2012-05-29 17:30:46 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const IBus = imports.gi.IBus;
|
|
|
|
const Lang = imports.lang;
|
2013-02-14 22:36:32 +00:00
|
|
|
const Signals = imports.signals;
|
2012-05-29 17:30:46 +00:00
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const BoxPointer = imports.ui.boxpointer;
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
|
|
|
|
const MAX_CANDIDATES_PER_PAGE = 16;
|
|
|
|
|
2014-07-07 12:30:40 +00:00
|
|
|
const DEFAULT_INDEX_LABELS = [ '1', '2', '3', '4', '5', '6', '7', '8',
|
|
|
|
'9', '0', 'a', 'b', 'c', 'd', 'e', 'f' ];
|
|
|
|
|
2012-05-29 17:30:46 +00:00
|
|
|
const CandidateArea = new Lang.Class({
|
|
|
|
Name: 'CandidateArea',
|
|
|
|
|
|
|
|
_init: function() {
|
2013-01-02 13:24:37 +00:00
|
|
|
this.actor = new St.BoxLayout({ vertical: true,
|
|
|
|
visible: false });
|
|
|
|
this._candidateBoxes = [];
|
2012-05-29 17:30:46 +00:00
|
|
|
for (let i = 0; i < MAX_CANDIDATES_PER_PAGE; ++i) {
|
2013-01-16 23:21:14 +00:00
|
|
|
let box = new St.BoxLayout({ style_class: 'candidate-box',
|
|
|
|
reactive: true,
|
|
|
|
track_hover: true });
|
2013-01-02 13:24:37 +00: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 23:21:14 +00:00
|
|
|
|
|
|
|
let j = i;
|
|
|
|
box.connect('button-release-event', Lang.bind(this, function(actor, event) {
|
|
|
|
this.emit('candidate-clicked', j, event.get_button(), event.get_state());
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2013-01-16 23:21:14 +00:00
|
|
|
}));
|
2012-05-29 17:30:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 22:36:32 +00:00
|
|
|
this._buttonBox = new St.BoxLayout({ style_class: 'candidate-page-button-box' });
|
|
|
|
|
2015-01-16 14:33:56 +00:00
|
|
|
this._previousButton = new St.Button({ style_class: 'candidate-page-button candidate-page-button-previous button' });
|
2013-02-14 22:36:32 +00:00
|
|
|
this._previousButton.child = new St.Icon({ style_class: 'candidate-page-button-icon' });
|
|
|
|
this._buttonBox.add(this._previousButton, { expand: true });
|
|
|
|
|
2015-01-16 14:33:56 +00:00
|
|
|
this._nextButton = new St.Button({ style_class: 'candidate-page-button candidate-page-button-next button' });
|
2013-02-14 22:36:32 +00: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);
|
|
|
|
|
|
|
|
this._previousButton.connect('clicked', Lang.bind(this, function() {
|
|
|
|
this.emit('previous-page');
|
|
|
|
}));
|
|
|
|
this._nextButton.connect('clicked', Lang.bind(this, function() {
|
|
|
|
this.emit('next-page');
|
|
|
|
}));
|
|
|
|
|
2012-05-29 17:30:46 +00:00
|
|
|
this._orientation = -1;
|
|
|
|
this._cursorPosition = 0;
|
|
|
|
},
|
|
|
|
|
2013-02-14 22:37:35 +00:00
|
|
|
setOrientation: function(orientation) {
|
2012-05-29 17:30:46 +00:00
|
|
|
if (this._orientation == orientation)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._orientation = orientation;
|
|
|
|
|
2013-02-14 22:36:32 +00:00
|
|
|
if (this._orientation == IBus.Orientation.HORIZONTAL) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this.actor.vertical = false;
|
2013-02-14 22:36:32 +00: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 13:24:37 +00:00
|
|
|
this.actor.vertical = true;
|
2013-02-14 22:36:32 +00: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';
|
|
|
|
}
|
2012-05-29 17:30:46 +00:00
|
|
|
},
|
|
|
|
|
2013-02-14 22:37:35 +00:00
|
|
|
setCandidates: function(indexes, candidates, cursorPosition, cursorVisible) {
|
2012-05-29 17:30:46 +00:00
|
|
|
for (let i = 0; i < MAX_CANDIDATES_PER_PAGE; ++i) {
|
|
|
|
let visible = i < candidates.length;
|
2013-01-02 13:24:37 +00:00
|
|
|
let box = this._candidateBoxes[i];
|
|
|
|
box.visible = visible;
|
2012-05-29 17:30:46 +00:00
|
|
|
|
|
|
|
if (!visible)
|
|
|
|
continue;
|
|
|
|
|
2014-07-07 12:30:40 +00:00
|
|
|
box._indexLabel.text = ((indexes && indexes[i]) ? indexes[i] : DEFAULT_INDEX_LABELS[i]);
|
2013-01-02 13:24:37 +00:00
|
|
|
box._candidateLabel.text = candidates[i];
|
2012-05-29 17:30:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
this._candidateBoxes[this._cursorPosition].remove_style_pseudo_class('selected');
|
2012-05-29 17:30:46 +00:00
|
|
|
this._cursorPosition = cursorPosition;
|
|
|
|
if (cursorVisible)
|
2013-01-02 13:24:37 +00:00
|
|
|
this._candidateBoxes[cursorPosition].add_style_pseudo_class('selected');
|
2012-05-29 17:30:46 +00:00
|
|
|
},
|
2013-02-14 22:36:32 +00:00
|
|
|
|
|
|
|
updateButtons: function(wrapsAround, page, nPages) {
|
|
|
|
if (nPages < 2) {
|
|
|
|
this._buttonBox.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._buttonBox.show();
|
|
|
|
this._previousButton.reactive = wrapsAround || page > 0;
|
|
|
|
this._nextButton.reactive = wrapsAround || page < nPages - 1;
|
|
|
|
},
|
2012-05-29 17:30:46 +00:00
|
|
|
});
|
2013-02-14 22:36:32 +00:00
|
|
|
Signals.addSignalMethods(CandidateArea.prototype);
|
2012-05-29 17:30:46 +00:00
|
|
|
|
|
|
|
const CandidatePopup = new Lang.Class({
|
|
|
|
Name: 'CandidatePopup',
|
|
|
|
|
|
|
|
_init: function() {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._boxPointer = new BoxPointer.BoxPointer(St.Side.TOP);
|
|
|
|
this._boxPointer.actor.visible = false;
|
|
|
|
this._boxPointer.actor.style_class = 'candidate-popup-boxpointer';
|
2013-02-14 22:36:32 +00:00
|
|
|
Main.layoutManager.addChrome(this._boxPointer.actor);
|
2012-05-29 17:30:46 +00:00
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
let box = new St.BoxLayout({ style_class: 'candidate-popup-content',
|
|
|
|
vertical: true });
|
|
|
|
this._boxPointer.bin.set_child(box);
|
2012-05-29 17:30:46 +00:00
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
this._preeditText = new St.Label({ style_class: 'candidate-popup-text',
|
|
|
|
visible: false });
|
|
|
|
box.add(this._preeditText);
|
2012-05-29 17:30:46 +00:00
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
this._auxText = new St.Label({ style_class: 'candidate-popup-text',
|
|
|
|
visible: false });
|
|
|
|
box.add(this._auxText);
|
2012-05-29 17:30:46 +00:00
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
this._candidateArea = new CandidateArea();
|
|
|
|
box.add(this._candidateArea.actor);
|
2012-05-29 17:30:46 +00:00
|
|
|
|
2013-02-14 22:36:32 +00:00
|
|
|
this._candidateArea.connect('previous-page', Lang.bind(this, function() {
|
|
|
|
this._panelService.page_up();
|
|
|
|
}));
|
|
|
|
this._candidateArea.connect('next-page', Lang.bind(this, function() {
|
|
|
|
this._panelService.page_down();
|
|
|
|
}));
|
2013-01-16 23:21:14 +00:00
|
|
|
this._candidateArea.connect('candidate-clicked', Lang.bind(this, function(ca, index, button, state) {
|
|
|
|
this._panelService.candidate_clicked(index, button, state);
|
|
|
|
}));
|
2013-02-14 22:36:32 +00:00
|
|
|
|
2012-05-29 17:30:46 +00:00
|
|
|
this._panelService = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
setPanelService: function(panelService) {
|
|
|
|
this._panelService = panelService;
|
|
|
|
if (!panelService)
|
|
|
|
return;
|
|
|
|
|
|
|
|
panelService.connect('set-cursor-location',
|
|
|
|
Lang.bind(this, function(ps, x, y, w, h) {
|
2014-07-30 12:40:39 +00:00
|
|
|
this._setDummyCursorGeometry(x, y, w, h);
|
|
|
|
}));
|
|
|
|
panelService.connect('set-cursor-location-relative',
|
|
|
|
Lang.bind(this, function(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);
|
2012-05-29 17:30:46 +00:00
|
|
|
}));
|
|
|
|
panelService.connect('update-preedit-text',
|
|
|
|
Lang.bind(this, function(ps, text, cursorPosition, visible) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._preeditText.visible = visible;
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
this._preeditText.text = text.get_text();
|
2012-05-29 17:30:46 +00:00
|
|
|
|
|
|
|
let attrs = text.get_attributes();
|
|
|
|
if (attrs)
|
2013-01-02 13:24:37 +00:00
|
|
|
this._setTextAttributes(this._preeditText.clutter_text,
|
2012-05-29 17:30:46 +00:00
|
|
|
attrs);
|
|
|
|
}));
|
|
|
|
panelService.connect('show-preedit-text',
|
|
|
|
Lang.bind(this, function(ps) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._preeditText.show();
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
panelService.connect('hide-preedit-text',
|
|
|
|
Lang.bind(this, function(ps) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._preeditText.hide();
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
panelService.connect('update-auxiliary-text',
|
|
|
|
Lang.bind(this, function(ps, text, visible) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._auxText.visible = visible;
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
this._auxText.text = text.get_text();
|
2012-05-29 17:30:46 +00:00
|
|
|
}));
|
|
|
|
panelService.connect('show-auxiliary-text',
|
|
|
|
Lang.bind(this, function(ps) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._auxText.show();
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
panelService.connect('hide-auxiliary-text',
|
|
|
|
Lang.bind(this, function(ps) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._auxText.hide();
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
panelService.connect('update-lookup-table',
|
|
|
|
Lang.bind(this, function(ps, lookupTable, visible) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._candidateArea.actor.visible = visible;
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
|
2013-02-14 22:36:32 +00:00
|
|
|
let nCandidates = lookupTable.get_number_of_candidates();
|
2012-05-29 17:30:46 +00:00
|
|
|
let cursorPos = lookupTable.get_cursor_pos();
|
|
|
|
let pageSize = lookupTable.get_page_size();
|
2013-02-14 22:36:32 +00:00
|
|
|
let nPages = Math.ceil(nCandidates / pageSize);
|
2012-05-29 17:30:46 +00:00
|
|
|
let page = ((cursorPos == 0) ? 0 : Math.floor(cursorPos / pageSize));
|
|
|
|
let startIndex = page * pageSize;
|
2013-02-14 22:36:32 +00:00
|
|
|
let endIndex = Math.min((page + 1) * pageSize, nCandidates);
|
|
|
|
|
2012-05-29 17:30:46 +00:00
|
|
|
let indexes = [];
|
|
|
|
let indexLabel;
|
|
|
|
for (let i = 0; indexLabel = lookupTable.get_label(i); ++i)
|
|
|
|
indexes.push(indexLabel.get_text());
|
|
|
|
|
|
|
|
let candidates = [];
|
|
|
|
for (let i = startIndex; i < endIndex; ++i)
|
|
|
|
candidates.push(lookupTable.get_candidate(i).get_text());
|
|
|
|
|
2013-01-02 13:24:37 +00:00
|
|
|
this._candidateArea.setCandidates(indexes,
|
|
|
|
candidates,
|
|
|
|
cursorPos % pageSize,
|
|
|
|
lookupTable.is_cursor_visible());
|
2013-02-14 22:37:35 +00:00
|
|
|
this._candidateArea.setOrientation(lookupTable.get_orientation());
|
2013-02-14 22:36:32 +00:00
|
|
|
this._candidateArea.updateButtons(lookupTable.is_round(), page, nPages);
|
2012-05-29 17:30:46 +00:00
|
|
|
}));
|
|
|
|
panelService.connect('show-lookup-table',
|
|
|
|
Lang.bind(this, function(ps) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._candidateArea.actor.show();
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
panelService.connect('hide-lookup-table',
|
|
|
|
Lang.bind(this, function(ps) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._candidateArea.actor.hide();
|
2012-05-29 17:30:46 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
panelService.connect('focus-out',
|
|
|
|
Lang.bind(this, function(ps) {
|
2013-01-02 13:24:37 +00:00
|
|
|
this._boxPointer.hide(BoxPointer.PopupAnimation.NONE);
|
2012-05-29 17:30:46 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2014-07-30 12:40:39 +00:00
|
|
|
_setDummyCursorGeometry: function(x, y, w, h) {
|
|
|
|
Main.layoutManager.setDummyCursorGeometry(x, y, w, h);
|
|
|
|
if (this._boxPointer.actor.visible)
|
|
|
|
this._boxPointer.setPosition(Main.layoutManager.dummyCursor, 0);
|
|
|
|
},
|
|
|
|
|
2012-05-29 17:30:46 +00:00
|
|
|
_updateVisibility: function() {
|
2013-01-02 13:24:37 +00:00
|
|
|
let isVisible = (this._preeditText.visible ||
|
|
|
|
this._auxText.visible ||
|
|
|
|
this._candidateArea.actor.visible);
|
|
|
|
|
|
|
|
if (isVisible) {
|
2013-02-15 09:56:34 +00:00
|
|
|
this._boxPointer.setPosition(Main.layoutManager.dummyCursor, 0);
|
2013-01-02 13:24:37 +00:00
|
|
|
this._boxPointer.show(BoxPointer.PopupAnimation.NONE);
|
|
|
|
this._boxPointer.actor.raise_top();
|
|
|
|
} else {
|
|
|
|
this._boxPointer.hide(BoxPointer.PopupAnimation.NONE);
|
|
|
|
}
|
2012-05-29 17:30:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_setTextAttributes: function(clutterText, ibusAttrList) {
|
|
|
|
let attr;
|
|
|
|
for (let i = 0; attr = ibusAttrList.get(i); ++i)
|
|
|
|
if (attr.get_attr_type() == IBus.AttrType.BACKGROUND)
|
|
|
|
clutterText.set_selection(attr.get_start_index(), attr.get_end_index());
|
|
|
|
}
|
|
|
|
});
|