view togglePanel.ur @ 22:4362b15220e4

Comment corrections based on code review.
author Karn Kallio <kkallio@eka>
date Mon, 22 Aug 2011 05:06:15 -0430
parents 90be8b8917d5
children 16447dc6a68c
line wrap: on
line source
datatype panelState = Open | Closed

type formatCtl = {FormatPanel : xbody -> xbody -> xbody,
                  OpenCtl : transaction unit -> xbody,
                  CloseCtl : transaction unit -> xbody}

val defaultFormat = {FormatPanel = fn ctl panel => <xml>{ctl}{panel}</xml>,
                     OpenCtl = fn behaviour => <xml><button value="Open" onclick={behaviour}/></xml>,
                     CloseCtl = fn behaviour => <xml><button value="Close" onclick={behaviour}/></xml>}
                    
con togglePanel t = {PanelState : source panelState,
                     FormatCtl : formatCtl,
                     Content : t}

open Gui

fun create [t ::: Type] (toXml : gui t) (f : formatCtl) (content : t) (startOpen : bool) : transaction (togglePanel t) =
    state <- source (if startOpen then Open else Closed);

    return {PanelState = state,
            FormatCtl = f,
            Content = content}

fun render [t ::: Type] (_ : gui t) (panel : togglePanel t) =
    let
        val openCtl = panel.FormatCtl.CloseCtl (set panel.PanelState Closed)
        val closeCtl = panel.FormatCtl.OpenCtl (set panel.PanelState Open)

        val content = toXml panel.Content
    in
        panel.FormatCtl.FormatPanel
            <xml>
              <dyn signal={c <- signal panel.PanelState;
                           return
                               (case c of
                                    Open => <xml>{openCtl}</xml>
                                  | Closed => <xml>{closeCtl}</xml>)
                          }/>
            </xml>

            <xml>
              <dyn signal={c <- signal panel.PanelState;
                           return
                               (case c of
                                    Open => <xml>{content}</xml>
                                  | Closed => <xml/>)
                          }/>
            </xml>
    end

fun gui_togglePanel [t ::: Type] (_ : gui t) = mkGui render