GUAC-1480: Do not poll - just hook into events where the clipboard may have 
changed.

Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/commit/31eb5ec7
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/tree/31eb5ec7
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/diff/31eb5ec7

Branch: refs/heads/master
Commit: 31eb5ec73f6205b185c9e543367d6f4fe5ba7db4
Parents: 8740d36
Author: Michael Jumper <[email protected]>
Authored: Thu Feb 4 17:33:26 2016 -0800
Committer: Michael Jumper <[email protected]>
Committed: Thu Feb 4 17:35:13 2016 -0800

----------------------------------------------------------------------
 .../app/client/services/clipboardService.js     | 36 ++++++++++++++------
 1 file changed, 25 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/31eb5ec7/guacamole/src/main/webapp/app/client/services/clipboardService.js
----------------------------------------------------------------------
diff --git a/guacamole/src/main/webapp/app/client/services/clipboardService.js 
b/guacamole/src/main/webapp/app/client/services/clipboardService.js
index c0ff6f7..b981b8b 100644
--- a/guacamole/src/main/webapp/app/client/services/clipboardService.js
+++ b/guacamole/src/main/webapp/app/client/services/clipboardService.js
@@ -104,21 +104,30 @@ angular.module('client').factory('clipboardService', 
['$injector',
 
         var deferred = $q.defer();
 
-        // Clear and select the clipboard DOM element
-        clipboardContent.value = '';
-        clipboardContent.select();
+        // Wait for the next event queue run before attempting to read
+        // clipboard data (in case the copy/cut has not yet completed)
+        window.setTimeout(function deferredClipboardRead() {
 
-        // Attempt paste local clipboard into clipboard DOM element
-        if (document.execCommand('paste'))
-            deferred.resolve(clipboardContent.value);
-        else
-            deferred.reject();
+            // Clear and select the clipboard DOM element
+            clipboardContent.value = '';
+            clipboardContent.select();
+
+            // Attempt paste local clipboard into clipboard DOM element
+            if (document.execCommand('paste'))
+                deferred.resolve(clipboardContent.value);
+            else
+                deferred.reject();
+
+        }, 10);
 
         return deferred.promise;
     };
 
-    // Periodically attempt to read the clipboard, firing an event if 
successful
-    window.setInterval(function periodicallyReadClipboard() {
+    /**
+     * Checks whether the clipboard data has changed, firing a new
+     * "guacClipboard" event if it has.
+     */
+    var checkClipboard = function checkClipboard() {
         service.getLocalClipboard().then(function clipboardRead(data) {
 
             // Fire clipboard event if the data has changed
@@ -128,7 +137,12 @@ angular.module('client').factory('clipboardService', 
['$injector',
             }
 
         });
-    }, 100);
+    };
+
+    // Attempt to read the clipboard if it may have changed
+    window.addEventListener('copy',  checkClipboard, true);
+    window.addEventListener('cut',   checkClipboard, true);
+    window.addEventListener('focus', checkClipboard, true);
 
     return service;
 

Reply via email to