/** * Ext.space.Focus is an API that lets applications register a callback function * that fires when the application switches foreground and background and back. * * To register a callback: * * Ext.space.Focus.onToggle(function(isForeground) { * if (isForeground) { * // something... * } else { * // something else... * } * }); * */Ext.define("Ext.space.Focus", { extend: Ext.space.Observable, singleton: true, /** * @private */ constructor: function() { Ext.onSpaceReady().then(function() { Ext.space.Focus.superclass.constructor.apply(this, arguments); this.init(); }.bind(this)); }, /** * @private */ init: function() { // listen for toggling to/from foreground/background Ext.space.Communicator.send({ command: "ApplicationFocus#registerHandler", callbacks: { onApplicationFocusChange: this._onToggle.bind(this), onSuccess: function() { /* no need to do anything */ } } }); // TODO: when Observable gets the ability to remove listeners, call // removeHandler when there are no more listeners left }, /** * Callback that fires when the application changes to/from fullscreen mode * * @private * @param {boolean} isForeground true if the application is now in fullscreen mode; false if not */ _onToggle: function(isForeground) { this.invokeListeners(isForeground); }, /** * Register a callback to run when the application moves from the foreground to * the background, or vice versa. * * The callback will be passed a single parameter, a boolean value indicating * whether or not the application is now in the foreground (the callback runs * after the mode change is finished, so e.g., a value of `true` indicates that * the application has switched into fullscreen mode). * * function onFocusChanged(isForeground) { * Ext.space.Logger.log("Application focus switched; now " + * (isForeground ? "foreground" : "background")); * } * * Ext.space.Focus.onToggle(onFocusChanged); * * @param {Function} callback Callback to fire when the application changes from * foreground to background or vice versa. */ onToggle: function(callback) { this.addListener.apply(this, arguments); }});