| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * Full-screen implementation for apps v2, using chrome.AppWindow. | 7 * Full-screen implementation for apps v2, using chrome.AppWindow. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
| 13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * @constructor | 16 * @constructor |
| 17 * @implements {remoting.Fullscreen} | 17 * @implements {remoting.Fullscreen} |
| 18 */ | 18 */ |
| 19 remoting.FullscreenAppsV2 = function() { | 19 remoting.FullscreenAppsV2 = function() { |
| 20 /** | 20 /** |
| 21 * @type {boolean} True if the next onRestored event should cause callbacks | 21 * @type {boolean} True if the window is minimized. onRestored fires when the |
| 22 * to be notified of a full-screen changed event. onRestored fires when | 22 * the window transitions from minimized to any other state, but since we |
| 23 * full-screen mode is exited and also when the window is restored from | 23 * only want transitions from full-screen to windowed to cause a callback, |
| 24 * being minimized; callbacks should not be notified of the latter. | 24 * we must keep track of the minimized state of the window. |
| 25 * @private | 25 * @private |
| 26 */ | 26 */ |
| 27 this.notifyCallbacksOnRestore_ = this.isActive(); | 27 this.isMinimized_ = chrome.app.window.current().isMinimized(); |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * @type {string} Internal 'full-screen changed' event name | 30 * @type {?boolean} The most recent full-screen state passed to the callback. |
| 31 * This guards against redundant invocations, as as would otherwise occur |
| 32 * in response to a full-screen -> maximized -> unmaximized transition, |
| 33 * because this results in two onRestored callbacks. |
| 34 */ |
| 35 this.previousCallbackState_ = null; |
| 36 |
| 37 /** |
| 38 * @type {string} Internal 'full-screen changed' event name. |
| 31 * @private | 39 * @private |
| 32 */ | 40 */ |
| 33 this.kEventName_ = '_fullscreenchanged'; | 41 this.kEventName_ = '_fullscreenchanged'; |
| 34 | 42 |
| 35 /** | 43 /** |
| 36 * @type {base.EventSource} | 44 * @type {base.EventSource} |
| 37 * @private | 45 * @private |
| 38 */ | 46 */ |
| 39 this.eventSource_ = new base.EventSource(); | 47 this.eventSource_ = new base.EventSource(); |
| 40 this.eventSource_.defineEvents([this.kEventName_]); | 48 this.eventSource_.defineEvents([this.kEventName_]); |
| 41 | 49 |
| 42 chrome.app.window.current().onFullscreened.addListener( | 50 chrome.app.window.current().onFullscreened.addListener( |
| 43 this.onFullscreened_.bind(this)); | 51 this.onFullscreened_.bind(this)); |
| 44 chrome.app.window.current().onRestored.addListener( | 52 chrome.app.window.current().onRestored.addListener( |
| 45 this.onRestored_.bind(this)); | 53 this.onRestored_.bind(this)); |
| 54 chrome.app.window.current().onMinimized.addListener( |
| 55 this.onMinimized_.bind(this)); |
| 46 }; | 56 }; |
| 47 | 57 |
| 58 /** |
| 59 * @param {boolean} fullscreen True to enter full-screen mode; false to leave. |
| 60 * @param {function():void=} opt_onDone Optional completion callback. |
| 61 */ |
| 48 remoting.FullscreenAppsV2.prototype.activate = function( | 62 remoting.FullscreenAppsV2.prototype.activate = function( |
| 49 fullscreen, opt_onDone) { | 63 fullscreen, opt_onDone) { |
| 50 if (opt_onDone) { | 64 if (opt_onDone) { |
| 51 if (this.isActive() == fullscreen) { | 65 if (this.isActive() == fullscreen) { |
| 52 opt_onDone(); | 66 opt_onDone(); |
| 53 } else { | 67 } else { |
| 54 /** @type {remoting.Fullscreen} */ | 68 /** @type {remoting.Fullscreen} */ |
| 55 var that = this; | 69 var that = this; |
| 56 var callbackAndRemoveListener = function() { | 70 var callbackAndRemoveListener = function() { |
| 57 that.removeListener(callbackAndRemoveListener); | 71 that.removeListener(callbackAndRemoveListener); |
| 58 opt_onDone(); | 72 opt_onDone(); |
| 59 }; | 73 }; |
| 60 this.addListener(callbackAndRemoveListener); | 74 this.addListener(callbackAndRemoveListener); |
| 61 } | 75 } |
| 62 } | 76 } |
| 63 | 77 |
| 64 if (fullscreen) { | 78 if (fullscreen) { |
| 65 chrome.app.window.current().fullscreen(); | 79 chrome.app.window.current().fullscreen(); |
| 66 } else if (this.isActive()) { | 80 } else if (this.isActive()) { |
| 67 chrome.app.window.current().restore(); | 81 chrome.app.window.current().restore(); |
| 68 } | 82 } |
| 69 }; | 83 }; |
| 70 | 84 |
| 71 remoting.FullscreenAppsV2.prototype.toggle = function() { | 85 remoting.FullscreenAppsV2.prototype.toggle = function() { |
| 72 this.activate(!this.isActive()); | 86 this.activate(!this.isActive()); |
| 73 }; | 87 }; |
| 74 | 88 |
| 89 /** |
| 90 * @return {boolean} |
| 91 */ |
| 75 remoting.FullscreenAppsV2.prototype.isActive = function() { | 92 remoting.FullscreenAppsV2.prototype.isActive = function() { |
| 76 return chrome.app.window.current().isFullscreen(); | 93 return chrome.app.window.current().isFullscreen(); |
| 77 }; | 94 }; |
| 78 | 95 |
| 79 /** | 96 /** |
| 80 * @param {function(boolean=):void} callback | 97 * @param {function(boolean=):void} callback |
| 81 */ | 98 */ |
| 82 remoting.FullscreenAppsV2.prototype.addListener = function(callback) { | 99 remoting.FullscreenAppsV2.prototype.addListener = function(callback) { |
| 83 this.eventSource_.addEventListener(this.kEventName_, callback); | 100 this.eventSource_.addEventListener(this.kEventName_, callback); |
| 84 }; | 101 }; |
| 85 | 102 |
| 86 /** | 103 /** |
| 87 * @param {function(boolean=):void} callback | 104 * @param {function(boolean=):void} callback |
| 88 */ | 105 */ |
| 89 remoting.FullscreenAppsV2.prototype.removeListener = function(callback) { | 106 remoting.FullscreenAppsV2.prototype.removeListener = function(callback) { |
| 90 this.eventSource_.removeEventListener(this.kEventName_, callback); | 107 this.eventSource_.removeEventListener(this.kEventName_, callback); |
| 91 }; | 108 }; |
| 92 | 109 |
| 110 /** |
| 111 * @private |
| 112 */ |
| 93 remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() { | 113 remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() { |
| 94 this.notifyCallbacksOnRestore_ = true; | 114 this.isMinimized_ = false; |
| 95 this.eventSource_.raiseEvent(this.kEventName_, true); | 115 this.raiseEvent_(true); |
| 96 document.body.classList.add('fullscreen'); | 116 document.body.classList.add('fullscreen'); |
| 97 }; | 117 }; |
| 98 | 118 |
| 119 /** |
| 120 * @private |
| 121 */ |
| 99 remoting.FullscreenAppsV2.prototype.onRestored_ = function() { | 122 remoting.FullscreenAppsV2.prototype.onRestored_ = function() { |
| 100 document.body.classList.remove('fullscreen'); | 123 if (!this.isMinimized_) { |
| 101 if (this.notifyCallbacksOnRestore_) { | 124 document.body.classList.remove('fullscreen'); |
| 102 this.notifyCallbacksOnRestore_ = false; | 125 this.raiseEvent_(false); |
| 103 this.eventSource_.raiseEvent(this.kEventName_, false); | 126 } |
| 127 this.isMinimized_ = false; |
| 128 }; |
| 129 |
| 130 /** |
| 131 * @private |
| 132 */ |
| 133 remoting.FullscreenAppsV2.prototype.onMinimized_ = function() { |
| 134 this.isMinimized_ = true; |
| 135 }; |
| 136 |
| 137 /** |
| 138 * @param {boolean} isFullscreen |
| 139 * @private |
| 140 */ |
| 141 remoting.FullscreenAppsV2.prototype.raiseEvent_ = function(isFullscreen) { |
| 142 if (isFullscreen !== this.previousCallbackState_) { |
| 143 this.previousCallbackState_ = isFullscreen; |
| 144 this.eventSource_.raiseEvent(this.kEventName_, isFullscreen); |
| 104 } | 145 } |
| 105 }; | 146 }; |
| OLD | NEW |