From f14389a462d87ea47d02166ba42dc15345f09f33 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Thu, 16 Feb 2023 07:41:08 -0500 Subject: [PATCH] refactor b/c uri handlers not supported on Windows or Linux --- app/src/processing/app/Base.java | 29 +++++++++++++++++ .../app/platform/DefaultPlatform.java | 31 ------------------- .../processing/app/platform/MacPlatform.java | 10 ++++++ todo.txt | 1 + 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 3eaad41d0..8a50023c1 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1361,6 +1361,35 @@ public class Base { } + /** + * Handling pde:// URLs + * @param location everything after pde:// + */ + public void handleLocation(String location) { + // if it leads with a slash, then it's a file url + if (location.charAt(0) == '/') { + handleOpen(location); // it's a full path to a file + } else { + // turn it into an https url + final String url = "https://" + location; + if (location.toLowerCase().endsWith(".pdez") || + location.toLowerCase().endsWith(".pdex")) { + String extension = location.substring(location.length() - 5); + try { + File tempFile = File.createTempFile("scheme", extension); + if (PApplet.saveStream(tempFile, Util.createInput(url))) { + handleOpen(tempFile.getAbsolutePath()); + } else { + System.err.println("Could not open " + tempFile); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** * Open a sketch from the path specified. Do not use for untitled sketches. * Note that the user may have selected/double-clicked any .pde in a sketch. diff --git a/app/src/processing/app/platform/DefaultPlatform.java b/app/src/processing/app/platform/DefaultPlatform.java index 89931da84..ec0a0a874 100644 --- a/app/src/processing/app/platform/DefaultPlatform.java +++ b/app/src/processing/app/platform/DefaultPlatform.java @@ -98,37 +98,6 @@ public class DefaultPlatform { public void initBase(Base base) { this.base = base; - - final Desktop desktop = Desktop.getDesktop(); - desktop.setOpenURIHandler((event) -> { - // https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/URI.html - URI uri = event.getURI(); - String location = uri.toString().substring(6); - if (location.length() > 0) { - System.out.println("location is " + location); - // if it leads with a slash, then it's a file url - if (location.charAt(0) == '/') { - base.handleOpen(location); // it's a full path to a file - } else { - // turn it into an https url - final String url = "https://" + location; - if (location.toLowerCase().endsWith(".pdez") || - location.toLowerCase().endsWith(".pdex")) { - String extension = location.substring(location.length() - 5); - try { - File tempFile = File.createTempFile("scheme", extension); - if (PApplet.saveStream(tempFile, Util.createInput(url))) { - base.handleOpen(tempFile.getAbsolutePath()); - } else { - System.err.println("Could not open " + tempFile); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - }); } diff --git a/app/src/processing/app/platform/MacPlatform.java b/app/src/processing/app/platform/MacPlatform.java index f4f56525e..d834c0c98 100644 --- a/app/src/processing/app/platform/MacPlatform.java +++ b/app/src/processing/app/platform/MacPlatform.java @@ -33,6 +33,7 @@ import javax.swing.JMenuBar; import processing.app.Base; import processing.app.Messages; +import processing.app.Util; import processing.app.ui.About; import processing.core.PApplet; import processing.data.StringList; @@ -91,6 +92,15 @@ public class MacPlatform extends DefaultPlatform { quitResponse.cancelQuit(); } }); + + desktop.setOpenURIHandler((event) -> { + // https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/URI.html + URI uri = event.getURI(); + String location = uri.toString().substring(6); + if (location.length() > 0) { + base.handleLocation(location); + } + }); } diff --git a/todo.txt b/todo.txt index 408b38dd7..3ced6d41a 100755 --- a/todo.txt +++ b/todo.txt @@ -6,6 +6,7 @@ _ https://github.com/processing/processing4/issues/666 _ add a protocol handler for pdex and pdez _ https://github.com/processing/processing4/issues/559 X implemented for macOS +_ Windows implementation https://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/?id=94914f2852f0c32e60361648ec82da153b8d70ae _ need to also warn people about running 'em