From f1a93ee0f67e114daf26711c9bb5216816813115 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Sat, 7 May 2016 20:14:57 -0400
Subject: [PATCH] try to make sense of the cut/copy/paste/selectall event
 handlers The fact that nwjs menubar shortcuts don't propagate to the DOM
 makes this tricky The fact that OSX has its own OSX-app menu bar thingy makes
 it even trickier

---
 pd/nw/pd_canvas.js | 55 ++++++++++++++++++++++++++++++++++++----------
 pd/nw/pd_menus.js  | 14 ++++++++++++
 2 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 9ec61c055..8aa41aa0a 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -298,33 +298,41 @@ var canvas_events = (function() {
 
                     case 65:
                         if (cmd_or_ctrl_key(evt)) { // ctrl-a
-                            pdgui.pdsend(name, "selectall");
+                            // This is handled in the nwjs menu, but we
+                            // add a way to toggle the window menubar
+                            // the following command should be uncommented...
+                            //pdgui.pdsend(name, "selectall");
                             hack = 0; // not sure what to report here...
                         }
                         break;
                     case 88:
                         if (cmd_or_ctrl_key(evt)) { // ctrl-x
-                            pdgui.pdsend(name, "cut");
+                            // This is handled in the nwjs menubar. If we
+                            // add a way to toggle the menubar it will be
+                            // handled with the "cut" DOM listener, so we
+                            // can probably remove this code...
+                            //pdgui.pdsend(name, "cut");
                             hack = 0; // not sure what to report here...
                         }
                         break;
                     case 67:
                         if (cmd_or_ctrl_key(evt)) { // ctrl-c
-                            pdgui.pdsend(name, "copy");
+                            // Handled in nwjs menubar (see above)
+                            //pdgui.pdsend(name, "copy");
                             hack = 0; // not sure what to report here...
                         }
                         break;
                     case 86:
                         if (cmd_or_ctrl_key(evt)) { // ctrl-v
-                            // Crazy workaround: instead of sending the
-                            // "paste" message to Pd here, we wait for the
-                            // "paste" listener to pick it up. That way it
-                            // can check to see if there's anything in the
-                            // paste buffer, and if so forward it to Pd.
-
-                            // We could also use "cut" and "copy" handlers
-                            // above for symmetry, but we're not currently
-                            // doing anything with those buffers.
+                            // Instead of sending the "paste" message to Pd
+                            // here, we wait for the "paste" DOM listener to
+                            // pick it up. That way it can check to see if
+                            // there's anything in the paste buffer, and if so
+                            // forward it to Pd.
+
+                            // We also use "cut" and "copy" DOM event handlers
+                            // and leave this code in case we need to change
+                            // tactics for some reason.
                             //pdgui.pdsend(name, "paste");
                             hack = 0; // not sure what to report here...
                         }
@@ -337,6 +345,9 @@ var canvas_events = (function() {
                             // inside an object box we need to use, and a
                             // menu item shortcut would not propogate down
                             // to the DOM in that case.
+
+                            // Not sure if DOM has listeners for undo/redo,
+                            // so for now (May 7, 2016) we catch them here...
                             if (evt.shiftKey) {
                                 pdgui.pdsend(name, "redo");
                             } else {
@@ -595,6 +606,26 @@ var canvas_events = (function() {
         evt.preventDefault();
     });
 
+    // Cut event
+    document.addEventListener("cut", function(evt) {
+        // This event doesn't currently get called because the
+        // nw menubar receives the event and doesn't propagate
+        // to the DOM. But if we add the ability to toggle menubar
+        // display, we might need to rely on this listener.
+        pdgui.post("cut detected by DOM listener");
+        pdgui.pdsend(name, "cut");
+    });
+
+    // Copy event
+    document.addEventListener("copy", function(evt) {
+        // This event doesn't currently get called because the
+        // nw menubar receives the event and doesn't propagate
+        // to the DOM. But if we add the ability to toggle menubar
+        // display, we might need to rely on this listener.
+        pdgui.post("copy detected by DOM listener");
+        pdgui.pdsend(name, "copy");
+    });
+
     // Listen to paste event using the half-baked Clipboard API from HTML5
     document.addEventListener("paste", function(evt) {
         var clipboard_data = evt.clipboardData.getData("text"),
diff --git a/pd/nw/pd_menus.js b/pd/nw/pd_menus.js
index d0aa7af9c..2924f5ada 100644
--- a/pd/nw/pd_menus.js
+++ b/pd/nw/pd_menus.js
@@ -122,16 +122,28 @@ function create_menu(gui, type) {
         editMenu.append(new gui.MenuItem({ type: "separator" }));
         editMenu.append(m.edit.cut = new gui.MenuItem({
             label: l("menu.cut"),
+            key: "x",
+            modifiers: cmd_or_ctrl,
             tooltip: l("menu.cut_tt")
         }));
     }
     editMenu.append(m.edit.copy = new gui.MenuItem({
         label: l("menu.copy"),
+        key: "c",
+        modifiers: cmd_or_ctrl,
         tooltip: l("menu.copy_tt")
     }));
     if (canvas_menu) {
+        // The nwjs menubar keybindings don't propagate down
+        // to the DOM. Here, we need the DOM to receive the
+        // "paste" event in case we want to paste a Pd file
+        // from an external buffer. So unfortunately we can't
+        // do the keybindings here. The side-effect is that
+        // "Ctrl-V" isn't displayed in the menu item.
         editMenu.append(m.edit.paste = new gui.MenuItem({
             label: l("menu.paste"),
+            //key: "v",
+            //modifiers: cmd_or_ctrl,
             tooltip: l("menu.paste_tt")
         }));
         editMenu.append(m.edit.duplicate = new gui.MenuItem({
@@ -143,6 +155,8 @@ function create_menu(gui, type) {
     }
     editMenu.append(m.edit.selectall = new gui.MenuItem({
         label: l("menu.selectall"),
+        key: "a",
+        modifiers: cmd_or_ctrl,
         tooltip: l("menu.selectall_tt")
     }));
     if (canvas_menu) {
-- 
GitLab