2017-12-05 14:05:00 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const IBus = imports.gi.IBus;
|
|
|
|
const Keyboard = imports.ui.status.keyboard;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
|
|
|
var InputMethod = new Lang.Class({
|
|
|
|
Name: 'InputMethod',
|
|
|
|
Extends: Clutter.InputMethod,
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_init() {
|
2017-12-05 14:05:00 -05:00
|
|
|
this.parent();
|
|
|
|
this._hints = 0;
|
|
|
|
this._purpose = 0;
|
|
|
|
this._enabled = true;
|
|
|
|
this._currentFocus = null;
|
|
|
|
this._ibus = IBus.Bus.new_async();
|
2017-12-01 19:27:35 -05:00
|
|
|
this._ibus.connect('connected', this._onConnected.bind(this));
|
|
|
|
this._ibus.connect('disconnected', this._clear.bind(this));
|
|
|
|
this.connect('notify::can-show-preedit', this._updateCapabilities.bind(this));
|
2017-12-05 14:05:00 -05:00
|
|
|
|
|
|
|
this._inputSourceManager = Keyboard.getInputSourceManager();
|
|
|
|
this._sourceChangedId = this._inputSourceManager.connect('current-source-changed',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._onSourceChanged.bind(this));
|
2017-12-05 14:05:00 -05:00
|
|
|
this._currentSource = this._inputSourceManager.currentSource;
|
|
|
|
|
|
|
|
if (this._ibus.is_connected())
|
|
|
|
this._onConnected();
|
|
|
|
},
|
|
|
|
|
|
|
|
get currentFocus() {
|
|
|
|
return this._currentFocus;
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateCapabilities() {
|
2017-12-05 14:05:00 -05:00
|
|
|
let caps = 0;
|
|
|
|
|
|
|
|
if (this.can_show_preedit)
|
|
|
|
caps |= IBus.Capabilite.PREEDIT_TEXT;
|
|
|
|
|
|
|
|
if (this._currentFocus)
|
|
|
|
caps |= IBus.Capabilite.FOCUS | IBus.Capabilite.SURROUNDING_TEXT;
|
|
|
|
else
|
|
|
|
caps |= IBus.Capabilite.PREEDIT_TEXT | IBus.Capabilite.AUXILIARY_TEXT | IBus.Capabilite.LOOKUP_TABLE | IBus.Capabilite.PROPERTY;
|
|
|
|
|
|
|
|
if (this._context)
|
|
|
|
this._context.set_capabilities(caps);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onSourceChanged() {
|
2017-12-05 14:05:00 -05:00
|
|
|
this._currentSource = this._inputSourceManager.currentSource;
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onConnected() {
|
2017-12-05 14:05:00 -05:00
|
|
|
this._ibus.create_input_context_async ('gnome-shell', -1, null,
|
2017-12-01 19:27:35 -05:00
|
|
|
this._setContext.bind(this));
|
2017-12-05 14:05:00 -05:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_setContext(bus, res) {
|
2017-12-05 14:05:00 -05:00
|
|
|
this._context = this._ibus.create_input_context_async_finish(res);
|
2017-10-30 20:38:18 -04:00
|
|
|
this._context.connect('enabled', () => { this._enabled = true });
|
|
|
|
this._context.connect('disabled', () => { this._enabled = false });
|
2017-12-01 19:27:35 -05:00
|
|
|
this._context.connect('commit-text', this._onCommitText.bind(this));
|
|
|
|
this._context.connect('delete-surrounding-text', this._onDeleteSurroundingText.bind(this));
|
|
|
|
this._context.connect('update-preedit-text', this._onUpdatePreeditText.bind(this));
|
2017-12-05 14:05:00 -05:00
|
|
|
|
|
|
|
this._updateCapabilities();
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_clear() {
|
2017-12-05 14:05:00 -05:00
|
|
|
this._context = null;
|
|
|
|
this._hints = 0;
|
|
|
|
this._purpose = 0;
|
|
|
|
this._enabled = false;
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_emitRequestSurrounding() {
|
2017-12-05 14:05:00 -05:00
|
|
|
if (this._context.needs_surrounding_text())
|
|
|
|
this.emit('request-surrounding');
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onCommitText(context, text) {
|
2017-12-05 14:05:00 -05:00
|
|
|
this.commit(text.get_text());
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDeleteSurroundingText(context) {
|
2017-12-05 14:05:00 -05:00
|
|
|
this.delete_surrounding();
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onUpdatePreeditText(context, text, pos, visible) {
|
2017-12-05 14:05:00 -05:00
|
|
|
let str = null;
|
|
|
|
if (visible && text != null)
|
|
|
|
str = text.get_text();
|
|
|
|
|
|
|
|
this.set_preedit_text(str, pos);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_focus_in(focus) {
|
2017-12-05 14:05:00 -05:00
|
|
|
this._currentFocus = focus;
|
|
|
|
if (this._context) {
|
|
|
|
this._context.focus_in();
|
|
|
|
this._updateCapabilities();
|
|
|
|
this._emitRequestSurrounding();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_focus_out() {
|
2017-12-05 14:05:00 -05:00
|
|
|
this._currentFocus = null;
|
|
|
|
if (this._context) {
|
|
|
|
this._context.focus_out();
|
|
|
|
this._updateCapabilities();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset any preedit text
|
|
|
|
this.set_preedit_text(null, 0);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_reset() {
|
2017-12-05 14:05:00 -05:00
|
|
|
if (this._context) {
|
|
|
|
this._context.reset();
|
|
|
|
this._emitRequestSurrounding();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset any preedit text
|
|
|
|
this.set_preedit_text(null, 0);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_set_cursor_location(rect) {
|
2017-12-05 14:05:00 -05:00
|
|
|
if (this._context) {
|
|
|
|
this._context.set_cursor_location(rect.get_x(), rect.get_y(),
|
|
|
|
rect.get_width(), rect.get_height());
|
|
|
|
this._emitRequestSurrounding();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_set_surrounding(text, cursor, anchor) {
|
2017-12-05 14:05:00 -05:00
|
|
|
if (this._context)
|
|
|
|
this._context.set_surrounding_text(text, cursor, anchor);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_update_content_hints(hints) {
|
2017-12-05 14:05:00 -05:00
|
|
|
let ibusHints = 0;
|
|
|
|
if (hints & Clutter.InputContentHintFlags.COMPLETION)
|
|
|
|
ibusHints |= IBus.InputHints.WORD_COMPLETION;
|
|
|
|
if (hints & Clutter.InputContentHintFlags.SPELLCHECK)
|
|
|
|
ibusHints |= IBus.InputHints.SPELLCHECK;
|
|
|
|
if (hints & Clutter.InputContentHintFlags.AUTO_CAPITALIZATION)
|
|
|
|
ibusHints |= IBus.InputHints.UPPERCASE_SENTENCES;
|
|
|
|
if (hints & Clutter.InputContentHintFlags.LOWERCASE)
|
|
|
|
ibusHints |= IBus.InputHints.LOWERCASE;
|
|
|
|
if (hints & Clutter.InputContentHintFlags.UPPERCASE)
|
|
|
|
ibusHints |= IBus.InputHints.UPPERCASE_CHARS;
|
|
|
|
if (hints & Clutter.InputContentHintFlags.TITLECASE)
|
|
|
|
ibusHints |= IBus.InputHints.UPPERCASE_WORDS;
|
|
|
|
|
|
|
|
this._hints = ibusHints;
|
|
|
|
if (this._context)
|
|
|
|
this._context.set_content_type(this._purpose, this._hints);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_update_content_purpose(purpose) {
|
2017-12-05 14:05:00 -05:00
|
|
|
let ibusPurpose = 0;
|
|
|
|
if (purpose == Clutter.InputContentPurpose.NORMAL)
|
|
|
|
ibusPurpose = IBus.InputPurpose.FREE_FORM;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.ALPHA)
|
|
|
|
ibusPurpose = IBus.InputPurpose.ALPHA;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.DIGITS)
|
|
|
|
ibusPurpose = IBus.InputPurpose.DIGITS;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.NUMBER)
|
|
|
|
ibusPurpose = IBus.InputPurpose.NUMBER;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.PHONE)
|
|
|
|
ibusPurpose = IBus.InputPurpose.PHONE;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.URL)
|
|
|
|
ibusPurpose = IBus.InputPurpose.URL;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.EMAIL)
|
|
|
|
ibusPurpose = IBus.InputPurpose.EMAIL;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.NAME)
|
|
|
|
ibusPurpose = IBus.InputPurpose.NAME;
|
|
|
|
else if (purpose == Clutter.InputContentPurpose.PASSWORD)
|
|
|
|
ibusPurpose = IBus.InputPurpose.PASSWORD;
|
|
|
|
|
|
|
|
this._purpose = ibusPurpose;
|
|
|
|
if (this._context)
|
|
|
|
this._context.set_content_type(this._purpose, this._hints);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_filter_key_event(event) {
|
2017-12-05 14:05:00 -05:00
|
|
|
if (!this._context || !this._enabled)
|
|
|
|
return false;
|
2018-03-15 09:01:51 -04:00
|
|
|
if (!this._currentSource)
|
2017-12-05 14:05:00 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
let state = event.get_state();
|
|
|
|
if (state & IBus.ModifierType.IGNORED_MASK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (event.type() == Clutter.EventType.KEY_RELEASE)
|
|
|
|
state |= IBus.ModifierType.RELEASE_MASK;
|
|
|
|
this._context.process_key_event_async(event.get_key_symbol(),
|
|
|
|
event.get_key_code() - 8, // Convert XKB keycodes to evcodes
|
|
|
|
state, -1, null,
|
2017-10-30 20:38:18 -04:00
|
|
|
(context, res) => {
|
2017-12-05 14:05:00 -05:00
|
|
|
try {
|
|
|
|
let retval = context.process_key_event_async_finish(res);
|
|
|
|
this.notify_key_event(event, retval);
|
|
|
|
} catch (e) {
|
|
|
|
log('Error processing key on IM: ' + e.message);
|
|
|
|
}
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2017-12-05 14:05:00 -05:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|