src/Pure/GUI/gui_thread.scala
author wenzelm
Wed, 30 Dec 2015 21:57:52 +0100
changeset 62002 f1599e98c4d0
parent 61556 0d4ee4168e41
child 62263 2c76c66897fc
permissions -rw-r--r--
isabelle update_cartouches -c -t;

/*  Title:      Pure/GUI/gui_thread.scala
    Module:     PIDE-GUI
    Author:     Makarius

Evaluation within the GUI thread (for AWT/Swing).
*/

package isabelle


import javax.swing.SwingUtilities


object GUI_Thread
{
  /* context check */

  def assert[A](body: => A) =
  {
    Predef.assert(SwingUtilities.isEventDispatchThread())
    body
  }

  def require[A](body: => A) =
  {
    Predef.require(SwingUtilities.isEventDispatchThread())
    body
  }


  /* event dispatch queue */

  def now[A](body: => A): A =
  {
    if (SwingUtilities.isEventDispatchThread()) body
    else {
      lazy val result = { assert { Exn.capture(body) } }
      SwingUtilities.invokeAndWait(new Runnable { def run = result })
      Exn.release(result)
    }
  }

  def later(body: => Unit)
  {
    if (SwingUtilities.isEventDispatchThread()) body
    else SwingUtilities.invokeLater(new Runnable { def run = body })
  }


  /* delayed events */

  def delay_first(delay: => Time, cancel: () => Unit = () => ())(event: => Unit)
    : Standard_Thread.Delay = Standard_Thread.delay_first(delay, cancel) { later { event } }

  def delay_last(delay: => Time, cancel: () => Unit = () => ())(event: => Unit)
    : Standard_Thread.Delay = Standard_Thread.delay_last(delay, cancel) { later { event } }
}