@@ -1,4 +1,5 @@
1{-# LANGUAGE RecordWildCards #-}
1{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE PatternSynonyms #-}
2-----------------------------------------------------------------------------
3-----------------------------------------------------------------------------
4-- Module : TestJSC
5-- Module : TestJSC
@@ -19,21 +20,19 @@ module Language.Javascript.JSaddle.Test (
20import Control.Applicative
21import Control.Applicative
21import Prelude hiding((!!), catch)
22import Prelude hiding((!!), catch)
22−import Graphics.UI.Gtk
23+import GI.Gtk
23− (Window, widgetDestroy, postGUIAsync, postGUISync, widgetShowAll,
24+ (Window, widgetDestroy, widgetShowAll,
24− mainGUI, mainQuit, on, objectDestroy, containerAdd, scrolledWindowNew,
25+ mainQuit, containerAdd, scrolledWindowNew,
25− windowSetPosition, windowSetDefaultSize, timeoutAddFull, windowNew,
26+ windowSetPosition, windowSetDefaultSize, windowNew)
27import Control.Concurrent
27import Control.Concurrent
28 (tryTakeMVar, forkIO, newMVar, putMVar, takeMVar, newEmptyMVar,
28 (tryTakeMVar, forkIO, newMVar, putMVar, takeMVar, newEmptyMVar,
30−import System.Glib.MainLoop (priorityLow)
31−import Graphics.UI.Gtk.General.Enums (WindowPosition(..))
30+import GI.Gtk.Enums (WindowType(..), WindowPosition(..))
32−import Graphics.UI.Gtk.WebKit.WebView
31+import GI.WebKit.Objects.WebView
33 (webViewGetMainFrame, webViewNew)
32 (webViewGetMainFrame, webViewNew)
34import System.IO.Unsafe (unsafePerformIO)
33import System.IO.Unsafe (unsafePerformIO)
35import Control.Monad.Trans.Reader (runReaderT)
34import Control.Monad.Trans.Reader (runReaderT)
36−import Graphics.UI.Gtk.WebKit.JavaScriptCore.WebFrame
35+import GI.WebKit.Objects.WebFrame
37 (webFrameGetGlobalContext)
36 (webFrameGetGlobalContext)
38import Language.Javascript.JSaddle
37import Language.Javascript.JSaddle
39import qualified Data.Text as T
38import qualified Data.Text as T
@@ -42,8 +41,40 @@ import Control.Monad (forM, when)
42import Control.Lens.Getter ((^.))
41import Control.Lens.Getter ((^.))
43import Data.Monoid ((<>))
42import Data.Monoid ((<>))
44import Control.Concurrent.MVar (MVar)
43import Control.Concurrent.MVar (MVar)
44+import qualified GI.Gtk.Functions as Gtk (init, main)
45+import GI.GLib.Functions (timeoutAdd)
46+import GI.Gtk.Enums (WindowPosition(..))
47+import Data.GI.Base.Signals (on)
48+import GI.GLib (idleAdd)
49+import GI.GLib.Constants(pattern PRIORITY_DEFAULT, pattern PRIORITY_LOW)
50+import GI.Gtk.Objects.Widget (onWidgetDestroy)
51+import GI.JavaScriptCore.Structs.GlobalContext (GlobalContext(..))
52+import Foreign.ForeignPtr (withForeignPtr)
53+import Foreign.Ptr (castPtr)
54+import GI.Gtk.Objects.Adjustment (Adjustment(..))
56+-- | Post an action to be run in the main GUI thread.
58+-- The current thread blocks until the action completes and the result is
61+postGUISync :: IO a -> IO a
62+postGUISync action = do
63+ resultVar <- newEmptyMVar
64+ idleAdd PRIORITY_DEFAULT $ action >>= putMVar resultVar >> return False
67+-- | Post an action to be run in the main GUI thread.
69+-- The current thread continues and does not wait for the result of the
72+postGUIAsync :: IO () -> IO ()
73+postGUIAsync action = do
74+ idleAdd PRIORITY_DEFAULT $ action >> return False
46−data TestState = TestState { jsContext :: JSContextRef, window :: Window }
77+data TestState = TestState { jsContext :: GlobalContext, window :: Window }
48state :: MVar (Maybe TestState)
79state :: MVar (Maybe TestState)
49state = unsafePerformIO $ newMVar Nothing
80state = unsafePerformIO $ newMVar Nothing
@@ -77,18 +108,18 @@ testJSaddle' showWindow f = do
77 debugLog "fork"
108 debugLog "fork"
78 _ <- forkIO $ do
109 _ <- forkIO $ do
79 debugLog "initGUI"
110 debugLog "initGUI"
80− _ <- initGUI
111+ _ <- Gtk.init Nothing
81 debugLog "windowNew"
112 debugLog "windowNew"
82− window <- windowNew
113+ window <- windowNew WindowTypeToplevel
83 debugLog "timeoutAdd"
114 debugLog "timeoutAdd"
84− _ <- timeoutAddFull (yield >> return True) priorityLow 10
115+ _ <- timeoutAdd PRIORITY_LOW 10 $ yield >> return True
85 windowSetDefaultSize window 900 600
116 windowSetDefaultSize window 900 600
86− windowSetPosition window WinPosCenter
117+ windowSetPosition window WindowPositionCenter
87− scrollWin <- scrolledWindowNew Nothing Nothing
118+ scrollWin <- scrolledWindowNew (Nothing :: Maybe Adjustment) (Nothing :: Maybe Adjustment)
88 webView <- webViewNew
119 webView <- webViewNew
89 window `containerAdd` scrollWin
120 window `containerAdd` scrollWin
90 scrollWin `containerAdd` webView
121 scrollWin `containerAdd` webView
91− _ <- on window objectDestroy $ do
122+ _ <- onWidgetDestroy window $ do
92 debugLog "onDestroy"
123 debugLog "onDestroy"
93 _ <- tryTakeMVar state
124 _ <- tryTakeMVar state
94 debugLog "put state"
125 debugLog "put state"
@@ -105,19 +136,22 @@ testJSaddle' showWindow f = do
105 debugLog "maybe show"
136 debugLog "maybe show"
106 when showWindow $ widgetShowAll window
137 when showWindow $ widgetShowAll window
107 debugLog "mainGUI"
138 debugLog "mainGUI"
108− mainGUI
139+ Gtk.main
109 debugLog "mainGUI exited"
140 debugLog "mainGUI exited"
110 takeMVar newState
141 takeMVar newState
111 Just s@TestState {..} -> do
142 Just s@TestState {..} -> do
112 debugLog "maybe show (2)"
143 debugLog "maybe show (2)"
113 when showWindow . postGUISync $ widgetShowAll window
144 when showWindow . postGUISync $ widgetShowAll window
115− x <- postGUISync $ runReaderT ((f >>= valToText >>= liftIO . putStrLn . T.unpack)
146+ x <- postGUISync $ withContext jsContext $ runReaderT ((f >>= valToText >>= liftIO . putStrLn . T.unpack)
116− `catch` \ (JSException e) -> valToText e >>= liftIO . putStrLn . T.unpack) jsContext
147+ `catch` \ (JSException e) -> valToText e >>= liftIO . putStrLn . T.unpack)
117 debugLog "put state"
148 debugLog "put state"
118 putMVar state $ Just TestState {..}
149 putMVar state $ Just TestState {..}
152+withContext :: GlobalContext -> (JSContextRef -> IO a) -> IO a
153+withContext (GlobalContext fptr) f = withForeignPtr fptr $ f . castPtr
121listWindowProperties :: IO ()
155listWindowProperties :: IO ()
122listWindowProperties = testJSaddle $ T.pack . show <$> do
156listWindowProperties = testJSaddle $ T.pack . show <$> do
123 window <- jsg "window"
157 window <- jsg "window"