Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / crd / js / fullscreen_v2.js
blob616c77dc5dc7ef467313ad0626f8e978ae37d83c
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
3 // found in the LICENSE file.
5 /**
6  * @fileoverview
7  * Full-screen implementation for apps v2, using chrome.AppWindow.
8  */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16  * @constructor
17  * @implements {remoting.Fullscreen}
18  */
19 remoting.FullscreenAppsV2 = function() {
20   /**
21    * @type {boolean} True if the window is minimized. onRestored fires when the
22    *     the window transitions from minimized to any other state, but since we
23    *     only want transitions from full-screen to windowed to cause a callback,
24    *     we must keep track of the minimized state of the window.
25    * @private
26    */
27   this.isMinimized_ = chrome.app.window.current().isMinimized();
29   /**
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;
37   /** @private {string} Internal 'full-screen changed' event name. */
38   this.kEventName_ = '_fullscreenchanged';
40   /** @private {base.EventSourceImpl} */
41   this.eventSource_ = new base.EventSourceImpl();
42   this.eventSource_.defineEvents([this.kEventName_]);
44   chrome.app.window.current().onFullscreened.addListener(
45       this.onFullscreened_.bind(this));
46   chrome.app.window.current().onRestored.addListener(
47       this.onRestored_.bind(this));
48   chrome.app.window.current().onMinimized.addListener(
49       this.onMinimized_.bind(this));
51   document.body.classList.toggle('fullscreen', this.isActive());
54 /**
55  * @param {boolean} fullscreen True to enter full-screen mode; false to leave.
56  * @param {function():void=} opt_onDone Optional completion callback.
57  */
58 remoting.FullscreenAppsV2.prototype.activate = function(
59     fullscreen, opt_onDone) {
60   if (opt_onDone) {
61     if (this.isActive() == fullscreen) {
62       opt_onDone();
63     } else {
64       /** @type {remoting.Fullscreen} */
65       var that = this;
66       var callbackAndRemoveListener = function() {
67         that.removeListener(callbackAndRemoveListener);
68         opt_onDone();
69       };
70       this.addListener(callbackAndRemoveListener);
71     }
72   }
74   if (fullscreen) {
75     chrome.app.window.current().fullscreen();
76   } else if (this.isActive()) {
77     chrome.app.window.current().restore();
78   }
81 remoting.FullscreenAppsV2.prototype.toggle = function() {
82   this.activate(!this.isActive());
85 /**
86  * @return {boolean}
87  */
88 remoting.FullscreenAppsV2.prototype.isActive = function() {
89   return chrome.app.window.current().isFullscreen();
92 /**
93  * @param {function(boolean=):void} callback
94  */
95 remoting.FullscreenAppsV2.prototype.addListener = function(callback) {
96   this.eventSource_.addEventListener(this.kEventName_, callback);
99 /**
100  * @param {function(boolean=):void} callback
101  */
102 remoting.FullscreenAppsV2.prototype.removeListener = function(callback) {
103   this.eventSource_.removeEventListener(this.kEventName_, callback);
107  * @private
108  */
109 remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() {
110   this.isMinimized_ = false;
111   this.raiseEvent_(true);
112   document.body.classList.add('fullscreen');
116  * @private
117  */
118 remoting.FullscreenAppsV2.prototype.onRestored_ = function() {
119   if (!this.isMinimized_) {
120     // ChromeOS fires a spurious onRestored event going maximized->fullscreen.
121     // TODO(jamiewalch): Remove this work-around when crbug.com/394819 is fixed.
122     if (remoting.platformIsChromeOS() && this.isActive()) {
123       return;
124     }
125     document.body.classList.remove('fullscreen');
126     this.raiseEvent_(false);
127   }
128   this.isMinimized_ = false;
132  * @private
133  */
134 remoting.FullscreenAppsV2.prototype.onMinimized_ = function() {
135   this.isMinimized_ = true;
139  * @param {boolean} isFullscreen
140  * @private
141  */
142 remoting.FullscreenAppsV2.prototype.raiseEvent_ = function(isFullscreen) {
143   if (isFullscreen !== this.previousCallbackState_) {
144     this.previousCallbackState_ = isFullscreen;
145     this.eventSource_.raiseEvent(this.kEventName_, isFullscreen);
146   }