changeset 156:dc13cacf3a43

API changes (themed, hinted, add-handlers); formatting.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 16 Apr 2014 00:10:48 +0400
parents c3782e84486f
children 4fea68ec12f4
files src/indyvon/core.clj src/indyvon/demo.clj src/indyvon/viewport.clj src/indyvon/views.clj
diffstat 4 files changed, 58 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/src/indyvon/core.clj	Tue Apr 15 23:45:14 2014 +0400
+++ b/src/indyvon/core.clj	Wed Apr 16 00:10:48 2014 +0400
@@ -105,8 +105,8 @@
   (height [geom] [geom width])
   (anchor-x [geom h-align width]
     "Returns the x coordinate of the anchor point for the specified
-     horizontal alignment and width, h-align could be :left, :center
-     or :right.")
+    horizontal alignment and width, h-align could be :left, :center
+    or :right.")
   (anchor-y [geom v-align height]
     "Returns the y coordinate of the anchor point for the specified
     vertical alignment and height, v-align could be :top, :center
@@ -218,16 +218,16 @@
      "Listen for events on the specified AWT Component.")
   (create-dispatcher [this handle handlers]
      "Returns new event dispatcher associated with the specified event
-      handlers (an event-id -> handler-fn map). Handle is used to
-      match the contexts between commits.")
+     handlers (an event-id -> handler-fn map). Handle is used to match
+     the contexts between commits.")
   (commit [this]
      "Apply the registered handlers for event processing.")
   (handle-picked? [this handle]
      "Returns true if the specified handle received the :mouse-pressed
-      event and have not yet received :moused-released.")
+     event and have not yet received :moused-released.")
   (handle-hovered? [this handle]
      "Returns true if the specified handle received the :mouse-entered
-      event and have not yet received :mouse-exited."))
+     event and have not yet received :mouse-exited."))
 
 (defn- assoc-cons [m key val]
   (->> (get m key) (cons val) (assoc m key)))
@@ -366,8 +366,8 @@
 ;; Use faster clipping calculation provided by Graphics2D.
 (defn- clip
   "Intersect clipping area with the specified Shape in current
-   transform coordinates. Returns new clip in the AWT component
-   coordinates (Shape or nil if empty)."
+  transform coordinates. Returns new clip in the AWT component
+  coordinates (Shape or nil if empty)."
   [^Shape shape]
   (let [^Graphics2D clip-g (.create *graphics*)]
     (try
@@ -383,7 +383,7 @@
 
 (defn- ^Graphics2D apply-theme
   "Set graphics' color and font to match theme.
-   Modifies and returns the first argument."
+  Modifies and returns the first argument."
   ([]
      (apply-theme *graphics* *theme*))
   ([^Graphics2D graphics theme]
@@ -438,6 +438,10 @@
   `(binding [*theme* (merge *theme* ~theme)]
      ~@body))
 
+(defn with-theme* [theme f & args]
+  (with-theme theme
+    (apply f args)))
+
 (defmacro with-color [color-or-key & body]
   `(let [color# ~color-or-key
          color# (get *theme* color# color#)
--- a/src/indyvon/demo.clj	Tue Apr 15 23:45:14 2014 +0400
+++ b/src/indyvon/demo.clj	Wed Apr 16 00:10:48 2014 +0400
@@ -46,11 +46,11 @@
       (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-clicked _ (apply callback args))))
+    [:mouse-entered _ (repaint)]
+    [:mouse-exited _ (repaint)]
+    [:mouse-pressed _ (repaint)]
+    [:mouse-released _ (repaint)]
+    [:mouse-clicked _ (apply callback args)]))
 
 (defn combine-colors
   "Returns color between color1 and color2. When c (0 <= c <= 1.0) is
@@ -112,15 +112,15 @@
             (draw! (border border-width padding content)
                    offset offset width height))
           ;; Event handlers
-          (:mouse-entered _
-            (reset! animation-speed 4)
-            (repaint))
-          (:mouse-exited _
-            (reset! animation-speed -2)
-            (repaint))
-          (:mouse-pressed _ (repaint))
-          (:mouse-released _ (repaint))
-          (:mouse-clicked _ (apply callback args))))
+          [:mouse-entered _
+           (reset! animation-speed 4)
+           (repaint)]
+          [:mouse-exited _
+           (reset! animation-speed -2)
+           (repaint)]
+          [:mouse-pressed _ (repaint)]
+          [:mouse-released _ (repaint)]
+          [:mouse-clicked _ (apply callback args)]))
      (geometry [button]
        (let [face-geom (geometry face)]
          (->Size (+ (width face-geom) shadow-offset)
@@ -139,14 +139,14 @@
      (with-handlers view
        (with-color (if (hovered? view) Color/ORANGE Color/RED)
          (.fillRect *graphics* 0 0 *width* *height*))
-       (:mouse-entered e
+       [:mouse-entered e
         (repaint)
-        (println e))
-       (:mouse-exited e
+        (println e)]
+       [:mouse-exited e
         (repaint)
-        (println e))
-       (:mouse-moved e
-        (println e))))
+        (println e)]
+       [:mouse-moved e
+        (println e)]))
    (geometry [view]
      (->Size 30 20))))
 
--- a/src/indyvon/viewport.clj	Tue Apr 15 23:45:14 2014 +0400
+++ b/src/indyvon/viewport.clj	Wed Apr 16 00:10:48 2014 +0400
@@ -92,14 +92,14 @@
                   *viewport-transform* transform]
           (with-transform transform
             (draw! content 0 0 (width geom) (height geom) false))))
