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 {Element} toolbar The HTML element representing the tool-bar.
19 remoting
.Toolbar = function(toolbar
) {
24 this.toolbar_
= toolbar
;
29 this.stub_
= 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.Toolbar} */
49 window
.addEventListener('mousemove', remoting
.Toolbar
.onMouseMove
, false);
50 window
.addEventListener('resize', function() { that
.center(); }, false);
52 // Prevent the preview canceling if the user is interacting with the tool-bar.
53 var stopTimer = function() {
55 window
.clearTimeout(that
.timerId_
);
59 this.toolbar_
.addEventListener('mousemove', stopTimer
, false);
63 * Preview the tool-bar functionality by showing it for 3s.
64 * @return {void} Nothing.
66 remoting
.Toolbar
.prototype.preview = function() {
67 addClass(this.toolbar_
, remoting
.Toolbar
.VISIBLE_CLASS_
);
69 window
.clearTimeout(this.timerId_
);
72 /** @type {remoting.Toolbar} */
74 var endPreview = function() {
75 removeClass(that
.toolbar_
, remoting
.Toolbar
.VISIBLE_CLASS_
);
77 this.timerId_
= window
.setTimeout(endPreview
, 3000);
81 * Center the tool-bar horizonally.
83 remoting
.Toolbar
.prototype.center = function() {
84 var toolbarX
= (window
.innerWidth
- this.toolbar_
.clientWidth
) / 2;
85 this.toolbar_
.style
['left'] = toolbarX
+ 'px';
86 var r
= this.stub_
.getBoundingClientRect();
87 this.stubLeft_
= r
.left
;
88 this.stubRight_
= r
.right
;
92 * Toggle the tool-bar visibility.
94 remoting
.Toolbar
.prototype.toggle = function() {
95 if (hasClass(this.toolbar_
.className
,
96 remoting
.Toolbar
.VISIBLE_CLASS_
)) {
97 removeClass(this.toolbar_
, remoting
.Toolbar
.VISIBLE_CLASS_
);
99 addClass(this.toolbar_
, remoting
.Toolbar
.VISIBLE_CLASS_
);
104 * Test the specified co-ordinate to see if it is close enough to the stub
107 * @param {number} x The x co-ordinate.
108 * @param {number} y The y co-ordinate.
109 * @return {boolean} True if the position should activate the tool-bar stub, or
113 remoting
.Toolbar
.prototype.hitTest_ = function(x
, y
) {
115 return (x
>= this.stubLeft_
- threshold
&&
116 x
<= this.stubRight_
+ threshold
&&
121 * Called whenever the mouse moves in the document. This is used to make the
122 * active area of the tool-bar stub larger without making a corresponding area
123 * of the host screen inactive.
125 * @param {Event} event The mouse move event.
126 * @return {void} Nothing.
128 remoting
.Toolbar
.onMouseMove = function(event
) {
129 if (remoting
.toolbar
) {
130 if (remoting
.toolbar
.hitTest_(event
.x
, event
.y
)) {
131 addClass(remoting
.toolbar
.stub_
, remoting
.Toolbar
.STUB_EXTENDED_CLASS_
);
133 removeClass(remoting
.toolbar
.stub_
,
134 remoting
.Toolbar
.STUB_EXTENDED_CLASS_
);
137 document
.removeEventListener('mousemove',
138 remoting
.Toolbar
.onMouseMove
, false);
142 /** @type {remoting.Toolbar} */
143 remoting
.toolbar
= null;
146 remoting
.Toolbar
.STUB_EXTENDED_CLASS_
= 'toolbar-stub-extended';
148 remoting
.Toolbar
.VISIBLE_CLASS_
= 'toolbar-visible';