Skip to content

Commit

Permalink
changed around things to make it easier to add hiccup-layers
Browse files Browse the repository at this point in the history
  • Loading branch information
saikyun committed Aug 1, 2021
1 parent ae952c4 commit 9f6f13e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 122 deletions.
14 changes: 12 additions & 2 deletions freja/frp.janet
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Emits events when rerendering is needed.
(var keyboard nil)
(var frame-chan nil)
(var rerender nil)
(var screen-size @{})


(var delay-left @{})
Expand Down Expand Up @@ -189,6 +190,14 @@ Emits events when rerendering is needed.
# ^ but using struct to visualise
(def callbacks @{:event/changed false})


(varfn handle-resize
[]
(when (window-resized?)
(-> screen-size
(e/put! :screen/width (get-screen-width))
(e/put! :screen/height (get-screen-height)))))

(defn push-callback!
[ev cb]
(e/update! callbacks ev (fn [chan]
Expand Down Expand Up @@ -550,6 +559,7 @@ Emits events when rerendering is needed.
[dt]
(handle-keys dt)
(handle-scroll)
(handle-resize)

(ec/push! frame-chan @[:dt dt])

Expand All @@ -571,8 +581,8 @@ Emits events when rerendering is needed.
and a callback (e.g. single arity function).
Creates a regular subscription."
[emitter cb]
(unless (find |(= $ cb) (get-in deps [:deps emitter]))
(update-in deps [:deps emitter] array/push cb)))
(unless (find |(= $ cb) (get-in deps [:deps emitter] []))
(update-in deps [:deps emitter] |(array/push (or $ @[]) cb))))


(defn subscribe-finally!
Expand Down
122 changes: 63 additions & 59 deletions freja/hiccup.janet
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
(import freja-layout/sizing/definite :as def-siz)
(import freja-layout/sizing/relative :as rel-siz)
(import freja-layout/compile-hiccup :as ch)
(import freja-layout/jaylib-tags :as jt)

(import spork/test)


# TODO: remove
(use jaylib)

(defonce render-tree @{})

(var children-on-event nil)
Expand Down Expand Up @@ -79,23 +76,23 @@

(with-dyns [:text/font text/font
:text/size text/size
:text/get-font a/font]
:text/get-font a/font]
#(print "compiling tree...")
(def root #(test/timeit
(ch/compile [hiccup props]
:tags tags
:element old-root)
#)
)
(ch/compile [hiccup props]
:tags tags
:element old-root)
#)
)

#(print "sizing tree...")
(def root-with-sizes
#(test/timeit
(-> root
(def-siz/set-definite-sizes max-width max-height)
(rel-siz/set-relative-size max-width max-height))
#)
)
(-> root
(def-siz/set-definite-sizes max-width max-height)
(rel-siz/set-relative-size max-width max-height))
#)
)

(put props :compilation/changed false)

Expand All @@ -119,6 +116,48 @@
(put l :on-event (fn [& _])))
(put named-layers name nil))

(def default-hiccup-renderer
{:draw (fn [self dt]
(with-dyns [:text/get-font a/font]
((self :render)
(self :root))))
:on-event (fn [self ev]
(match ev
@{:screen/width w
:screen/height h}
(do
(put self :max-width w)
(put self :max-height h)

(put self :root
(compile-tree
(self :hiccup)
(self :props)
:tags (self :tags)
:max-width (self :max-width)
:max-height (self :max-height)
:text/font (self :text/font)
:text/size (self :text/size)
:old-root (self :root))))

[:dt dt]
(:draw self dt)

'(table? ev)
(do # (print "compiling tree!")
(put self :props ev)
(put self :root
(compile-tree
(self :hiccup)
ev
:tags (self :tags)
:max-width (self :max-width)
:max-height (self :max-height)
:text/font (self :text/font)
:text/size (self :text/size)
:old-root (self :root))))

(handle-ev (self :root) ev)))})

