summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/crd/js/window_frame.js
blob: 0bb0b6475823cb1518d3b17c2ccc32e213b63771 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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
 * Apps v2 custom title bar implementation
 */

'use strict';

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

/**
 * @param {HTMLElement} titleBar The root node of the title-bar DOM hierarchy.
 * @constructor
 */
remoting.WindowFrame = function(titleBar) {
  /** @private {remoting.DesktopConnectedView} */
  this.desktopConnectedView_ = null;

  /** @private {HTMLElement} */
  this.titleBar_ = titleBar;

  /** @private {HTMLElement} */
  this.title_ = /** @type {HTMLElement} */
      (titleBar.querySelector('.window-title'));
  base.debug.assert(this.title_ != null);

  /** @private {HTMLElement} */
  this.maximizeRestoreControl_ = /** @type {HTMLElement} */
      (titleBar.querySelector('.window-maximize-restore'));
  base.debug.assert(this.maximizeRestoreControl_ != null);

  var optionsButton = titleBar.querySelector('.window-options');
  base.debug.assert(optionsButton != null);
  this.optionMenuButton_ = new remoting.MenuButton(
      optionsButton,
      this.onShowOptionsMenu_.bind(this),
      this.onHideOptionsMenu_.bind(this));

  /** @private {HTMLElement} */
  this.optionsMenuList_ = /** @type {HTMLElement} */
      (optionsButton.querySelector('.window-options-menu'));
  base.debug.assert(this.optionsMenuList_ != null);

  /**
   * @type {Array<{cls:string, fn: function()}>}
   */
  var handlers = [
    { cls: 'window-disconnect', fn: this.disconnectSession_.bind(this) },
    { cls: 'window-maximize-restore',
      fn: this.maximizeOrRestoreWindow_.bind(this) },
    { cls: 'window-minimize', fn: this.minimizeWindow_.bind(this) },
    { cls: 'window-close', fn: remoting.app.quit.bind(remoting.app) },
    { cls: 'window-controls-stub', fn: this.toggleWindowControls_.bind(this) }
  ];
  for (var i = 0; i < handlers.length; ++i) {
    var element = titleBar.querySelector('.' + handlers[i].cls);
    base.debug.assert(element != null);
    element.addEventListener('click', handlers[i].fn, false);
  }

  // Ensure that tool-tips are always correct.
  this.handleWindowStateChange_();
  chrome.app.window.current().onMaximized.addListener(
      this.handleWindowStateChange_.bind(this));
  chrome.app.window.current().onRestored.addListener(
      this.handleWindowStateChange_.bind(this));
  chrome.app.window.current().onFullscreened.addListener(
      this.handleWindowStateChange_.bind(this));
  chrome.app.window.current().onFullscreened.addListener(
      this.showWindowControlsPreview_.bind(this));
};

/**
 * @return {remoting.OptionsMenu}
 */
remoting.WindowFrame.prototype.createOptionsMenu = function() {
  return new remoting.OptionsMenu(
      this.titleBar_.querySelector('.menu-send-ctrl-alt-del'),
      this.titleBar_.querySelector('.menu-send-print-screen'),
      this.titleBar_.querySelector('.menu-resize-to-client'),
      this.titleBar_.querySelector('.menu-shrink-to-fit'),
      this.titleBar_.querySelector('.menu-new-connection'),
      this.titleBar_.querySelector('.window-fullscreen'),
      this.titleBar_.querySelector('.menu-start-stop-recording'));
};

/**
 * @param {remoting.DesktopConnectedView} desktopConnectedView The view for the
 *     current session, or null if there is no connection.
 */
remoting.WindowFrame.prototype.setDesktopConnectedView = function(
    desktopConnectedView) {
  this.desktopConnectedView_ = desktopConnectedView;
  var windowTitle = document.head.querySelector('title');
  if (this.desktopConnectedView_) {
    this.title_.innerText = desktopConnectedView.getHostDisplayName();
    windowTitle.innerText = desktopConnectedView.getHostDisplayName() + ' - ' +
        remoting.app.getApplicationName();
  } else {
    this.title_.innerHTML = '&nbsp;';
    windowTitle.innerText = remoting.app.getApplicationName();
  }
  this.handleWindowStateChange_();
};

/**
 * @return {{width: number, height: number}} The size of the window, ignoring
 *     the title-bar and window borders, if visible.
 */
remoting.WindowFrame.prototype.getClientArea = function() {
  if (chrome.app.window.current().isFullscreen()) {
    return { 'height': window.innerHeight, 'width': window.innerWidth };
  } else {
    var kBorderWidth = 1;
    var titleHeight = this.titleBar_.clientHeight;
    return {
      'height': window.innerHeight - titleHeight - 2 * kBorderWidth,
      'width': window.innerWidth - 2 * kBorderWidth
    };
  }
};

/**
 * @private
 */
remoting.WindowFrame.prototype.disconnectSession_ = function() {
  remoting.app.disconnect();
};

/**
 * @private
 */
remoting.WindowFrame.prototype.maximizeOrRestoreWindow_ = function() {
  /** @type {boolean} */
  var restore =
      chrome.app.window.current().isFullscreen() ||
      chrome.app.window.current().isMaximized();
  if (restore) {
    chrome.app.window.current().restore();
  } else {
    chrome.app.window.current().maximize();
  }
};

/**
 * @private
 */
remoting.WindowFrame.prototype.minimizeWindow_ = function() {
  chrome.app.window.current().minimize();
};

/**
 * @private
 */
remoting.WindowFrame.prototype.toggleWindowControls_ = function() {
  this.titleBar_.classList.toggle('opened');
};

/**
 * Update the tool-top for the maximize/full-screen/restore icon to reflect
 * its current behaviour.
 *
 * @private
 */
remoting.WindowFrame.prototype.handleWindowStateChange_ = function() {
  // Set the title for the maximize/restore/full-screen button
  /** @type {string} */
  var tag = '';
  if (chrome.app.window.current().isFullscreen()) {
    tag = /*i18n-content*/'EXIT_FULL_SCREEN';
  } else if (chrome.app.window.current().isMaximized()) {
    tag = /*i18n-content*/'RESTORE_WINDOW';
  } else {
    tag = /*i18n-content*/'MAXIMIZE_WINDOW';
  }
  this.maximizeRestoreControl_.title = l10n.getTranslationOrError(tag);

  // Ensure that the options menu aligns correctly for the side of the window
  // it occupies.
  if (chrome.app.window.current().isFullscreen()) {
    this.optionsMenuList_.classList.add('right-align');
  } else {
    this.optionsMenuList_.classList.remove('right-align');
  }
};

/**
 * Callback invoked when the options menu is shown.
 * @private
 */
remoting.WindowFrame.prototype.onShowOptionsMenu_ = function() {
  remoting.optionsMenu.onShow();
  this.titleBar_.classList.add('menu-opened');
};

/**
 * Callback invoked when the options menu is shown.
 * @private
 */
remoting.WindowFrame.prototype.onHideOptionsMenu_ = function() {
  this.titleBar_.classList.remove('menu-opened');
};

/**
 * Show the window controls for a few seconds
 *
 * @private
 */
remoting.WindowFrame.prototype.showWindowControlsPreview_ = function() {
  /**
   * @type {HTMLElement}
   */
  var target =  this.titleBar_;
  var kPreviewTimeoutMs = 3000;
  var hidePreview = function() {
    target.classList.remove('preview');
  };
  target.classList.add('preview');
  window.setTimeout(hidePreview, kPreviewTimeoutMs);
};


/** @type {remoting.WindowFrame} */
remoting.windowFrame = null;