From 280676c7e9df28f703003f68340989d30b2c0f48 Mon Sep 17 00:00:00 2001
From: Samuel Mannehed <samuel@cendio.se>
Date: Thu, 16 Feb 2017 13:32:11 +0100
Subject: [PATCH] Properly encapsulate the scale in Display

Other parts of the code shouldn't have to care about this. Let Display
convert between canvas coordinates and framebuffer coordinates.
---
 app/ui.js             |  8 +-------
 core/display.js       |  6 ++----
 core/input/devices.js |  4 ----
 tests/test.display.js | 16 ++++++++++++----
 4 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/app/ui.js b/app/ui.js
index 98bb5f6..074fcd6 100644
--- a/app/ui.js
+++ b/app/ui.js
@@ -1227,7 +1227,6 @@ var UI;
                 var display = UI.rfb.get_display();
                 var resizeMode = UI.getSetting('resize');
                 display.set_scale(1);
-                UI.rfb.get_mouse().set_scale(1);
 
                 if (resizeMode === 'remote') {
 
@@ -1247,12 +1246,7 @@ var UI;
 
                 } else if (resizeMode === 'scale' || resizeMode === 'downscale') {
                     var downscaleOnly = resizeMode === 'downscale';
-                    var scaleRatio = display.autoscale(screen.w, screen.h, downscaleOnly);
-
-                    if (!UI.rfb.get_view_only()) {
-                        UI.rfb.get_mouse().set_scale(scaleRatio);
-                        Util.Debug('Scaling by ' + UI.rfb.get_mouse().get_scale());
-                    }
+                    display.autoscale(screen.w, screen.h, downscaleOnly);
                 }
             }
         },
diff --git a/core/display.js b/core/display.js
index 8da8031..57e07d5 100644
--- a/core/display.js
+++ b/core/display.js
@@ -193,11 +193,11 @@
         },
 
         absX: function (x) {
-            return x + this._viewportLoc.x;
+            return x / this._scale + this._viewportLoc.x;
         },
 
         absY: function (y) {
-            return y + this._viewportLoc.y;
+            return y / this._scale + this._viewportLoc.y;
         },
 
         resize: function (width, height) {
@@ -589,8 +589,6 @@
             }
 
             this._rescale(scaleRatio);
-
-            return scaleRatio;  // so that the mouse, etc scale can be set
         },
 
         // Private Methods
diff --git a/core/input/devices.js b/core/input/devices.js
index 164442f..2e41122 100644
--- a/core/input/devices.js
+++ b/core/input/devices.js
@@ -161,7 +161,6 @@
         Util.set_defaults(this, defaults, {
             'target': document,
             'focused': true,
-            'scale': 1.0,
             'touchButton': 1
         });
 
@@ -343,8 +342,6 @@
             } else {
                 y = e.clientY - bounds.top;
             }
-            x = x / this._scale;
-            y = y / this._scale;
             return {x:x, y:y};
         },
 
@@ -398,7 +395,6 @@
         ['target',         'ro', 'dom'],   // DOM element that captures mouse input
         ['notify',         'ro', 'func'],  // Function to call to notify whenever a mouse event is received
         ['focused',        'rw', 'bool'],  // Capture and send mouse clicks/movement
-        ['scale',          'rw', 'float'], // Viewport scale factor 0.0 - 1.0
 
         ['onMouseButton',  'rw', 'func'],  // Handler for mouse button click/release
         ['onMouseMove',    'rw', 'func'],  // Handler for mouse movement
diff --git a/tests/test.display.js b/tests/test.display.js
index 5f4eed1..e4e3348 100644
--- a/tests/test.display.js
+++ b/tests/test.display.js
@@ -274,13 +274,17 @@ describe('Display/Canvas Helper', function () {
         });
 
         it('should use width to determine scale when the current aspect ratio is wider than the target', function () {
-            expect(display.autoscale(9, 16)).to.equal(9 / 4);
+            display.autoscale(9, 16);
+            expect(display.absX(9)).to.equal(4);
+            expect(display.absY(18)).to.equal(8);
             expect(canvas.clientWidth).to.equal(9);
             expect(canvas.clientHeight).to.equal(7); // round 9 / (4 / 3)
         });
 
         it('should use height to determine scale when the current aspect ratio is taller than the target', function () {
-            expect(display.autoscale(16, 9)).to.equal(3); // 9 / 3
+            display.autoscale(16, 9);
+            expect(display.absX(9)).to.equal(3);
+            expect(display.absY(18)).to.equal(6);
             expect(canvas.clientWidth).to.equal(12);  // 16 * (4 / 3)
             expect(canvas.clientHeight).to.equal(9);
 
@@ -293,11 +297,15 @@ describe('Display/Canvas Helper', function () {
         });
 
         it('should not upscale when downscaleOnly is true', function () {
-            expect(display.autoscale(2, 2, true)).to.equal(0.5);
+            display.autoscale(2, 2, true);
+            expect(display.absX(9)).to.equal(18);
+            expect(display.absY(18)).to.equal(36);
             expect(canvas.clientWidth).to.equal(2);
             expect(canvas.clientHeight).to.equal(2);
 
-            expect(display.autoscale(16, 9, true)).to.equal(1.0);
+            display.autoscale(16, 9, true);
+            expect(display.absX(9)).to.equal(9);
+            expect(display.absY(18)).to.equal(18);
             expect(canvas.clientWidth).to.equal(4);
             expect(canvas.clientHeight).to.equal(3);
         });
-- 
GitLab