r/Clojure • u/vlaaad • Mar 28 '20
Year of Clojure on the Desktop
https://vlaaad.github.io/year-of-clojure-on-the-desktop2
u/mac Mar 31 '20
Will this work with Open JDK? Quite important given the licensing changes Oracle has made to the Oracle JDK.
3
1
u/adwelly Apr 01 '20
It looks really good, and the structure of UI as data makes perfect sense. I admit though, I'm having trouble understanding the semantics of the render function. Could you expand on this topic please?
1
u/vlaaad Apr 01 '20
You mean renderer? It is a thread-safe stateful function that maintains created JavaFX nodes on JavaFX UI thread. You can call it a thousands times per second with new component descriptions, and it will render the view only once per JavaFX UI tick with the latest received description, so the semantics are "...and now my application view is described by this data".
1
u/adwelly Apr 07 '20
vlaaad.github.io/year-o...
Sorry about the delay in replying - COVID-19 etc.
My question was woefully non-specific. What i should have asked was about the nature of the map you are giving renderer. So in the readme, you give the example of:
(renderer {:fx/type root :showing true})
Where root is a function returning a data structure describing a UI (and there's an fx/type in that map too set to :stage). and :showing which is set in the root function by destructuring. The use of :showing seems clear. if a map just containing :showing was given to the root function root would return a complete renderable data structure. I'm confused about the :fx/type however - does this always name the function that is called to create the describing data structure?
To put it another way, could I say that renderer takes a map that must have :fx/type set to a function that expects a map and returns a data structure describing a UI. That function is given the map that was given to renderer in the first place.
Is that how it works?
1
u/vlaaad Apr 07 '20
No worries about the delay, I'm very well aware of the times we live in :)
About renderer semantics, there are 2 concepts in play: renderer input and component description. In that example renderer input IS component description — that's how it behaves by default, but it does not have to be that.
Renderer input could be anything, but it has to be transformed via
:middlewareto component description: see code sample in atoms section: the input to renderer is just{:title "App Title"}that is getting transformed by the middleware to component description (by setting:fx/typekey on a map).Component description is a map with
:fx/typekey. Value does not have to be a function: it can also be a keyword like:stageor other stuff that we should skip for now to keep it simple. When:fx/typehas a function value, that function will be called with that map itself.So, you could say that renderer takes a value that needs to be transformed to a component description — that is a map with
:fx/typeset to some valid value such as keyword or function.Here is another example to illustrate the point:
(def renderer (fx/create-renderer)) (defn close-button [{:keys [text]}] {:fx/type :button :text text :on-action (fn [_] (renderer {:fx/type :stage :showing false :scene {:fx/type :scene :root {:fx/type close-button :text "Closed, but you can't see it!"}}}))}) (renderer {:fx/type :stage :showing true :scene {:fx/type :scene :root {:fx/type close-button :text "Close"}}})
1
u/mikeivanov Apr 13 '20
That's cool, though there is one problem with cljfx. The way it is designed is that it implicitly initializes jfx upon import, which makes AOT compilation (among other things) impossible. Run lein uberjar (with {:aot :all} in profile.clj) to see what I mean. It is possible that I'm doing something wrong, but a quick code inspections confirms that there is no way postpone jfx initialization when using cljfx.
2
u/vlaaad Apr 13 '20
Yes, unfortunately it's a requirement of JavaFX — its runtime has to be initialized before loading classes in controls package. There is an issue with a workaround for lein specifically. The solution is to call
(javafx.application.Platform/exit)after compilation finishes, unfortunately lein seems to not provide a hook to do that.1
1
u/mikeivanov Apr 13 '20
By any chance, has anyone tried to compile it with graal?
2
u/vlaaad Apr 13 '20
I did, it fails so far. Here is the current state of my experiments: https://github.com/cljfx/hn/blob/native-image/build/native_image.clj
3
u/didibus Mar 28 '20 edited Mar 28 '20
Awesome work! I'm so happy to see Clojure on the desktop continues to evolve and grow.
3
3
u/ackerleytng Mar 29 '20
Thanks u/vlaaad! Nice of you to put together a nice example of how to use jpackage with cljfx!!
2
u/rbc1971 Mar 29 '20
Web vs Desktop is about control, particularly of user data. But there will always be a "desktop" market as long as there are users of devices (other than ChromeBook, I mean). As big, distributed systems become more decentralized individuals will again want local control (or at least the illusion of it) of their own data, possibly even of their own UX. My main concern with cljfx (or any Java-based commercial desktop app, for that matter) is Oracle's new commercial licensing model. The "cloud" obscures the potential negative impact of that, but selling commercial JVM software to commercial businesses that run on their machines now has licensing implications it didn't used to have. Nevertheless: bringing React-style programming to the "Destkop" is technologically a major win. I hated all UI coding until the reactive model made it not just tolerable but arguably pleasant. Great work!
2
u/jamesd3142 Mar 31 '20
Thanks u/vlaaad I am really excited to try out the library. My day job is maintenance of a Spring/AWT app. I know that we will never rewrite our existing system but it is nice to know that there is a better way. Helps keep me sane.
1
u/slifin Mar 28 '20
https://github.com/roman01la/proton-native-cljs was a cool experiment and seemed a bit more native than Java things out the box but wasn't a native executable
0
Mar 28 '20
[deleted]
1
u/vlaaad Mar 28 '20
Thank you for kind words! I haven't aot-compiled this app to keep the example simple, that would significantly speed up the startup time. It's also slow because it performs a lot of http requests to load individual comments/stories, some batching will speed it up as well — which was also left out for simplicity :)
GraalVM has faster startup time but performs slower than JVM-optimized code.
0
Mar 28 '20
[deleted]
5
u/oakes Mar 28 '20
I understand the desire, but that level of abstraction gives me the heebie jeebies just thinking about it...
7
u/yogthos Mar 28 '20
I think it would be reasonable to use a common API for expressing business logic of the application, and then writing platform specific UIs against them. For example, if we used re-frame as the common engine, and then write separate UI libs against it for each target platform. All the widgets need to do is observe some reactive container and repaint whenever its state changes. So, you'd still need to write a UI for each platform, but everything else would be reusable.
And another advantage of creating this kind of separation would be that all of the logic can be tested headless.
1
u/phronmophobic Mar 31 '20
check out https://github.com/phronmophobic/membrane. There's currently only adapters for skia (the c++ graphics library chrome uses on the desktop), and webgl, but adding more is on the roadmap (especially the DOM).
5
u/editor_of_the_beast Mar 28 '20
I feel like all of the points supported that the web is the better distribution platform overall.