summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/fullscreen_v2.js
blob: 3aaaab16f3006b4543de81343e387385df7bea85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 * Full-screen implementation for apps v2, using chrome.AppWindow.
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/**
 * @constructor
 * @implements {remoting.Fullscreen}
 */
remoting.FullscreenAppsV2 = function() {
  /**
   * @type {boolean} True if maximize/restore events are being hooked.
   * @private
   */
  this.hookingWindowEvents_ = false;

  /**
   * @type {boolean} True if the next onRestored event should cause callbacks
   *     to be notified of a full-screen changed event. onRestored fires when
   *     full-screen mode is exited and also when the window is restored from
   *     being minimized; callbacks should not be notified of the latter.
   * @private
   */
  this.notifyCallbacksOnRestore_ = this.isActive();

  /**
   * @type {string} Internal 'full-screen changed' event name
   * @private
   */
  this.kEventName_ = '_fullscreenchanged';

  /**
   * @type {base.EventSource}
   * @private
   */
  this.eventSource_ = new base.EventSource();
  this.eventSource_.defineEvents([this.kEventName_]);

  chrome.app.window.current().onFullscreened.addListener(
      this.onFullscreened_.bind(this));
  chrome.app.window.current().onMaximized.addListener(
      this.onMaximized_.bind(this));
  chrome.app.window.current().onRestored.addListener(
      this.onRestored_.bind(this));
};

remoting.FullscreenAppsV2.prototype.activate = function(
    fullscreen, opt_onDone) {
  if (opt_onDone) {
    if (this.isActive() == fullscreen) {
      opt_onDone();
    } else {
      /** @type {remoting.Fullscreen} */
      var that = this;
      var callbackAndRemoveListener = function() {
        that.removeListener(callbackAndRemoveListener);
        opt_onDone();
      };
      this.addListener(callbackAndRemoveListener);
    }
  }

  if (fullscreen) {
    chrome.app.window.current().fullscreen();
  } else if (this.isActive()) {
    chrome.app.window.current().restore();
  }
};

remoting.FullscreenAppsV2.prototype.toggle = function() {
  this.activate(!this.isActive());
};

remoting.FullscreenAppsV2.prototype.isActive = function() {
  return chrome.app.window.current().isFullscreen();
};

remoting.FullscreenAppsV2.prototype.addListener = function(callback) {
  this.eventSource_.addEventListener(this.kEventName_, callback);
};

remoting.FullscreenAppsV2.prototype.removeListener = function(callback) {
  this.eventSource_.removeEventListener(this.kEventName_, callback);
};

remoting.FullscreenAppsV2.prototype.syncWithMaximize = function(sync) {
  if (sync && chrome.app.window.current().isMaximized()) {
    this.activate(true);
  }
  this.hookingWindowEvents_ = sync;
};

remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() {
  this.notifyCallbacksOnRestore_ = true;
  this.eventSource_.raiseEvent(this.kEventName_, true);
  document.body.classList.add('fullscreen');
};

remoting.FullscreenAppsV2.prototype.onMaximized_ = function() {
  if (this.hookingWindowEvents_) {
    this.activate(true);
  }
};

remoting.FullscreenAppsV2.prototype.onRestored_ = function() {
  document.body.classList.remove('fullscreen');
  if (this.hookingWindowEvents_) {
    this.activate(false);
  }
  if (this.notifyCallbacksOnRestore_) {
    this.notifyCallbacksOnRestore_ = false;
    this.eventSource_.raiseEvent(this.kEventName_, false);
  }
};