1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_GHC -Wno-duplicate-exports #-}
module Graphics.UI.Threepenny.Editors.Types
(
-- * GenericWidgets
GenericWidget(..)
, edited
, contents
, widgetControl
, widgetTidings
-- * Editors
, Editor(.., Horizontally, horizontally, Vertically, vertically)
, liftElement
, dimapE
, applyE
-- ** Editor composition
, (|*|), (|*), (*|)
, (-*-), (-*), (*-)
, field
, fieldLayout
-- ** Editor constructors
, editorUnit
, editorIdentity
, editorString
, editorText
, editorCheckBox
, editorReadShow
, editorEnumBounded
, editorSelection
, editorSum
, editorJust
, EditorCollection(..)
, editorCollection
, editorList
, EditorCollectionConfig(..)
, defaultEditorCollectionConfig
-- ** Representation of empty values
, HasEmpty(..)
) where
import Control.Monad
import Data.Biapplicative
import Data.Maybe
import Data.HasEmpty
import qualified Data.Foldable as F
import Data.Functor.Compose
import Data.Functor.Identity
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Profunctor
import Data.Text (Text)
import qualified Data.Text as Text
import Graphics.UI.Threepenny.Attributes
import Graphics.UI.Threepenny.Core as UI hiding (empty)
import Graphics.UI.Threepenny.Editors.Layout
import Graphics.UI.Threepenny.Editors.Utils
import Graphics.UI.Threepenny.Elements
import Graphics.UI.Threepenny.Events
import Graphics.UI.Threepenny.Widgets
import Text.Read
data GenericWidget control a = GenericWidget
{ widgetTidings :: Tidings a -- ^ The dynamic contents of the widget.
, widgetControl :: control -- ^ The actual widget.
}
deriving Functor
instance Bifunctor GenericWidget where
bimap f g (GenericWidget t e) = GenericWidget (g <$> t) (f e)
traverseControl :: Applicative f => (control -> f control') -> GenericWidget control a -> f (GenericWidget control' a)
traverseControl f (GenericWidget t e) = GenericWidget t <$> f e
edited :: GenericWidget el a -> Event a
edited = rumors . widgetTidings
contents :: GenericWidget el a -> Behavior a
contents = facts . widgetTidings
instance Widget el => Widget (GenericWidget el a) where
getElement = getElement . widgetControl
instance Renderable el => Renderable (GenericWidget el a) where
render = render . widgetControl
renderEditor :: Renderable w => GenericWidget w a -> UI (GenericWidget Element a)
renderEditor = traverseControl render
-- | An editor for values of type @inner@ inside a datatype @outer@ realized by a @widget@.
--
-- All the three type arguments are functorial, but @outer@ is contravariant, so @Editor@ is a 'Biapplicative' functor and a 'Profunctor' (via 'dimapE').
--
-- 'Biapplicative' allows to compose editors on both their @widget@ and @inner@ structure. When @widget@ is monoidal, widget composition is implicit and 'Applicative' suffices.
--
-- 'Profunctor' allows to apply an @inner@ editor to an @outer@ datatype.
--
-- Once 'create'd, an 'Editor' yields a tuple of an @widget@ and a @Tidings inner@ which can be integrated in a threepenny app.
--
newtype Editor outer widget inner = Editor {
create :: Behavior outer -> UI (GenericWidget widget inner)
}
-- | Lift an HTML element into a vacuous editor.
liftElement :: UI el -> Editor a el ()
liftElement el = Editor $ \_ -> GenericWidget (pure ()) <$> el
bimapEditor :: (el -> el') -> (b -> b') -> Editor a el b -> Editor a el' b'
bimapEditor g h = Editor . fmap (fmap (bimap g h)) . create
dimapE :: (a' -> a) -> (b -> b') -> Editor a el b -> Editor a' el b'
dimapE g h = unCoer . dimap (fmap g) h . coer
where
coer = Star . (Compose .) . create
unCoer = Editor . fmap getCompose . runStar
applyE :: (el1 -> el2 -> el) -> Editor in_ el1 (a -> b) -> Editor in_ el2 a -> Editor in_ el b
applyE combineElements a b = Editor $ \s -> do
a <- create a s
b <- create b s
return $ GenericWidget (widgetTidings a <*> widgetTidings b) (widgetControl a `combineElements` widgetControl b)
instance Functor (Editor a el) where
fmap = dimapE id
instance Bifunctor (Editor a) where
bimap = bimapEditor
instance Biapplicative (Editor a) where
bipure w o = Editor $ \_ -> return $ GenericWidget (pure o) w
(<<*>>) = applyE ($)
instance Monoid el => Applicative (Editor a el) where
pure = bipure mempty
(<*>) = applyE mappend
-- | Applicative modifier for vertical composition of editor factories.
-- This can be used in conjunction with ApplicativeDo as:
--
-- > editorPerson = vertically $ do
-- > firstName <- Vertically $ field "First:" firstName editor
-- > lastName <- Vertically $ field "Last:" lastName editor
-- > age <- Vertically $ field "Age:" age editor
-- > return Person{..}
--
-- DEPRECATED: Use the 'Vertical' layout builder instead
pattern Vertically :: Editor a Layout b -> Editor a Vertical b
pattern Vertically {vertically} <- (withLayout getVertical -> vertically) where Vertically a = withLayout Vertical a
-- | Applicative modifier for horizontal composition of editor factories.
-- This can be used in conjunction with ApplicativeDo as:
--
-- > editorPerson = horizontally $ do
-- > firstName <- Horizontally $ field "First:" firstName editor
-- > lastName <- Horizontally $ field "Last:" lastName editor
-- > age <- Horizontally $ field "Age:" age editor
-- > return Person{..}
--
-- DEPRECATED: Use the 'Horizontal' layout builder instead
pattern Horizontally :: Editor a Layout b -> Editor a Horizontal b
pattern Horizontally {horizontally} <- (withLayout getHorizontal -> horizontally) where Horizontally a = withLayout Horizontal a
infixl 4 |*|, -*-
infixl 5 |*, *|, -*, *-
-- | Apply a layout builder.
withLayout :: (layout -> layout') -> Editor a layout b -> Editor a layout' b
withLayout f = bimap f id
-- | Left-right editor composition
(|*|) :: Editor s Layout (b -> a) -> Editor s Layout b -> Editor s Layout a
a |*| b = withLayout getHorizontal $ withLayout Horizontal a <*> withLayout Horizontal b
-- | Left-right composition of an element with a editor
(*|) :: UI Element -> Editor s Layout a -> Editor s Layout a
e *| a = withLayout getHorizontal $ liftElement(return $ horizontal e) *> withLayout Horizontal a
-- | Left-right composition of an element with a editor
(|*) :: Editor s Layout a -> UI Element -> Editor s Layout a
a |* e = withLayout getHorizontal $ withLayout Horizontal a <* liftElement(return $ horizontal e)
-- | Left-right editor composition
(-*-) :: Editor s Layout (b -> a) -> Editor s Layout b -> Editor s Layout a
a -*- b = withLayout getVertical $ withLayout Vertical a <*> withLayout Vertical b
-- | Left-right composition of an element with a editor
(*-) :: UI Element -> Editor s Layout a -> Editor s Layout a
e *- a = withLayout getVertical $ liftElement(return $ vertical e) *> withLayout Vertical a
-- | Left-right composition of an element with a editor
(-*) :: Editor s Layout a -> UI Element -> Editor s Layout a
a -* e = withLayout getVertical $ withLayout Vertical a <* liftElement(return $ vertical e)
-- | A helper that arranges a label and an editor horizontally,
-- wrapped in the given monoidal layout builder.
fieldLayout :: (Renderable m, Renderable m') => (Layout -> m') -> String -> (out -> inn) -> Editor inn m a -> Editor out m' a
fieldLayout l name f e = withLayout l (string name *| first getLayout (dimapE f id e))
-- | A helper that arranges a label
-- and an editor horizontally.
field :: Renderable m => String -> (out -> inn) -> Editor inn m a -> Editor out Layout a
field name f e = string name *| first getLayout (dimapE f id e)
editorUnit :: Editor b Element b
editorUnit = Editor $ \b -> do
t <- new
return $ GenericWidget (tidings b never) t
editorCheckBox :: Editor Bool Element Bool
editorCheckBox = Editor $ \b -> do
t <- sink checked b $ input # set type_ "checkbox"
return $ GenericWidget (tidings b $ checkedChange t) t
editorString :: Editor String TextEntry String
editorString = Editor $ \b -> do
w <- askWindow
t <- entry b
liftIOLater $ do
initialValue <- currentValue b
_ <- runUI w $ set value initialValue (element t)
return ()
return $ GenericWidget (userText t) t
editorText :: Editor Text TextEntry Text
editorText = dimapE Text.unpack Text.pack editorString
editorReadShow :: (Read a, Show a) => Editor (Maybe a) TextEntry (Maybe a)
editorReadShow = Editor $ \b -> do
e <- create editorString (maybe "" show <$> b)
let readIt "" = Nothing
readIt x = readMaybe x
let t = tidings b (readIt <$> edited e)
return $ GenericWidget t (widgetControl e)
-- An editor that presents a choice of values.
editorEnumBounded
:: (Bounded a, Enum a, Ord a, Show a)
=> Behavior(a -> UI Element) -> Editor (Maybe a) (ListBox a) (Maybe a)
editorEnumBounded = editorSelection (pure $ enumFrom minBound)
-- | An editor that presents a dynamic choice of values.
editorSelection
:: Ord a
=> Behavior [a] -> Behavior(a -> UI Element) -> Editor (Maybe a) (ListBox a) (Maybe a)
editorSelection options display = Editor $ \b -> do
l <- listBox options b display
return $ GenericWidget (tidings b (rumors $ userSelection l)) l
-- | Ignores 'Nothing' values and only updates for 'Just' values
editorJust :: Editor (Maybe b) el (Maybe b) -> Editor b el b
editorJust (Editor editor) = Editor $ \b -> do
e <- editor (Just <$> b)
let ev = filterJust (edited e)
return $ GenericWidget (tidings b ev) (widgetControl e)
-- | An editor for union types, built from editors for its constructors.
editorSum
:: (Ord tag, Show tag, Renderable el)
=> (Layout -> Layout -> Layout) -> [(tag, Editor a el a)] -> (a -> tag) -> Editor a Layout a
editorSum combineLayout options selector = Editor $ \ba -> do
options <- mapM (\(tag, Editor mk) -> (tag,) <$> (mk ba >>= renderEditor)) options
let tag = selector <$> ba
tag' <- calmB tag
let build a = lookup a options
-- build a tag selector following the current tag
l <- listBox (pure $ fmap fst options) (Just <$> tag) (pure (string . show))
-- a placeholder for the constructor editor
nestedEditor <-
new # sink children ((\x -> [maybe (error "editorSum") widgetControl (build x)]) <$> tag')
--
let composed = combineLayout (Single (return $ getElement l)) (Single $ return nestedEditor)
-- the result event fires when any of the nested editors or the tag selector fire.
let editedEvents = fmap (edited . snd) options
eTag = filterJust $ rumors (userSelection l)
taggedOptions = sequenceA [(tag, ) <$> contents e | (tag, e) <- options]
editedTag = filterJust $ flip lookup <$> taggedOptions <@> eTag
editedE = head <$> unions (editedTag : editedEvents)
return $ GenericWidget (tidings ba editedE) composed
editorIdentity :: Editor a el a -> Editor (Identity a) el (Identity a)
editorIdentity = dimapE runIdentity Identity
--------------------------
-- EditorCollection
data EditorCollection k w = EditorCollection
{ selector :: ListBox k
, add, remove :: Element
, selected :: w
}
instance Renderable w => Renderable (EditorCollection k w) where
render EditorCollection{..} =
column
[row [ element selector, element add, element remove]
,render selected]
data EditorCollectionConfig k v = EditorCollectionConfig
{ eccNewKey :: Behavior k -- ^ Current value to use for creating a new key
, eccAfterDelKey :: Behavior (Maybe k) -- ^ Current value to use if the selected key is deleted
, eccTemplate :: v -- ^ Value to use for creating new items
, eccOptions :: Behavior (Set k) -- ^ Currently user select able keys
, eccDisplay :: Behavior (k -> UI Element) -- ^ How to render a key
}
defaultEditorCollectionConfig
:: (Enum k, Ord k, Show k, HasEmpty v)
=> Behavior (Maybe k, Map k v) -> EditorCollectionConfig k v
defaultEditorCollectionConfig db = EditorCollectionConfig
{ eccTemplate = emptyValue
, eccOptions = options
, eccDisplay = pure (UI.string . show)
, eccNewKey = maybe (toEnum 0) succ . Set.lookupMax <$> options
, eccAfterDelKey = deletedKey <$> (fst <$> db) <*> options
}
where
options = Map.keysSet . snd <$> db
deletedKey Nothing _ = Nothing
deletedKey (Just k) kk = Set.lookupLT k kk `mplus` Set.lookupGT k kk
-- | A barebones editor for collections of editable items.
-- Displays an index selector, add and delete buttons, and an editor for the selected item.
-- Limitations:
-- - Won't work with recursive data structures, due to the lack of FRP switch.
editorCollection
:: forall k v w.
(Ord k, Renderable w)
=> (Behavior (Maybe k, Map k v) -> EditorCollectionConfig k v)
-> Editor v w v
-> Editor (Maybe k, Map k v) (EditorCollection k w) (Maybe k, Map k v)
editorCollection mkConfig editorOne = Editor $ \(ba :: Behavior (Maybe k, Map k v)) -> mdo
let EditorCollectionConfig{..} = mkConfig ba
(selectedKey, db) = (fst <$> ba, snd <$> ba)
sel <- create (editorSelection (Set.toList <$> eccOptions) eccDisplay) (fst <$> ba)
one <- create editorOne $ (\(k, db) -> fromMaybe eccTemplate (k >>= (`Map.lookup` db))) <$> ba
addB <- button #+ [string "+"]
remB <- button #+ [string "-"]
let insert i = Map.insert i eccTemplate
editsDb = head <$> unions
[ replace <$> ba <@> edited one
, insert <$> eccNewKey <*> db <@ click addB
, delete <$> ba <@ click remB
]
editsKey = head <$> unions
[ edited sel
, Just <$> eccNewKey <@ click addB
, eccAfterDelKey <@ click remB
]
tids = (,) <$> tidings selectedKey editsKey <*> tidings db editsDb
return $ GenericWidget tids (EditorCollection (widgetControl sel) addB remB (widgetControl one))
where
replace (Just i,xx) x = Map.alter (const $ Just x) i xx
replace (Nothing,x) _ = x
delete (Just i,xx) = Map.delete i xx
delete (_,xx) = xx
-- | A barebones editor for collections of editable items.
-- Displays an index selector, add and delete buttons, and an editor for the selected item.
-- Limitations:
-- - Won't work with recursive data structures, due to the lack of FRP switch.
editorList
:: (HasEmpty a, Renderable w)
=> Editor a w a -> Editor (Maybe Int, [a]) (EditorCollection Int w) (Maybe Int, [a])
editorList e =
dimapE (second (Map.fromAscList . zip [0 ..])) (second F.toList) $
editorCollection config e
where
(<&>) = flip (<$>)
infixl 1 <&>
config ba =
(defaultEditorCollectionConfig ba)
{ eccAfterDelKey =
ba <&> (\(i,m) ->
i >>= (\i ->
if Map.member (i + 1) m
then return i
else let i' = max 0 (i - 1)
in guard(i'>=0) >> return i'))
}
|