view togglePanel.ur @ 18:16447dc6a68c

Introduction of multi-parameter type class for gui components
author Ron de Bruijn <rmbruijn@gmail.com>
date Thu, 22 Sep 2011 19:24:01 +0200
parents 90be8b8917d5
children 3a303df9ae92
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 xbody) (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 xbody) (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 xbody) = mkGui render