Skip to content

Instantly share code, notes, and snippets.

@john-kelly
Created March 1, 2018 00:47
Show Gist options
  • Save john-kelly/5707768886b80f531a978121bc33aa2a to your computer and use it in GitHub Desktop.
Save john-kelly/5707768886b80f531a978121bc33aa2a to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Color
import Element exposing (..)
import Element.Background as Background
import Element.Border as Border
import Element.Events as Events
import Element.Font as Font
import Html exposing (Html)
main =
Html.program
{ view = view
, update = update
, init = init
, subscriptions = \_ -> Sub.none
}
view : Model -> Html Msg
view model =
Element.layout
[ Background.color Color.blue
]
<|
el
[ width (px 500), height (px 500) ]
(draw model.wm)
update : Msg -> Model -> ( Model, Cmd Msg )
update (Click int) model =
( { wm = split (toOrientation model.flip) int model.wm
, flip = not model.flip
}
, Cmd.none
)
toOrientation : Bool -> Orientation
toOrientation flip =
if flip then
Horizontal
else
Vertical
init : ( Model, Cmd Msg )
init =
( { wm = Panel [], flip = True }, Cmd.none )
type alias Model =
{ wm : WM, flip : Bool }
type Msg
= Click Path
type Orientation
= Horizontal
| Vertical
type Direction
= Left
| Right
type alias Path =
List Direction
type WM
= Split Path Orientation WM WM
| Panel Path
split : Orientation -> Path -> WM -> WM
split newSplitOrientation splitPath wm =
-- NOTE: naive approach... O(n). we can follow the path and get O(log n)
case wm of
Panel path ->
if splitPath == path then
Split path
newSplitOrientation
(Panel (Left :: path))
(Panel (Right :: path))
else
Panel path
Split path orientation left right ->
-- trying to split a split -- just return out. no recursion.
if splitPath == path then
Split path orientation left right
else
Split path
orientation
(split newSplitOrientation splitPath left)
(split newSplitOrientation splitPath right)
draw : WM -> Element Msg
draw wm =
case wm of
Panel path ->
el
[ height fill
, width fill
, Border.width 1
, Border.color Color.black
, Border.solid
, Events.onClick (Click path)
]
Element.empty
Split _ _ wm1 wm2 ->
case o of
Horizontal ->
row
[ height fill, width fill ]
[ draw wm1, draw wm2 ]
Vertical ->
column
[ height fill, width fill ]
[ draw wm1, draw wm2 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment