src/Pure/GUI/jfx_thread.scala
changeset 53783 f5e9d182f645
parent 49066 1067a639d42a
child 53853 e8430d668f44
equal deleted inserted replaced
53782:3746a78a2c01 53783:f5e9d182f645
       
     1 /*  Title:      Pure/GUI/jfx_thread.scala
       
     2     Author:     Makarius
       
     3 
       
     4 Evaluation within the Java FX application thread.
       
     5 */
       
     6 
       
     7 package isabelle
       
     8 
       
     9 import javafx.application.{Platform => JFX_Platform}
       
    10 
       
    11 
       
    12 object JFX_Thread
       
    13 {
       
    14   /* checks */
       
    15 
       
    16   def assert() = Predef.assert(JFX_Platform.isFxApplicationThread())
       
    17   def require() = Predef.require(JFX_Platform.isFxApplicationThread())
       
    18 
       
    19 
       
    20   /* asynchronous context switch */
       
    21 
       
    22   def later(body: => Unit)
       
    23   {
       
    24     if (JFX_Platform.isFxApplicationThread()) body
       
    25     else JFX_Platform.runLater(new Runnable { def run = body })
       
    26   }
       
    27 
       
    28   def future[A](body: => A): Future[A] =
       
    29   {
       
    30     if (JFX_Platform.isFxApplicationThread()) Future.value(body)
       
    31     else {
       
    32       val promise = Future.promise[A]
       
    33       later { promise.fulfill_result(Exn.capture(body)) }
       
    34       promise
       
    35     }
       
    36   }
       
    37 }