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;
29 this.stub_ = /** @type {HTMLElement} */toolbar.querySelector('.toolbar-stub');
31 * @type {number?} The id of the preview timer, if any.
36 * @type {number} The left edge of the tool-bar stub, updated on resize.
41 * @type {number} The right edge of the tool-bar stub, updated on resize.
46 * @type {remoting.OptionsMenu}
49 this.optionsMenu_ = new remoting.OptionsMenu(
50 document.getElementById('send-ctrl-alt-del'),
51 document.getElementById('send-print-screen'),
52 document.getElementById('screen-resize-to-client'),
53 document.getElementById('screen-shrink-to-fit'),
54 document.getElementById('new-connection'),
55 document.getElementById('toggle-full-screen'));
57 window.addEventListener('mousemove', remoting.Toolbar.onMouseMove, false);
58 window.addEventListener('resize', this.center.bind(this), false);
60 registerEventListener('toolbar-disconnect', 'click', remoting.disconnect);
61 registerEventListener('toolbar-stub', 'click',
62 function() { remoting.toolbar.toggle(); });
64 // Prevent the preview canceling if the user is interacting with the tool-bar.
65 /** @type {remoting.Toolbar} */
67 var stopTimer = function() {
69 window.clearTimeout(that.timerId_);
73 this.toolbar_.addEventListener('mousemove', stopTimer, false);
77 * Preview the tool-bar functionality by showing it for 3s.
78 * @return {void} Nothing.
80 remoting.Toolbar.prototype.preview = function() {
81 this.toolbar_.classList.add(remoting.Toolbar.VISIBLE_CLASS_);
83 window.clearTimeout(this.timerId_);
86 var classList = this.toolbar_.classList;
87 this.timerId_ = window.setTimeout(
88 classList.remove.bind(classList, remoting.Toolbar.VISIBLE_CLASS_),
93 * Center the tool-bar horizonally.
95 remoting.Toolbar.prototype.center = function() {
96 var toolbarX = (window.innerWidth - this.toolbar_.clientWidth) / 2;
97 this.toolbar_.style['left'] = toolbarX + 'px';
98 var r = this.stub_.getBoundingClientRect();
99 this.stubLeft_ = r.left;
100 this.stubRight_ = r.right;
104 * Toggle the tool-bar visibility.
106 remoting.Toolbar.prototype.toggle = function() {
107 this.toolbar_.classList.toggle(remoting.Toolbar.VISIBLE_CLASS_);
111 * @param {remoting.ClientSession} clientSession The active session, or null if
112 * there is no connection.
114 remoting.Toolbar.prototype.setClientSession = function(clientSession) {
115 this.optionsMenu_.setClientSession(clientSession);
116 var connectedTo = document.getElementById('connected-to');
117 connectedTo.innerText =
118 clientSession ? clientSession.getHostDisplayName() : "";
122 * Test the specified co-ordinate to see if it is close enough to the stub
125 * @param {number} x The x co-ordinate.
126 * @param {number} y The y co-ordinate.
127 * @return {boolean} True if the position should activate the tool-bar stub, or
131 remoting.Toolbar.prototype.hitTest_ = function(x, y) {
133 return (x >= this.stubLeft_ - threshold &&
134 x <= this.stubRight_ + threshold &&
139 * Called whenever the mouse moves in the document. This is used to make the
140 * active area of the tool-bar stub larger without making a corresponding area
141 * of the host screen inactive.
143 * @param {Event} event The mouse move event.
144 * @return {void} Nothing.
146 remoting.Toolbar.onMouseMove = function(event) {
147 if (remoting.toolbar) {
148 var toolbarStub = remoting.toolbar.stub_;
149 if (remoting.toolbar.hitTest_(event.x, event.y)) {
150 toolbarStub.classList.add(remoting.Toolbar.STUB_EXTENDED_CLASS_);
152 toolbarStub.classList.remove(remoting.Toolbar.STUB_EXTENDED_CLASS_);
155 document.removeEventListener('mousemove',
156 remoting.Toolbar.onMouseMove, false);
160 /** @type {remoting.Toolbar} */
161 remoting.toolbar = null;
164 remoting.Toolbar.STUB_EXTENDED_CLASS_ = 'toolbar-stub-extended';
166 remoting.Toolbar.VISIBLE_CLASS_ = 'toolbar-visible';