1 // Copyright (c) 2014 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.
6 * A controller class detects mouse inactivity and hides "tool" elements.
8 * @param {Element} container The main DOM container.
9 * @param {number=} opt_timeout Hide timeout in ms.
10 * @param {function():boolean=} opt_toolsActive Function that returns |true|
11 * if the tools are active and should not be hidden.
14 function MouseInactivityWatcher(container, opt_timeout, opt_toolsActive) {
15 this.container_ = container;
16 this.timeout_ = opt_timeout || MouseInactivityWatcher.DEFAULT_TIMEOUT;
17 this.toolsActive_ = opt_toolsActive || function() { return false; };
19 this.onTimeoutBound_ = this.onTimeout_.bind(this);
20 this.timeoutID_ = null;
21 this.mouseOverTool_ = false;
27 * Indicates if the inactivity watcher is enabled or disabled. Use getters
32 this.disabled_ = false;
33 this.__defineSetter__('disabled', function(value) {
34 this.disabled_ = value;
40 this.__defineGetter__('disabled', function() {
41 return this.disabled_;
44 this.container_.addEventListener('mousemove', this.onMouseMove_.bind(this));
45 var tools = this.container_.querySelector('.tool');
46 for (var i = 0; i < tools.length; i++) {
47 tools[i].addEventListener('mouseover', this.onToolMouseOver_.bind(this));
48 tools[i].addEventListener('mouseout', this.onToolMouseOut_.bind(this));
51 // Show tools when the user touches the screen.
52 this.container_.addEventListener(
53 'touchstart', this.activityStarted_.bind(this));
54 var initiateFading = this.activityStopped_.bind(this, this.timeout_);
55 this.container_.addEventListener('touchend', initiateFading);
56 this.container_.addEventListener('touchcancel', initiateFading);
60 * Default inactivity timeout.
62 MouseInactivityWatcher.DEFAULT_TIMEOUT = 3000;
65 * @param {boolean} on True if show, false if hide.
67 MouseInactivityWatcher.prototype.showTools = function(on) {
69 this.container_.setAttribute('tools', 'true');
71 this.container_.removeAttribute('tools');
75 * To be called when the user started activity. Shows the tools
76 * and cancels the countdown.
79 MouseInactivityWatcher.prototype.activityStarted_ = function() {
82 if (this.timeoutID_) {
83 clearTimeout(this.timeoutID_);
84 this.timeoutID_ = null;
89 * Called when user activity has stopped. Re-starts the countdown.
90 * @param {number=} opt_timeout Timeout.
93 MouseInactivityWatcher.prototype.activityStopped_ = function(opt_timeout) {
94 if (this.disabled_ || this.mouseOverTool_ || this.toolsActive_())
98 clearTimeout(this.timeoutID_);
100 this.timeoutID_ = setTimeout(
101 this.onTimeoutBound_, opt_timeout || this.timeout_);
105 * Called when a user performed a short action (such as a click or a key press)
106 * that should show the tools if they are not visible.
107 * @param {number=} opt_timeout Timeout.
109 MouseInactivityWatcher.prototype.kick = function(opt_timeout) {
110 this.activityStarted_();
111 this.activityStopped_(opt_timeout);
115 * Check if the tools are active and update the tools visibility accordingly.
117 MouseInactivityWatcher.prototype.check = function() {
118 if (this.toolsActive_())
119 this.activityStarted_();
121 this.activityStopped_();
125 * Mouse move handler.
127 * @param {Event} e Event.
130 MouseInactivityWatcher.prototype.onMouseMove_ = function(e) {
131 if (this.clientX_ == e.clientX && this.clientY_ == e.clientY) {
132 // The mouse has not moved, must be the cursor change triggered by
133 // some of the attributes on the root container. Ignore the event.
136 this.clientX_ = e.clientX;
137 this.clientY_ = e.clientY;
146 * Mouse over handler on a tool element.
148 * @param {Event} e Event.
151 MouseInactivityWatcher.prototype.onToolMouseOver_ = function(e) {
152 this.mouseOverTool_ = true;
158 * Mouse out handler on a tool element.
160 * @param {Event} e Event.
163 MouseInactivityWatcher.prototype.onToolMouseOut_ = function(e) {
164 this.mouseOverTool_ = false;
173 MouseInactivityWatcher.prototype.onTimeout_ = function() {
174 this.timeoutID_ = null;
175 if (!this.disabled_ && !this.toolsActive_())
176 this.showTools(false);