| author | wenzelm | 
| Wed, 28 Dec 2016 10:39:50 +0100 | |
| changeset 64677 | 8dc24130e8fe | 
| parent 64370 | 865b39487b5d | 
| child 66094 | 24658c9d7c78 | 
| permissions | -rw-r--r-- | 
| 57612 
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
 wenzelm parents: 
56773diff
changeset | 1 | /* Title: Pure/GUI/gui_thread.scala | 
| 29202 | 2 | Author: Makarius | 
| 3 | ||
| 57613 | 4 | Evaluation within the GUI thread (for AWT/Swing). | 
| 29202 | 5 | */ | 
| 6 | ||
| 7 | package isabelle | |
| 8 | ||
| 55618 | 9 | |
| 56773 | 10 | import javax.swing.SwingUtilities | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 11 | |
| 29202 | 12 | |
| 57612 
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
 wenzelm parents: 
56773diff
changeset | 13 | object GUI_Thread | 
| 29202 | 14 | {
 | 
| 56773 | 15 | /* context check */ | 
| 32494 | 16 | |
| 52859 | 17 | def assert[A](body: => A) = | 
| 18 |   {
 | |
| 19 | Predef.assert(SwingUtilities.isEventDispatchThread()) | |
| 20 | body | |
| 21 | } | |
| 32494 | 22 | |
| 52859 | 23 | def require[A](body: => A) = | 
| 24 |   {
 | |
| 25 | Predef.require(SwingUtilities.isEventDispatchThread()) | |
| 26 | body | |
| 27 | } | |
| 52477 | 28 | |
| 32494 | 29 | |
| 56773 | 30 | /* event dispatch queue */ | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 31 | |
| 34026 | 32 | def now[A](body: => A): A = | 
| 33 |   {
 | |
| 48000 
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
 wenzelm parents: 
47989diff
changeset | 34 | if (SwingUtilities.isEventDispatchThread()) body | 
| 
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
 wenzelm parents: 
47989diff
changeset | 35 |     else {
 | 
| 56662 
f373fb77e0a4
avoid "Adaptation of argument list by inserting ()" -- deprecated in scala-2.11.0;
 wenzelm parents: 
55618diff
changeset | 36 |       lazy val result = { assert { Exn.capture(body) } }
 | 
| 48000 
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
 wenzelm parents: 
47989diff
changeset | 37 |       SwingUtilities.invokeAndWait(new Runnable { def run = result })
 | 
| 
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
 wenzelm parents: 
47989diff
changeset | 38 | Exn.release(result) | 
| 
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
 wenzelm parents: 
47989diff
changeset | 39 | } | 
| 29649 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 40 | } | 
| 29202 | 41 | |
| 34299 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 42 | def later(body: => Unit) | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 43 |   {
 | 
| 32494 | 44 | if (SwingUtilities.isEventDispatchThread()) body | 
| 29649 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 45 |     else SwingUtilities.invokeLater(new Runnable { def run = body })
 | 
| 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 46 | } | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 47 | |
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 48 | |
| 56770 
e160ae47db94
mane delayed events outside of Swing thread -- triggers no longer require Swing_Thread.later;
 wenzelm parents: 
56662diff
changeset | 49 | /* delayed events */ | 
| 49195 | 50 | |
| 62263 | 51 | def delay_first(delay: => Time)(event: => Unit): Standard_Thread.Delay = | 
| 52 |     Standard_Thread.delay_first(delay) { later { event } }
 | |
| 49196 
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
 wenzelm parents: 
49195diff
changeset | 53 | |
| 62263 | 54 | def delay_last(delay: => Time)(event: => Unit): Standard_Thread.Delay = | 
| 55 |     Standard_Thread.delay_last(delay) { later { event } }
 | |
| 29202 | 56 | } |