File: Progress.hs

package info (click to toggle)
haskell-gtk3 0.15.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,756 kB
  • sloc: haskell: 3,375; ansic: 826; makefile: 160
file content (37 lines) | stat: -rw-r--r-- 1,070 bytes parent folder | download | duplicates (9)
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
-- Example of concurrent Haskell and Gtk.
--
-- As long as GHC does not support OS level threads by default, a trick has
-- to be used to let Haskell programs continue while the GUI is running.
-- We attach a call to the Haskell function "yield" to the timeout handler of
-- Gtk's main loop. Thus GHC regularly gets a chance to execute Haskell
-- threads.

import Graphics.UI.Gtk

import Control.Applicative
import Prelude
import Control.Concurrent

main :: IO ()
main = do
  initGUI
  dia <- dialogNew
  dialogAddButton dia stockClose ResponseClose
  contain <- castToBox <$> dialogGetContentArea dia
  pb <- progressBarNew
  boxPackStart contain pb PackNatural 0
  widgetShowAll dia
  forkIO (doTask pb)

  -- 50ms timeout, so GHC will get a chance to scheule about 20 times a second
  -- which gives reasonable latency without the polling generating too much
  -- cpu load.
  timeoutAddFull (yield >> return True) priorityDefaultIdle 50
  dialogRun dia
  return ()

doTask :: ProgressBar -> IO ()
doTask pb = do
  progressBarPulse pb
  threadDelay 100000
  doTask pb