From ab317b2ebd4ad93f0057a7039b794fbd08ee0614 Mon Sep 17 00:00:00 2001 From: Jonathan Wilkes <jon.w.wilkes@gmail.com> Date: Mon, 16 Jul 2018 22:54:24 -0400 Subject: [PATCH] move global events and global state attributes to an "init" method inside canvas_events --- pd/nw/pd_canvas.js | 360 +++++++++++++++++++++++---------------------- 1 file changed, 183 insertions(+), 177 deletions(-) diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js index 570b20e99..e0eadc449 100644 --- a/pd/nw/pd_canvas.js +++ b/pd/nw/pd_canvas.js @@ -11,14 +11,6 @@ pdgui.skin.apply(window); var l = pdgui.get_local_string; -console.log("my working dir is " + pdgui.get_pwd()); -document.getElementById("saveDialog") - .setAttribute("nwworkingdir", pdgui.get_pwd()); -document.getElementById("fileDialog") - .setAttribute("nwworkingdir", pdgui.get_pwd()); -document.getElementById("fileDialog").setAttribute("accept", - Object.keys(pdgui.pd_filetypes).toString()); - function close_save_dialog() { document.getElementById("save_before_quit").close(); } @@ -153,7 +145,7 @@ function canvas_find_reset() { var canvas_events = (function() { var name, state, - scalar_draggables = {}, // elements of a scalar which have the "drag" event enabled + scalar_draggables = {}, // scalar child with the "drag" event enabled draggable_elem, // last scalar we dragged draggable_label, // kluge for handling [cnv] label/size anchors last_draggable_x, // last x coord for the element we're dragging @@ -683,174 +675,6 @@ var canvas_events = (function() { } ; - // Dialog events -- these are set elsewhere now because of a bug - // with nwworkingdir - document.querySelector("#saveDialog").addEventListener("change", - function(evt) { - pdgui.saveas_callback(name, evt.target.value, 0); - // reset value so that we can open the same file twice - evt.target.value = null; - console.log("tried to save something"); - }, false - ); - - // Whoa-- huge workaround! - // Right now we're getting the popup menu the way Pd Vanilla does it: - // 1) send a mouse(down) message to Pd - // 2) Pd checks whether it wants to send us a popup - // 3) Pd checks what popup menu items are available for this object/canvas - // 4) Pd sends GUI back a message with this info - // 5) GUI finally displays the popup - // 6) GUI keeps a _global_ _variable_ to remember where the popup coords - // 7) User clicks an option in the popup - // 8) GUI sends a message back to Pd with the popup index and coords - // 9) Pd walks the linked list of objects to look up the object - // 10) Pd asks that object if it reacts to popups, and if it reacts to the - // selected item in the popup - // 11) Pd sends a message to the relevant object for the item in question - // nw.js has a nice little "contextmenu" event handler, but it's too - // difficult to use when we're passing between GUI and Pd (twice). In the - // future we should just do all the popup menu event handling in the GUI, - // and only pass a message to Pd when the user has clicked an item. - // For now, however, we just turn off its default behavior and control - // it with a bunch of complicated callbacks. :( - document.addEventListener("contextmenu", function(evt) { - console.log("got a context menu evt..."); - 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.pdsend(name, "cut"); - }); - - // Copy event - document.addEventListener("copy", function(evt) { - // On OSX, this event gets triggered when we're editing - // inside an object/message box. So we only forward the - // copy message to Pd if we're in a "normal" canvas state - if (canvas_events.get_state() === "normal") { - pdgui.pdsend(name, "copy"); - } - }); - - // Listen to paste event - // XXXTODO: Not sure whether this is even needed any more, as the - // paste-from-clipboard functionality has been moved to its own menu - // option. So this code may possibly be removed in the future. -ag - document.addEventListener("paste", function(evt) { - if (canvas_events.get_state() !== "normal") { - return; - } - // Send a canvas "paste" message to Pd - pdgui.pdsend(name, "paste"); - }); - - // MouseWheel event for zooming - document.addEventListener("wheel", function(evt) { - var d = { deltaX: 0, deltaY: 0, deltaZ: 0 }; - Object.keys(d).forEach(function(key) { - if (evt[key] < 0) { - d[key] = -1; - } else if (evt[key] > 0) { - d[key] = 1; - } else { - d[key] = 0; - } - }); - if (pdgui.cmd_or_ctrl_key(evt)) { - // scroll up for zoom-in, down for zoom-out - nw_window_zoom(name, -d.deltaY); - } - // Send a message on to Pd for the [mousewheel] legacy object - // (in the future we can refcount if we want to prevent forwarding - // these messages when there's no extant receiver) - pdgui.pdsend(name, "legacy_mousewheel", d.deltaX, d.deltaY, d.deltaZ); - }); - - // The following is commented out because we have to set the - // event listener inside nw_create_pd_window_menus due to a - // bug with nwworkingdir - - //document.querySelector("#fileDialog").addEventListener("change", - // function(evt) { - // var file_array = this.value; - // // reset value so that we can open the same file twice - // this.value = null; - // pdgui.menu_open(file_array); - // console.log("tried to open something\n\n\n\n\n\n\n\n"); - // }, false - //); - document.querySelector("#openpanel_dialog").addEventListener("change", - function(evt) { - var file_string = evt.target.value; - // reset value so that we can open the same file twice - evt.target.value = null; - pdgui.file_dialog_callback(file_string); - console.log("tried to openpanel something"); - }, false - ); - document.querySelector("#savepanel_dialog").addEventListener("change", - function(evt) { - var file_string = evt.target.value; - // reset value so that we can open the same file twice - evt.target.value = null; - pdgui.file_dialog_callback(file_string); - console.log("tried to savepanel something"); - }, false - ); - document.querySelector("#canvas_find_text").addEventListener("focusin", - canvas_find_focus, false - ); - - // disable drag and drop for the time being - window.addEventListener("dragover", function (evt) { - evt.preventDefault(); - }, false); - window.addEventListener("drop", function (evt) { - evt.preventDefault(); - }, false); - - // Add placeholder text... this all needs to be collected into an - // add_events function similiar to the one in index.js - document.querySelector("#canvas_find_text").placeholder = - l("canvas.find.placeholder"); - document.querySelector("#canvas_find_text").addEventListener("blur", - canvas_find_blur, false - ); - document.querySelector("#canvas_find_button").addEventListener("click", - events.find_click); - // We need to separate these into nw_window events and html5 DOM events - // closing the Window this isn't actually closing the window yet - gui.Window.get().on("close", function() { - pdgui.pdsend(name, "menuclose 0"); - }); - // update viewport size when window size changes - gui.Window.get().on("maximize", function() { - pdgui.gui_canvas_get_scroll(name); - }); - gui.Window.get().on("unmaximize", function() { - pdgui.gui_canvas_get_scroll(name); - }); - gui.Window.get().on("resize", function() { - pdgui.gui_canvas_get_scroll(name); - }); - gui.Window.get().on("focus", function() { - nw_window_focus_callback(name); - }); - gui.Window.get().on("blur", function() { - nw_window_blur_callback(name); - }); - gui.Window.get().on("move", function(x, y) { - var w = gui.Window.get(); - pdgui.pdsend(name, "setbounds", x, y, x + w.width, y + w.height); - }); - // set minimum window size - gui.Window.get().setMinimumSize(150, 100); return { none: function() { @@ -985,6 +809,186 @@ var canvas_events = (function() { close_without_saving: function(cid, force) { pdgui.pdsend(name, "dirty 0"); pdgui.pdsend(cid, "menuclose", force); + }, + init: function() { + document.getElementById("saveDialog") + .setAttribute("nwworkingdir", pdgui.get_pwd()); + document.getElementById("fileDialog") + .setAttribute("nwworkingdir", pdgui.get_pwd()); + document.getElementById("fileDialog").setAttribute("accept", + Object.keys(pdgui.pd_filetypes).toString()); + // Dialog events -- these are set elsewhere now because of a bug + // with nwworkingdir + document.querySelector("#saveDialog").addEventListener("change", + function(evt) { + pdgui.saveas_callback(name, evt.target.value, 0); + // reset value so that we can open the same file twice + evt.target.value = null; + console.log("tried to save something"); + }, false + ); + // Whoa-- huge workaround! Right now we're getting + // the popup menu the way Pd Vanilla does it: + // 1) send a mouse(down) message to Pd + // 2) Pd checks whether it wants to send us a popup + // 3) Pd checks what popup menu items are available for obj/canvas + // 4) Pd sends GUI back a message with this info + // 5) GUI finally displays the popup + // 6) GUI keeps a _global_ _variable_ to remember the popup coords + // 7) User clicks an option in the popup + // 8) GUI sends a message back to Pd with the popup index and coords + // 9) Pd walks the linked list of objects to look up the object + // 10) Pd asks the object if it reacts to popups, and if it reacts + // to the selected item in the popup + // 11) Pd sends a message to the relevant object for the item in + // question + // nw.js has a nice little "contextmenu" event handler, but it's too + // difficult to use when passing between GUI and Pd (twice). In the + // future we should do all popup menu event handling in the GUI, + // and only pass a message to Pd when the user has clicked an item. + // For now, however, we just turn off its default behavior and + // control it with a bunch of complicated callbacks. + document.addEventListener("contextmenu", function(evt) { + console.log("got a context menu evt..."); + 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.pdsend(name, "cut"); + }); + + // Copy event + document.addEventListener("copy", function(evt) { + // On OSX, this event gets triggered when we're editing + // inside an object/message box. So we only forward the + // copy message to Pd if we're in a "normal" canvas state + if (canvas_events.get_state() === "normal") { + pdgui.pdsend(name, "copy"); + } + }); + + // Listen to paste event + // XXXTODO: Not sure whether this is even needed any more, as the + // paste-from-clipboard functionality has been moved to its own menu + // option. So this code may possibly be removed in the future. -ag + document.addEventListener("paste", function(evt) { + if (canvas_events.get_state() !== "normal") { + return; + } + // Send a canvas "paste" message to Pd + pdgui.pdsend(name, "paste"); + }); + + // MouseWheel event for zooming + document.addEventListener("wheel", function(evt) { + var d = { deltaX: 0, deltaY: 0, deltaZ: 0 }; + Object.keys(d).forEach(function(key) { + if (evt[key] < 0) { + d[key] = -1; + } else if (evt[key] > 0) { + d[key] = 1; + } else { + d[key] = 0; + } + }); + if (pdgui.cmd_or_ctrl_key(evt)) { + // scroll up for zoom-in, down for zoom-out + nw_window_zoom(name, -d.deltaY); + } + // Send a message on to Pd for the [mousewheel] legacy object + // (in the future we can refcount to prevent forwarding + // these messages when there's no extant receiver) + pdgui.pdsend(name, "legacy_mousewheel", + d.deltaX, d.deltaY, d.deltaZ); + }); + + // The following is commented out because we have to set the + // event listener inside nw_create_pd_window_menus due to a + // bug with nwworkingdir + + //document.querySelector("#fileDialog").addEventListener("change", + // function(evt) { + // var file_array = this.value; + // // reset value so that we can open the same file twice + // this.value = null; + // pdgui.menu_open(file_array); + // console.log("tried to open something\n\n\n\n\n\n\n\n"); + // }, false + //); + document.querySelector("#openpanel_dialog") + .addEventListener("change", function(evt) { + var file_string = evt.target.value; + // reset value so that we can open the same file twice + evt.target.value = null; + pdgui.file_dialog_callback(file_string); + console.log("tried to openpanel something"); + }, false + ); + document.querySelector("#savepanel_dialog") + .addEventListener("change", function(evt) { + var file_string = evt.target.value; + // reset value so that we can open the same file twice + evt.target.value = null; + pdgui.file_dialog_callback(file_string); + console.log("tried to savepanel something"); + }, false + ); + document.querySelector("#canvas_find_text") + .addEventListener("focusin", canvas_find_focus, false + ); + + // disable drag and drop for the time being + window.addEventListener("dragover", function (evt) { + evt.preventDefault(); + }, false); + window.addEventListener("drop", function (evt) { + evt.preventDefault(); + }, false); + + // Add placeholder text... this all needs to be collected into an + // add_events function similiar to the one in index.js + document.querySelector("#canvas_find_text").placeholder = + l("canvas.find.placeholder"); + document.querySelector("#canvas_find_text").addEventListener("blur", + canvas_find_blur, false + ); + document.querySelector("#canvas_find_button") + .addEventListener("click", events.find_click + ); + // We need to separate these into nw_window events and html5 DOM + // events closing the Window this isn't actually closing the window + // yet + gui.Window.get().on("close", function() { + pdgui.pdsend(name, "menuclose 0"); + }); + // update viewport size when window size changes + gui.Window.get().on("maximize", function() { + pdgui.gui_canvas_get_scroll(name); + }); + gui.Window.get().on("unmaximize", function() { + pdgui.gui_canvas_get_scroll(name); + }); + gui.Window.get().on("resize", function() { + pdgui.gui_canvas_get_scroll(name); + }); + gui.Window.get().on("focus", function() { + nw_window_focus_callback(name); + }); + gui.Window.get().on("blur", function() { + nw_window_blur_callback(name); + }); + gui.Window.get().on("move", function(x, y) { + var w = gui.Window.get(); + pdgui.pdsend(name, "setbounds", x, y, + x + w.width, y + w.height); + }); + // set minimum window size + gui.Window.get().setMinimumSize(150, 100); } }; }()); @@ -1022,6 +1026,8 @@ function register_window_id(cid, attr_array) { } create_popup_menu(cid); canvas_events.register(cid); + // Initialize global DOM state/events + canvas_events.init(document); translate_form(); // Trigger a "focus" event so that OSX updates the menu for this window nw_window_focus_callback(cid); -- GitLab