<commit_before>{-|
A module aimed at making working with GtkBuilder easier.
It's meant to be used like this (requires OverloadedStrings):

> buildUI :: BuildFn ()
> buildUI = do
>     mainWin <- buildMainWin
>     buildAboutDialog
>
>     widgetShowAll mainWin
>
> buildMainWin :: BuildFn ApplicationWindow
> buildMainWin = do
>     buildHeaderBar
>     buildMenuBar
>     buildMainArea
>
>     mainWin <- getObject' ApplicationWindow "mainWin"
>     on mainWin Destroy mainQuit
>     return mainWin
>
> buildAboutDialog :: BuildFn AboutDialog
> ...
>
> buildHeaderBar :: BuildFn HeaderBar
> ...
>
> buildMenuBar :: BuildFn MenuBar
> ...
>
> buildMainArea :: BuildFn Grid
> ...
-}
module Data.GI.Gtk.BuildFn
    ( BuildFn
    , buildWithBuilder
    , getObject
    ) where

import           Control.Monad.Reader (ReaderT, runReaderT, ask, MonadIO, liftIO)
import           Data.GI.Base (GObject, castTo)
import           Data.Maybe (fromJust)
import qualified Data.Text as T
import           Foreign.ForeignPtr (ForeignPtr)

import           GI.Gtk hiding (main)

type BuildFn a = ReaderT Builder IO a

buildWithBuilder :: MonadIO m => BuildFn a -> Builder -> m a
buildWithBuilder fn builder = liftIO $ runReaderT fn builder

getObject :: GObject a => (ForeignPtr a -> a) -> T.Text -> BuildFn a
getObject ctor name = do
    builder <- ask
    obj <- builderGetObject builder name
    liftIO $ fromJust <$> castTo ctor obj<commit_msg>Fix getObject for newer gi-gtk<commit_after>{-|
A module aimed at making working with GtkBuilder easier.
It's meant to be used like this (requires OverloadedStrings):

> buildUI :: BuildFn ()
> buildUI = do
>     mainWin <- buildMainWin
>     buildAboutDialog
>
>     widgetShowAll mainWin
>
> buildMainWin :: BuildFn ApplicationWindow
> buildMainWin = do
>     buildHeaderBar
>     buildMenuBar
>     buildMainArea
>
>     mainWin <- getObject' ApplicationWindow "mainWin"
>     on mainWin Destroy mainQuit
>     return mainWin
>
> buildAboutDialog :: BuildFn AboutDialog
> ...
>
> buildHeaderBar :: BuildFn HeaderBar
> ...
>
> buildMenuBar :: BuildFn MenuBar
> ...
>
> buildMainArea :: BuildFn Grid
> ...
-}
module Data.GI.Gtk.BuildFn
    ( BuildFn
    , buildWithBuilder
    , getObject
    ) where

import           Control.Monad.Reader (ReaderT, runReaderT, ask, MonadIO, liftIO)
import           Data.GI.Base (GObject, castTo)
import           Data.GI.Base.BasicTypes (nullToNothing)
import           Data.Maybe (fromJust)
import qualified Data.Text as T
import           Foreign.ForeignPtr (ForeignPtr)

import           GI.Gtk hiding (main)

type BuildFn a = ReaderT Builder IO a

buildWithBuilder :: MonadIO m => BuildFn a -> Builder -> m a
buildWithBuilder fn builder = liftIO $ runReaderT fn builder

getObject :: GObject a => (ForeignPtr a -> a) -> T.Text -> BuildFn a
getObject ctor name = do
    builder <- ask
    Just obj <- nullToNothing $ builderGetObject builder name
    liftIO $ fromJust <$> castTo ctor obj
{'fraction_non_alphanumeric': 0.09625126646403243, 'fraction_numerical': 0.0, 'mean_word_length': 3.0189959294436908, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 75, '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': '1045535', 'n_tokens_mistral': 910, 'n_tokens_neox': 892, 'n_words': 280}