From 940fa5410fb49b29681317e8a1e12ed42586ff12 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Wed, 15 Feb 2023 22:33:34 -0500 Subject: [PATCH] start work on protocol handler; first on macOS --- .../app/platform/DefaultPlatform.java | 34 +++++++++++++++++++ build/build.xml | 4 +++ todo.txt | 7 ++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/app/src/processing/app/platform/DefaultPlatform.java b/app/src/processing/app/platform/DefaultPlatform.java index c2b858093..89931da84 100644 --- a/app/src/processing/app/platform/DefaultPlatform.java +++ b/app/src/processing/app/platform/DefaultPlatform.java @@ -26,6 +26,8 @@ package processing.app.platform; import java.awt.Desktop; import java.awt.Font; import java.io.File; +import java.io.IOException; +import java.net.URI; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; @@ -37,6 +39,7 @@ import com.sun.jna.Native; import processing.app.Base; import processing.app.Preferences; +import processing.app.Util; import processing.app.ui.Toolkit; import processing.awt.ShimAWT; import processing.core.PApplet; @@ -95,6 +98,37 @@ 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/build/build.xml b/build/build.xml index 0591b5f82..e78441953 100644 --- a/build/build.xml +++ b/build/build.xml @@ -571,6 +571,10 @@ role="Viewer"> + + + + diff --git a/todo.txt b/todo.txt index c80517c03..408b38dd7 100755 --- a/todo.txt +++ b/todo.txt @@ -3,6 +3,11 @@ X application exported to the wrong folder X https://github.com/processing/processing4/issues/601 X more /tmp folder problems with Linux (may be the subdirectories?) _ 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 +_ need to also warn people about running 'em + python (api changes mean this is 4.2) X move ExportDialog to processing.app.ui (out of Java Mode) so it can be used by Jython @@ -31,8 +36,6 @@ _ export to IntelliJ? how tricky? _ just copy jars to /lib? _ point to binaries in /Applications/Processing.app? (no) -_ add a protocol handler for pdex and pdez -_ https://github.com/processing/processing4/issues/559 known issues