From 2636beca4ab44171eed28dffca2b442193c80fab Mon Sep 17 00:00:00 2001 From: Roland Sadowski Date: Fri, 4 Sep 2009 21:41:07 +0200 Subject: [PATCH] run takes :interactive, bind-applets is private --- src/rosado/processing/applet.clj | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/rosado/processing/applet.clj b/src/rosado/processing/applet.clj index 3559769..0967e81 100644 --- a/src/rosado/processing/applet.clj +++ b/src/rosado/processing/applet.clj @@ -1,38 +1,44 @@ (ns rosado.processing.applet - (:use [rosado.processing] - [clojure.contrib.java-utils :only [as-str]]) - (:import (java.awt Frame))) + (:use [rosado.processing]) + (:import (javax.swing JFrame))) -(defn bind-applets +(defn- bind-applets "Turn the method map into something one that update-proxy can use." [methods [method-name f]] (assoc methods (name method-name) - (fn [this & args] - (binding [*applet* this] - (apply f args))))) + `(fn [this# & args#] + (binding [*applet* this#] + (apply ~f args#))))) (defmacro defapplet "Define an applet. Takes an app-name and a map of options." [app-name & opts] (let [options (assoc (apply hash-map opts) :name (str app-name)) - fns (dissoc options :name :title :height :width)] + fns (dissoc options :name :title :height :width) + methods (reduce bind-applets {} fns)] `(def ~app-name (let [frame# (atom nil) - methods# (reduce bind-applets {} ~fns) prx# (proxy [processing.core.PApplet clojure.lang.IMeta] [] (meta [] (assoc ~options :frame frame#)))] - (update-proxy prx# methods#) + (update-proxy prx# ~methods) prx#)))) -(defn run [applet] +(defn run + "Launches the applet. If given the flag :interactive, it won't exit + on clicking the close button - it will only dispose the window." + [applet & interactive?] (.init applet) (let [m (.meta applet) width (or (:width m) 200) - height (or (:height m) 200)] + height (or (:height m) 200) + close-op (if (first interactive?) + JFrame/DISPOSE_ON_CLOSE + JFrame/EXIT_ON_CLOSE)] (.size applet width height) (reset! (:frame m) - (doto (Frame. (or (:title m) (:name m))) + (doto (JFrame. (or (:title m) (:name m))) + (.setDefaultCloseOperation close-op) (.setSize width height) (.add applet) (.pack)