membrane

genekim 2023-02-20T08:44:57.570059Z

@smith.adriane Got furniture inventory working in about 45m — half the time was modifying the simulator, half was rendering it in membrane. So great!!! https://capture.dropbox.com/NlfuIwVUpN5AuDVQ

genekim 2023-02-20T08:46:24.784359Z

Oh, the best part! The number slider as a scrubber! https://capture.dropbox.com/wbNYMpqdTmmzePHm

jlmr 2023-02-20T09:51:35.956009Z

@smith.adriane this code fails to compile:

(defui test-defui
  [{:keys [mpos]}]
  (ui/on
   :mouse-move
   (fn [mpos]
     [[::set-mouse-pos $mpos mpos]])
   (ui/label "Hello mouse!")))
The message is: Unable to resolve symbol: $mpos in this context. I can work around it by renaming mpos in the fn to something else. Is this expected behaviour?

phronmophobic 2023-02-20T19:06:28.766869Z

Yes, that is expected. $mpos is the reference for the current lexical value of mpos. I can see how it might be convenient for your example to have $mpos refer to the mpos from the outer lexical scope, but I think maintaining that $sym is always the reference for the current lexical value of sym is important for reasoning about more complicated cases.

(defui test-defui
  [{:keys [other-mpos]}]
  (ui/on
   :mouse-move
   (fn [_]
     (let [mpos other-mpos]
       [[::set-mouse-pos $mpos mpos]]))
   (ui/label "Hello mouse!")))

(ui/mouse-move (test-defui {:other-mpos 42}) [ 0 0])
;; ([:ui.membrane-ui/set-mouse-pos [(keypath :other-mpos) []] 42])

phronmophobic 2023-02-23T06:51:02.196189Z

Added this idea here, https://github.com/phronmophobic/membrane/discussions/66

jlmr 2023-02-21T18:44:24.919009Z

Ok good to know it is expected 🙂

phronmophobic 2023-02-21T18:44:51.949979Z

I'm open to changes if there are good reasons, but that's the current rationale.

phronmophobic 2023-02-21T18:46:01.558619Z

Or maybe figuring out how to add warnings or better error messages.

jlmr 2023-02-21T18:54:20.717379Z

I think a warning would be nice if possible. It took me some time figuring out what was going wrong. And I don’t have a strong opinion yet on whether the current way is good or could be better.

jlmr 2023-02-20T12:35:54.283739Z

I’m also trying to get contextual state working. I’ve made the following toy example:

(def the-state (atom {:scale 1.0
                      :subs [{:x 0
                              :y 0
                              :text "hello"}
                             {:x 10
                              :y 10
                              :text "world"}]}))

(defui sub-component
  [{:keys [x y text]}]
  (ui/translate x y
                (ui/label (str text))))

(defeffect ::scale
  [$scale factor]
  (dispatch! :update $scale #(* % factor)))

(defui main-component
  [{:keys [^:membrane.component/contextual scale subs]}]
  (ui/on-key-press
   (fn [k]
     (case k
       "+"
       [[::scale $scale 1.01]]

       "-"
       [[::scale $scale 0.99]]

       []))

   (apply ui/scale scale scale
             (for [sub subs]
               (sub-component sub)))))

(skia/run (component/make-app #'main-component the-state) {:include-container-info true})
I’d like the scale value to propagate to all sub-components. However, with the ^:membrane.component/contextual meta information the app no longer works. How is this supposed to work?

👀 1
phronmophobic 2023-02-20T19:41:21.031309Z

contextual state lives under the :membrane.component/context key of the app state.

(defui sub-component
  [{:keys [x y text
           ^:membrane.component/contextual scale]}]
  (ui/on
   :mouse-down
   (fn [_]
     [[:set $scale 3.0]])
   (ui/translate x y
                 (ui/label (str text "-" scale)))))

(defeffect ::scale
  [$scale factor]
  (dispatch! :update $scale #(* % factor)))

(defui main-component
  [{:keys [^:membrane.component/contextual scale
           subs]}]
  (ui/on-key-press
   (fn [k]
     (case k
       "+"
       [[::scale $scale 1.01]]

       "-"
       [[::scale $scale 0.99]]

       []))

   (apply ui/scale scale scale
          (for [sub subs]
            (sub-component sub)))))

(skia/run (make-app #'main-component the-state) {:include-container-info true})

phronmophobic 2023-02-20T19:47:39.615269Z

If you want it to live somewhere else, there are options, but it might depend on what you're trying to do.

jlmr 2023-02-21T18:44:12.681809Z

Storing it under :membrane.component/context is fine, this works great, thanks!

🎉 1
genekim 2023-02-20T00:38:39.084159Z

Not urgent — I need to figure out how to get the heights of each room smaller, so I can crank up the density of rooms — is there an easy way I can tighten up the line spacing between the 3x “work remaining” lines? https://github.com/realgenekim/simulator-couch-paint/blob/6c9b6a9833f50f81622e426bb13d9412fefbc23f/src/ui/membrane_ui.clj#L276 Thank you! Another super fun couple of hours making great progress on my app!

genekim 2023-02-20T00:55:28.606309Z

Trying :height…. Can you feed in height of < 1.0? Nothing seems to work when I try that.. Hmm… no value I try seems to make a difference… https://github.com/realgenekim/simulator-couch-paint/blob/8ed0afb2f362a9702cf1a1eef5176a0e08507834/src/ui/membrane_ui.clj#L275

genekim 2023-02-20T00:56:11.031199Z

Wait a second.. I think it worked. Just didn’t update!

genekim 2023-02-20T01:05:38.194899Z

Never mind — it works! 🎉

phronmophobic 2023-02-20T01:08:49.912389Z

I was doing the same thing.

phronmophobic 2023-02-20T01:10:14.217979Z

Just setting the line :height to 1 seems to tighten everything. For whatever reason, adding emoji adjusts the default :height to something greater than 1.

phronmophobic 2023-02-20T01:10:24.991259Z

That might also fix the couch error from earlier

phronmophobic 2023-02-20T01:13:21.798739Z

Another gotcha here is that defui components use caching. That means if you update a function that component relies on, it won't re-evaluate the component unless it gets a new value to render. You can force the component to re-evaluate to by re-evaluating any defui component.

genekim 2023-02-20T01:14:01.027659Z

Baffled by this one — recording (this time in 4K, is that better?) font-size isn’t being picked up by component. Caching? Okay, let me try that. But, color changes, but not font size? https://capture.dropbox.com/HQelqbh1xxyglXTg (Gimme 30s. will check into repo.)

phronmophobic 2023-02-20T01:15:46.035379Z

I bet if you make changes and then re-evaluate any defui component, that it will update everything.

genekim 2023-02-20T01:16:57.829959Z

Hmm. I’m reloading entire namespace everytime, which should re-eval all defui, yes?

phronmophobic 2023-02-20T01:17:09.740359Z

yea

genekim 2023-02-20T01:21:54.153919Z

DOH! Big typo on my part. I think I found it.

phronmophobic 2023-02-20T01:22:28.141569Z

oh, right. I think :font-size should be part of the style map.

genekim 2023-02-20T01:23:58.206409Z

Yes. Sorry about that!!! Okay, that’s a wrap for today! Wooot! Thx again!