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