Skip to content

Instantly share code, notes, and snippets.

@yogthos
Last active March 13, 2023 19:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yogthos/81d560da28da7dd5b86dcda3003b35e7 to your computer and use it in GitHub Desktop.
Save yogthos/81d560da28da7dd5b86dcda3003b35e7 to your computer and use it in GitHub Desktop.
Gjs ClojureScript example
(ns gjs-example.core)
(defn main []
(set! (-> js/imports .-gi .-versions .-Gtk) "3.0")
(let [Gtk (doto (-> js/imports .-gi .-Gtk) (.init nil))
window (Gtk.Window.
(clj->js
{:type (-> Gtk .-WindowType .-TOPLEVEL)
:title "A default title"
:default_width 300
:default_height 250
:window_position (-> Gtk .-WindowPosition .-CENTER)}))
button (Gtk.Button.
(clj->js
{:label "close"
:visible true
:valign (-> Gtk .-Align .-CENTER)
:halign (-> Gtk .-Align .-CENTER)}))]
(set! (.-title window) "Hello World")
(.connect window "delete-event" #(boolean (js/log "delete event")))
(.connect window "destroy" #(.main_quit Gtk))
(.connect button "clicked" #(.destroy window))
(.add window button)
(.show window)
(.main Gtk)))
(main)
(defproject gjs-test "0.1.0"
:dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/clojurescript "1.10.520"]]
:plugins [[lein-cljsbuild "1.1.7"]]
:cljsbuild {:builds [{:source-paths ["src"]
:compiler {:output-to "target/app.js"
:optimizations :simple
:target :nodejs
:pretty-print true}}]})
@yogthos
Copy link
Author

yogthos commented Jul 9, 2019

the script can be run with gjs-console target/app.js

@sublimemm
Copy link

🔥 👍

@titonbarua
Copy link

I have been looking for a way to use Clojurescript to write gnome applications for a long time now. Was it always possible? Or maybe something changed in the new version of Clojurescript compiler?

Thanks for sharing!

@yogthos
Copy link
Author

yogthos commented Jul 16, 2019

I'm not sure how long this was possible actually. I randomly ran across an app using GJS, and got curious to see if I could drive the API from Clojurescript. Was pleasantly surprised how quickly I got it working.

@titonbarua
Copy link

Looks like js/console.log(as used in line 20) does not work with gjs, but js/log does; as gjs has it's own logging functions:
https://gitlab.gnome.org/GNOME/gjs/blob/master/doc/Logging.md

@yogthos
Copy link
Author

yogthos commented Jul 16, 2019

Ah thanks for the heads up

@ieugen
Copy link

ieugen commented Apr 28, 2020

That sounds great. I'll give it a try at some point.

@titonbarua
Copy link

I have implemented a new target for shadow-cljs to compile Clojurescript for Gjs environment, with some additional import syntax sugar. If anyone is interested, please take a look at https://github.com/titonbarua/shadow-cljs-gjs-target . Contributions and feed-backs are welcome.

@yogthos
Copy link
Author

yogthos commented Jun 28, 2020

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment