changeset 175:eb1bedf22731

Marked transaction-unsafe functions (this also solves a name collision with Clojure 1.7.0)
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 18 Sep 2015 04:03:38 +0300
parents f4b82e358751
children 6a29fcb6fe9a
files src/indyvon/async.clj src/indyvon/component.clj src/indyvon/core.clj src/indyvon/demo.clj src/indyvon/viewport.clj src/indyvon/views.clj
diffstat 6 files changed, 69 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/src/indyvon/async.clj	Wed Dec 10 20:01:45 2014 +0300
+++ b/src/indyvon/async.clj	Fri Sep 18 04:03:38 2015 +0300
@@ -1,5 +1,5 @@
 ;;
-;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
+;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
 ;;
 ;; This file is part of Indyvon.
 ;;
@@ -81,7 +81,7 @@
   (when (-> (:buffers av)
             (swap! apply-flip update-in [:front-readers] dec)
             :flipped)
-    (update av)))
+    (update! av)))
 
 (defn- take-back! [av]
   (-> (:buffers av)
@@ -92,7 +92,7 @@
   (when (-> (:buffers av)
             (swap! apply-flip assoc :back-ready true)
             :flipped)
-    (update av)))
+    (update! av)))
 
 (defn- draw-offscreen! [async-view]
   ;;(Thread/sleep 1000)
@@ -121,10 +121,10 @@
                                (create-buffers async-view device-conf)))))
       (let [bound-draw! (bound-fn* draw-offscreen!)
             async-draw! #(pool-execute! % (partial bound-draw! %))]
-        (add-observer async-view
-                      (:scene async-view)
-                      ;; Must not hold onto async-view.
-                      (fn [a _] (async-draw! a)))
+        (add-observer! async-view
+                       (:scene async-view)
+                       ;; Must not hold onto async-view.
+                       (fn [a _] (async-draw! a)))
         (async-draw! async-view)))))
 
 (defrecord AsyncView [scene width height executor buffers]
@@ -137,7 +137,7 @@
           (.drawImage g b 0 0 nil)
           (finally
             (release-front! view))))
-      (repaint-on-update view)))
+      (repaint-on-update! view)))
   (geometry [view]
     (->Size width height)))
 
--- a/src/indyvon/component.clj	Wed Dec 10 20:01:45 2014 +0300
+++ b/src/indyvon/component.clj	Fri Sep 18 04:03:38 2015 +0300
@@ -1,5 +1,5 @@
 ;;
-;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
+;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
 ;;
 ;; This file is part of Indyvon.
 ;;
@@ -52,12 +52,12 @@
         {"paintComponent" #(paint-component %1 %2 scene)
          "getPreferredSize" #(preferred-size % scene)})
        (.setBackground panel (theme-get :back-color))
-       (add-observer panel scene (fn [w _]
-                                   ;; Use the first observer argument
-                                   ;; instead of closing over panel to
-                                   ;; allow the panel and associated
-                                   ;; observer to be gc'd.
-                                   (.repaint ^Component w)))
+       (add-observer! panel scene (fn [w _]
+                                    ;; Use the first observer argument
+                                    ;; instead of closing over panel to
+                                    ;; allow the panel and associated
+                                    ;; observer to be gc'd.
+                                    (.repaint ^Component w)))
        (listen! event-dispatcher panel)
        panel)))
 
--- a/src/indyvon/core.clj	Wed Dec 10 20:01:45 2014 +0300
+++ b/src/indyvon/core.clj	Fri Sep 18 04:03:38 2015 +0300
@@ -1,5 +1,5 @@
 ;;
-;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
+;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
 ;;
 ;; This file is part of Indyvon.
 ;;
@@ -275,8 +275,8 @@
 ;; The mechanism used by views to request repaints
 ;;
 
-(def ^ConcurrentMap observers
-     (-> (MapMaker.) (.weakKeys) (.makeMap)))
+(def ^:private ^ConcurrentMap observers
+  (-> (MapMaker.) (.weakKeys) (.makeMap)))
 
 (defn- cm-replace!
   "Wrap ConcurrentMap replace method to treat nil value as absent
@@ -297,59 +297,59 @@
         new
         (recur)))))
 
