Created
November 1, 2015 21:16
-
-
Save dmgolubovsky/72803f4f9f179bd5b93b to your computer and use it in GitHub Desktop.
A minimal example of attaching a DOM event handler written in Haskell using Webkit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Initial wkhs.cabal generated by cabal init. For further documentation, | |
-- see http://haskell.org/cabal/users-guide/ | |
name: wkhs | |
version: 0.1.0.0 | |
-- synopsis: | |
-- description: | |
license: PublicDomain | |
license-file: LICENSE | |
author: Dmitry Golubovsky | |
maintainer: golubovsky@gmail.com | |
-- copyright: | |
category: Graphics | |
build-type: Simple | |
-- extra-source-files: | |
cabal-version: >=1.10 | |
executable wkhs | |
main-is: wkhs.hs | |
-- other-modules: | |
-- other-extensions: | |
build-depends: base >=4.8 && <4.9, webkitgtk3, gtk3, transformers, string-quote | |
-- hs-source-dirs: | |
default-language: Haskell2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- A minimal example of attaching a DOM event handler written in Haskell | |
-- using Webkit. Based on few other examples if Webkit initialization | |
-- and loading the initial page. Dependent packages for this program: | |
-- base >=4.8 && <4.9, webkitgtk3, gtk3, transformers, string-quote | |
{-# Language QuasiQuotes, ScopedTypeVariables #-} | |
module Main where | |
import Data.Maybe | |
import Data.String.Quote | |
import Control.Monad.IO.Class | |
import Graphics.UI.Gtk | |
import Graphics.UI.Gtk.WebKit.Types | |
import Graphics.UI.Gtk.WebKit.WebView | |
import Graphics.UI.Gtk.WebKit.DOM.MouseEvent | |
import Graphics.UI.Gtk.WebKit.DOM.Document | |
import Graphics.UI.Gtk.WebKit.DOM.HTMLDocument | |
import Graphics.UI.Gtk.WebKit.DOM.EventTarget | |
import Graphics.UI.Gtk.WebKit.DOM.EventTargetClosures | |
fromMaybeM :: Monad m => String -> Maybe a -> m a | |
fromMaybeM st = maybe (fail st) return | |
main = do | |
-- initialize Gtk | |
_ <- initGUI | |
-- create a new window, a scrolled window, and a new webview | |
w <- windowNew | |
sw <- scrolledWindowNew Nothing Nothing | |
wv <- webViewNew | |
-- set the child of the parent to the scrolled window, | |
-- and set some others attributes | |
set w | |
[ containerChild := sw | |
, windowDefaultWidth := 1000 | |
, windowDefaultHeight := 800 | |
, containerBorderWidth := 0 | |
] | |
-- set the child of the scrolled windows to the webview. | |
set sw [ containerChild := wv ] | |
-- load our HTML string on the webview. | |
webViewLoadString wv [s| | |
<div id="abc"> | |
<h1>Hello (with click handler)</h1> | |
</div> | |
<div id="def"> | |
<h1>Hello (without handler)</h1> | |
</div> | |
|] (Just "text/html") "" | |
-- on destroying event, we quit the mainloop | |
-- onDestroy w mainQuit | |
w `on` deleteEvent $ liftIO mainQuit >> return False | |
-- show all widgets starting from the root window | |
widgetShowAll w | |
-- once the page is loaded, install event handlers as needed | |
wv `on` documentLoadFinished $ \wf -> do | |
doc <- webViewGetDomDocument wv >>= fromMaybeM "no document" | |
abc <- getElementById doc "abc" >>= fromMaybeM "no element id=abc found" | |
evlstn <- eventListenerNew $ \(e :: MouseEvent) -> do | |
x <- getClientX e | |
y <- getClientY e | |
putStrLn $ "evlist called at (" ++ show x ++ ", " ++ show y ++ ")" | |
putStrLn "Installing handler" | |
addEventListener abc "click" (Just evlstn) True | |
-- start GTK main loop | |
mainGUI | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment