Commit 2ae0cba2 authored by David Davó's avatar David Davó

Added Play widget

parent ad2b61a7
...@@ -71,6 +71,7 @@ library ...@@ -71,6 +71,7 @@ library
IHaskell.Display.Widgets.Int.BoundedInt.BoundedIntText IHaskell.Display.Widgets.Int.BoundedInt.BoundedIntText
IHaskell.Display.Widgets.Int.BoundedInt.IntProgress IHaskell.Display.Widgets.Int.BoundedInt.IntProgress
IHaskell.Display.Widgets.Int.BoundedInt.IntSlider IHaskell.Display.Widgets.Int.BoundedInt.IntSlider
IHaskell.Display.Widgets.Int.BoundedInt.Play
IHaskell.Display.Widgets.Int.BoundedIntRange.IntRangeSlider IHaskell.Display.Widgets.Int.BoundedIntRange.IntRangeSlider
IHaskell.Display.Widgets.Float.FloatText IHaskell.Display.Widgets.Float.FloatText
IHaskell.Display.Widgets.Float.BoundedFloat.BoundedFloatText IHaskell.Display.Widgets.Float.BoundedFloat.BoundedFloatText
......
...@@ -14,6 +14,7 @@ import IHaskell.Display.Widgets.Int.IntText as X ...@@ -14,6 +14,7 @@ import IHaskell.Display.Widgets.Int.IntText as X
import IHaskell.Display.Widgets.Int.BoundedInt.BoundedIntText as X import IHaskell.Display.Widgets.Int.BoundedInt.BoundedIntText as X
import IHaskell.Display.Widgets.Int.BoundedInt.IntProgress as X import IHaskell.Display.Widgets.Int.BoundedInt.IntProgress as X
import IHaskell.Display.Widgets.Int.BoundedInt.IntSlider as X import IHaskell.Display.Widgets.Int.BoundedInt.IntSlider as X
import IHaskell.Display.Widgets.Int.BoundedInt.Play as X
import IHaskell.Display.Widgets.Int.BoundedIntRange.IntRangeSlider as X import IHaskell.Display.Widgets.Int.BoundedIntRange.IntRangeSlider as X
import IHaskell.Display.Widgets.Float.FloatText as X import IHaskell.Display.Widgets.Float.FloatText as X
......
...@@ -97,6 +97,10 @@ pattern Loop = S.SLoop ...@@ -97,6 +97,10 @@ pattern Loop = S.SLoop
pattern Controls = S.SControls pattern Controls = S.SControls
pattern Options = S.SOptions pattern Options = S.SOptions
pattern EnsureOption = S.SEnsureOption pattern EnsureOption = S.SEnsureOption
pattern Playing = S.SPlaying
pattern Repeat = S.SRepeat
pattern Interval = S.SInterval
pattern ShowRepeat = S.SShowRepeat
-- | Close a widget's comm -- | Close a widget's comm
closeWidget :: IHaskellWidget w => w -> IO () closeWidget :: IHaskellWidget w => w -> IO ()
......
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module IHaskell.Display.Widgets.Int.BoundedInt.Play
( -- * The Play Widget
Play
-- * Constructor
, mkPlay
) 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 Data.Vinyl (Rec(..), (<+>))
import IHaskell.Eval.Widgets
import IHaskell.IPython.Message.UUID as U
import IHaskell.Display (IHaskellWidget(..))
import IHaskell.Display.Widgets.Types
import IHaskell.Display.Widgets.Common
-- | 'Play' represents an Play widget from IPython.html.widgets.
type Play = IPythonWidget 'PlayType
-- | Create a new widget
mkPlay :: IO Play
mkPlay = do
-- Default properties, with a random uuid
wid <- U.random
let boundedIntAttrs = defaultBoundedIntWidget "PlayView" "PlayModel"
playAttrs = (Playing =:: True)
:& (Repeat =:: True)
:& (Interval =:: 100)
:& (StepInt =:: Just 1)
:& (Disabled =:: False)
:& (ShowRepeat =:: True)
:& RNil
widgetState = WidgetState $ boundedIntAttrs <+> playAttrs
stateIO <- newIORef widgetState
let widget = IPythonWidget wid stateIO
-- Open a comm for this widget, and store it in the kernel state
widgetSendOpen widget $ toJSON widgetState
-- Return the widget
return widget
instance IHaskellWidget Play where
getCommUUID = uuid
comm widget val _ =
case nestedObjectLookup val ["state", "value"] of
Just (Number value) -> do
void $ setField' widget IntValue (Sci.coefficient value)
triggerChange widget
_ -> pure ()
...@@ -103,5 +103,9 @@ singletons ...@@ -103,5 +103,9 @@ singletons
| Controls | Controls
| Options | Options
| EnsureOption | EnsureOption
| Playing
| Repeat
| Interval
| ShowRepeat
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
|] |]
...@@ -242,6 +242,10 @@ type family FieldType (f :: Field) :: * where ...@@ -242,6 +242,10 @@ type family FieldType (f :: Field) :: * where
FieldType 'S.Controls = Bool FieldType 'S.Controls = Bool
FieldType 'S.Options = [Text] FieldType 'S.Options = [Text]
FieldType 'S.EnsureOption = Bool FieldType 'S.EnsureOption = Bool
FieldType 'S.Playing = Bool
FieldType 'S.Repeat = Bool
FieldType 'S.Interval = Integer
FieldType 'S.ShowRepeat = Bool
-- | Can be used to put different widgets in a list. Useful for dealing with children widgets. -- | Can be used to put different widgets in a list. Useful for dealing with children widgets.
data ChildWidget = forall w. RecAll Attr (WidgetFields w) ToPairs => ChildWidget (IPythonWidget w) data ChildWidget = forall w. RecAll Attr (WidgetFields w) ToPairs => ChildWidget (IPythonWidget w)
...@@ -294,6 +298,7 @@ data WidgetType = ButtonType ...@@ -294,6 +298,7 @@ data WidgetType = ButtonType
| IntTextType | IntTextType
| BoundedIntTextType | BoundedIntTextType
| IntSliderType | IntSliderType
| PlayType
| IntProgressType | IntProgressType
| IntRangeSliderType | IntRangeSliderType
| FloatTextType | FloatTextType
...@@ -351,6 +356,9 @@ type family WidgetFields (w :: WidgetType) :: [Field] where ...@@ -351,6 +356,9 @@ type family WidgetFields (w :: WidgetType) :: [Field] where
WidgetFields 'IntSliderType = WidgetFields 'IntSliderType =
BoundedIntClass :++ BoundedIntClass :++
[ 'S.StepInt, 'S.Orientation, 'S.ReadOut, 'S.ReadOutFormat, 'S.ContinuousUpdate, 'S.Disabled ] [ 'S.StepInt, 'S.Orientation, 'S.ReadOut, 'S.ReadOutFormat, 'S.ContinuousUpdate, 'S.Disabled ]
WidgetFields 'PlayType =
BoundedIntClass :++
[ 'S.Playing, 'S.Repeat, 'S.Interval, 'S.StepInt, 'S.Disabled, 'S.ShowRepeat ]
WidgetFields 'IntProgressType = WidgetFields 'IntProgressType =
BoundedIntClass :++ ['S.Orientation, 'S.BarStyle] BoundedIntClass :++ ['S.Orientation, 'S.BarStyle]
WidgetFields 'IntRangeSliderType = WidgetFields 'IntRangeSliderType =
...@@ -622,6 +630,18 @@ instance ToPairs (Attr 'S.Options) where ...@@ -622,6 +630,18 @@ instance ToPairs (Attr 'S.Options) where
instance ToPairs (Attr 'S.EnsureOption) where instance ToPairs (Attr 'S.EnsureOption) where
toPairs x = ["ensure_option" .= toJSON x] toPairs x = ["ensure_option" .= toJSON x]
instance ToPairs (Attr 'S.Playing) where
toPairs x = ["playing" .= toJSON x]
instance ToPairs (Attr 'S.Repeat) where
toPairs x = ["repeat" .= toJSON x]
instance ToPairs (Attr 'S.Interval) where
toPairs x = ["interval" .= toJSON x]
instance ToPairs (Attr 'S.ShowRepeat) where
toPairs x = ["show_repeat" .= toJSON x]
-- | Store the value for a field, as an object parametrized by the Field. No verification is done -- | Store the value for a field, as an object parametrized by the Field. No verification is done
-- for these values. -- for these values.
(=::) :: (SingI f, Typeable (FieldType f)) => Sing f -> FieldType f -> Attr f (=::) :: (SingI f, Typeable (FieldType f)) => Sing f -> FieldType f -> Attr f
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment