annotate togglePanel.ur @ 20:554e342665fe

Add a new parameter to Gui.gui
author Adam Chlipala <adam@chlipala.net>
date Sat, 24 Sep 2011 15:47:00 -0400
parents 3a303df9ae92
children 30f9a763f5fb
rev   line source
kkallio@8 1 datatype panelState = Open | Closed
kkallio@8 2
adam@20 3 con formatCtl = fn ctx :: {Unit} => [body ~ ctx] =>
adam@20 4 {FormatPanel : xbody -> xbody -> xml (body ++ ctx) [] [],
adam@20 5 OpenCtl : transaction unit -> xbody,
adam@20 6 CloseCtl : transaction unit -> xbody}
kkallio@8 7
adam@20 8 val defaultFormat [body ~ []] =
adam@20 9 {FormatPanel = fn ctl panel => <xml>{ctl}{panel}</xml>,
adam@20 10 OpenCtl = fn behaviour => <xml><button value="Open" onclick={behaviour}/></xml>,
adam@20 11 CloseCtl = fn behaviour => <xml><button value="Close" onclick={behaviour}/></xml>}
kkallio@8 12
adam@20 13 con togglePanel t ctx = {PanelState : source panelState,
adam@20 14 FormatCtl : formatCtl ctx,
adam@20 15 Content : t}
kkallio@8 16
kkallio@8 17 open Gui
kkallio@8 18
adam@20 19 fun create [t ::: Type] [ctx ::: {Unit}] (f : formatCtl ctx) (content : t) (startOpen : bool) : transaction (togglePanel t ctx) =
kkallio@8 20 state <- source (if startOpen then Open else Closed);
kkallio@8 21
kkallio@8 22 return {PanelState = state,
adam@20 23 FormatCtl = @f,
kkallio@8 24 Content = content}
kkallio@8 25
adam@20 26 fun render [t ::: Type] [ctx ::: {Unit}] (_ : gui t ctx) [body ~ ctx] (panel : togglePanel t ctx) =
kkallio@8 27 let
adam@20 28 val format = panel.FormatCtl !
adam@20 29 val openCtl = format.CloseCtl (set panel.PanelState Closed)
adam@20 30 val closeCtl = format.OpenCtl (set panel.PanelState Open)
kkallio@8 31
kkallio@8 32 val content = toXml panel.Content
kkallio@8 33 in
adam@20 34 format.FormatPanel
kkallio@8 35 <xml>
kkallio@8 36 <dyn signal={c <- signal panel.PanelState;
kkallio@8 37 return
kkallio@8 38 (case c of
kkallio@8 39 Open => <xml>{openCtl}</xml>
kkallio@8 40 | Closed => <xml>{closeCtl}</xml>)
kkallio@8 41 }/>
kkallio@8 42 </xml>
kkallio@8 43
kkallio@8 44 <xml>
kkallio@8 45 <dyn signal={c <- signal panel.PanelState;
kkallio@8 46 return
kkallio@8 47 (case c of
kkallio@8 48 Open => <xml>{content}</xml>
kkallio@8 49 | Closed => <xml/>)
kkallio@8 50 }/>
kkallio@8 51 </xml>
kkallio@8 52 end
kkallio@8 53
adam@20 54 fun gui_togglePanel [t ::: Type] [ctx ::: {Unit}] (g : gui t ctx) = mkGui (@render g)