comparison 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
comparison
equal deleted inserted replaced
19:3a303df9ae92 20:554e342665fe
1 datatype panelState = Open | Closed 1 datatype panelState = Open | Closed
2 2
3 con formatCtl :: {Unit} -> Type = fn other_ctx => {FormatPanel : (xml ([Body] ++ other_ctx) [] []) -> (xml ([Body] ++ other_ctx) [] []) -> (xml ([Body] ++ other_ctx) [] []), 3 con formatCtl = fn ctx :: {Unit} => [body ~ ctx] =>
4 OpenCtl : transaction unit -> xml ([Body] ++ other_ctx) [] [], 4 {FormatPanel : xbody -> xbody -> xml (body ++ ctx) [] [],
5 CloseCtl : transaction unit -> xml ([Body] ++ other_ctx) [] []} 5 OpenCtl : transaction unit -> xbody,
6 CloseCtl : transaction unit -> xbody}
6 7
7 val defaultFormat = {FormatPanel = fn ctl panel => <xml>{ctl}{panel}</xml>, 8 val defaultFormat [body ~ []] =
8 OpenCtl = fn behaviour => <xml><button value="Open" onclick={behaviour}/></xml>, 9 {FormatPanel = fn ctl panel => <xml>{ctl}{panel}</xml>,
9 CloseCtl = fn behaviour => <xml><button value="Close" onclick={behaviour}/></xml>} 10 OpenCtl = fn behaviour => <xml><button value="Open" onclick={behaviour}/></xml>,
11 CloseCtl = fn behaviour => <xml><button value="Close" onclick={behaviour}/></xml>}
10 12
11 con togglePanel t other_ctx = {PanelState : source panelState, 13 con togglePanel t ctx = {PanelState : source panelState,
12 FormatCtl : formatCtl other_ctx, 14 FormatCtl : formatCtl ctx,
13 Content : t} 15 Content : t}
14 16
15 open Gui 17 open Gui
16 18
17 fun create [t ::: Type] [other_ctx:::{Unit}] [other_ctx ~ body] (toXml : gui t (xml ([Body] ++ other_ctx) [] [])) (f : formatCtl other_ctx) (content : t) (startOpen : bool) : transaction (togglePanel t other_ctx) = 19 fun create [t ::: Type] [ctx ::: {Unit}] (f : formatCtl ctx) (content : t) (startOpen : bool) : transaction (togglePanel t ctx) =
18 state <- source (if startOpen then Open else Closed); 20 state <- source (if startOpen then Open else Closed);
19 21
20 return {PanelState = state, 22 return {PanelState = state,
21 FormatCtl = f, 23 FormatCtl = @f,
22 Content = content} 24 Content = content}
23 25
24 fun render [t ::: Type] [other_ctx:::{Unit}] [other_ctx ~ body] (_ : gui t (xml ([Body] ++ other_ctx) [] [])) (panel : togglePanel t other_ctx) = 26 fun render [t ::: Type] [ctx ::: {Unit}] (_ : gui t ctx) [body ~ ctx] (panel : togglePanel t ctx) =
25 let 27 let
26 val openCtl = panel.FormatCtl.CloseCtl (set panel.PanelState Closed) 28 val format = panel.FormatCtl !
27 val closeCtl = panel.FormatCtl.OpenCtl (set panel.PanelState Open) 29 val openCtl = format.CloseCtl (set panel.PanelState Closed)
30 val closeCtl = format.OpenCtl (set panel.PanelState Open)
28 31
29 val content = toXml panel.Content 32 val content = toXml panel.Content
30 in 33 in
31 panel.FormatCtl.FormatPanel 34 format.FormatPanel
32 <xml> 35 <xml>
33 <dyn signal={c <- signal panel.PanelState; 36 <dyn signal={c <- signal panel.PanelState;
34 return 37 return
35 (case c of 38 (case c of
36 Open => <xml>{openCtl}</xml> 39 Open => <xml>{openCtl}</xml>
46 | Closed => <xml/>) 49 | Closed => <xml/>)
47 }/> 50 }/>
48 </xml> 51 </xml>
49 end 52 end
50 53
51 fun gui_togglePanel [t ::: Type] [other_ctx:::{Unit}] [other_ctx ~ body] (_ : gui t (xml ([Body] ++ other_ctx) [] [])) = mkGui render 54 fun gui_togglePanel [t ::: Type] [ctx ::: {Unit}] (g : gui t ctx) = mkGui (@render g)