bohonghuang / cl-gtk4

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

Hoping for examples or documents about dialogs #63

Closed apr3vau closed 7 months ago

apr3vau commented 7 months ago

I feel not easy when I trying to programming with dialogs, as the usual conversion rules seems not really adaptable...

For example, some dialog classes and methods seem missing or being mapped to different names compared to official document, like Adw.AlertDialog (the adw:message-dialog seems the identical one), the constructor function of AlertDialog (there isn't a make-alert-dialog in my environment...), and present functions like adw_dialog_present (But it works well by just using window-present). And some other essential functions are also difficult to use, like the adw:message-dialog-choose - I can't make it works, the user_data argument cannot automatically convert to correct pointer type, and the application will get stuck while running the callback. Maybe is a multiprocessing problem, but it's difficult to find out for me as a newer in Gtk.

I'm still confusing about where the problems happen, but hopefully there's always someway to suite my needs. Maybe they're my faults, but I do think things can be easier if there's a trivial example/document to demonstrate what it should be...

My environment is CCL (due to unsolvable floating points error under SBCL), Windows11 with msys.

Thanks for your efforts, and I must say that this is a really awesome and beautiful library~

bohonghuang commented 7 months ago

For example, some dialog classes and methods seem missing or being mapped to different names compared to official document, like Adw.AlertDialog (the adw:message-dialog seems the identical one), the constructor function of AlertDialog (there isn't a make-alert-dialog in my environment...), and present functions like adw_dialog_present (But it works well by just using window-present).

Both AlertDialog and Dialog are only available in libadwaita versions higher than 1.5, but most distributions currently offer versions lower than that.

I can't make it works, the user_data argument cannot automatically convert to correct pointer type, and the application will get stuck while running the callback.

The last 2 parameters should be given as CFFI callback and pointer, like:

(adw:message-dialog-choose dialog nil (cffi:callback foo) (cffi:null-pointer))

It seems this pattern is quite common in libadwaita, so I will develop a pattern for passing Lisp closures to such functions later on. Actually, similar functionality already exists in my glib bindings, but I haven't exposed it yet.

Most callbacks in GTK4 are implemented through signals, and AdwMessageDialog also provides a response signal. Perhaps at this stage, you could try going forward using signals:

(in-package #:gtk4.example)

(define-application (:name message-dialog-test
                     :id "org.bohonghuang.gtk4-example.message-dialog-test")
  (define-main-window (window (make-application-window :application *application*))
    (adw:init)
    (setf (window-title window) "Message Dialog Test")
    (let ((box (make-box :orientation +orientation-vertical+ :spacing 4)))
      (let ((button (make-button :label "Show Message Dialog")))
        (connect button "clicked" (lambda (button)
                                    (let ((dialog (adw:make-message-dialog :parent window :heading "Test Message" :body "Test Message Body")))
                                      (adw:message-dialog-add-response dialog "cancel" "Cancel")
                                      (adw:message-dialog-add-response dialog "ok" "OK")
                                      (connect dialog "response" (lambda (dialog respose)
                                                                   (declare (ignore dialog))
                                                                   (alexandria:eswitch (respose :test #'string=)
                                                                     ("ok" (setf (widget-sensitive-p button) nil))
                                                                     ("cancel"))))
                                      (window-present dialog))))
        (box-append box button))
      (setf (window-child window) box))
    (unless (widget-visible-p window)
      (window-present window))))

My environment is CCL (due to unsolvable floating points error under SBCL), Windows11 with msys.

Just quickload float-features and:

(float-features:with-float-traps-masked t (run-your-application))
apr3vau commented 7 months ago

Thank you, your explanations really save my life :D! Thanks for your effort!