It is traditional to introduce programming using a new programming language or user interface toolkit using an application that simply displays the message, "Hello, world".
In Clojure Desktop Toolkit, this application illustrates how to:
Here is the code:
(defn hello []
(application ; (1)
(shell SWT/SHELL_TRIM ; (2, 3)
"Hello application" ; (4)
:layout (FillLayout.) ; (5, 6)
(label "Hello, world")))) ; (7)
Here's what this looks like when running on Linux:
Although this application seems trivial, it already illustrates many of the main features of the Clojure Desktop Toolkit.
Let's examine each expression (numbered above) and see what we can learn:
The application
function creates the underlying SWT Display
object and runs the event loop as long as there is at least one top-level window open.
A shell
can be a top-level window, a dialog box, a borderless window, a child window of another window, and so forth. This one is the application
's child.
SWT/SHELL_TRIM
All SWT user interface widgets share the same constructor signature: (parent, style-bits)
.
parent
is automatically supplied from the parent in the call tree. That is why this parameter doesn't appear explicitly in our code.shell
objects, this parent is the application's Display
.SWT/SHELL_TRIM
is the style-bits
we're passing here. This parameter is an integer bitmap that selects features to enable for the thing being constructed.
SWT/SHELL_TRIM
is a convenience value for the bits normally used to designate an application top-level shell.SWT/NONE
."Hello application"
If a string appears by itself (e.g.: is not being explicitly set into a property), it is set into its parent's text
JavaBeans property. e.g.: This bare string is equivalent to roughly the following Java code:
theShell.setText("Hello application");
:layout
Clojure keywords name a JavaBean property to set using the value specified by the following parameter.
Here we're using Java interop to directly construct a FillLayout
to set into the layout
property. e.g.: (5) and (6) combine to effectively write:
theShell.setLayout(new FillLayout());
(label
creates a SWT Label
widget as a child of the shell using SWT/NONE
style bits. Since "Hello, world"
isn't assigned to a particular property, it is set into the label's text
property like "Hello application"
was above. SWT's naming consistency makes Clojure Desktop Toolkit conventions like this possible, pleasant, and effective.To run this application from within a Clojure -main
function, simply call it.
To run this application from the REPL, one must create a separate thread for it to avoid blocking the REPL thread while the application is running. Here's a comment form one can use for this purpose:
(comment
"Run the `hello` application"
(def app (future (hello)))
:eoc)
"Hello, world" has illustrated several principles:
:layout
) preceeding the value to set.text
JavaBeans property of their parent.Although we haven't fully seen this in action, Clojure names are identical to the corresponding Java names, but in kebab-case
form.
At this point, you might be appreciating the clarity and succinctness of code written using Clojure Desktop Toolkit. At the same time, you may feel like all of the syntactic sugar is obscuring what is happening under the hood.
In our next episode, we'll pull back the curtain and rewrite "Hello, world" without using any of Clojure Desktop Toolkit's syntax sugar. You'll see exactly what the library is doing and learn how you can extend it yourself--the same way it was written itself.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close