https://github.com/bohonghuang/cl-gtk4
GTK4/Libadwaita/WebKit2 bindings for Common Lisp.
https://github.com/bohonghuang/cl-gtk4
Last synced: 2 months ago
JSON representation
GTK4/Libadwaita/WebKit2 bindings for Common Lisp.
- Host: GitHub
- URL: https://github.com/bohonghuang/cl-gtk4
- Owner: bohonghuang
- License: lgpl-3.0
- Created: 2022-09-15T13:07:19.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-01-11T03:49:09.000Z (4 months ago)
- Last Synced: 2025-01-11T04:38:41.888Z (4 months ago)
- Language: Common Lisp
- Homepage:
- Size: 1.39 MB
- Stars: 222
- Watchers: 8
- Forks: 10
- Open Issues: 25
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
- curated-awesome-cl - cl-gtk4 - GTK4/Libadwaita/WebKit binding for Common Lisp. [LGPL3][9]. (Python ##)
README
#+TITLE: cl-gtk4
[[https://docs.gtk.org/gtk4/gtk-logo.svg]]GTK4/Libadwaita/WebKit bindings for Common Lisp.
* Requirement
Before getting started, please ensure these libraries are available in your system:
- GTK4
- GObject Introspection
- WebkitGTK (optional)
- libadwaita (optional)
Theoretically, the application built with ~cl-gtk4~ can run on most systems supported by GTK4 and most implementations that support CFFI callback (required by ~cl-gobject-introspection~).
The [[file:examples/][examples]] are tested to run on following implementations:
- SBCL
- Microsoft Windows \\
[[file:examples/screenshots/adw-win.png]]
- MacOS \\
See: [[https://ibb.co/7KZz3r2]]
- GNU/Linux \\
See the screenshots in the [[examples][Examples]] section.
- CCL
- ECL
- ABCL
* Usage
1. Currently, ~cl-gtk4~ is available on [[https://ultralisp.org][Ultralisp]], so it can be downloaded via Quicklisp with Ultralisp installed as its distribution.
To install ~cl-gtk4~ manually, you can clone this repository along with the following dependencies into the ~local-projects~ under your Quicklisp installation root:
- [[https://github.com/bohonghuang/cl-gobject-introspection-wrapper][cl-gobject-introspection-wrapper]]
- [[https://github.com/bohonghuang/cl-glib][cl-glib]]
2. Load the library with:
- ~(ql:quickload :cl-gtk4)~
- ~(ql:quickload :cl-gtk4.adw)~ (if you need libadwaita)
- ~(ql:quickload :cl-gtk4.webkit)~ (if you need WebkitGTK)
3. For GTK4 usage, please refer to [[https://docs.gtk.org/gtk4/][GTK API reference]] and check out the [[https://github.com/bohonghuang/cl-gobject-introspection-wrapper#conversion-rules][conversion rules]] for these APIs.
** Multi-threading
Please note that GTK runs in a single thread and is NOT thread-safe, so all the UI-related operations must happen in GTK [[https://docs.gtk.org/glib/main-loop.html][main event loop]],
which means you cannot write the code like this:#+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_SRCGLib 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:
- ~gtk:run-in-main-event-loop~ \\
Execute the body in GTK main event loop, in which we can access the UI safely:
#+BEGIN_SRC lisp
(let ((label (make-label :str "0"))
(count 0))
(bt:make-thread
(lambda ()
(loop :repeat 5
:do (gtk:run-in-main-event-loop
(setf (label-text label) (format nil "~A" (incf count))))
(sleep 1))))) ; Don't put this into `gtk:run-in-main-event-loop'
#+END_SRC
** Interactive Programming
*** Live Reload
You can reload the application without closing the window constructed in ~define-main-window~ by recompiling the ~define-application~ macro in top-level (Simply stroke =C-c C-c= if using Slime/Sly in Emacs):[[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.
Additionally, you can interrupt and quit the GTK program by typing =C-c C-c= in Emacs(Slime/Sly)'s REPL.
* Examples
See the [[file:examples/][examples]] folder.
* Deployment
The [[file:examples/][examples]] are ready for being built into executable if the implementation supports ~:program-op~:
#+BEGIN_SRC lisp
(asdf:operate :program-op :cl-gtk4/example)
#+END_SRC
Then you could find the executable file under the ~examples~ folder.Note that:
- On ECL, for unknown reason, the ~:entry-point~ of the ASDF system is ignored.
This command should be used instead:
#+BEGIN_SRC lisp
(asdf:make-build :cl-gtk4/example :type :program :epilogue-code '(progn (uiop:symbol-call :gtk4.example :simple) (si:exit)))
#+END_SRC
- On Microsoft Windows, it's recommended to launch your application via [[https://www.dependencywalker.com/][Dependency Walker]], then the shared libraries used by your application would appear in it.
You should copy all these ~.dll~ files into the folder where you place the executable file. If you are using MSYS2, the folder structure might be like this:#+BEGIN_EXAMPLE
.
├── bin
│ ├── gdbus.exe
│ ├── libgio-2.0-0.dll
│ ├── libgirepository-1.0-1.dll
│ ├── libglib-2.0-0.dll
│ ├── libgobject-2.0-0.dll
│ ├── libgtk-4-1.dll
│ ├── your_application.exe
│ └── ...
├── lib
│ ├── girepository-1.0
│ ├── gtk-4.0
│ └── ...
└── share
├── icons
└── ...
#+END_EXAMPLEThe folder ~lib/girepository-1.0~ is mandatory, without which your application won't work as expected.