<commit_before>module UI.Widget.Choice (
  choiceWidget
  ) where

import Control.Applicative

import Reactive.Banana
import Reactive.Banana.Extra

import UI.TclTk
import UI.TclTk.AST
import UI.TclTk.Builder
import UI.Command



choiceWidget :: [(String, Event t a -> GUI t p TkName)] -- List of choices
             -> Event t a
             -> GUI t p ()
choiceWidget [] _ = return ()
choiceWidget xs evt = do
  (cmd,idxEvt) <- addTclEvent
  let Cmd pref _ = cmd undefined -- FIXME: ugly!
  -- notebook widget
  note <- notebook [] [Fill FillX] $
    [ (title, gui $ tabEvents i evt idxEvt)
    | (i, (title,gui)) <- zip [0::Int ..] xs 
    ]
  -- Bind tab change event
  stmt $ Stmt [ Name  "notebook_event_tab"
              , Name  pref
              , WName note
              ]



-- Sum of events. 
data TabEvt a
  = Evt a                       -- Event
  | Tab Int                     -- Tab change events.

-- Fiter events which apply only for current tab
tabEvents :: Int -> Event t a -> Event t Int -> Event t a
tabEvents n evt tabs
  = filterJust
  $ fmap select
  $ scanE go (Nothing,Nothing)
  $ (Evt <$> evt) `union` (Tab <$> tabs)
  where
    go (_,i) (Evt a) = (Just a, i)
    go (e,_) (Tab i) = (e, Just i)

    select (Just e, Just i)
      | i == n = Just e
    select _   = Nothing<commit_msg>Fix choice widget.<commit_after>module UI.Widget.Choice (
  choiceWidget
  ) where

import Control.Applicative

import Reactive.Banana
import Reactive.Banana.Extra

import UI.TclTk
import UI.TclTk.AST
import UI.TclTk.Builder
import UI.Command



choiceWidget :: [(String, Event t a -> GUI t p TkName)] -- List of choices
             -> Event t a
             -> GUI t p ()
choiceWidget [] _ = return ()
choiceWidget xs evt = do
  (cmd,idxEvt) <- addTclEvent
  let Cmd pref _ = cmd undefined -- FIXME: ugly!
  -- notebook widget
  note <- notebook [] [Fill FillX] $
    [ (title, gui $ tabEvents i evt idxEvt)
    | (i, (title,gui)) <- zip [0::Int ..] xs 
    ]
  -- Bind tab change event
  stmt $ Stmt [ Name  "notebook_event_tab"
              , Name  pref
              , WName note
              ]



-- Sum of events. 
data TabEvt a
  = Evt a                       -- Event
  | Tab Int                     -- Tab change events.

-- Fiter events which apply only for current tab
tabEvents :: Int -> Event t a -> Event t Int -> Event t a
tabEvents n evt tabs
  = filterJust
  $ fmap select
  $ scanE go (Nothing, Just 0)
  $ (Evt <$> evt) `union` (Tab <$> tabs)
  where
    go (_,i) (Evt a) = (Just a, i)
    go (e,_) (Tab i) = (e, Just i)

    select (Just e, Just i)
      | i == n = Just e
    select _   = Nothing
{'fraction_non_alphanumeric': 0.11726755218216318, 'fraction_numerical': 0.0011385199240986717, 'mean_word_length': 1.9684684684684686, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6771348', 'n_tokens_mistral': 921, 'n_tokens_neox': 889, 'n_words': 311}