From 2b4afdb9870c861a1d6fb3ca9351e5dd065b0d7d Mon Sep 17 00:00:00 2001
From: David Rowe <david@ctrlaltstudio.com>
Date: Sun, 2 Apr 2017 12:58:11 +1200
Subject: [PATCH 1/2] Fix tablet window getting stuck to pointer if release
 mouse off tablet

---
 interface/src/ui/overlays/Web3DOverlay.cpp | 40 ++++++++++++++++------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/interface/src/ui/overlays/Web3DOverlay.cpp b/interface/src/ui/overlays/Web3DOverlay.cpp
index 20f798d1a8..99558c8503 100644
--- a/interface/src/ui/overlays/Web3DOverlay.cpp
+++ b/interface/src/ui/overlays/Web3DOverlay.cpp
@@ -363,6 +363,16 @@ void Web3DOverlay::handlePointerEventAsTouch(const PointerEvent& event) {
     QEvent::Type touchType;
     Qt::TouchPointState touchPointState;
     QEvent::Type mouseType;
+
+    Qt::MouseButton button = Qt::NoButton;
+    Qt::MouseButtons buttons = Qt::NoButton;
+    if (event.getButton() == PointerEvent::PrimaryButton) {
+        button = Qt::LeftButton;
+    }
+    if (event.getButtons() & PointerEvent::PrimaryButton) {
+        buttons |= Qt::LeftButton;
+    }
+
     switch (event.getType()) {
         case PointerEvent::Press:
             touchType = QEvent::TouchBegin;
@@ -378,6 +388,26 @@ void Web3DOverlay::handlePointerEventAsTouch(const PointerEvent& event) {
             touchType = QEvent::TouchUpdate;
             touchPointState = Qt::TouchPointMoved;
             mouseType = QEvent::MouseMove;
+
+            if (((event.getButtons() & PointerEvent::PrimaryButton) > 0) != this->_pressed) {
+                // Mouse was pressed/released while off the overlay; convert touch and mouse events to press/release to reflect
+                // current mouse/touch status.
+                this->_pressed = !this->_pressed;
+                if (this->_pressed) {
+                    touchType = QEvent::TouchBegin;
+                    touchPointState = Qt::TouchPointPressed;
+                    mouseType = QEvent::MouseButtonPress;
+
+                } else {
+                    touchType = QEvent::TouchEnd;
+                    touchPointState = Qt::TouchPointReleased;
+                    mouseType = QEvent::MouseButtonRelease;
+
+                }
+                button = Qt::LeftButton;
+                buttons |= Qt::LeftButton;
+            }
+
             break;
         default:
             return;
@@ -403,16 +433,6 @@ void Web3DOverlay::handlePointerEventAsTouch(const PointerEvent& event) {
     // FIXME: Scroll bar dragging is a bit unstable in the tablet (content can jump up and down at times). 
     // This may be improved in Qt 5.8. Release notes: "Cleaned up touch and mouse event delivery".
 
-    Qt::MouseButtons buttons = Qt::NoButton;
-    if (event.getButtons() & PointerEvent::PrimaryButton) {
-        buttons |= Qt::LeftButton;
-    }
-
-    Qt::MouseButton button = Qt::NoButton;
-    if (event.getButton() == PointerEvent::PrimaryButton) {
-        button = Qt::LeftButton;
-    }
-
     QMouseEvent* mouseEvent = new QMouseEvent(mouseType, windowPoint, windowPoint, windowPoint, button, buttons, Qt::NoModifier);
     QCoreApplication::postEvent(_webSurface->getWindow(), mouseEvent);
 }

From 405e8cfc9e83cd4aa6b61f20d64a6514b02be7cd Mon Sep 17 00:00:00 2001
From: David Rowe <david@ctrlaltstudio.com>
Date: Sun, 2 Apr 2017 12:58:24 +1200
Subject: [PATCH 2/2] Fix typo noticed in passing

---
 libraries/shared/src/PointerEvent.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libraries/shared/src/PointerEvent.h b/libraries/shared/src/PointerEvent.h
index cdea0aa3ed..ab77328fc1 100644
--- a/libraries/shared/src/PointerEvent.h
+++ b/libraries/shared/src/PointerEvent.h
@@ -64,7 +64,7 @@ private:
     glm::vec3 _normal;    // surface normal
     glm::vec3 _direction; // incoming direction of pointer ray.
 
-    Button _button { NoButtons };  // button assosiated with this event, (if type is Press, this will be the button that is pressed)
+    Button _button { NoButtons };  // button associated with this event, (if type is Press, this will be the button that is pressed)
     uint32_t _buttons { NoButtons }; // the current state of all the buttons.
     Qt::KeyboardModifiers _keyboardModifiers; // set of keys held when event was generated
 };