-(defn add-observer
-  "Add observer fn for the target. Watcher identifies the group of
+(defn add-observer!
+  "Add observer fn for the subject. Watcher identifies the group of
   observers and could be used to remove the group. Watcher is weakly
   referenced, all associated observers will be removed when the
   wathcer is removed by gc. The observer fn will be called with
-  watcher and target arguments and any additional arguments specified
-  in update call."
-  [watcher target f]
-  (cm-swap! observers watcher assoc-cons target f)
+  watcher and subject arguments and any additional arguments that was
+  passed to the update! function."
+  [watcher subject f]
+  (cm-swap! observers watcher assoc-cons subject f)
   nil)
 
-(defn remove-observers
+(defn remove-observers!
   "Remove group of observers associated with the specified watcher."
   [watcher]
   (.remove observers watcher)
   nil)
 
-(defn- replace-observers-watcher
+(defn- replace-observers-watcher!
   [old-watcher new-watcher]
   (if-let [old (.remove observers old-watcher)]
     (.put observers new-watcher old))
   nil)
 
-(defn update
-  "Notify observers."
-  [target & args]
+(defn update!
+  [subject & args]
   (doseq [entry observers
-          f (get (val entry) target)]
-    (apply f (key entry) target args)))
+          f (get (val entry) subject)]
+    (apply f (key entry) subject args)))
 
-(defn add-context-observer
+(defn add-context-observer!
   "Observer registered with this function will be automatically
   removed after the next repaint is complete."
-  [target f]
-  (add-observer *scene* target f))
+  [subject f]
+  (add-observer! *scene* subject f))
 
-(defn repaint-on-update
-  "Trigger repaint of the current scene when the target updates."
-  [target]
+(defn repaint-on-update!
+  "Trigger repaint of the current scene when update! is
+  called with the subject."
+  [subject]
   (let [scene *scene*]
-    (if-not (identical? scene target)
-      (add-observer scene target (fn [w _] (update w))))))
+    (if-not (identical? scene subject)
+      (add-observer! scene subject (fn [w _] (update! w))))))
 
-(defn repaint
+(defn repaint!
   "Requests repaint of the current scene. If handle and state are
   specified, the handle will be associated with the state in the
   *states* map for the next paint iteration."
   ([]
-     (update *scene*))
+     (update! *scene*))
   ([handle state]
      (let [scene *scene*]
        (swap! (:next-state scene) assoc handle state)
-       (update scene))))
+       (update! scene))))
 
 (defmacro binding-fast
   "Faster alternative to core/binding. Works only with vars that are
@@ -943,11 +943,11 @@
       ;; Keep current context observers until the rendering is
       ;; complete. Some observers may be invoked twice if they
       ;; appear in both groups until tmp-watcher is removed.
-      (replace-observers-watcher scene tmp-watcher)
+      (replace-observers-watcher! scene tmp-watcher)
       (try
         (render! (:view scene))
         (finally
-         (remove-observers tmp-watcher)
+         (remove-observers! tmp-watcher)
          (commit (:event-dispatcher scene)))))))
 
 (defn- scene-font-context [scene]
--- a/src/indyvon/demo.clj	Wed Dec 10 20:01:45 2014 +0300
+++ b/src/indyvon/demo.clj	Fri Sep 18 04:03:38 2015 +0300
@@ -1,5 +1,5 @@
 ;;
-;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
+;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
 ;;
 ;; This file is part of Indyvon.
 ;;
@@ -46,10 +46,10 @@
       (draw! (border border-width padding content)
              offset offset width height))
     ;; Event handlers
