src/Pure/GUI/gui_thread.scala
changeset 71716 d1538d4de057
parent 71704 b9a5eb0f3b43
child 73120 c3589f2dff31
equal deleted inserted replaced
71715:9e2f52d0aec3 71716:d1538d4de057
    14 {
    14 {
    15   /* context check */
    15   /* context check */
    16 
    16 
    17   def assert[A](body: => A): A =
    17   def assert[A](body: => A): A =
    18   {
    18   {
    19     Predef.assert(SwingUtilities.isEventDispatchThread())
    19     Predef.assert(SwingUtilities.isEventDispatchThread)
    20     body
    20     body
    21   }
    21   }
    22 
    22 
    23   def require[A](body: => A): A =
    23   def require[A](body: => A): A =
    24   {
    24   {
    25     Predef.require(SwingUtilities.isEventDispatchThread())
    25     Predef.require(SwingUtilities.isEventDispatchThread)
    26     body
    26     body
    27   }
    27   }
    28 
    28 
    29 
    29 
    30   /* event dispatch queue */
    30   /* event dispatch queue */
    31 
    31 
    32   def now[A](body: => A): A =
    32   def now[A](body: => A): A =
    33   {
    33   {
    34     if (SwingUtilities.isEventDispatchThread()) body
    34     if (SwingUtilities.isEventDispatchThread) body
    35     else {
    35     else {
    36       lazy val result = { assert { Exn.capture(body) } }
    36       lazy val result = assert { Exn.capture(body) }
    37       SwingUtilities.invokeAndWait(new Runnable { def run = result })
    37       SwingUtilities.invokeAndWait(() => result)
    38       Exn.release(result)
    38       Exn.release(result)
    39     }
    39     }
    40   }
    40   }
    41 
    41 
    42   def later(body: => Unit)
    42   def later(body: => Unit)
    43   {
    43   {
    44     if (SwingUtilities.isEventDispatchThread()) body
    44     if (SwingUtilities.isEventDispatchThread) body
    45     else SwingUtilities.invokeLater(new Runnable { def run = body })
    45     else SwingUtilities.invokeLater(() => body)
    46   }
    46   }
    47 
    47 
    48   def future[A](body: => A): Future[A] =
    48   def future[A](body: => A): Future[A] =
    49   {
    49   {
    50     if (SwingUtilities.isEventDispatchThread()) Future.value(body)
    50     if (SwingUtilities.isEventDispatchThread) Future.value(body)
    51     else {
    51     else {
    52       val promise = Future.promise[A]
    52       val promise = Future.promise[A]
    53       later { promise.fulfill_result(Exn.capture(body)) }
    53       later { promise.fulfill_result(Exn.capture(body)) }
    54       promise
    54       promise
    55     }
    55     }