module tut11_3_2

// ********************************************************************************
// Clean tutorial example program.
//
// This program creates a simple program that uses the stopwatch process.
// The program only has a menu to open the stopwatch and control it.
// ********************************************************************************

import StdEnv, StdIO
import tut11_3_2_stopwatch

Start :: *World -> *World
Start world
# (stopwatchId, world)  = openRId world
= startIO SDI Void (initialise stopwatchId) [] world

initialise :: (RId StopwatchCommands) (PSt .l) -> PSt .l
initialise stopwatchId pst
# (error, pst)      = openMenu Void mdef pst
| error <> NoError  = closeProcess pst
| otherwise         = openProcesses (stopwatch stopwatchId) pst
where
    mdef = Menu "&Stopwatch" (MenuItem "&Reset"     [MenuFunction (noLS (send Reset))] :+:
                              MenuItem "&Pause"     [MenuFunction (noLS (send Pause))] :+:
                              MenuItem "C&ontinue"  [MenuFunction (noLS (send Continue))] :+:
                              MenuItem "&Close"     [MenuFunction (noLS (send Close))] :+:
                              MenuSeparator         [] :+:
                              MenuItem "&Quit"      [MenuFunction (noLS (closeProcess o (send Close)))]) []
    where
        send msg pst
        # (error, pst)      = syncSend stopwatchId msg pst
        | error <> SendOk   = appPIO beep pst
        | otherwise         = pst