{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module IHaskell.Display.Widgets.String.Combobox
(
ComboboxWidget
, mkCombobox
) where
import Prelude
import Control.Monad (when)
import Data.Aeson
import Data.IORef (newIORef)
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
type ComboboxWidget = IPythonWidget 'ComboboxType
mkCombobox :: IO ComboboxWidget
mkCombobox :: IO ComboboxWidget
mkCombobox = do
UUID
wid <- IO UUID
U.random
IPythonWidget 'LayoutType
layout <- IO (IPythonWidget 'LayoutType)
mkLayout
DescriptionStyle
dstyle <- IO DescriptionStyle
mkDescriptionStyle
let txtWidget :: Rec Attr TextClass
txtWidget = FieldType 'ViewName
-> FieldType 'ModelName
-> IPythonWidget 'LayoutType
-> StyleWidget
-> Rec Attr TextClass
defaultTextWidget Text
"ComboboxView" Text
"ComboboxModel" IPythonWidget 'LayoutType
layout forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
RecAll Attr (WidgetFields w) ToPairs =>
IPythonWidget w -> StyleWidget
StyleWidget DescriptionStyle
dstyle
boxWidget :: Rec Attr '[ 'Options, 'EnsureOption]
boxWidget = (forall {a :: Field}. (a ~ 'Options) => SField a
Options forall (f :: Field).
(SingI f, Typeable (FieldType f)) =>
Sing f -> FieldType f -> Attr f
=:: [])
forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& (forall {a :: Field}. (a ~ 'EnsureOption) => SField a
EnsureOption 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 'ComboboxType
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, 'StringValue,
'Placeholder, 'Disabled, 'ContinuousUpdate, 'SubmitHandler,
'ChangeHandler]
txtWidget forall {k} (f :: k -> *) (as :: [k]) (bs :: [k]).
Rec f as -> Rec f bs -> Rec f (as ++ bs)
<+> Rec Attr '[ 'Options, 'EnsureOption]
boxWidget
IORef (WidgetState 'ComboboxType)
stateIO <- forall a. a -> IO (IORef a)
newIORef WidgetState 'ComboboxType
widgetState
let widget :: ComboboxWidget
widget = forall (w :: WidgetType).
UUID -> IORef (WidgetState w) -> IPythonWidget w
IPythonWidget UUID
wid IORef (WidgetState 'ComboboxType)
stateIO
forall a. IHaskellWidget a => a -> Value -> IO ()
widgetSendOpen ComboboxWidget
widget forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Value
toJSON WidgetState 'ComboboxType
widgetState
forall (m :: * -> *) a. Monad m => a -> m a
return ComboboxWidget
widget
instance IHaskellWidget ComboboxWidget where
getCommUUID :: ComboboxWidget -> UUID
getCommUUID = forall (w :: WidgetType). IPythonWidget w -> UUID
uuid
comm :: ComboboxWidget -> Value -> (Value -> IO ()) -> IO ()
comm ComboboxWidget
tw Value
val Value -> IO ()
_ = do
case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"state", Text
"value"] of
Just (String Text
value) -> forall (f :: Field) (w :: WidgetType).
(f ∈ WidgetFields w, IHaskellWidget (IPythonWidget w)) =>
IPythonWidget w -> SField f -> FieldType f -> IO (Attr f)
setField' ComboboxWidget
tw forall {a :: Field}. (a ~ 'StringValue) => SField a
StringValue Text
value forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (w :: WidgetType).
('ChangeHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerChange ComboboxWidget
tw
Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
case Value -> [Text] -> Maybe Value
nestedObjectLookup Value
val [Text
"content", Text
"event"] of
Just (String Text
event) -> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text
event forall a. Eq a => a -> a -> Bool
== Text
"submit") forall a b. (a -> b) -> a -> b
$ forall (w :: WidgetType).
('SubmitHandler ∈ WidgetFields w) =>
IPythonWidget w -> IO ()
triggerSubmit ComboboxWidget
tw
Maybe Value
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()