-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhow-do-i-inline-an-html-page-on-start-up.hs
77 lines (74 loc) · 2.49 KB
/
how-do-i-inline-an-html-page-on-start-up.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{-
webviewhs
(C) 2019 David Lettier
lettier.com
-}
{-# LANGUAGE
OverloadedStrings
, QuasiQuotes
#-}
import Control.Monad
import Data.Text
import Language.Javascript.JMacro
import qualified Graphics.UI.Webviewhs as WHS
main :: IO ()
main =
WHS.withWindowLoop
WHS.WindowParams
{ WHS.windowParamsTitle = "webviewhs - How do I inline an HTML page on start up?"
-- This can be "http://", "https://", "file://", or "data:text/html,".
, WHS.windowParamsUri =
Data.Text.unlines
[ "data:text/html,"
, "<html>"
, " <head>"
, " <title>webviewhs</title>"
, " <style>"
, " button { background-color: #1ae; color: #111; }"
, " .rotated { transform: rotate(30deg); }"
, " </style>"
, " </head>"
, " <body>"
, " <button id='button'>Button</button>"
, " <script>"
, " var button = document.getElementById('button');"
, " setTimeout("
, " function() {"
, " button.style.backgroundColor = '#ea1';"
, " },"
, " 1000"
, " );"
, " /* When the button is clicked, invoke the Haskell callback. */"
, " button.addEventListener('click', function() {"
, " /* Send the Haskell callback the message 'clicked'. */"
, " window.external.invoke('clicked');"
, " });"
, " </script>"
, " </body>"
, "</html>"
]
, WHS.windowParamsWidth = 800
, WHS.windowParamsHeight = 600
, WHS.windowParamsResizable = True
, WHS.windowParamsDebuggable = True
}
-- This is the callback JavaScript can execute.
(\ window text ->
-- If the message is "clicked"...
when (text == "clicked")
$ void
$ WHS.runJavaScript
window
[jmacro|
// ...toggle the rotation of the button.
var button = document.getElementById("button");
button.classList.toggle("rotated");
|]
)
-- This function runs before the loop.
(WHS.WithWindowLoopSetUp (\ _window -> print ("Setting up." :: Data.Text.Text)))
-- This function runs after the loop.
(WHS.WithWindowLoopTearDown (\ _window -> print ("Tearing down." :: Data.Text.Text)))
-- This function runs every window loop.
-- Return True to continue the loop or False to exit the loop.
(return . const True)