From ab1ace383e24b2963feb23a59fe50d7aba1c2733 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 26 Jul 2018 14:15:59 +0200 Subject: [PATCH 1/2] Handle fractional screen sizes With high DPI systems we can end up with a container with a size that is not an integer number of CSS pixels. Make sure we can handle those cases by allowing a fractional size for the output canvas. Framebuffer size and viewport coordinates are still restricted to integer dimensions though. Based on initial patch by Alexander E. Patrakov. --- core/display.js | 7 +++++-- core/rfb.js | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/core/display.js b/core/display.js index 4955ce20..d6db075c 100644 --- a/core/display.js +++ b/core/display.js @@ -168,6 +168,9 @@ export default class Display { height = this._fb_height; } + width = Math.floor(width); + height = Math.floor(height); + if (width > this._fb_width) { width = this._fb_width; } @@ -524,8 +527,8 @@ export default class Display { // style width to a number, the canvas is cleared. // However, if you set the style width to a string // ('NNNpx'), the canvas is scaled without clearing. - const width = Math.round(factor * vp.w) + 'px'; - const height = Math.round(factor * vp.h) + 'px'; + const width = factor * vp.w + 'px'; + const height = factor * vp.h + 'px'; if ((this._target.style.width !== width) || (this._target.style.height !== height)) { diff --git a/core/rfb.js b/core/rfb.js index 34330302..f09e2415 100644 --- a/core/rfb.js +++ b/core/rfb.js @@ -546,7 +546,8 @@ export default class RFB extends EventTargetMixin { } const size = this._screenSize(); - RFB.messages.setDesktopSize(this._sock, size.w, size.h, + RFB.messages.setDesktopSize(this._sock, + Math.floor(size.w), Math.floor(size.h), this._screen_id, this._screen_flags); Log.Debug('Requested new desktop size: ' + @@ -555,8 +556,8 @@ export default class RFB extends EventTargetMixin { // Gets the the size of the available screen _screenSize() { - return { w: this._screen.offsetWidth, - h: this._screen.offsetHeight }; + let r = this._screen.getBoundingClientRect(); + return { w: r.width, h: r.height }; } _fixScrollbars() { From 7407c1f4e29a7c7d998edbcc4cbf379edd6b5d76 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 16 Aug 2018 17:36:54 +0200 Subject: [PATCH 2/2] Replace bad sinon stub in mouse tests It screwed up important calls inside the code being tested. Avoid the stub by creating a temporary element with the desired properties. --- tests/test.mouse.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/test.mouse.js b/tests/test.mouse.js index dd8046cf..9630706e 100644 --- a/tests/test.mouse.js +++ b/tests/test.mouse.js @@ -5,12 +5,23 @@ import Mouse from '../core/input/mouse.js'; describe('Mouse Event Handling', function() { "use strict"; - // This function is only used on target (the canvas) - // and for these tests we can assume that the canvas is 100x100 - // located at coordinates 10x10 - sinon.stub(Element.prototype, 'getBoundingClientRect').returns( - {left: 10, right: 110, top: 10, bottom: 110, width: 100, height: 100}); - const target = document.createElement('canvas'); + let target; + + beforeEach(function () { + // For these tests we can assume that the canvas is 100x100 + // located at coordinates 10x10 + target = document.createElement('canvas'); + target.style.position = "absolute"; + target.style.top = "10px"; + target.style.left = "10px"; + target.style.width = "100px"; + target.style.height = "100px"; + document.body.appendChild(target); + }); + afterEach(function () { + document.body.removeChild(target); + target = null; + }); // The real constructors might not work everywhere we // want to run these tests