Skip to content

Commit 75aa52c

Browse files
author
Mitsutoshi Aoe
committed
Embed data files into binary for easier deployment
1 parent 34cf9dc commit 75aa52c

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

GUI/DataFiles.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
module GUI.DataFiles
3+
( ui
4+
, loadLogo
5+
) where
6+
import System.IO
7+
8+
import Data.FileEmbed
9+
import Graphics.UI.Gtk (Pixbuf, pixbufNewFromFile)
10+
import Language.Haskell.TH
11+
import System.IO.Temp
12+
import qualified Data.ByteString as B
13+
import qualified Data.Text.Encoding as TE
14+
15+
uiFile :: FilePath
16+
uiFile = "threadscope.ui"
17+
18+
logoFile :: FilePath
19+
logoFile = "threadscope.png"
20+
21+
-- | Textual representaion of the UI file
22+
ui :: Q Exp
23+
ui = [| TE.decodeUtf8 $(makeRelativeToProject uiFile >>= embedFile) |]
24+
25+
renderLogo :: B.ByteString -> IO Pixbuf
26+
renderLogo bytes =
27+
withSystemTempFile logoFile $ \path h -> do
28+
B.hPut h bytes
29+
hClose h
30+
pixbufNewFromFile path
31+
32+
-- | Load the logo file as a 'Pixbuf'.
33+
loadLogo :: Q Exp
34+
loadLogo = [| renderLogo $(makeRelativeToProject logoFile >>= embedFile) |]

GUI/Dialogs.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
{-# LANGUAGE TemplateHaskell #-}
12
module GUI.Dialogs where
23

3-
import Paths_threadscope (getDataFileName, version)
4+
import GUI.DataFiles (loadLogo)
5+
import Paths_threadscope (version)
46

57
import Graphics.UI.Gtk
68

@@ -13,8 +15,7 @@ import System.FilePath
1315
aboutDialog :: WindowClass window => window -> IO ()
1416
aboutDialog parent
1517
= do dialog <- aboutDialogNew
16-
logoPath <- getDataFileName "threadscope.png"
17-
logo <- pixbufNewFromFile logoPath
18+
logo <- $loadLogo
1819
set dialog [
1920
aboutDialogName := "ThreadScope",
2021
aboutDialogVersion := showVersion version,

GUI/Main.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE TemplateHaskell #-}
23
module GUI.Main (runGUI) where
34

45
-- Imports for GTK
@@ -16,13 +17,12 @@ import Control.Exception
1617
import Data.Array
1718
import Data.Maybe
1819

19-
import Paths_threadscope
20-
2120
-- Imports for ThreadScope
2221
import qualified GUI.App as App
2322
import qualified GUI.MainWindow as MainWindow
2423
import GUI.Types
2524
import Events.HECs hiding (Event)
25+
import GUI.DataFiles (ui)
2626
import GUI.Dialogs
2727
import Events.ReadEvents
2828
import GUI.EventsView
@@ -117,7 +117,7 @@ constructUI :: IO UIEnv
117117
constructUI = failOnGError $ do
118118

119119
builder <- Gtk.builderNew
120-
Gtk.builderAddFromFile builder =<< getDataFileName "threadscope.ui"
120+
Gtk.builderAddFromString builder $ui
121121

122122
eventQueue <- Chan.newChan
123123
let post = postEvent eventQueue

GUI/MainWindow.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE TemplateHaskell #-}
12
module GUI.MainWindow (
23
MainWindow,
34
mainWindowNew,
@@ -10,12 +11,10 @@ module GUI.MainWindow (
1011

1112
) where
1213

13-
import Paths_threadscope
14-
15-
-- Imports for GTK
1614
import Graphics.UI.Gtk as Gtk
1715
import qualified System.Glib.GObject as Glib
1816

17+
import GUI.DataFiles (loadLogo)
1918

2019
-------------------------------------------------------------------------------
2120

@@ -146,8 +145,8 @@ mainWindowNew builder actions = do
146145

147146
------------------------------------------------------------------------
148147

149-
logoPath <- getDataFileName "threadscope.png"
150-
windowSetIconFromFile mainWindow logoPath
148+
logo <- $loadLogo
149+
set mainWindow [ windowIcon := Just logo ]
151150

152151
------------------------------------------------------------------------
153152
-- Status bar functionality

threadscope.cabal

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ Executable threadscope
5858
containers >= 0.2 && < 0.6,
5959
deepseq >= 1.1,
6060
text < 1.3,
61-
time >= 1.1 && < 1.9
61+
time >= 1.1 && < 1.9,
62+
bytestring < 0.11,
63+
file-embed < 0.1,
64+
template-haskell < 2.13,
65+
temporary >= 1.1 && < 1.3
6266
if os(osx)
6367
build-depends: gtk-mac-integration
6468

@@ -74,6 +78,7 @@ Executable threadscope
7478
GUI.Main,
7579
GUI.MainWindow,
7680
GUI.EventsView,
81+
GUI.DataFiles,
7782
GUI.Dialogs,
7883
GUI.SaveAs,
7984
GUI.Timeline,

0 commit comments

Comments
 (0)