on master help sign in
Switch from Gtk2Hs to haskell-gi
commit5d0ed254aaa0ba1d13aa2dc8f5f6208355aa6ec3 authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateSat 9 Apr 2016, 23:56 (11 years ago) parent654f5e9
4 files changed+100−61
jsaddle.cabal+13−27 src/Language/Javascript/JSaddle/Monad.hs+34−6 src/Language/Javascript/JSaddle/Test.hs+53−19 tests/DocTest.hs−9
jsaddle.cabal+13−27
@@ -1,5 +1,5 @@
11name: jsaddle
2−version: 0.3.0.3
2+version: 0.4.0.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -17,10 +17,6 @@ source-repository head
1717 type: git
1818 location: https://github.com/ghcjs/jsaddle
1919
20−flag gtk3
21− description:
22− Use GTK3 rather than GTK2
23−
2420library
2521
2622 if impl(ghcjs -any)
@@ -28,18 +24,13 @@ library
2824 ghcjs-base -any,
2925 ghcjs-prim -any
3026 else
31− if flag(gtk3)
32− build-depends:
33− gtk3 >=0.14.2 && <0.15,
34− webkitgtk3 >=0.13.0.0 && <0.15,
35− webkitgtk3-javascriptcore >=0.13.1.2 && <0.14
36− else
37− build-depends:
38− gtk >=0.14.2 && <0.15,
39− webkit >=0.13.0.0 && <0.15,
40− webkit-javascriptcore >=0.13.1.2 && <0.14
4127 build-depends:
42− glib -any
28+ haskell-gi-base >= 0.13 && < 0.14,
29+ gi-glib >=0.2.44 && <0.2.47,
30+ gi-gtk >=0.3.16 && <0.3.19,
31+ gi-webkit >=0.2.4 && <0.2.5,
32+ gi-javascriptcore >=0.2.4 && <0.2.5,
33+ webkitgtk3-javascriptcore >=0.14.0.0 && <0.15
4334 exposed-modules:
4435 Language.Javascript.JSaddle
4536 Language.Javascript.JSaddle.Arguments
@@ -69,18 +60,13 @@ test-suite test-tool
6960 if impl(ghcjs -any)
7061 buildable: False
7162 else
72− if flag(gtk3)
73− build-depends:
74− gtk3 -any,
75− webkitgtk3 -any,
76− webkitgtk3-javascriptcore -any
77− else
78− build-depends:
79− gtk -any,
80− webkit -any,
81− webkit-javascriptcore -any
8263 build-depends:
83− glib -any
64+ haskell-gi-base >= 0.13 && < 0.14,
65+ gi-glib >=0.2.44 && <0.2.47,
66+ gi-gtk >=0.3.16 && <0.3.19,
67+ gi-webkit >=0.2.4 && <0.2.5,
68+ gi-javascriptcore >=0.2.4 && <0.2.5,
69+ webkitgtk3-javascriptcore >=0.14.0.0 && <0.15
8470 type: exitcode-stdio-1.0
8571 main-is: DocTest.hs
8672 build-depends:
src/Language/Javascript/JSaddle/Monad.hs+34−6
@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE PatternSynonyms #-}
23-----------------------------------------------------------------------------
34--
45-- Module : Language.Javascript.JSaddle.Monad
@@ -41,15 +42,41 @@ import qualified JavaScript.Array as Array (create, read)
4142import Language.Javascript.JSaddle.Native (makeNewJSVal)
4243import Foreign (nullPtr, alloca)
4344import Foreign.Storable (Storable(..))
44−import Graphics.UI.Gtk.WebKit.Types
45+import GI.WebKit.Types
4546 (WebView(..))
46−import Graphics.UI.Gtk.WebKit.WebView
47+import GI.WebKit.Objects.WebView
4748 (webViewGetMainFrame)
48−import Graphics.UI.Gtk.WebKit.JavaScriptCore.WebFrame
49+import GI.WebKit.Objects.WebFrame
4950 (webFrameGetGlobalContext)
50−import Graphics.UI.Gtk.General.General (postGUIAsync, postGUISync)
51+import GI.GLib (idleAdd)
52+import GI.GLib.Constants(pattern PRIORITY_DEFAULT)
5153#endif
5254import qualified Control.Exception as E (Exception, catch, bracket)
55+import Control.Concurrent.MVar (takeMVar, putMVar, newEmptyMVar)
56+import Foreign.Ptr (castPtr)
57+import GI.JavaScriptCore.Structs.GlobalContext (GlobalContext(..))
58+import Foreign.ForeignPtr (withForeignPtr)
59+
60+-- | Post an action to be run in the main GUI thread.
61+--
62+-- The current thread blocks until the action completes and the result is
63+-- returned.
64+--
65+postGUISync :: IO a -> IO a
66+postGUISync action = do
67+ resultVar <- newEmptyMVar
68+ idleAdd PRIORITY_DEFAULT $ action >>= putMVar resultVar >> return False
69+ takeMVar resultVar
70+
71+-- | Post an action to be run in the main GUI thread.
72+--
73+-- The current thread continues and does not wait for the result of the
74+-- action.
75+--
76+postGUIAsync :: IO () -> IO ()
77+postGUIAsync action = do
78+ idleAdd PRIORITY_DEFAULT $ action >> return False
79+ return ()
5380
5481-- | Wrapped version of 'E.catch' that runs in a MonadIO that works
5582-- a bit better with 'JSM'
@@ -101,8 +128,9 @@ runJSaddle _ f = runReaderT f ()
101128#else
102129runJSaddle :: WebView -> JSM a -> IO a
103130runJSaddle webView f = do
104− gctxt <- webViewGetMainFrame webView >>= webFrameGetGlobalContext
105− runReaderT f gctxt
131+ GlobalContext gctxt <- webViewGetMainFrame webView >>= webFrameGetGlobalContext
132+ withForeignPtr gctxt $ \ptr ->
133+ runReaderT f (castPtr ptr)
106134#endif
107135{-# INLINE runJSaddle #-}
108136
src/Language/Javascript/JSaddle/Test.hs+53−19
@@ -1,4 +1,5 @@
11{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE PatternSynonyms #-}
23-----------------------------------------------------------------------------
34--
45-- Module : TestJSC
@@ -19,21 +20,19 @@ module Language.Javascript.JSaddle.Test (
1920
2021import Control.Applicative
2122import Prelude hiding((!!), catch)
22−import Graphics.UI.Gtk
23− (Window, widgetDestroy, postGUIAsync, postGUISync, widgetShowAll,
24− mainGUI, mainQuit, on, objectDestroy, containerAdd, scrolledWindowNew,
25− windowSetPosition, windowSetDefaultSize, timeoutAddFull, windowNew,
26− initGUI)
23+import GI.Gtk
24+ (Window, widgetDestroy, widgetShowAll,
25+ mainQuit, containerAdd, scrolledWindowNew,
26+ windowSetPosition, windowSetDefaultSize, windowNew)
2727import Control.Concurrent
2828 (tryTakeMVar, forkIO, newMVar, putMVar, takeMVar, newEmptyMVar,
2929 yield)
30−import System.Glib.MainLoop (priorityLow)
31−import Graphics.UI.Gtk.General.Enums (WindowPosition(..))
32−import Graphics.UI.Gtk.WebKit.WebView
30+import GI.Gtk.Enums (WindowType(..), WindowPosition(..))
31+import GI.WebKit.Objects.WebView
3332 (webViewGetMainFrame, webViewNew)
3433import System.IO.Unsafe (unsafePerformIO)
3534import Control.Monad.Trans.Reader (runReaderT)
36−import Graphics.UI.Gtk.WebKit.JavaScriptCore.WebFrame
35+import GI.WebKit.Objects.WebFrame
3736 (webFrameGetGlobalContext)
3837import Language.Javascript.JSaddle
3938import qualified Data.Text as T
@@ -42,8 +41,40 @@ import Control.Monad (forM, when)
4241import Control.Lens.Getter ((^.))
4342import Data.Monoid ((<>))
4443import 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(..))
55+
56+-- | Post an action to be run in the main GUI thread.
57+--
58+-- The current thread blocks until the action completes and the result is
59+-- returned.
60+--
61+postGUISync :: IO a -> IO a
62+postGUISync action = do
63+ resultVar <- newEmptyMVar
64+ idleAdd PRIORITY_DEFAULT $ action >>= putMVar resultVar >> return False
65+ takeMVar resultVar
66+
67+-- | Post an action to be run in the main GUI thread.
68+--
69+-- The current thread continues and does not wait for the result of the
70+-- action.
71+--
72+postGUIAsync :: IO () -> IO ()
73+postGUIAsync action = do
74+ idleAdd PRIORITY_DEFAULT $ action >> return False
75+ return ()
4576
46−data TestState = TestState { jsContext :: JSContextRef, window :: Window }
77+data TestState = TestState { jsContext :: GlobalContext, window :: Window }
4778
4879state :: MVar (Maybe TestState)
4980state = unsafePerformIO $ newMVar Nothing
@@ -77,18 +108,18 @@ testJSaddle' showWindow f = do
77108 debugLog "fork"
78109 _ <- forkIO $ do
79110 debugLog "initGUI"
80− _ <- initGUI
111+ _ <- Gtk.init Nothing
81112 debugLog "windowNew"
82− window <- windowNew
113+ window <- windowNew WindowTypeToplevel
83114 debugLog "timeoutAdd"
84− _ <- timeoutAddFull (yield >> return True) priorityLow 10
115+ _ <- timeoutAdd PRIORITY_LOW 10 $ yield >> return True
85116 windowSetDefaultSize window 900 600
86− windowSetPosition window WinPosCenter
87− scrollWin <- scrolledWindowNew Nothing Nothing
117+ windowSetPosition window WindowPositionCenter
118+ scrollWin <- scrolledWindowNew (Nothing :: Maybe Adjustment) (Nothing :: Maybe Adjustment)
88119 webView <- webViewNew
89120 window `containerAdd` scrollWin
90121 scrollWin `containerAdd` webView
91− _ <- on window objectDestroy $ do
122+ _ <- onWidgetDestroy window $ do
92123 debugLog "onDestroy"
93124 _ <- tryTakeMVar state
94125 debugLog "put state"
@@ -105,19 +136,22 @@ testJSaddle' showWindow f = do
105136 debugLog "maybe show"
106137 when showWindow $ widgetShowAll window
107138 debugLog "mainGUI"
108− mainGUI
139+ Gtk.main
109140 debugLog "mainGUI exited"
110141 takeMVar newState
111142 Just s@TestState {..} -> do
112143 debugLog "maybe show (2)"
113144 when showWindow . postGUISync $ widgetShowAll window
114145 return s
115− x <- postGUISync $ runReaderT ((f >>= valToText >>= liftIO . putStrLn . T.unpack)
116− `catch` \ (JSException e) -> valToText e >>= liftIO . putStrLn . T.unpack) jsContext
146+ x <- postGUISync $ withContext jsContext $ runReaderT ((f >>= valToText >>= liftIO . putStrLn . T.unpack)
147+ `catch` \ (JSException e) -> valToText e >>= liftIO . putStrLn . T.unpack)
117148 debugLog "put state"
118149 putMVar state $ Just TestState {..}
119150 return x
120151
152+withContext :: GlobalContext -> (JSContextRef -> IO a) -> IO a
153+withContext (GlobalContext fptr) f = withForeignPtr fptr $ f . castPtr
154+
121155listWindowProperties :: IO ()
122156listWindowProperties = testJSaddle $ T.pack . show <$> do
123157 window <- jsg "window"
tests/DocTest.hs−9
@@ -8,16 +8,7 @@ import Test.DocTest
88main :: IO ()
99main = doctest [
1010 "-hide-all-packages",
11−#ifdef VERSION_gtk3
12− "-package=gtk3-" ++ VERSION_gtk3,
13− "-package=webkitgtk3-" ++ VERSION_webkitgtk3,
1411 "-package=webkitgtk3-javascriptcore-" ++ VERSION_webkitgtk3_javascriptcore,
15−#else
16− "-package=gtk-" ++ VERSION_gtk,
17− "-package=webkit-" ++ VERSION_webkit,
18− "-package=webkit-javascriptcore-" ++ VERSION_webkit_javascriptcore,
19−#endif
20− "-package=glib-" ++ VERSION_glib,
2112 "-package=template-haskell-" ++ VERSION_template_haskell,
2213 "-package=base-" ++ VERSION_base,
2314 "-package=lens-" ++ VERSION_lens,