blob: 9d81b9a6d76015ad159cf5dc90b1952c5bdcef9b (
plain) (
blame)
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
|
implementation module Gtk.State
import StdEnv
import Control.Monad
import Control.Monad.Identity
import Control.Monad.State
import qualified Data.Map as M
from Data.Map import :: Map
import Data.Maybe
import qualified Gtk.Internal as I
from Gtk.Internal import :: GSignalArgs{..}
import Gtk.Signal
newGtkState :: GtkState
newGtkState =
{ world = ()
, return = False
, signal_handlers = 'M'.newMap
, signal_counter = 0
}
runGtk :: !(GtkM a) !*World -> (!a, !*World)
runGtk f w = (evalState wrapped_f newGtkState, w)
where
wrapped_f =
toState 'I'.gtk_init >>|
f >>= \x ->
main >>|
pure x
main =
toStateR 'I'.gtk_main_iteration >>|
handle_signals >>|
gets (\st -> st.return) >>= \return
| return -> pure ()
| otherwise -> main
handle_signals =
toStateR 'I'.g_signal_pop >>= \sig_args -> case sig_args of
Nothing ->
pure ()
Just sig_args ->
gets (\st -> 'M'.get sig_args.sig_id st.signal_handlers) >>= \(Just handler) ->
run handler >>|
handle_signals
with
run handler = case handler of
GSHI_Void st -> st >>| handle_signals
toState :: !(A.a: a -> a) -> GtkM ()
toState f = state \st -> let w = f st.world in ((), {st & world=w})
toStateR :: !(A.a: a -> (r,a)) -> GtkM r
toStateR f = state \st -> let (r,w) = f st.world in (r, {st & world=w})
quit :: GtkM ()
quit = modify \st -> {st & return=True}
|