Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / crd / js / fullscreen_v2.js
bloba6007b9798b9ff6a5a7755aa605059e63a803e05
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   /**
38    * @type {string} Internal 'full-screen changed' event name.
39    * @private
40    */
41   this.kEventName_ = '_fullscreenchanged';
43   /**
44    * @type {base.EventSourceImpl}
45    * @private
46    */
47   this.eventSource_ = new base.EventSourceImpl();
48   this.eventSource_.defineEvents([this.kEventName_]);
50   chrome.app.window.current().onFullscreened.addListener(
51       this.onFullscreened_.bind(this));
52   chrome.app.window.current().onRestored.addListener(
53       this.onRestored_.bind(this));
54   chrome.app.window.current().onMinimized.addListener(
55       this.onMinimized_.bind(this));
57   document.body.classList.toggle('fullscreen', this.isActive());
60 /**
61  * @param {boolean} fullscreen True to enter full-screen mode; false to leave.
62  * @param {function():void=} opt_onDone Optional completion callback.
63  */
64 remoting.FullscreenAppsV2.prototype.activate = function(
65     fullscreen, opt_onDone) {
66   if (opt_onDone) {
67     if (this.isActive() == fullscreen) {
68       opt_onDone();
69     } else {
70       /** @type {remoting.Fullscreen} */
71       var that = this;
72       var callbackAndRemoveListener = function() {
73         that.removeListener(callbackAndRemoveListener);
74         opt_onDone();
75       };
76       this.addListener(callbackAndRemoveListener);
77     }
78   }
80   if (fullscreen) {
81     chrome.app.window.current().fullscreen();
82   } else if (this.isActive()) {
83     chrome.app.window.current().restore();
84   }
87 remoting.FullscreenAppsV2.prototype.toggle = function() {
88   this.activate(!this.isActive());
91 /**
92  * @return {boolean}
93  */
94 remoting.FullscreenAppsV2.prototype.isActive = function() {
95   return chrome.app.window.current().isFullscreen();
98 /**
99  * @param {function(boolean=):void} callback
100  */
101 remoting.FullscreenAppsV2.prototype.addListener = function(callback) {
102   this.eventSource_.addEventListener(this.kEventName_, callback);
106  * @param {function(boolean=):void} callback
107  */
108 remoting.FullscreenAppsV2.prototype.removeListener = function(callback) {
109   this.eventSource_.removeEventListener(this.kEventName_, callback);
113  * @private
114  */
115 remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() {
116   this.isMinimized_ = false;
117   this.raiseEvent_(true);
118   document.body.classList.add('fullscreen');
122  * @private
123  */
124 remoting.FullscreenAppsV2.prototype.onRestored_ = function() {
125   if (!this.isMinimized_) {
126     document.body.classList.remove('fullscreen');
127     this.raiseEvent_(false);
128   }
129   this.isMinimized_ = false;
133  * @private
134  */
135 remoting.FullscreenAppsV2.prototype.onMinimized_ = function() {
136   this.isMinimized_ = true;
140  * @param {boolean} isFullscreen
141  * @private
142  */
143 remoting.FullscreenAppsV2.prototype.raiseEvent_ = function(isFullscreen) {
144   if (isFullscreen !== this.previousCallbackState_) {
145     this.previousCallbackState_ = isFullscreen;
146     this.eventSource_.raiseEvent(this.kEventName_, isFullscreen);
147   }