daveray/seesaw — Issue #68: Convenience macros for bulk components selection/creation
Author: dpx-infinity
Created: 2011-12-09 19:10:18+00:00
State: open

Hello,

Currently there is a need to write fairly big amount of boilerplate code if we want to do many things with many components at once (e.g. establish listeners on them, create bindings, lay them out etc). Namely, we have to write something like

```clojure
(let [component-id-1 (select form [:#component-id-1])
component-id-2 (select form [:#component-id-2])
...]
...)
```
when we want to use existing components, and something like

```clojure
(let [component-id-1 (component-1 :id component-id-1 :text "Text")
component-id-2 (component-2 :id component-id-2 :model [1 2 3])
...]
...)
```
when we want to create new components.
I haven't found facilities to ease this in the library by default so I had to write them myself.

Here are the macros which target the cases described above.
1.

```clojure
(defmacro bind-components [form components & body]
`(let [~@(mapcat #(vector % `(seesaw.core/select ~form [~(keyword (str \# %))])) components)]
~@body))
```
2.

```clojure
(defmacro with-components [components & body]
`(let [~@(mapcat #(let [[id form] %
[comp & args] form]
[id `(~comp :id ~(keyword id) ~@args)]) (partition 2 components))]
~@body))
```

`bind-components` macro targets the first situation, so these forms are equivalent:

```clojure
(bind-components form
[comp1 comp2 comp3]
...)

(let [comp1 (select form [:#comp1])
comp2 (select form [:#comp2])
comp3 (select form [:#comp3])]
...)
```
while `with-components` tries to solve second problem, so these forms are equivalent too:

```clojure
(with-components [comp1 (component-1 :text "abcd")
comp2 (component-2 :model [1 2 3])]
...)

(let [comp1 (component-1 :id :comp1 :text "abcd")
comp2 (component-2 :id :comp2 :model [1 2 3])]
...)
```

As you can see, these macros work on assumption that components identifiers are the same as the symbols to which all these components are bound. I think it is the most convenient and obvious way to use seesaw - it greatly reduces the need to remember lots of things at once. These macros go even further - you don't have to type the same identifier twice, first as identifier, second as keyword.

I'd be very grateful if something like these macros would be added to the library.

Best regards,
Vladimir.