-    [:mouse-entered _ (repaint)]
-    [:mouse-exited _ (repaint)]
-    [:mouse-pressed _ (repaint)]
-    [:mouse-released _ (repaint)]
+    [:mouse-entered _ (repaint!)]
+    [:mouse-exited _ (repaint!)]
+    [:mouse-pressed _ (repaint!)]
+    [:mouse-released _ (repaint!)]
     [:mouse-clicked _ (apply callback args)]))
 
 (defn combine-colors
@@ -66,7 +66,7 @@
 
 (defn animate
   "Changes the value of atom according to the specified range, speed,
-   and current frame interval.  Invokes repaint if change happens."
+   and current frame interval.  Invokes repaint! if change happens."
   [atom from to speed]
   (let [prev @atom
         state (cond
@@ -79,7 +79,7 @@
        (let [interval (if (= state :start) 1 *interval*)
              step (* speed interval 1E-9)
              val (swap! atom #(-> % (+ step) (max from) (min to)))]
-         (repaint)
+         (repaint!)
          val))))
 
 (defn animated-button
@@ -114,12 +114,12 @@
           ;; Event handlers
           [:mouse-entered _
            (reset! animation-speed 4)
-           (repaint)]
+           (repaint!)]
           [:mouse-exited _
            (reset! animation-speed -2)
-           (repaint)]
-          [:mouse-pressed _ (repaint)]
-          [:mouse-released _ (repaint)]
+           (repaint!)]
+          [:mouse-pressed _ (repaint!)]
+          [:mouse-released _ (repaint!)]
           [:mouse-clicked _ (apply callback args)]))
      (geometry [button]
        (let [face-geom (geometry face)]
@@ -140,10 +140,10 @@
        (with-color (if (hovered? view) Color/ORANGE Color/RED)
          (.fillRect *graphics* 0 0 *width* *height*))
        [:mouse-entered e
-        (repaint)
+        (repaint!)
         (println e)]
        [:mouse-exited e
-        (repaint)
+        (repaint!)
         (println e)]
        [:mouse-moved e
         (println e)]))
@@ -173,7 +173,6 @@
   (reify
    View
    (render! [view]
-     ;;(repaint)
      (doto *graphics*
        (.drawLine 0 0 *width* *height*)
        (.drawLine *width* 0 0 *height*)
--- a/src/indyvon/viewport.clj	Wed Dec 10 20:01:45 2014 +0300
+++ b/src/indyvon/viewport.clj	Fri Sep 18 04:03:38 2015 +0300
@@ -1,5 +1,5 @@
 ;;
-;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
+;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
 ;;
 ;; This file is part of Indyvon.
 ;;
@@ -82,7 +82,7 @@
 (defrecord Viewport [content h-align v-align state]
   View
   (render! [view]
-    (repaint-on-update view)
+    (repaint-on-update! view)
     (with-handlers view
       (let [geom (geometry content)
             new-state (swap! state update-viewport geom h-align v-align)
@@ -109,7 +109,7 @@
                               (- (:y-on-screen e) (:fix-y s)))
                   :fix-x (:x-on-screen e)
                   :fix-y (:y-on-screen e))))
-       (update view)]
+       (update! view)]
       [:mouse-wheel e
        (scale-viewport!
         view
@@ -160,11 +160,11 @@
      (scale-viewport! viewport s relative? nil nil))
   ([viewport s relative? x y]
      (swap! (:state viewport) scale-viewport viewport s relative? x y)
-     (update viewport)))
+     (update! viewport)))
 
 (defn reset-viewport! [viewport]
   (reset! (:state viewport) viewport-initial-state)
-  (update viewport))
+  (update! viewport))
 
 (defn ^AffineTransform viewport-transform [viewport]
   (:transform @(:state viewport)))
@@ -200,7 +200,7 @@
   [m-width m-height viewport]
   (let [miniature (miniature m-width m-height (:content viewport))]
     (decorate-view miniature [l]
-      (repaint-on-update viewport)
+      (repaint-on-update! viewport)
       (let [geom (geometry (:content viewport))
             s (scaling (width geom) (height geom) m-width m-height)
             vp-state @(:state viewport)
@@ -223,7 +223,7 @@
                           :transform (pre-translate tr (- x) (- y)))))
             move-vp! (fn [x y]
                        (swap! (:state viewport) move-vp x y)
-                       (update viewport))]
+                       (update! viewport))]
         (with-color :alt-back-color
           (.fillRect *graphics* 0 0 *width* *height*))
         (with-transform transform
--- a/src/indyvon/views.clj	Wed Dec 10 20:01:45 2014 +0300
+++ b/src/indyvon/views.clj	Fri Sep 18 04:03:38 2015 +0300
@@ -1,5 +1,5 @@
 ;;
-;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
+;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
 ;;
 ;; This file is part of Indyvon.
 ;;
@@ -272,7 +272,7 @@
   (reify
    ImageObserver
    (imageUpdate [this img infoflags x y width height]
-     (update view)
+     (update! view)
      (zero? (bit-and infoflags
                      (bit-or ImageObserver/ALLBITS
                              ImageObserver/ABORT))))))
@@ -287,7 +287,7 @@
     (reify
      View
      (render! [view]
-       (repaint-on-update view)
+       (repaint-on-update! view)
        (.drawImage *graphics* image 0 0 (image-observer view)))
      (geometry [view]
        (let [observer (image-observer view)
@@ -306,14 +306,14 @@
    (let [v (reify
              View
              (render! [v]
-               (repaint-on-update v)
+               (repaint-on-update! v)
                (when-let [view (view-fn @view-ref)]
                  (render! view)))
              (geometry [_]
                (if-let [view (view-fn @view-ref)]
                  (geometry view)
                  (->Size 1 1))))]
-     (add-watch view-ref v (fn [_ _ _ _] (update v)))
+     (add-watch view-ref v (fn [_ _ _ _] (update! v)))
      v)))
 
 ;;