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 diff
     1.1 --- a/src/indyvon/async.clj	Wed Dec 10 20:01:45 2014 +0300
     1.2 +++ b/src/indyvon/async.clj	Fri Sep 18 04:03:38 2015 +0300
     1.3 @@ -1,5 +1,5 @@
     1.4  ;;
     1.5 -;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
     1.6 +;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
     1.7  ;;
     1.8  ;; This file is part of Indyvon.
     1.9  ;;
    1.10 @@ -81,7 +81,7 @@
    1.11    (when (-> (:buffers av)
    1.12              (swap! apply-flip update-in [:front-readers] dec)
    1.13              :flipped)
    1.14 -    (update av)))
    1.15 +    (update! av)))
    1.16  
    1.17  (defn- take-back! [av]
    1.18    (-> (:buffers av)
    1.19 @@ -92,7 +92,7 @@
    1.20    (when (-> (:buffers av)
    1.21              (swap! apply-flip assoc :back-ready true)
    1.22              :flipped)
    1.23 -    (update av)))
    1.24 +    (update! av)))
    1.25  
    1.26  (defn- draw-offscreen! [async-view]
    1.27    ;;(Thread/sleep 1000)
    1.28 @@ -121,10 +121,10 @@
    1.29                                 (create-buffers async-view device-conf)))))
    1.30        (let [bound-draw! (bound-fn* draw-offscreen!)
    1.31              async-draw! #(pool-execute! % (partial bound-draw! %))]
    1.32 -        (add-observer async-view
    1.33 -                      (:scene async-view)
    1.34 -                      ;; Must not hold onto async-view.
    1.35 -                      (fn [a _] (async-draw! a)))
    1.36 +        (add-observer! async-view
    1.37 +                       (:scene async-view)
    1.38 +                       ;; Must not hold onto async-view.
    1.39 +                       (fn [a _] (async-draw! a)))
    1.40          (async-draw! async-view)))))
    1.41  
    1.42  (defrecord AsyncView [scene width height executor buffers]
    1.43 @@ -137,7 +137,7 @@
    1.44            (.drawImage g b 0 0 nil)
    1.45            (finally
    1.46              (release-front! view))))
    1.47 -      (repaint-on-update view)))
    1.48 +      (repaint-on-update! view)))
    1.49    (geometry [view]
    1.50      (->Size width height)))
    1.51  
     2.1 --- a/src/indyvon/component.clj	Wed Dec 10 20:01:45 2014 +0300
     2.2 +++ b/src/indyvon/component.clj	Fri Sep 18 04:03:38 2015 +0300
     2.3 @@ -1,5 +1,5 @@
     2.4  ;;
     2.5 -;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
     2.6 +;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
     2.7  ;;
     2.8  ;; This file is part of Indyvon.
     2.9  ;;
    2.10 @@ -52,12 +52,12 @@
    2.11          {"paintComponent" #(paint-component %1 %2 scene)
    2.12           "getPreferredSize" #(preferred-size % scene)})
    2.13         (.setBackground panel (theme-get :back-color))
    2.14 -       (add-observer panel scene (fn [w _]
    2.15 -                                   ;; Use the first observer argument
    2.16 -                                   ;; instead of closing over panel to
    2.17 -                                   ;; allow the panel and associated
    2.18 -                                   ;; observer to be gc'd.
    2.19 -                                   (.repaint ^Component w)))
    2.20 +       (add-observer! panel scene (fn [w _]
    2.21 +                                    ;; Use the first observer argument
    2.22 +                                    ;; instead of closing over panel to
    2.23 +                                    ;; allow the panel and associated
    2.24 +                                    ;; observer to be gc'd.
    2.25 +                                    (.repaint ^Component w)))
    2.26         (listen! event-dispatcher panel)
    2.27         panel)))
    2.28  
     3.1 --- a/src/indyvon/core.clj	Wed Dec 10 20:01:45 2014 +0300
     3.2 +++ b/src/indyvon/core.clj	Fri Sep 18 04:03:38 2015 +0300
     3.3 @@ -1,5 +1,5 @@
     3.4  ;;
     3.5 -;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
     3.6 +;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
     3.7  ;;
     3.8  ;; This file is part of Indyvon.
     3.9  ;;
    3.10 @@ -275,8 +275,8 @@
    3.11  ;; The mechanism used by views to request repaints
    3.12  ;;
    3.13  
    3.14 -(def ^ConcurrentMap observers
    3.15 -     (-> (MapMaker.) (.weakKeys) (.makeMap)))
    3.16 +(def ^:private ^ConcurrentMap observers
    3.17 +  (-> (MapMaker.) (.weakKeys) (.makeMap)))
    3.18  
    3.19  (defn- cm-replace!
    3.20    "Wrap ConcurrentMap replace method to treat nil value as absent
    3.21 @@ -297,59 +297,59 @@
    3.22          new
    3.23          (recur)))))
    3.24  
    3.25 -(defn add-observer
    3.26 -  "Add observer fn for the target. Watcher identifies the group of
    3.27 +(defn add-observer!
    3.28 +  "Add observer fn for the subject. Watcher identifies the group of
    3.29    observers and could be used to remove the group. Watcher is weakly
    3.30    referenced, all associated observers will be removed when the
    3.31    wathcer is removed by gc. The observer fn will be called with
    3.32 -  watcher and target arguments and any additional arguments specified
    3.33 -  in update call."
    3.34 -  [watcher target f]
    3.35 -  (cm-swap! observers watcher assoc-cons target f)
    3.36 +  watcher and subject arguments and any additional arguments that was
    3.37 +  passed to the update! function."
    3.38 +  [watcher subject f]
    3.39 +  (cm-swap! observers watcher assoc-cons subject f)
    3.40    nil)
    3.41  
    3.42 -(defn remove-observers
    3.43 +(defn remove-observers!
    3.44    "Remove group of observers associated with the specified watcher."
    3.45    [watcher]
    3.46    (.remove observers watcher)
    3.47    nil)
    3.48  
    3.49 -(defn- replace-observers-watcher
    3.50 +(defn- replace-observers-watcher!
    3.51    [old-watcher new-watcher]
    3.52    (if-let [old (.remove observers old-watcher)]
    3.53      (.put observers new-watcher old))
    3.54    nil)
    3.55  
    3.56 -(defn update
    3.57 -  "Notify observers."
    3.58 -  [target & args]
    3.59 +(defn update!
    3.60 +  [subject & args]
    3.61    (doseq [entry observers
    3.62 -          f (get (val entry) target)]
    3.63 -    (apply f (key entry) target args)))
    3.64 +          f (get (val entry) subject)]
    3.65 +    (apply f (key entry) subject args)))
    3.66  
    3.67 -(defn add-context-observer
    3.68 +(defn add-context-observer!
    3.69    "Observer registered with this function will be automatically
    3.70    removed after the next repaint is complete."
    3.71 -  [target f]
    3.72 -  (add-observer *scene* target f))
    3.73 +  [subject f]
    3.74 +  (add-observer! *scene* subject f))
    3.75  
    3.76 -(defn repaint-on-update
    3.77 -  "Trigger repaint of the current scene when the target updates."
    3.78 -  [target]
    3.79 +(defn repaint-on-update!
    3.80 +  "Trigger repaint of the current scene when update! is
    3.81 +  called with the subject."
    3.82 +  [subject]
    3.83    (let [scene *scene*]
    3.84 -    (if-not (identical? scene target)
    3.85 -      (add-observer scene target (fn [w _] (update w))))))
    3.86 +    (if-not (identical? scene subject)
    3.87 +      (add-observer! scene subject (fn [w _] (update! w))))))
    3.88  
    3.89 -(defn repaint
    3.90 +(defn repaint!
    3.91    "Requests repaint of the current scene. If handle and state are
    3.92    specified, the handle will be associated with the state in the
    3.93    *states* map for the next paint iteration."
    3.94    ([]
    3.95 -     (update *scene*))
    3.96 +     (update! *scene*))
    3.97    ([handle state]
    3.98       (let [scene *scene*]
    3.99         (swap! (:next-state scene) assoc handle state)
   3.100 -       (update scene))))
   3.101 +       (update! scene))))
   3.102  
   3.103  (defmacro binding-fast
   3.104    "Faster alternative to core/binding. Works only with vars that are
   3.105 @@ -943,11 +943,11 @@
   3.106        ;; Keep current context observers until the rendering is
   3.107        ;; complete. Some observers may be invoked twice if they
   3.108        ;; appear in both groups until tmp-watcher is removed.
   3.109 -      (replace-observers-watcher scene tmp-watcher)
   3.110 +      (replace-observers-watcher! scene tmp-watcher)
   3.111        (try
   3.112          (render! (:view scene))
   3.113          (finally
   3.114 -         (remove-observers tmp-watcher)
   3.115 +         (remove-observers! tmp-watcher)
   3.116           (commit (:event-dispatcher scene)))))))
   3.117  
   3.118  (defn- scene-font-context [scene]
     4.1 --- a/src/indyvon/demo.clj	Wed Dec 10 20:01:45 2014 +0300
     4.2 +++ b/src/indyvon/demo.clj	Fri Sep 18 04:03:38 2015 +0300
     4.3 @@ -1,5 +1,5 @@
     4.4  ;;
     4.5 -;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
     4.6 +;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
     4.7  ;;
     4.8  ;; This file is part of Indyvon.
     4.9  ;;
    4.10 @@ -46,10 +46,10 @@
    4.11        (draw! (border border-width padding content)
    4.12               offset offset width height))
    4.13      ;; Event handlers
    4.14 -    [:mouse-entered _ (repaint)]
    4.15 -    [:mouse-exited _ (repaint)]
    4.16 -    [:mouse-pressed _ (repaint)]
    4.17 -    [:mouse-released _ (repaint)]
    4.18 +    [:mouse-entered _ (repaint!)]
    4.19 +    [:mouse-exited _ (repaint!)]
    4.20 +    [:mouse-pressed _ (repaint!)]
    4.21 +    [:mouse-released _ (repaint!)]
    4.22      [:mouse-clicked _ (apply callback args)]))
    4.23  
    4.24  (defn combine-colors
    4.25 @@ -66,7 +66,7 @@
    4.26  
    4.27  (defn animate
    4.28    "Changes the value of atom according to the specified range, speed,
    4.29 -   and current frame interval.  Invokes repaint if change happens."
    4.30 +   and current frame interval.  Invokes repaint! if change happens."
    4.31    [atom from to speed]
    4.32    (let [prev @atom
    4.33          state (cond
    4.34 @@ -79,7 +79,7 @@
    4.35         (let [interval (if (= state :start) 1 *interval*)
    4.36               step (* speed interval 1E-9)
    4.37               val (swap! atom #(-> % (+ step) (max from) (min to)))]
    4.38 -         (repaint)
    4.39 +         (repaint!)
    4.40           val))))
    4.41  
    4.42  (defn animated-button
    4.43 @@ -114,12 +114,12 @@
    4.44            ;; Event handlers
    4.45            [:mouse-entered _
    4.46             (reset! animation-speed 4)
    4.47 -           (repaint)]
    4.48 +           (repaint!)]
    4.49            [:mouse-exited _
    4.50             (reset! animation-speed -2)
    4.51 -           (repaint)]
    4.52 -          [:mouse-pressed _ (repaint)]
    4.53 -          [:mouse-released _ (repaint)]
    4.54 +           (repaint!)]
    4.55 +          [:mouse-pressed _ (repaint!)]
    4.56 +          [:mouse-released _ (repaint!)]
    4.57            [:mouse-clicked _ (apply callback args)]))
    4.58       (geometry [button]
    4.59         (let [face-geom (geometry face)]
    4.60 @@ -140,10 +140,10 @@
    4.61         (with-color (if (hovered? view) Color/ORANGE Color/RED)
    4.62           (.fillRect *graphics* 0 0 *width* *height*))
    4.63         [:mouse-entered e
    4.64 -        (repaint)
    4.65 +        (repaint!)
    4.66          (println e)]
    4.67         [:mouse-exited e
    4.68 -        (repaint)
    4.69 +        (repaint!)
    4.70          (println e)]
    4.71         [:mouse-moved e
    4.72          (println e)]))
    4.73 @@ -173,7 +173,6 @@
    4.74    (reify
    4.75     View
    4.76     (render! [view]
    4.77 -     ;;(repaint)
    4.78       (doto *graphics*
    4.79         (.drawLine 0 0 *width* *height*)
    4.80         (.drawLine *width* 0 0 *height*)
     5.1 --- a/src/indyvon/viewport.clj	Wed Dec 10 20:01:45 2014 +0300
     5.2 +++ b/src/indyvon/viewport.clj	Fri Sep 18 04:03:38 2015 +0300
     5.3 @@ -1,5 +1,5 @@
     5.4  ;;
     5.5 -;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
     5.6 +;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
     5.7  ;;
     5.8  ;; This file is part of Indyvon.
     5.9  ;;
    5.10 @@ -82,7 +82,7 @@
    5.11  (defrecord Viewport [content h-align v-align state]
    5.12    View
    5.13    (render! [view]
    5.14 -    (repaint-on-update view)
    5.15 +    (repaint-on-update! view)
    5.16      (with-handlers view
    5.17        (let [geom (geometry content)
    5.18              new-state (swap! state update-viewport geom h-align v-align)
    5.19 @@ -109,7 +109,7 @@
    5.20                                (- (:y-on-screen e) (:fix-y s)))
    5.21                    :fix-x (:x-on-screen e)
    5.22                    :fix-y (:y-on-screen e))))
    5.23 -       (update view)]
    5.24 +       (update! view)]
    5.25        [:mouse-wheel e
    5.26         (scale-viewport!
    5.27          view
    5.28 @@ -160,11 +160,11 @@
    5.29       (scale-viewport! viewport s relative? nil nil))
    5.30    ([viewport s relative? x y]
    5.31       (swap! (:state viewport) scale-viewport viewport s relative? x y)
    5.32 -     (update viewport)))
    5.33 +     (update! viewport)))
    5.34  
    5.35  (defn reset-viewport! [viewport]
    5.36    (reset! (:state viewport) viewport-initial-state)
    5.37 -  (update viewport))
    5.38 +  (update! viewport))
    5.39  
    5.40  (defn ^AffineTransform viewport-transform [viewport]
    5.41    (:transform @(:state viewport)))
    5.42 @@ -200,7 +200,7 @@
    5.43    [m-width m-height viewport]
    5.44    (let [miniature (miniature m-width m-height (:content viewport))]
    5.45      (decorate-view miniature [l]
    5.46 -      (repaint-on-update viewport)
    5.47 +      (repaint-on-update! viewport)
    5.48        (let [geom (geometry (:content viewport))
    5.49              s (scaling (width geom) (height geom) m-width m-height)
    5.50              vp-state @(:state viewport)
    5.51 @@ -223,7 +223,7 @@
    5.52                            :transform (pre-translate tr (- x) (- y)))))
    5.53              move-vp! (fn [x y]
    5.54                         (swap! (:state viewport) move-vp x y)
    5.55 -                       (update viewport))]
    5.56 +                       (update! viewport))]
    5.57          (with-color :alt-back-color
    5.58            (.fillRect *graphics* 0 0 *width* *height*))
    5.59          (with-transform transform
     6.1 --- a/src/indyvon/views.clj	Wed Dec 10 20:01:45 2014 +0300
     6.2 +++ b/src/indyvon/views.clj	Fri Sep 18 04:03:38 2015 +0300
     6.3 @@ -1,5 +1,5 @@
     6.4  ;;
     6.5 -;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
     6.6 +;; Copyright 2010-2015 Mikhail Kryshen <mikhail@kryshen.net>
     6.7  ;;
     6.8  ;; This file is part of Indyvon.
     6.9  ;;
    6.10 @@ -272,7 +272,7 @@
    6.11    (reify
    6.12     ImageObserver
    6.13     (imageUpdate [this img infoflags x y width height]
    6.14 -     (update view)
    6.15 +     (update! view)
    6.16       (zero? (bit-and infoflags
    6.17                       (bit-or ImageObserver/ALLBITS
    6.18                               ImageObserver/ABORT))))))
    6.19 @@ -287,7 +287,7 @@
    6.20      (reify
    6.21       View
    6.22       (render! [view]
    6.23 -       (repaint-on-update view)
    6.24 +       (repaint-on-update! view)
    6.25         (.drawImage *graphics* image 0 0 (image-observer view)))
    6.26       (geometry [view]
    6.27         (let [observer (image-observer view)
    6.28 @@ -306,14 +306,14 @@
    6.29     (let [v (reify
    6.30               View
    6.31               (render! [v]
    6.32 -               (repaint-on-update v)
    6.33 +               (repaint-on-update! v)
    6.34                 (when-let [view (view-fn @view-ref)]
    6.35                   (render! view)))
    6.36               (geometry [_]
    6.37                 (if-let [view (view-fn @view-ref)]
    6.38                   (geometry view)
    6.39                   (->Size 1 1))))]
    6.40 -     (add-watch view-ref v (fn [_ _ _ _] (update v)))
    6.41 +     (add-watch view-ref v (fn [_ _ _ _] (update! v)))
    6.42       v)))
    6.43  
    6.44  ;;