diff options
Diffstat (limited to 'src/Gtk/Signal.icl')
-rw-r--r-- | src/Gtk/Signal.icl | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/src/Gtk/Signal.icl b/src/Gtk/Signal.icl index 254523a..543c7a2 100644 --- a/src/Gtk/Signal.icl +++ b/src/Gtk/Signal.icl @@ -76,14 +76,37 @@ where pushLc handleSignal_int_int_pointer_pointer_bool } -instance tune w SignalHandler | gtkWidget w -where - tune (SignalHandler handler) widget = installSignalHandler handler widget - instance tune w GSignalHandler | gtkWidget w where tune handler widget = installSignalHandler handler widget +addTimeout :: !GtkTimeout !(GtkM Bool) -> GtkM () +addTimeout interval callback = + modState (\st -> + let id = st.timeout_counter+1 in + { st + & timeouts = 'Data.Map'.put id (wrapped_callback id) st.timeouts + , timeout_counter = id + }) >>= \{timeout_counter=id} -> + case interval of + Milliseconds ms -> toState (g_timeout_add ms get_handleTimeout_address id) + Seconds s -> toState (g_timeout_add_seconds s get_handleTimeout_address id) +where + wrapped_callback id = + callback >>= \r -> + if r + getState + (modState \st -> {st & timeouts='Data.Map'.del id st.timeouts}) >>| + pure r + + get_handleTimeout_address :: Pointer + get_handleTimeout_address = code { + pushLc handleTimeout + } + +withPossibleCallback :: !(GtkM a) -> GtkM a +withPossibleCallback m = saveState >>| m >>= \r -> retrieveState >>| pure r + // NB: low-level hacking to use and modify the GtkState from within callbacks. // We use a CAF to keep track of the state. In runGtk, the state is saved with // saveState. This state is retrieved with retrieveState there (to check @@ -111,9 +134,6 @@ saveState = getState >>= \state -> toState (save_state state) retrieveState :: GtkM GtkState retrieveState = modState (const saved_state.[0]) -withPossibleCallback :: !(GtkM a) -> GtkM a -withPossibleCallback m = saveState >>| m >>= \r -> retrieveState >>| pure r - foreign export handleSignal_void handleSignal_void :: !Pointer !Int -> Int handleSignal_void _ id = handleSignal id \h -> case h of @@ -159,30 +179,6 @@ handleSignal id handle # (r,st) = f st -> save_state st r -addTimeout :: !GtkTimeout !(GtkM Bool) -> GtkM () -addTimeout interval callback = - modState (\st -> - let id = st.timeout_counter+1 in - { st - & timeouts = 'Data.Map'.put id (wrapped_callback id) st.timeouts - , timeout_counter = id - }) >>= \{timeout_counter=id} -> - case interval of - Milliseconds ms -> toState (g_timeout_add ms get_handleTimeout_address id) - Seconds s -> toState (g_timeout_add_seconds s get_handleTimeout_address id) -where - wrapped_callback id = - callback >>= \r -> - if r - getState - (modState \st -> {st & timeouts='Data.Map'.del id st.timeouts}) >>| - pure r - - get_handleTimeout_address :: Pointer - get_handleTimeout_address = code { - pushLc handleTimeout - } - foreign export handleTimeout handleTimeout :: !Int -> Int handleTimeout id |