bohonghuang / cl-gtk4

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

How do I get the list of application windows? #7

Closed bigos closed 1 year ago

bigos commented 1 year ago

https://docs.gtk.org/gtk4/method.Application.get_windows.html

I am trying to find the list of windows associated with the application. Also, how do I programmatically exit from Gtk4 application close all windows and return to REPL?

bohonghuang commented 1 year ago

You can obtain and close all windows with:

(let ((windows (mapcar (lambda (ptr)
                         (gobj:pointer-object ptr 'window))
                       (glib:glist-list (application-windows gio:*application*)))))
  (mapc #'window-destroy windows))

Having closed all windows, the application quits automatically. Also, you may use (gtk::destroy-all-windows-and-quit), which is currently internal and can get changed or removed in the future, however.

bigos commented 1 year ago

READ error during LOAD:

Symbol "GLIST-LIST" not found in the GLIB package.

Line: 661, Column: 94, File-Position: 25012

https://github.com/bigos/Pyrulis/blob/a80b6e18534ed89f59b2e1e596fc8532ac9be9e9/Lisp/cl-gtk4-tictactoe.lisp#L661

bigos commented 1 year ago

I use my fork of your project, could that be a problem?

https://github.com/bigos/cl-gtk4

bigos commented 1 year ago
       (gir:invoke (glib:*ns* "List" 'foreach)
                                                                   (gtk4:application-windows app)
                                                                   #'gtk4:window-close
                                                                   nil)

This approach did not work.

I also tried to add wrappers for glib, but they do not expose all the symbols I need.

When I read /usr/share/gir-1.0/GLib-2.0.gir and search for g_list_foreach I get the impression that the symbols are not introspectable. Do you have any suggestions on how to solve it?

bigos commented 1 year ago

Could that be a solution?

    (loop for aw = (gtk4:application-active-window app)
                                                             until (null aw)
                                                             do (gtk4:window-close aw))
bohonghuang commented 1 year ago

I use my fork of your project, could that be a problem?

Please update cl-gobject-introspection-wrapper, cl-glib, and cl-gtk4 to the latest Git version, where I added some new APIs a few days ago.

bohonghuang commented 1 year ago

I get the impression that the symbols are not introspectable. Do you have any suggestions on how to solve it?

Although they cannot be introspected via GIR, they are accessible through CFFI. The latest cl-glib provides glist-list, by which you can convert a GLib.List to a list in Lisp: https://github.com/bohonghuang/cl-glib/blob/84b128192d6b11cf03f1150e474a23368f07edff/glib/glist.lisp#L179

bohonghuang commented 1 year ago

Could that be a solution?

I may not recommend this method, since it's not guaranteed that window-close affects application-active-window in one event loop, especially when you attach a custom handler to the close-request signal for a window.

bigos commented 1 year ago

I get the impression that the symbols are not introspectable. Do you have any suggestions on how to solve it?

Although they cannot be introspected via GIR, they are accessible through CFFI. The latest cl-glib provides glist-list, by which you can convert a GLib.List to a list in Lisp: https://github.com/bohonghuang/cl-glib/blob/84b128192d6b11cf03f1150e474a23368f07edff/glib/glist.lisp#L179

That is the nice response I was looking for. I thought about it as well, but my CFFI skills are very limited and I could not translate CFFI manual to the case I was working on. Your example taught me how to solve those problems where I encounter gir imperfections. Thank you very much!

bohonghuang commented 1 year ago

You are welcome.