Hi! Coming back to use membrane after not using it for awhile a while. I'm trying to use java2d from Windows. My expectation is when I change the draw function by re-evaluating it in the repl, (after changing the text in the label for example) the ui I'm looking at should immediately change. But I'm finding that sometimes it does work like this but then other times when I change it there will be a long lag or it sometimes wont change at all. Sometimes resizing or clicking the window seems to help but sometimes not. Any tips? I'm wondering if maybe it has something to do with this note https://github.com/phronmophobic/membrane/blob/ddc9d2b1b493bc0421ac4c13a68ce52d628b383e/src/membrane/java2d.clj#L1098-L1101, something is not causing repaint to happen. Any tips?
(defn draw! []
(ui/vertical-layout
(ui/spacer 25 0)
(ui/label "123")
(ui/with-color [0.0 0.0 1.0]
(ui/rounded-rectangle 100 100 22))))
(comment
(java2d/run #'draw!
{:window-title "title"
:window-start-width 400
:window-start-height 400})
)In general, repainting can be kind of expensive, so by default, membrane tries to avoid repainting if it can. The java2d/run function returns a map with window information. One of the keys is ::java2d/repaint which a is a 0 arg function you can call to force repaints.
You should be able to do something like the following to automatically setup repainting:
(defn show! [v opts]
(let [winfo (java2d/run #'draw! opts)
repaint! (::java2d/repaint winfo)]
(add-watch v ::repaint
(fn [k r old new]
(repaint!)))
winfo))
(comment
(show! #'draw!
{:window-title "title"
:window-start-width 400
:window-start-height 400})
,)Ah, thanks! I saw that repaint function but assumed it was used internally, that makes sense now.
It also includes the jframe window if you want to make changes there too.
As a recommendation, I might call draw! something like main-view. It's a pure function that returns data and can be called whenever from any thread.
Good point