I'm trying to make a simple app in Haskell using GTK3 and WebKit. This code creates and shows a window containing a WebView
inside, which displays a random number each time a key gets pressed.
import Control.Monad.Trans (lift)
import Control.Concurrent (forkOS)
import System.Random (randomIO)
import Graphics.UI.Gtk -- gtk3
import Graphics.UI.Gtk.WebKit.WebView -- webkitgtk3
main = forkOS $ do
-- Init GTK.
initGUI
-- Create a window which would finish the GTK loop
-- after being closed.
window <- windowNew
window `after` objectDestroy $
mainQuit
-- Create a WebView inside.
webView <- webViewNew
set window [containerChild := webView]
-- Make the WebView display a random number on key press.
webView `on` keyReleaseEvent $ lift $ do
x <- randomIO :: IO Int
webViewLoadString webView (show x) Nothing Nothing ""
return True
-- Run GTK.
widgetShowAll window
mainGUI
When I run it in GHCi (7.8.3), it works fine. However, when I run it again without quitting GHCi, the WebView
never shows anything – just plain white area. This is upsetting, as I like to tinker with code in GHCi.
Of course, everything works just fine if I don't use forkOS
and run the whole thing in the main thread. What's the reason for this limitation (I thought all GTK functions considered the “main” thread to be the one in which initGUI
was called), and can it be overcome somehow?