rabbibotton / clog

CLOG - The Common Lisp Omnificent GUI
Other
1.48k stars 101 forks source link

set-on-click is repeating data #305

Closed K1D77A closed 10 months ago

K1D77A commented 10 months ago

Hi I seem to have run into a problem I have my GUI made with clog-gui image

I then have the Rules dropdown which has one option and when selected I have: image

Now what is strange is that whenever i click on any of the options I get the same data displayed in the new window. image

No matter which I click on I only get the data for the PREMIUM category.

The code I have is the following:

(defun inspect-rules (o category rules)
  (let ((gui (create-gui-window o
                                :title (format nil "Substack ~S Rules" category)
                                :width 700)))
    (dolist (rule rules)
      (let ((d (create-div (window-content gui)
                           :class "rules"
                           :content
                           (spinneret:with-html-string
                             (:div :class "rule"
                                   (:h3 (format nil "Name: ~A"
                                                (breca::name rule)))
                                   (:h4 (format nil "Weight: ~D"
                                                (breca::weight rule)))
                                   (:pre
                                    (format nil "~S" (breca::fun-as-list rule))))))))))))

(defun substack-rules (body)
  (let ((r (gethash (find-class 'breca::substack) breca::*resource-rules*))
        (gui (create-gui-window body :title "Rule Categories")))
    (loop :for (category rules) :on r :by #'cddr 
          :do (let ((d (create-div (window-content gui) :class "rules"
                                                        :content
                                                        (spinneret:with-html-string
                                                          (:div :class "rule"
                                                                (:p category))))))
                (set-on-click d (lambda (o)
                                  (inspect-rules o category rules)))))))

And in my main I just have

         (rules (create-gui-menu-drop-down menu :content "Rules"))         
         (substack-rule (create-gui-menu-item rules :content "Substack"
                                                    :on-click 'substack-rules))

I have gone through and checked that all of the values in the set-on-click closure are correct...

Any help would be greatly appreciated. Thanks.

sabracrolleton commented 10 months ago

You need to do a little extra work to deal with the clog closures. Does this do what you want?

(defun substack-rules (body)
  (let ((r (gethash (find-class 'breca::substack) breca::*resource-rules*))
        (gui (create-gui-window body :title "Rule Categories")))
    (loop :for (category rules) :on r :by #'cddr 
          :do (let ((my-category category) ; for clog closure purposes
                    (my-rules rules) ; for clog closure purposes
                    (d (create-div (window-content gui) :class "rules"
                                   :content
                                   (spinneret:with-html-string
                                    (:div :class "rule"
                                          (:p my-category))))))
                (set-on-click d (lambda (o)
                                  (inspect-rules o my-category my-rules)))))))
K1D77A commented 10 months ago

Yes it did fix it. Thanks. What a silly problem :joy: