Create lenses for Widget.hs types

This commit is contained in:
Francisco Vallarino 2020-08-02 22:15:32 -03:00
parent ebb524cab6
commit 2dab19f9ae
3 changed files with 24 additions and 3 deletions

View File

@ -0,0 +1,21 @@
--{-# LANGUAGE ConstraintKinds #-}
--{-# LANGUAGE ExistentialQuantification #-}
--{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TemplateHaskell #-}
module Monomer.Widget.LensCore where
import Control.Lens.TH (abbreviatedFields, makeLensesWith)
import Monomer.Widget.Types
makeLensesWith abbreviatedFields ''SizePolicy
makeLensesWith abbreviatedFields ''SizeReq
makeLensesWith abbreviatedFields ''WidgetEnv
makeLensesWith abbreviatedFields ''WidgetRequest
makeLensesWith abbreviatedFields ''WidgetResult
makeLensesWith abbreviatedFields ''WidgetPlatform
makeLensesWith abbreviatedFields ''WidgetValue
makeLensesWith abbreviatedFields ''WidgetInstance

View File

@ -200,7 +200,7 @@ data Widget s e =
data WidgetInstance s e =
WidgetInstance {
-- | Type of the widget
_wiType :: !WidgetType,
_wiWidgetType :: !WidgetType,
-- | Key/Identifier of the widget
_wiKey :: Maybe WidgetKey,
-- | The path of the instance in the widget tree

View File

@ -24,7 +24,7 @@ rootPath = Seq.empty
defaultWidgetInstance :: WidgetType -> Widget s e -> WidgetInstance s e
defaultWidgetInstance widgetType widget = WidgetInstance {
_wiType = widgetType,
_wiWidgetType = widgetType,
_wiKey = Nothing,
_wiPath = Seq.empty,
_wiWidget = widget,
@ -86,7 +86,7 @@ useState (Just (WidgetState state)) = cast state
instanceMatches :: WidgetInstance s e -> WidgetInstance s e -> Bool
instanceMatches newInstance oldInstance = typeMatches && keyMatches where
typeMatches = _wiType oldInstance == _wiType newInstance
typeMatches = _wiWidgetType oldInstance == _wiWidgetType newInstance
keyMatches = _wiKey oldInstance == _wiKey newInstance
updateSizeReq :: SizeReq -> WidgetInstance s e -> SizeReq