Commit 26903b1e authored by Sumit Sahrawat's avatar Sumit Sahrawat

Switch to using Numeric.Natural

parent 05e33cbf
...@@ -78,6 +78,7 @@ library ...@@ -78,6 +78,7 @@ library
, unordered-containers >= 0.2.5.1 , unordered-containers >= 0.2.5.1
-- TODO: Need to check versions -- TODO: Need to check versions
, nats -any
, vinyl -any , vinyl -any
, singletons -any , singletons -any
......
...@@ -163,10 +163,3 @@ instance Show ImageFormatValue where ...@@ -163,10 +163,3 @@ instance Show ImageFormatValue where
instance ToJSON ImageFormatValue where instance ToJSON ImageFormatValue where
toJSON = toJSON . pack . show toJSON = toJSON . pack . show
newtype PosInt = PosInt { unwrap :: Int }
instance ToJSON PosInt where
toJSON (PosInt x)
| x > 0 = String . pack $ show x
| otherwise = ""
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE BangPatterns #-}
module IHaskell.Display.Widgets.Types where module IHaskell.Display.Widgets.Types where
import Control.Monad (when) import Control.Monad (when)
...@@ -30,6 +31,8 @@ import qualified Data.Vinyl.TypeLevel as TL ...@@ -30,6 +31,8 @@ import qualified Data.Vinyl.TypeLevel as TL
import Data.Singletons.TH import Data.Singletons.TH
import Numeric.Natural
import IHaskell.Eval.Widgets (widgetSendUpdate) import IHaskell.Eval.Widgets (widgetSendUpdate)
import IHaskell.Display (Base64, IHaskellWidget (..)) import IHaskell.Display (Base64, IHaskellWidget (..))
import IHaskell.IPython.Message.UUID import IHaskell.IPython.Message.UUID
...@@ -51,25 +54,25 @@ type family FieldType (f :: Field) :: * where ...@@ -51,25 +54,25 @@ type family FieldType (f :: Field) :: * where
FieldType ModelName = Text FieldType ModelName = Text
FieldType ViewModule = Text FieldType ViewModule = Text
FieldType ViewName = Text FieldType ViewName = Text
FieldType MsgThrottle = PosInt FieldType MsgThrottle = Natural
FieldType Version = PosInt FieldType Version = Natural
FieldType OnDisplayed = IO () FieldType OnDisplayed = IO ()
FieldType Visible = Bool FieldType Visible = Bool
FieldType CSS = [(Text, Text, Text)] FieldType CSS = [(Text, Text, Text)]
FieldType DOMClasses = [Text] FieldType DOMClasses = [Text]
FieldType Width = PosInt FieldType Width = Natural
FieldType Height = PosInt FieldType Height = Natural
FieldType Padding = PosInt FieldType Padding = Natural
FieldType Margin = PosInt FieldType Margin = Natural
FieldType Color = Text FieldType Color = Text
FieldType BackgroundColor = Text FieldType BackgroundColor = Text
FieldType BorderColor = Text FieldType BorderColor = Text
FieldType BorderWidth = PosInt FieldType BorderWidth = Natural
FieldType BorderRadius = PosInt FieldType BorderRadius = Natural
FieldType BorderStyle = BorderStyleValue FieldType BorderStyle = BorderStyleValue
FieldType FontStyle = FontStyleValue FieldType FontStyle = FontStyleValue
FieldType FontWeight = FontWeightValue FieldType FontWeight = FontWeightValue
FieldType FontSize = PosInt FieldType FontSize = Natural
FieldType FontFamily = Text FieldType FontFamily = Text
FieldType Description = Text FieldType Description = Text
FieldType ClickHandler = IO () FieldType ClickHandler = IO ()
...@@ -150,8 +153,8 @@ defaultWidget viewName = (SModelModule =:: "") ...@@ -150,8 +153,8 @@ defaultWidget viewName = (SModelModule =:: "")
:& (SModelName =:: "WidgetModel") :& (SModelName =:: "WidgetModel")
:& (SViewModule =:: "") :& (SViewModule =:: "")
:& (SViewName =:: viewName) :& (SViewName =:: viewName)
:& (SMsgThrottle =:: PosInt 3) :& (SMsgThrottle =:: 3)
:& (SVersion =:: PosInt 0) :& (SVersion =:: 0)
:& (SOnDisplayed =:: return ()) :& (SOnDisplayed =:: return ())
:& RNil :& RNil
...@@ -160,19 +163,19 @@ defaultDOMWidget viewName = defaultWidget viewName <+> domAttrs ...@@ -160,19 +163,19 @@ defaultDOMWidget viewName = defaultWidget viewName <+> domAttrs
where domAttrs = (SVisible =:: True) where domAttrs = (SVisible =:: True)
:& (SCSS =:: []) :& (SCSS =:: [])
:& (SDOMClasses =:: []) :& (SDOMClasses =:: [])
:& (SWidth =:: PosInt 0) :& (SWidth =:: 0)
:& (SHeight =:: PosInt 0) :& (SHeight =:: 0)
:& (SPadding =:: PosInt 0) :& (SPadding =:: 0)
:& (SMargin =:: PosInt 0) :& (SMargin =:: 0)
:& (SColor =:: "") :& (SColor =:: "")
:& (SBackgroundColor =:: "") :& (SBackgroundColor =:: "")
:& (SBorderColor =:: "") :& (SBorderColor =:: "")
:& (SBorderWidth =:: PosInt 0) :& (SBorderWidth =:: 0)
:& (SBorderRadius =:: PosInt 0) :& (SBorderRadius =:: 0)
:& (SBorderStyle =:: DefaultBorder) :& (SBorderStyle =:: DefaultBorder)
:& (SFontStyle =:: DefaultFont) :& (SFontStyle =:: DefaultFont)
:& (SFontWeight =:: DefaultWeight) :& (SFontWeight =:: DefaultWeight)
:& (SFontSize =:: PosInt 0) :& (SFontSize =:: 0)
:& (SFontFamily =:: "") :& (SFontFamily =:: "")
:& RNil :& RNil
...@@ -212,9 +215,9 @@ setField widget (sfield :: SField f) fval = do ...@@ -212,9 +215,9 @@ setField widget (sfield :: SField f) fval = do
let pairs = toPairs (Attr fval :: Attr f) let pairs = toPairs (Attr fval :: Attr f)
when (not . null $ pairs) $ widgetSendUpdate widget (object pairs) when (not . null $ pairs) $ widgetSendUpdate widget (object pairs)
-- | Change the value of a field, without notifying the frontend. For internal use. -- | Change the value of a field, without notifying the frontend. For internal use. Uses BangPattern.
setField' :: (f WidgetFields w, IHaskellWidget (Widget w), SingI f) => Widget w -> SField f -> FieldType f -> IO () setField' :: (f WidgetFields w, IHaskellWidget (Widget w), SingI f) => Widget w -> SField f -> FieldType f -> IO ()
setField' widget (sfield :: SField f) fval = modifyIORef (state widget) (WidgetState . rput (sfield =:: fval) . _getState) setField' widget (sfield :: SField f) !fval = modifyIORef (state widget) (WidgetState . rput (sfield =:: fval) . _getState)
-- | Get the value of a field. -- | Get the value of a field.
getField :: (f WidgetFields w) => Widget w -> SField f -> IO (FieldType f) getField :: (f WidgetFields w) => Widget w -> SField f -> IO (FieldType f)
...@@ -223,3 +226,7 @@ getField widget sfield = _unAttr <$> rget sfield <$> _getState <$> readIORef (st ...@@ -223,3 +226,7 @@ getField widget sfield = _unAttr <$> rget sfield <$> _getState <$> readIORef (st
-- | Useful with toJSON, and OverloadedStrings -- | Useful with toJSON, and OverloadedStrings
str :: String -> String str :: String -> String
str = id str = id
instance ToJSON Natural where
toJSON 0 = String ""
toJSON n = String . pack $ show n
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