(defn new-layer
[name
Expand All @@ -142,65 +181,30 @@

(put render-tree :hiccup hiccup)

(assert render "must provide a :render function to `new-layer`")
(default render jt/render)
(put render-tree :render render)

(default max-width (frp/screen-size :width))
(put render-tree :max-width max-width)
(assert max-width "must provide :max-width")

(default max-width (frp/screen-size :height))
(put render-tree :max-height max-height)
(assert max-height "must provide :max-height")

(default tags jt/tags)
(put render-tree :tags tags)
(assert tags "must provide :tags")

(put render-tree :text/font text/font)
(put render-tree :text/size text/size)

(merge-into
render-tree
@{:draw (fn [self dt]
(with-dyns [:text/get-font a/font]
((self :render)
(self :root))))
:on-event (fn [self ev]
(match ev
[:dt dt]
(do

(when (window-resized?)
(put self :max-width (get-screen-width))

(put self :root
(compile-tree
(self :hiccup)
(self :props)
:tags tags
:max-width (self :max-width)
:max-height (self :max-height)
:text/font text/font
:text/size text/size
:old-root (self :root))))

(:draw self dt))

'(table? ev)
(do # (print "compiling tree!")
(put self :props ev)
(put self :root
(compile-tree
(self :hiccup)
ev
:tags tags
:max-width (self :max-width)
:max-height (self :max-height)
:text/font text/font
:text/size text/size
:old-root (self :root))))

(handle-ev (self :root) ev)))})
default-hiccup-renderer)

(put props :event/changed true)

(put-in frp/deps [:deps props] [render-tree])
(frp/subscribe-finally! frp/frame-chan render-tree)
(frp/subscribe! frp/mouse render-tree)
(frp/subscribe! frp/screen-size render-tree)

render-tree)
68 changes: 7 additions & 61 deletions freja/newest-menu.janet
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
(import freja-layout/jaylib-tags :as jt)
(import freja-layout/sizing/relative :as rs)
(import freja-layout/compile-hiccup :as ch)
(use freja-layout/put-many)

(import freja/hiccup :as h)
Expand All @@ -9,7 +7,7 @@
(import freja/input :as i)
(import freja/new_gap_buffer :as gb)

(use ./defonce)
(use freja/defonce)
(use jaylib)

(setdyn :pretty-format "%.40M")
Expand All @@ -25,18 +23,6 @@

(def kws {:control "Ctrl"})

(defn event-handler
``
Useful for handling any kind of event, even if it is not
happening inside of the layout bounding box.
``
[props & _]
(-> (dyn :element)
(put-many
:on-event (props :on-event)
:relative-sizing rs/flow-sizing
:props props)))

(defn kw->string
[kw]
(get kws
Expand Down Expand Up @@ -117,10 +103,10 @@ happening inside of the layout bounding box.

(defn hiccup
[props & children]
[event-handler {:on-event
(fn [self [ev-kind]]
(when (= ev-kind :release)
(e/put! my-props :open-menu nil)))}
[:event-handler {:on-event
(fn [self [ev-kind]]
(when (= ev-kind :release)
(e/put! my-props :open-menu nil)))}

[:padding {:left 0 :top 0}
[:background {:color bar-bg}
Expand Down Expand Up @@ -159,53 +145,13 @@ happening inside of the layout bounding box.
[:padding {:all 8
:top 3}
[edit-menu props]]]]))]])
(comment
(defn hiccup
[props & children]
[:padding {:top 40 :left 600}
[:background {:color :red}
[:shrink {}
[:row {}
[:flow {:weight 1}
[:align {:horizontal :left}
[:padding {:right 100}
"Open"]]]

[:flow {:weight 1}
"wat"]]]]]))
(var c nil)

(def b 10)

b

(defn init
[]
(set c (h/new-layer :test-layer2
hiccup
my-props
:render jt/render
:tags jt/tags
:text/font "Poppins"
:text/size 22
:max-width (get-screen-width)
:max-height (get-screen-height))))
(h/new-layer :menu-layer hiccup my-props))

#
# this will only be true when running load-file inside freja
(when ((curenv) :freja/loading-file)
(print "reiniting :)")
(init))

(comment
# the below can be used to print the render tree
(import freja-layout/compile-hiccup :as ch :fresh true)
(import freja-layout/sizing/definite :as ds :fresh true)
(import freja-layout/sizing/relative :as rs :fresh true)

(let [el (ch/compile [hiccup my-props]
:tags jt/tags)
el (ds/set-definite-sizes el 800 600)
el (rs/set-relative-size el 800 600)]
(ch/print-tree el))
#
)
3 changes: 3 additions & 0 deletions test_resize.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(import freja/frp)

(frp/subscribe! frp/screen-size pp)

0 comments on commit 9f6f13e

Please sign in to comment.