-      (:mouse-pressed e
+      [:mouse-pressed e
        (swap! state assoc
               :fix-x (:x-on-screen e)
               :fix-y (:y-on-screen e))
-       (set-cursor! (Cursor. Cursor/MOVE_CURSOR)))
-      (:mouse-released e
-       (set-cursor! (Cursor. Cursor/DEFAULT_CURSOR)))
-      (:mouse-dragged e
+       (set-cursor! (Cursor. Cursor/MOVE_CURSOR))]
+      [:mouse-released e
+       (set-cursor! (Cursor. Cursor/DEFAULT_CURSOR))]
+      [:mouse-dragged e
        (swap! state
               (fn [s]
                 (assoc s
@@ -109,12 +109,12 @@
                               (- (:y-on-screen e) (:fix-y s)))
                   :fix-x (:x-on-screen e)
                   :fix-y (:y-on-screen e))))
-       (update view))
-      (:mouse-wheel e
+       (update view)]
+      [:mouse-wheel e
        (scale-viewport!
         view
         (Math/pow *viewport-scaling-step* (:wheel-rotation e))
-        true (:x e) (:y e)))))
+        true (:x e) (:y e))]))
   (geometry [_]
     (geometry content)))
 
@@ -231,8 +231,8 @@
             (.fillRect *graphics* 0 0 last-width last-height)))
         (with-handlers l
           (draw! miniature)
-          (:mouse-pressed e (move-vp! (:x e) (:y e)))
-          (:mouse-dragged e (move-vp! (:x e) (:y e))))
+          [:mouse-pressed e (move-vp! (:x e) (:y e))]
+          [:mouse-dragged e (move-vp! (:x e) (:y e))])
         (with-transform transform
           (with-color :border-color
             (.drawRect *graphics* 0 0 last-width last-height)))))))
--- a/src/indyvon/views.clj	Tue Apr 15 23:45:14 2014 +0400
+++ b/src/indyvon/views.clj	Wed Apr 16 00:10:48 2014 +0400
@@ -47,9 +47,6 @@
 
 (def empty-view (->Empty))
 
-;; TODO: change argument order for decorators, content should be the
-;; last.
-
 (defn padding
   "Adds padding to the content view."
   ([distance content]
@@ -264,7 +261,7 @@
 ;; View context decorators
 ;;
 
-(defmacro handler [view & handlers]
+(defmacro add-handlers [view & handlers]
   "Adds event handling to the view."
   `(let [view# ~view]
      (decorate-view view# [t#]
@@ -272,25 +269,21 @@
          (render! view#)
          ~@handlers))))
 
-(defn themed [view & map-or-keyvals]
-  (let [theme (if (== (count map-or-keyvals) 1)
-                (first map-or-keyvals)
-                (apply array-map map-or-keyvals))]
-    (reify
-     View
-     (render! [_]
-       (with-theme theme
-         (render! view)))
-     (geometry [_]
-       (with-theme theme
-         (geometry view))))))
+(defn themed [theme view]
+  (reify
+    View
+    (render! [_]
+      (with-theme* theme render! view))
+    (geometry [_]
+      (with-theme* theme geometry view))))
 
-(defn hinted [view & map-or-keyvals]
-  (let [hints (if (== (count map-or-keyvals) 1)
-                (first map-or-keyvals)
-                (apply array-map map-or-keyvals))]
-    (decorate-view view [_]
-      (with-hints* hints render! view))))
+(defn hinted [hints view]
+  (reify
+    View
+    (render! [_]
+      (with-hints* hints render! view))
+    (geometry [_]
+      (with-hints* hints geometry view))))
 
 ;;
 ;; Measuring time