;; open Widget ;; open Gctx (* At this point we've extended the demo to include *) (* 1. mutable state to track whether the bulb is lit. *) (* 2. a new handle function for the On/Off label that *) (* updates this state and changes the label. *) (* The next step is to rewrite this code using a *) (* notifier & notifier controller, so that we don't have *) (* to make a completely new widget. *) (* type notifier_controller = *) (* { add_listener : event_listener -> unit } *) (* val notifier : widget -> widget * notifier_controller *) type bulb_state = { mutable lit : bool } let bulb = { lit = false } let lw,lc = label "Off" let widget, l_nc = notifier lw (* { repaint = lw.repaint ; size = lw.size ; handle = mouseclick_listener (fun () -> ((if bulb.lit then lc.set_label "On " else lc.set_label "Off"); bulb.lit <- not (bulb.lit))) } *) ;; l_nc.add_event_listener (mouseclick_listener (fun () -> ((if bulb.lit then lc.set_label "On " else lc.set_label "Off"); bulb.lit <- not (bulb.lit)))) let sq_repaint (gc:gctx) : unit = let c = if bulb.lit then Gctx.yellow else Gctx.black in Gctx.fill_rect (Gctx.with_color gc c) (0,99) (99,99) let sq,_ = canvas (100,100) sq_repaint let quit_w, _, quit_nc = button "QUIT" ;; quit_nc.add_event_listener (mouseclick_listener (fun () -> exit 0)) let top = hpair sq (hpair (space (10,10)) (hpair (border widget)(border quit_w) )) ;; Eventloop.run top