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 Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
|
|
|
const CARETMOVED = 'object:text-caret-moved';
|
|
|
|
const STATECHANGED = 'object:state-changed';
|
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var FocusCaretTracker = new Lang.Class({
|
2013-09-04 13:53:36 -04:00
|
|
|
Name: 'FocusCaretTracker',
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._atspiListener = Atspi.EventListener.new(Lang.bind(this, this._onChanged));
|
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;
|
2013-09-04 13:53:36 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_onChanged: function(event) {
|
|
|
|
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);
|
2013-09-04 13:53:36 -04:00
|
|
|
},
|
|
|
|
|
2014-05-29 08:12:16 -04:00
|
|
|
_initAtspi: function() {
|
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
|
|
|
|
|
|
|
return this._atspiInited;
|
2014-05-29 08:12:16 -04:00
|
|
|
},
|
|
|
|
|
2013-09-04 13:53:36 -04:00
|
|
|
registerFocusListener: function() {
|
2017-07-17 06:55:20 -04:00
|
|
|
if (!this._initAtspi() || this._focusListenerRegistered)
|
2014-02-13 10:25:24 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
this._atspiListener.register(STATECHANGED + ':focused');
|
|
|
|
this._atspiListener.register(STATECHANGED + ':selected');
|
|
|
|
this._focusListenerRegistered = true;
|
2013-09-04 13:53:36 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
registerCaretListener: function() {
|
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;
|
2013-09-04 13:53:36 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deregisterFocusListener: function() {
|
2014-02-13 10:25:24 -05:00
|
|
|
if (!this._focusListenerRegistered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._atspiListener.deregister(STATECHANGED + ':focused');
|
|
|
|
this._atspiListener.deregister(STATECHANGED + ':selected');
|
|
|
|
this._focusListenerRegistered = false;
|
2013-09-04 13:53:36 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deregisterCaretListener: function() {
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
Signals.addSignalMethods(FocusCaretTracker.prototype);
|