2013-09-04 13:53:36 -04:00
|
|
|
/** -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Inclusive Design Research Centre, OCAD University.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Joseph Scheuhammer <clown@alum.mit.edu>
|
|
|
|
* Contributor:
|
|
|
|
* Magdalen Berns <m.berns@sms.ed.ac.uk>
|
|
|
|
*/
|
|
|
|
|
|
|
|
const Atspi = imports.gi.Atspi;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
|
|
|
const CARETMOVED = 'object:text-caret-moved';
|
|
|
|
const STATECHANGED = 'object:state-changed';
|
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var FocusCaretTracker = class FocusCaretTracker {
|
|
|
|
constructor() {
|
2017-12-01 19:27:35 -05:00
|
|
|
this._atspiListener = Atspi.EventListener.new(this._onChanged.bind(this));
|
2014-02-13 10:25:24 -05:00
|
|
|
|
2014-05-29 08:12:16 -04:00
|
|
|
this._atspiInited = false;
|
2014-02-13 10:25:24 -05:00
|
|
|
this._focusListenerRegistered = false;
|
|
|
|
this._caretListenerRegistered = false;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-09-04 13:53:36 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onChanged(event) {
|
2013-09-04 13:53:36 -04:00
|
|
|
if (event.type.indexOf(STATECHANGED) == 0)
|
2013-09-06 04:18:09 -04:00
|
|
|
this.emit('focus-changed', event);
|
2013-09-04 13:53:36 -04:00
|
|
|
else if (event.type == CARETMOVED)
|
2013-09-06 04:18:09 -04:00
|
|
|
this.emit('caret-moved', event);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-09-04 13:53:36 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_initAtspi() {
|
2017-07-17 06:55:20 -04:00
|
|
|
if (!this._atspiInited && Atspi.init() == 0) {
|
2014-05-29 08:12:16 -04:00
|
|
|
Atspi.set_timeout(250, 250);
|
|
|
|
this._atspiInited = true;
|
|
|
|
}
|
2017-07-17 06:55:20 -04:00
|
|
|
|
2019-01-29 15:18:46 -05:00
|
|
|
return this._atspiInited;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2014-05-29 08:12:16 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
registerFocusListener() {
|
2017-07-17 06:55:20 -04:00
|
|
|
if (!this._initAtspi() || this._focusListenerRegistered)
|
2014-02-13 10:25:24 -05:00
|
|
|
return;
|
|
|
|
|
2019-01-29 19:18:24 -05:00
|
|
|
this._atspiListener.register(`${STATECHANGED}:focused`);
|
|
|
|
this._atspiListener.register(`${STATECHANGED}:selected`);
|
2014-02-13 10:25:24 -05:00
|
|
|
this._focusListenerRegistered = true;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-09-04 13:53:36 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
registerCaretListener() {
|
2017-07-17 06:55:20 -04:00
|
|
|
if (!this._initAtspi() || this._caretListenerRegistered)
|
2014-02-13 10:25:24 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
this._atspiListener.register(CARETMOVED);
|
|
|
|
this._caretListenerRegistered = true;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-09-04 13:53:36 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
deregisterFocusListener() {
|
2014-02-13 10:25:24 -05:00
|
|
|
if (!this._focusListenerRegistered)
|
|
|
|
return;
|
|
|
|
|
2019-01-29 19:18:24 -05:00
|
|
|
this._atspiListener.deregister(`${STATECHANGED}:focused`);
|
|
|
|
this._atspiListener.deregister(`${STATECHANGED}:selected`);
|
2014-02-13 10:25:24 -05:00
|
|
|
this._focusListenerRegistered = false;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-09-04 13:53:36 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
deregisterCaretListener() {
|
2014-02-13 10:25:24 -05:00
|
|
|
if (!this._caretListenerRegistered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._atspiListener.deregister(CARETMOVED);
|
|
|
|
this._caretListenerRegistered = false;
|
2013-09-04 13:53:36 -04:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2013-09-04 13:53:36 -04:00
|
|
|
Signals.addSignalMethods(FocusCaretTracker.prototype);
|