diff waitbox.ur @ 0:37eefd0a2ed4

Import code from elsewhere
author Adam Chlipala <adam@chlipala.net>
date Tue, 14 Dec 2010 10:55:22 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waitbox.ur	Tue Dec 14 10:55:22 2010 -0500
@@ -0,0 +1,47 @@
+type t = {Milliseconds : int,
+          Action : source (string -> transaction {}),
+          Timer : source (option Timer.t),
+          Text : source string}
+
+fun create n =
+    s <- source "";
+    tmO <- source None;
+    f <- source (fn _ => return ());
+
+    return {Milliseconds = n, Action = f, Timer = tmO, Text = s}
+
+fun setAction r v = set r.Action v
+
+fun tickle r =
+    last <- get r.Timer;
+    (case last of
+         None => return ()
+       | Some tm => Timer.cancel tm);
+    tm <- Timer.create {Milliseconds = r.Milliseconds,
+                        Action = (set r.Timer None;
+                                  s <- get r.Text;
+                                  f <- get r.Action;
+                                  f s)};
+    set r.Timer (Some tm)
+
+fun render r = <xml>
+  <ctextbox source={r.Text}
+            onkeyup={fn _ => tickle r}/>
+</xml>
+
+fun clear r = (set r.Text "";
+               tm <- get r.Timer;
+               (case tm of
+                    None => return ()
+                  | Some tm => Timer.cancel tm);
+               set r.Timer None)
+
+fun trigger r =
+    last <- get r.Timer;
+    (case last of
+         None => return ()
+       | Some tm => Timer.cancel tm);
+    set r.Timer None;
+    s <- get r.Text;
+    f <- get r.Action;
+    f s