1 // Copyright 2015 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 * Implements a basic UX control for a connected remoting session.
10 /** @suppress {duplicate} */
11 var remoting = remoting || {};
18 * @param {remoting.ClientPlugin} plugin
19 * @param {HTMLElement} viewportElement
20 * @param {HTMLElement} cursorElement
23 * @implements {base.Disposable}
25 remoting.ConnectedView = function(plugin, viewportElement, cursorElement) {
27 this.viewportElement_ = viewportElement;
30 this.plugin_ = plugin;
32 /** @private {!Array<HTMLElement>} */
33 this.focusableElements_ = [];
36 this.cursor_ = new remoting.ConnectedView.Cursor(
37 plugin, viewportElement, cursorElement);
39 /** @private {Element} */
40 this.debugRegionContainer_ =
41 viewportElement.querySelector('.debug-region-container');
43 var pluginElement = plugin.element();
46 this.disposables_ = new base.Disposables(
48 new base.DomEventHook(pluginElement, 'blur',
49 this.onPluginLostFocus_.bind(this), false),
50 new base.DomEventHook(document, 'visibilitychange',
51 this.onVisibilityChanged_.bind(this), false),
52 new remoting.Clipboard(plugin)
55 // TODO(wez): Only allow mouse lock if the app has the pointerLock permission.
56 this.plugin_.allowMouseLock();
58 pluginElement.focus();
62 * @return {void} Nothing.
64 remoting.ConnectedView.prototype.dispose = function() {
65 base.dispose(this.disposables_);
66 this.disposables_ = null;
67 this.cursorRender_ = null;
72 * Called when the app window is hidden.
73 * @return {void} Nothing.
76 remoting.ConnectedView.prototype.onVisibilityChanged_ = function() {
77 var pause = document.hidden;
78 this.plugin_.pauseVideo(pause);
79 this.plugin_.pauseAudio(pause);
83 * Callback that the plugin invokes to indicate when the connection is
86 * @param {boolean} ready True if the connection is ready.
88 remoting.ConnectedView.prototype.onConnectionReady = function(ready) {
89 this.viewportElement_.classList.toggle('session-client-inactive', !ready);
93 * Callback function called when the plugin element loses focus.
96 remoting.ConnectedView.prototype.onPluginLostFocus_ = function(event) {
97 // Release all keys to prevent them becoming 'stuck down' on the host.
98 this.plugin_.releaseAllKeys();
100 // Focus should stay on the element, not (for example) the toolbar.
101 // Due to crbug.com/246335, we can't restore the focus immediately,
102 // otherwise the plugin gets confused about whether or not it has focus.
104 var delayedCallback = function() {
105 // When the 'blur' event handler is called document.activeElement has not
106 // been set to the correct target. We need to retrieve the target in this
108 var target = /** @type {!HTMLElement} */ (document.activeElement);
109 if (!that.isElementFocusable_(target)) {
110 that.plugin_.element().focus();
113 window.setTimeout(delayedCallback, 0);
117 * Return focus to the plugin.
119 remoting.ConnectedView.prototype.returnFocusToPlugin = function() {
120 this.plugin_.element().focus();
124 * Determines whether an element is allowed to grab focus from the plugin.
125 * @param {!HTMLElement} element
129 remoting.ConnectedView.prototype.isElementFocusable_ = function(element) {
130 return this.focusableElements_.indexOf(element) !== -1;
134 * Allow the given element to grab focus from the plugin.
135 * @param {!HTMLElement} element
138 remoting.ConnectedView.prototype.allowFocus = function(element) {
139 if (this.focusableElements_.indexOf(element) < 0) {
140 this.focusableElements_.push(element);
145 * Handles dirty region debug messages.
147 * @param {{rects:Array<Array<number>>}} region Dirty region of the latest
150 remoting.ConnectedView.prototype.handleDebugRegion = function(region) {
151 while (this.debugRegionContainer_.firstChild) {
152 this.debugRegionContainer_.removeChild(
153 this.debugRegionContainer_.firstChild);
156 var rects = region.rects;
157 for (var i = 0; i < rects.length; ++i) {
158 var rect = document.createElement('div');
159 rect.classList.add('debug-region-rect');
160 rect.style.left = rects[i][0] + 'px';
161 rect.style.top = rects[i][1] +'px';
162 rect.style.width = rects[i][2] +'px';
163 rect.style.height = rects[i][3] + 'px';
164 this.debugRegionContainer_.appendChild(rect);
170 * Enables or disables rendering of dirty regions for debugging.
171 * @param {boolean} enable True to enable rendering.
173 remoting.ConnectedView.prototype.enableDebugRegion = function(enable) {
175 this.plugin_.setDebugDirtyRegionHandler(this.handleDebugRegion.bind(this));
177 this.plugin_.setDebugDirtyRegionHandler(null);
182 * @param {remoting.ClientPlugin} plugin
183 * @param {HTMLElement} viewportElement
184 * @param {HTMLElement} cursorElement
187 * @implements {base.Disposable}
189 remoting.ConnectedView.Cursor = function(
190 plugin, viewportElement, cursorElement) {
192 this.plugin_ = plugin;
194 this.cursorElement_ = cursorElement;
196 this.eventHook_ = new base.DomEventHook(
197 viewportElement, 'mousemove', this.onMouseMoved_.bind(this), true);
199 this.plugin_.setMouseCursorHandler(this.onCursorChanged_.bind(this));
202 remoting.ConnectedView.Cursor.prototype.dispose = function() {
203 base.dispose(this.eventHook_);
204 this.eventHook_ = null;
205 this.plugin_.setMouseCursorHandler(
206 /** function(string, string, number) */ base.doNothing);
211 * @param {string} url
212 * @param {number} hotspotX
213 * @param {number} hotspotY
216 remoting.ConnectedView.Cursor.prototype.onCursorChanged_ = function(
217 url, hotspotX, hotspotY) {
218 this.cursorElement_.hidden = !url;
220 this.cursorElement_.style.marginLeft = '-' + hotspotX + 'px';
221 this.cursorElement_.style.marginTop = '-' + hotspotY + 'px';
222 this.cursorElement_.src = url;
227 * @param {Event} event
230 remoting.ConnectedView.Cursor.prototype.onMouseMoved_ = function(event) {
231 this.cursorElement_.style.top = event.offsetY + 'px';
232 this.cursorElement_.style.left = event.offsetX + 'px';