2013-09-04 17:53:36 +00: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>
|
|
|
|
*/
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import Atspi from 'gi://Atspi';
|
|
|
|
import * as Signals from '../misc/signals.js';
|
2013-09-04 17:53:36 +00:00
|
|
|
|
|
|
|
const CARETMOVED = 'object:text-caret-moved';
|
|
|
|
const STATECHANGED = 'object:state-changed';
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
export class FocusCaretTracker extends Signals.EventEmitter {
|
2017-10-31 01:19:44 +00:00
|
|
|
constructor() {
|
2022-07-04 22:30:44 +00:00
|
|
|
super();
|
|
|
|
|
2017-12-02 00:27:35 +00:00
|
|
|
this._atspiListener = Atspi.EventListener.new(this._onChanged.bind(this));
|
2014-02-13 15:25:24 +00:00
|
|
|
|
2014-05-29 12:12:16 +00:00
|
|
|
this._atspiInited = false;
|
2014-02-13 15:25:24 +00:00
|
|
|
this._focusListenerRegistered = false;
|
|
|
|
this._caretListenerRegistered = false;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-09-04 17:53:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onChanged(event) {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (event.type.indexOf(STATECHANGED) === 0)
|
2013-09-06 08:18:09 +00:00
|
|
|
this.emit('focus-changed', event);
|
2023-08-07 00:51:19 +00:00
|
|
|
else if (event.type === CARETMOVED)
|
2013-09-06 08:18:09 +00:00
|
|
|
this.emit('caret-moved', event);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-09-04 17:53:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_initAtspi() {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (!this._atspiInited && Atspi.init() === 0) {
|
2014-05-29 12:12:16 +00:00
|
|
|
Atspi.set_timeout(250, 250);
|
|
|
|
this._atspiInited = true;
|
|
|
|
}
|
2017-07-17 10:55:20 +00:00
|
|
|
|
2019-01-29 20:18:46 +00:00
|
|
|
return this._atspiInited;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2014-05-29 12:12:16 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
registerFocusListener() {
|
2017-07-17 10:55:20 +00:00
|
|
|
if (!this._initAtspi() || this._focusListenerRegistered)
|
2014-02-13 15:25:24 +00:00
|
|
|
return;
|
|
|
|
|
2019-01-30 00:18:24 +00:00
|
|
|
this._atspiListener.register(`${STATECHANGED}:focused`);
|
|
|
|
this._atspiListener.register(`${STATECHANGED}:selected`);
|
2014-02-13 15:25:24 +00:00
|
|
|
this._focusListenerRegistered = true;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-09-04 17:53:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
registerCaretListener() {
|
2017-07-17 10:55:20 +00:00
|
|
|
if (!this._initAtspi() || this._caretListenerRegistered)
|
2014-02-13 15:25:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
this._atspiListener.register(CARETMOVED);
|
|
|
|
this._caretListenerRegistered = true;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-09-04 17:53:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
deregisterFocusListener() {
|
2014-02-13 15:25:24 +00:00
|
|
|
if (!this._focusListenerRegistered)
|
|
|
|
return;
|
|
|
|
|
2019-01-30 00:18:24 +00:00
|
|
|
this._atspiListener.deregister(`${STATECHANGED}:focused`);
|
|
|
|
this._atspiListener.deregister(`${STATECHANGED}:selected`);
|
2014-02-13 15:25:24 +00:00
|
|
|
this._focusListenerRegistered = false;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-09-04 17:53:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
deregisterCaretListener() {
|
2014-02-13 15:25:24 +00:00
|
|
|
if (!this._caretListenerRegistered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._atspiListener.deregister(CARETMOVED);
|
|
|
|
this._caretListenerRegistered = false;
|
2013-09-04 17:53:36 +00:00
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|