bohonghuang / cl-gtk4

GTK4/Libadwaita/WebKit2 bindings for Common Lisp.
GNU Lesser General Public License v3.0
215 stars 9 forks source link

+TITLE: cl-gtk4

[[https://docs.gtk.org/gtk4/gtk-logo.svg]]

GTK4/Libadwaita/WebKit bindings for Common Lisp.

+BEGIN_SRC lisp

(let ((label (make-label :str "0")) (count 0)) (bt:make-thread (lambda () (loop :repeat 5 :do (setf (label-text label) (format nil "~A" (incf count))) (sleep 1)))))

+END_SRC

GLib provides ~idle_add~ and ~timeout_add~ to add a function to execute in the main event loop, which is thread-safe so that it can be called in other threads. [[https://github.com/bohonghuang/cl-glib][cl-glib]] wraps ~idle_add~ and ~timeout_add~, and [[https://github.com/bohonghuang/cl-gtk4][cl-gtk4]] create restarts for the handler passed for them to be invoked safely, even when conditions are signaled. It also provides the API for convenience:

[[file:screenshots/live-reload.gif]] *** Restarts The API in ~cl-gtk4~ handles almost all possible recoverable errors by providing restarts, by which you can recover the program or safely exit the GTK application when encountering an error.

Please note that stack unwinding into FFI functions is unsafe and may cause you to be unable to run the GTK application in the current session again, or even freeze or crash the Lisp implementation. Therefore, it is important to always use the restarts provided by ~cl-gtk4~ or other restarts that do not unwind the stack into FFI functions to recover or exit the application.

Additionally, when you attempt to interrupt a GTK program through Slime/Sly, such as =C-c C-c= in Emacs' REPL, remember to choose =ABORT-APPLICATION= provided by ~cl-gtk4~ instead of =ABORT= provided by the Lisp implementation.

The highlighted restarts as follows, for example, are safe, while the rest perform stack unwinding and are therefore unsafe in GTK applications:

[[file:screenshots/restart-1.png]]

[[file:screenshots/restart-2.png]]

Note that: