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.
4
5/**
6 * @fileoverview
7 * Full-screen implementation for apps v2, using chrome.AppWindow.
8 */
9
10'use strict';
11
12/** @suppress {duplicate} */
13var remoting = remoting || {};
14
15/**
16 * @constructor
17 * @implements {remoting.Fullscreen}
18 */
19remoting.FullscreenAppsV2 = function() {
20  /**
21   * @type {boolean} True if the next onRestored event should cause callbacks
22   *     to be notified of a full-screen changed event. onRestored fires when
23   *     full-screen mode is exited and also when the window is restored from
24   *     being minimized; callbacks should not be notified of the latter.
25   * @private
26   */
27  this.notifyCallbacksOnRestore_ = this.isActive();
28
29  /**
30   * @type {string} Internal 'full-screen changed' event name
31   * @private
32   */
33  this.kEventName_ = '_fullscreenchanged';
34
35  /**
36   * @type {base.EventSource}
37   * @private
38   */
39  this.eventSource_ = new base.EventSource();
40  this.eventSource_.defineEvents([this.kEventName_]);
41
42  chrome.app.window.current().onFullscreened.addListener(
43      this.onFullscreened_.bind(this));
44  chrome.app.window.current().onRestored.addListener(
45      this.onRestored_.bind(this));
46};
47
48remoting.FullscreenAppsV2.prototype.activate = function(
49    fullscreen, opt_onDone) {
50  if (opt_onDone) {
51    if (this.isActive() == fullscreen) {
52      opt_onDone();
53    } else {
54      /** @type {remoting.Fullscreen} */
55      var that = this;
56      var callbackAndRemoveListener = function() {
57        that.removeListener(callbackAndRemoveListener);
58        opt_onDone();
59      };
60      this.addListener(callbackAndRemoveListener);
61    }
62  }
63
64  if (fullscreen) {
65    chrome.app.window.current().fullscreen();
66  } else if (this.isActive()) {
67    chrome.app.window.current().restore();
68  }
69};
70
71remoting.FullscreenAppsV2.prototype.toggle = function() {
72  this.activate(!this.isActive());
73};
74
75remoting.FullscreenAppsV2.prototype.isActive = function() {
76  return chrome.app.window.current().isFullscreen();
77};
78
79remoting.FullscreenAppsV2.prototype.addListener = function(callback) {
80  this.eventSource_.addEventListener(this.kEventName_, callback);
81};
82
83remoting.FullscreenAppsV2.prototype.removeListener = function(callback) {
84  this.eventSource_.removeEventListener(this.kEventName_, callback);
85};
86
87remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() {
88  this.notifyCallbacksOnRestore_ = true;
89  this.eventSource_.raiseEvent(this.kEventName_, true);
90  document.body.classList.add('fullscreen');
91};
92
93remoting.FullscreenAppsV2.prototype.onRestored_ = function() {
94  document.body.classList.remove('fullscreen');
95  if (this.notifyCallbacksOnRestore_) {
96    this.notifyCallbacksOnRestore_ = false;
97    this.eventSource_.raiseEvent(this.kEventName_, false);
98  }
99};
100