1 // Copyright (c) 2012 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.
7 * Class representing the client tool-bar.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
16 * @param {HTMLElement} toolbar The HTML element representing the tool-bar.
19 remoting.Toolbar = function(toolbar) {
24 this.toolbar_ = toolbar;
30 /** @type {HTMLElement} */(toolbar.querySelector('.toolbar-stub'));
32 * @type {number?} The id of the preview timer, if any.
37 * @type {number} The left edge of the tool-bar stub, updated on resize.
42 * @type {number} The right edge of the tool-bar stub, updated on resize.
48 * @type {remoting.MenuButton}
51 this.screenOptionsMenu_ = new remoting.MenuButton(
52 document.getElementById('screen-options-menu'),
53 this.onShowOptionsMenu_.bind(this));
55 * @type {remoting.MenuButton}
58 this.sendKeysMenu_ = new remoting.MenuButton(
59 document.getElementById('send-keys-menu')
63 window.addEventListener('mousemove', remoting.Toolbar.onMouseMove, false);
64 window.addEventListener('resize', this.center.bind(this), false);
66 registerEventListener('toolbar-disconnect', 'click', remoting.disconnect);
67 registerEventListener('toolbar-stub', 'click',
68 function() { remoting.toolbar.toggle(); });
70 // Prevent the preview canceling if the user is interacting with the tool-bar.
71 /** @type {remoting.Toolbar} */
73 var stopTimer = function() {
75 window.clearTimeout(that.timerId_);
79 this.toolbar_.addEventListener('mousemove', stopTimer, false);
84 * @return {remoting.OptionsMenu}
86 remoting.Toolbar.prototype.createOptionsMenu = function() {
87 return new remoting.OptionsMenu(
88 document.getElementById('send-ctrl-alt-del'),
89 document.getElementById('send-print-screen'),
90 document.getElementById('screen-resize-to-client'),
91 document.getElementById('screen-shrink-to-fit'),
92 document.getElementById('new-connection'),
93 document.getElementById('toggle-full-screen'),
98 * Preview the tool-bar functionality by showing it for 3s.
99 * @return {void} Nothing.
101 remoting.Toolbar.prototype.preview = function() {
102 this.toolbar_.classList.add(remoting.Toolbar.VISIBLE_CLASS_);
104 window.clearTimeout(this.timerId_);
105 this.timerId_ = null;
107 var classList = this.toolbar_.classList;
108 this.timerId_ = window.setTimeout(
109 classList.remove.bind(classList, remoting.Toolbar.VISIBLE_CLASS_),
114 * Center the tool-bar horizonally.
116 remoting.Toolbar.prototype.center = function() {
117 var toolbarX = (window.innerWidth - this.toolbar_.clientWidth) / 2;
118 this.toolbar_.style['left'] = toolbarX + 'px';
119 var r = this.stub_.getBoundingClientRect();
120 this.stubLeft_ = r.left;
121 this.stubRight_ = r.right;
125 * Toggle the tool-bar visibility.
127 remoting.Toolbar.prototype.toggle = function() {
128 this.toolbar_.classList.toggle(remoting.Toolbar.VISIBLE_CLASS_);
132 * @param {remoting.DesktopConnectedView} desktopConnectedView The view for
133 * the active session, or null if there is no connection.
135 remoting.Toolbar.prototype.setDesktopConnectedView = function(
136 desktopConnectedView) {
137 var connectedTo = document.getElementById('connected-to');
138 connectedTo.innerText =
139 desktopConnectedView ? desktopConnectedView.getHostDisplayName() : "";
143 * Test the specified co-ordinate to see if it is close enough to the stub
146 * @param {number} x The x co-ordinate.
147 * @param {number} y The y co-ordinate.
148 * @return {boolean} True if the position should activate the tool-bar stub, or
152 remoting.Toolbar.prototype.hitTest_ = function(x, y) {
154 return (x >= this.stubLeft_ - threshold &&
155 x <= this.stubRight_ + threshold &&
160 * Called whenever the mouse moves in the document. This is used to make the
161 * active area of the tool-bar stub larger without making a corresponding area
162 * of the host screen inactive.
164 * @param {Event} event The mouse move event.
165 * @return {void} Nothing.
167 remoting.Toolbar.onMouseMove = function(event) {
168 if (remoting.toolbar) {
169 var toolbarStub = remoting.toolbar.stub_;
170 if (remoting.toolbar.hitTest_(event.x, event.y)) {
171 toolbarStub.classList.add(remoting.Toolbar.STUB_EXTENDED_CLASS_);
173 toolbarStub.classList.remove(remoting.Toolbar.STUB_EXTENDED_CLASS_);
176 document.removeEventListener('mousemove',
177 remoting.Toolbar.onMouseMove, false);
182 * Updates the options menu to reflect the current scale-to-fit and full-screen
184 * @return {void} Nothing.
187 remoting.Toolbar.prototype.onShowOptionsMenu_ = function() {
188 remoting.optionsMenu.onShow();
191 /** @type {remoting.Toolbar} */
192 remoting.toolbar = null;
195 remoting.Toolbar.STUB_EXTENDED_CLASS_ = 'toolbar-stub-extended';
197 remoting.Toolbar.VISIBLE_CLASS_ = 'toolbar-visible';