From 301714928bfd45a0f11d8cbaec8f9fe538cefe79 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Tue, 16 Nov 2021 09:38:14 +0100 Subject: [PATCH] Avoid scrolling on RFB object focus Chrome scrolls the view to show as much as possible of the canvas when we call focus(), which is likely not the desired behaviour. This also exposes the ability to pass on future options when focusing the RFB object manually. --- core/rfb.js | 6 +++--- docs/API.md | 9 ++++++++- tests/test.rfb.js | 7 +++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/rfb.js b/core/rfb.js index ea3bf58a..084a457a 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -432,8 +432,8 @@ export default class RFB extends EventTargetMixin { } } - focus() { - this._canvas.focus(); + focus(options) { + this._canvas.focus(options); } blur() { @@ -609,7 +609,7 @@ export default class RFB extends EventTargetMixin { return; } - this.focus(); + this.focus({ preventScroll: true }); } _setDesktopName(name) { diff --git a/docs/API.md b/docs/API.md index aa5aea7a..066e8950 100644 --- a/docs/API.md +++ b/docs/API.md @@ -328,7 +328,14 @@ Keyboard events will be sent to the remote server after this point. ##### Syntax - RFB.focus( ); + RFB.focus( [options] ); + +###### Parameters + +**`options`** *Optional* + - A `object` providing options to control how the focus will be + performed. Please see [`HTMLElement.focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) + for available options. #### RFB.blur() diff --git a/tests/test.rfb.js b/tests/test.rfb.js index 5f505818..f70bc14f 100644 --- a/tests/test.rfb.js +++ b/tests/test.rfb.js @@ -392,6 +392,13 @@ describe('Remote Frame Buffer Protocol Client', function () { client.focus(); expect(client._canvas.focus).to.have.been.calledOnce; }); + + it('should include focus options', function () { + client._canvas.focus = sinon.spy(); + client.focus({ foobar: 12, gazonk: true }); + expect(client._canvas.focus).to.have.been.calledOnce; + expect(client._canvas.focus).to.have.been.calledWith({ foobar: 12, gazonk: true}); + }); }); describe('#blur', function () {