Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

Commit

Permalink
feat(client): changed the tiny existing render code to use the update…
Browse files Browse the repository at this point in the history
…d core
  • Loading branch information
prescientmoon committed Oct 14, 2020
1 parent 8d06e63 commit 8ba7f2a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/client/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
, "effect"
, "console"
, "canvas"
, "run"
, "fixed-points"
, "matryoshka"
, "lunarflow-utils"
, "lunarflow-core"
, "lunarflow-geometry"
Expand Down
43 changes: 43 additions & 0 deletions packages/client/src/Render.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Lunarbox.Render where

import Prelude
import Data.List as List
import Lunarflow.Ast (AstF(..))
import Lunarflow.Geometry.Foreign (fitIntoBounds)
import Lunarflow.Geometry.Types as Shape
import Lunarflow.Layout (Layout, LayoutF)
import Matryoshka (Algebra, cata)
import Run (Run)
import Run.Reader (READER, local)

type RenderContext
= { doNotRender :: List.List Int
}

type RenderM r
= Run ( reader :: READER RenderContext | r )

-- | Prepare stuff for rendering inside a lambda.
shiftContext :: Int -> RenderContext -> RenderContext
shiftContext by ctx = ctx { doNotRender = ((+) by) <$> ctx.doNotRender }

{--
So:
- When we encounter a lambda, we draw the body and then the box around it
- When we encouner a var, we check where it is in scope and draw until here
--}
render :: forall r. Layout -> RenderM r Shape.Shape
render = cata algebra
where
algebra :: Algebra LayoutF (RenderM r Shape.Shape)
algebra (Lambda { args } body) = do
bodyShape <- local (shiftContext $ List.length args) body
let
bounds = fitIntoBounds bodyShape
pure
$ Shape.group {}
[ Shape.fromBounds { fill: "transparent", stroke: "red" } bounds
, bodyShape
]

algebra _ = pure mempty

0 comments on commit 8ba7f2a

Please sign in to comment.