membrane

Cal Herries 2023-10-16T09:15:30.858929Z

At the moment, viscous creates a new window every time inspect is called. How can I reuse the same window for new calls to inspect? e.g.

(viscous/inspect 1) ;; creates a new window
(viscous/inspect 2) ;; creates a 2nd window. but I want it to use the first

phronmophobic 2023-10-16T18:33:08.707209Z

The viscous viewer is a reusable component. I typically use it via https://github.com/phronmophobic/spreadsheet which is very alpha, but I use it regularly. I'm not exactly sure what other APIs folks might find useful. Below is a sketch for something that seems to match your intended usage. If you have some thoughts on what the API should look like, I can work to include it as part of viscous.

(require '[com.phronemophobic.viscous :as viscous])
(require 'membrane.component)
(require 'membrane.toolkit)
(require 'membrane.ui)

(defonce viscous-atom (atom nil))
(defn open
  "Open an inspector window to view obj."
  ([]
   (let [width 80
         height 40
         _ (reset! viscous-atom
                   {:obj (viscous/wrap nil)
                    :width width
                    :show-context? true
                    :height height})
         app (membrane.component/make-app #'viscous/inspector
                                          viscous-atom)

         [empty-width empty-height] (membrane.ui/bounds
                                     ((membrane.component/make-app #'viscous/inspector
                                                                   {:obj (viscous/wrap nil)
                                                                    :width 0
                                                                    :height 0})))
         window-width (+ 50
                         (max empty-width
                              (* @viscous/cell-width width)))
         window-height (+ 100
                          empty-height
                          height
                          (* @viscous/cell-height (inc height)))
         sync? false
         run (if sync?
               membrane.toolkit/run-sync
               membrane.toolkit/run)]
     (run @viscous/toolkit
       app
       {:window-title "Inspect"
        :window-start-width window-width
        :window-start-height window-height}))))



(defn inspect [obj]
  (swap! viscous-atom assoc :obj (viscous/wrap obj))
  nil)
Usage:
(open)
(inspect 1)
(inspect 2)

pez 2023-10-16T20:55:08.012249Z

How do I make an element fill the width of a view? Such that I can resize the window and still see the full width.

pez 2023-10-16T20:55:42.026019Z

And I’d like for it to be an aspect scale. 😃

phronmophobic 2023-10-16T21:04:13.811249Z

Something like this should work:

(defn aspect-fill [[w h] elem]
  (let [[ew eh] (ui/bounds elem)
        sx (/ w ew)
        sy (/ h eh)
        s (min sx sy)]
    (ui/scale s s
              elem)))

(def mpos (atom nil))
(defn debug [window-info]
  (let [size [400 600]
        [cw ch] (:container-size window-info)]
    (ui/on
     :mouse-move
     (fn [pos]
       (reset! mpos pos)
       nil)
     (aspect-fill
      [cw ch]
      (ui/vertical-layout
       (ui/label @mpos)
       (ui/label (pr-str (select-keys  window-info [:container-size])))
       (crop
        (compose (ui/image "mybackground.png")
                 {:width 1160
                  :title "ImageMagick + Pango + Babashka = ♥️"
                  :author "Peter Strömberg"
                  :description "Where there's a will there's a way. I can now hack on my ImageMagick + Pango script in my Babashka REPL on my Mac."}
                 )) []
       (ui/filled-rectangle [1 0 0]
                            10 10))))))
(comment
  (skia/run
    #'debug
    {:include-container-info true})
  (skia/run (constantly
             (ui/label "hello world")))
  ,)

phronmophobic 2023-10-16T21:05:30.577849Z

the key changes are: • the aspect-fill function • pass :include-container-info to skia/run as an option • debug now takes a single argument with the window info that includes a :container-size key with the container size as [w h]

pez 2023-10-16T21:10:44.314509Z

Thanks! Works like a charm.

🎉 1
pez 2023-10-16T21:11:00.595249Z

And, yes. It was the debug view I wanted it for. 😃

1