metabrush/src/app/MetaBrush/Util.hs

78 lines
2.2 KiB
Haskell
Raw Normal View History

2020-09-01 19:56:59 +00:00
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
2020-08-05 20:23:16 +00:00
2020-09-01 19:56:59 +00:00
module MetaBrush.Util
2020-08-04 06:15:06 +00:00
( withRGBA, showRGBA
2020-08-05 20:23:16 +00:00
, widgetAddClasses, widgetAddClass
2020-09-01 19:56:59 +00:00
, (>=?=>), (>>?=)
, Exists(..)
2020-08-05 20:23:16 +00:00
)
2020-08-04 06:15:06 +00:00
where
-- base
2020-09-01 19:56:59 +00:00
import Control.Monad
( (>=>) )
import Data.Coerce
( coerce )
2020-08-05 20:23:16 +00:00
import Data.Foldable
( for_ )
2020-08-04 06:15:06 +00:00
import GHC.Stack
( HasCallStack )
-- gi-gdk
import qualified GI.Gdk as GDK
2020-08-05 20:23:16 +00:00
-- gi-gtk
import qualified GI.Gtk as GTK
2020-08-04 06:15:06 +00:00
-- text
import Data.Text
( Text )
-- transformers
import Control.Monad.IO.Class
2020-08-05 20:23:16 +00:00
( MonadIO )
2020-09-01 19:56:59 +00:00
import Control.Monad.Trans.Maybe
( MaybeT(..) )
2020-08-04 06:15:06 +00:00
--------------------------------------------------------------------------------
withRGBA :: MonadIO m => GDK.RGBA -> ( Double -> Double -> Double -> Double -> m b ) -> m b
withRGBA rgba f = do
r <- GDK.getRGBARed rgba
g <- GDK.getRGBAGreen rgba
b <- GDK.getRGBABlue rgba
a <- GDK.getRGBAAlpha rgba
f r g b a
showRGBA :: MonadIO m => GDK.RGBA -> m String
showRGBA rgba = withRGBA rgba \ r g b a ->
pure $ "rgba(" ++ show r ++ "," ++ show g ++ "," ++ show b ++ "," ++ show a ++ ")"
widgetAddClasses :: ( HasCallStack, GTK.IsWidget widget, MonadIO m ) => widget -> [Text] -> m ()
widgetAddClasses widget classNames = do
styleContext <- GTK.widgetGetStyleContext widget
for_ classNames ( GTK.styleContextAddClass styleContext )
widgetAddClass :: ( HasCallStack, GTK.IsWidget widget, MonadIO m ) => widget -> Text -> m ()
widgetAddClass widget className = GTK.widgetGetStyleContext widget >>= ( `GTK.styleContextAddClass` className )
2020-09-01 19:56:59 +00:00
--------------------------------------------------------------------------------
infixr 1 >=?=>
(>=?=>) :: forall m a b c. Monad m => ( a -> m ( Maybe b ) ) -> ( b -> m ( Maybe c ) ) -> ( a -> m ( Maybe c ) )
(>=?=>) = coerce ( (>=>) @( MaybeT m ) @a @b @c )
infixl 1 >>?=
(>>?=) :: forall m a b. Monad m => m ( Maybe a ) -> ( a -> m ( Maybe b ) ) -> m ( Maybe b )
(>>?=) = coerce ( (>>=) @( MaybeT m ) @a @b )
--------------------------------------------------------------------------------
data Exists c where
Exists :: c a => a -> Exists c