{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}

{-# OPTIONS_GHC -fno-warn-orphans  #-}

module IHaskell.Display.Widgets.Float.BoundedFloatRange.FloatRangeSlider
  ( -- * The FloatRangeSlider Widget
    FloatRangeSlider
    -- * Constructor
  , mkFloatRangeSlider
  ) where

-- To keep `cabal repl` happy when running from the ihaskell repo
import           Prelude

import           Control.Monad (void)
import           Data.Aeson
import           Data.IORef (newIORef)
import qualified Data.Scientific as Sci
import qualified Data.Vector as V
import           Data.Vinyl (Rec(..), (<+>))

import           IHaskell.Display
import           IHaskell.Eval.Widgets
import           IHaskell.IPython.Message.UUID as U

import           IHaskell.Display.Widgets.Types
import           IHaskell.Display.Widgets.Common
import           IHaskell.Display.Widgets.Layout.LayoutWidget
import           IHaskell.Display.Widgets.Style.DescriptionStyle

-- | 'FloatRangeSlider' represents an FloatRangeSlider widget from IPython.html.widgets.
type FloatRangeSlider = IPythonWidget 'FloatRangeSliderType

-- | Create a new widget
mkFloatRangeSlider :: IO FloatRangeSlider
mkFloatRangeSlider :: IO FloatRangeSlider
mkFloatRangeSlider = do
  -- Default properties, with a random uuid
  UUID
wid <- IO UUID
U.random
  IPythonWidget 'LayoutType
layout <- IO (IPythonWidget 'LayoutType)
mkLayout
  DescriptionStyle
dstyle <- IO DescriptionStyle
mkDescriptionStyle

  let boundedFloatAttrs :: Rec Attr BoundedFloatRangeClass
boundedFloatAttrs = FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr BoundedFloatRangeClass
defaultBoundedFloatRangeWidget Text
"FloatRangeSliderView" Text
"FloatRangeSliderModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle
      sliderAttrs :: Rec
  Attr
  '[ 'StepFloat, 'Orientation, 'ReadOut, 'ReadOutFormat,
     'ContinuousUpdate, 'Disabled]
sliderAttrs = (forall {a :: Field}. (a ~ 'StepFloat) => SField a
StepFloat forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: forall a. a -> Maybe a
Just Double
0.1)
                    forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'Orientation) => SField a
Orientation forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: OrientationValue
HorizontalOrientation)
                    forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'ReadOut) => SField a
ReadOut forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Bool
True)
                    forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'ReadOutFormat) => SField a
ReadOutFormat forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Text
".2f")
                    forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'ContinuousUpdate) => SField a
ContinuousUpdate forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Bool
True)
                    forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'Disabled) => SField a
Disabled forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: Bool
False)
                    forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& forall {u} (a :: u -> *). Rec a '[]
RNil
      widgetState :: WidgetState 'FloatRangeSliderType
widgetState = forall (w :: WidgetType).
Rec Attr (WidgetFields w) -> WidgetState w
WidgetState forall a b. (a -> b) -> a -> b
$ Rec
  Attr
  '[ 'ViewModule, 'ViewModuleVersion, 'ModelModule,
     'ModelModuleVersion, 'ModelName, 'ViewName, 'DOMClasses, 'Tooltip,
     'Layout, 'DisplayHandler, 'Description, 'Style, 'FloatValue,
     'ChangeHandler, 'FloatPairValue, 'StepFloat, 'MinFloat, 'MaxFloat]
boundedFloatAttrs forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec
  Attr
  '[ 'StepFloat, 'Orientation, 'ReadOut, 'ReadOutFormat,
     'ContinuousUpdate, 'Disabled]
sliderAttrs

  IORef (WidgetState 'FloatRangeSliderType)
stateIO <- forall a. a -> IO (IORef a)
newIORef WidgetState 'FloatRangeSliderType
widgetState

  let widget :: FloatRangeSlider
widget = forall (w :: WidgetType).
UUID -> IORef (WidgetState w) -> IPythonWidget w
IPythonWidget UUID
wid IORef (WidgetState 'FloatRangeSliderType)
stateIO

  -- Open a comm for this widget, and store it in the kernel state
  forall a. IHaskellWidget a => a -> Value -> IO ()
widgetSendOpen FloatRangeSlider
widget forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Value
toJSON WidgetState 'FloatRangeSliderType
widgetState

  -- Return the widget
  forall (m :: * -> *) a. Monad m => a -> m a
return FloatRangeSlider
widget

instance IHaskellWidget FloatRangeSlider where
  getCommUUID :: FloatRangeSlider -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
  comm :: FloatRangeSlider -> Value -> (Value -> IO ()) -> IO ()
comm FloatRangeSlider
widget Value
val Value -> IO ()
_ =
    case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"state", Text
"value"] of
      Just (Array Array
values) ->
        case forall a b. (a -> b) -> [a] -> [b]
map (\(Number Scientific
x) -> forall a. RealFloat a => Scientific -> a
Sci.toRealFloat Scientific
x) forall a b. (a -> b) -> a -> b
$ forall a. Vector a -> [a]
V.toList Array
values of
          [Double
x, Double
y] -> do
            forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w)) =>
IPythonWidget w -> SField f -> FieldType f -> IO (Attr f)
setField' FloatRangeSlider
widget forall {a :: Field}. (a ~ 'FloatPairValue) => SField a
FloatPairValue (Double
x, Double
y)
            forall (w :: WidgetType).
('ChangeHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerChange FloatRangeSlider
widget
          [Double]
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()