{-|
Module      : Monomer.Widgets.Util.Theme
Copyright   : (c) 2018 Francisco Vallarino
License     : BSD-3-Clause (see the LICENSE file)
Maintainer  : fjvallarino@gmail.com
Stability   : experimental
Portability : non-portable

Helper functions for loading theme values.
-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Strict #-}

module Monomer.Widgets.Util.Theme where

import Control.Lens (Lens', (&), (^.), (^?), (.~), (?~), (<>~), at, non)
import Data.Default
import Data.Maybe

import Monomer.Core.StyleTypes
import Monomer.Core.ThemeTypes
import Monomer.Core.WidgetTypes

import qualified Monomer.Core.Lens as L

-- | Updates a the field of style with the field value from the active theme.
collectThemeField_
  :: WidgetEnv s e               -- ^ The widget environment (to get the theme).
  -> Lens' StyleState (Maybe t)  -- ^ The target field of the style.
  -> Lens' ThemeState (Maybe t)  -- ^ The source field of the theme.
  -> Style                       -- ^ The target style.
  -> Style                       -- ^ The updated style.
collectThemeField_ :: forall s e t.
WidgetEnv s e
-> Lens' StyleState (Maybe t)
-> Lens' ThemeState (Maybe t)
-> Style
-> Style
collectThemeField_ WidgetEnv s e
wenv Lens' StyleState (Maybe t)
fieldStyle Lens' ThemeState (Maybe t)
fieldTheme Style
target = Style
style where
  basic :: Maybe StyleState
basic = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Style
target forall s a. s -> Getting a s a -> a
^. forall s a. HasBasic s a => Lens' s a
L.basic forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Iso' (Maybe a) a
non forall a. Default a => a
def
    forall a b. a -> (a -> b) -> b
& Lens' StyleState (Maybe t)
fieldStyle forall s t a b. ASetter s t a b -> b -> s -> t
.~ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasBasic s a => Lens' s a
L.basic forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState (Maybe t)
fieldTheme
  hover :: Maybe StyleState
hover = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Style
target forall s a. s -> Getting a s a -> a
^. forall s a. HasHover s a => Lens' s a
L.hover forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Iso' (Maybe a) a
non forall a. Default a => a
def
    forall a b. a -> (a -> b) -> b
& Lens' StyleState (Maybe t)
fieldStyle forall s t a b. ASetter s t a b -> b -> s -> t
.~ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasHover s a => Lens' s a
L.hover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState (Maybe t)
fieldTheme
  focus :: Maybe StyleState
focus = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Style
target forall s a. s -> Getting a s a -> a
^. forall s a. HasFocus s a => Lens' s a
L.focus forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Iso' (Maybe a) a
non forall a. Default a => a
def
    forall a b. a -> (a -> b) -> b
& Lens' StyleState (Maybe t)
fieldStyle forall s t a b. ASetter s t a b -> b -> s -> t
.~ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasFocus s a => Lens' s a
L.focus forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState (Maybe t)
fieldTheme
  focusHover :: Maybe StyleState
focusHover = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Style
target forall s a. s -> Getting a s a -> a
^. forall s a. HasFocusHover s a => Lens' s a
L.focusHover forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Iso' (Maybe a) a
non forall a. Default a => a
def
    forall a b. a -> (a -> b) -> b
& Lens' StyleState (Maybe t)
fieldStyle forall s t a b. ASetter s t a b -> b -> s -> t
.~ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasFocusHover s a => Lens' s a
L.focusHover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState (Maybe t)
fieldTheme
  active :: Maybe StyleState
active = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Style
target forall s a. s -> Getting a s a -> a
^. forall s a. HasActive s a => Lens' s a
L.active forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Iso' (Maybe a) a
non forall a. Default a => a
def
    forall a b. a -> (a -> b) -> b
& Lens' StyleState (Maybe t)
fieldStyle forall s t a b. ASetter s t a b -> b -> s -> t
.~ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasActive s a => Lens' s a
L.active forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState (Maybe t)
fieldTheme
  disabled :: Maybe StyleState
disabled = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Style
target forall s a. s -> Getting a s a -> a
^. forall s a. HasDisabled s a => Lens' s a
L.disabled forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Iso' (Maybe a) a
non forall a. Default a => a
def
    forall a b. a -> (a -> b) -> b
& Lens' StyleState (Maybe t)
fieldStyle forall s t a b. ASetter s t a b -> b -> s -> t
.~ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasDisabled s a => Lens' s a
L.disabled forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState (Maybe t)
fieldTheme
  style :: Style
style = Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Style
Style Maybe StyleState
basic Maybe StyleState
hover Maybe StyleState
focus Maybe StyleState
focusHover Maybe StyleState
active Maybe StyleState
disabled

-- | Collects all the style states from a given field in the active theme.
collectTheme
  :: WidgetEnv s e               -- ^ The widget environment (to get the theme).
  -> Lens' ThemeState StyleState -- ^ The field into the theme
  -> Style                       -- ^ The collected style.
collectTheme :: forall s e. WidgetEnv s e -> Lens' ThemeState StyleState -> Style
collectTheme WidgetEnv s e
wenv Lens' ThemeState StyleState
fieldT = Style
style where
  basic :: Maybe StyleState
basic = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasBasic s a => Lens' s a
L.basic forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
fieldT
  hover :: Maybe StyleState
hover = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasHover s a => Lens' s a
L.hover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
fieldT
  focus :: Maybe StyleState
focus = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasFocus s a => Lens' s a
L.focus forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
fieldT
  focusHover :: Maybe StyleState
focusHover = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasFocusHover s a => Lens' s a
L.focusHover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
fieldT
  active :: Maybe StyleState
active = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasActive s a => Lens' s a
L.active forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
fieldT
  disabled :: Maybe StyleState
disabled = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasDisabled s a => Lens' s a
L.disabled forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
fieldT
  style :: Style
style = Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Style
Style Maybe StyleState
basic Maybe StyleState
hover Maybe StyleState
focus Maybe StyleState
focusHover Maybe StyleState
active Maybe StyleState
disabled

-- | Collects all the style states from a given entry in the map of user styles
--   in the active theme.
collectUserTheme
  :: WidgetEnv s e  -- ^ The widget environment (to get the theme).
  -> String         -- ^ The key into the user map.
  -> Style          -- ^ The collected style.
collectUserTheme :: forall s e. WidgetEnv s e -> String -> Style
collectUserTheme WidgetEnv s e
wenv String
name = Style
style where
  basic :: Maybe (IxValue (Map String StyleState))
basic = WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasBasic s a => Lens' s a
L.basic forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasUserStyleMap s a => Lens' s a
L.userStyleMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at String
name
  hover :: Maybe (IxValue (Map String StyleState))
hover = WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasHover s a => Lens' s a
L.hover forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasUserStyleMap s a => Lens' s a
L.userStyleMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at String
name
  focus :: Maybe (IxValue (Map String StyleState))
focus = WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasFocus s a => Lens' s a
L.focus forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasUserStyleMap s a => Lens' s a
L.userStyleMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at String
name
  focusHover :: Maybe (IxValue (Map String StyleState))
focusHover = WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasFocusHover s a => Lens' s a
L.focusHover forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasUserStyleMap s a => Lens' s a
L.userStyleMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at String
name
  active :: Maybe (IxValue (Map String StyleState))
active = WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasActive s a => Lens' s a
L.active forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasUserStyleMap s a => Lens' s a
L.userStyleMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at String
name
  disabled :: Maybe (IxValue (Map String StyleState))
disabled = WidgetEnv s e
wenv forall s a. s -> Getting a s a -> a
^. forall s a. HasTheme s a => Lens' s a
L.theme forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasDisabled s a => Lens' s a
L.disabled forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. HasUserStyleMap s a => Lens' s a
L.userStyleMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at String
name
  style :: Style
style = Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Maybe StyleState
-> Style
Style Maybe StyleState
basic Maybe StyleState
hover Maybe StyleState
focus Maybe StyleState
focusHover Maybe StyleState
active Maybe StyleState
disabled

{-|
Sets the given value, overwriting the previous one, for each of the states of a
theme.

Useful for customizing a base theme.

@
theme = darkTheme
  & setThemeValue L.sliderRadius (Just 10)
  & setThemeValue L.sliderWidth 10
@
-}
setThemeValue
  :: Lens' ThemeState a
  -> a
  -> Theme
  -> Theme
setThemeValue :: forall a. Lens' ThemeState a -> a -> Theme -> Theme
setThemeValue Lens' ThemeState a
field a
value Theme
theme = Theme
newTheme where
  newTheme :: Theme
newTheme = Theme
theme
    forall a b. a -> (a -> b) -> b
& forall s a. HasBasic s a => Lens' s a
L.basic forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState a
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasHover s a => Lens' s a
L.hover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState a
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasFocus s a => Lens' s a
L.focus forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState a
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasFocusHover s a => Lens' s a
L.focusHover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState a
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasActive s a => Lens' s a
L.active forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState a
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasDisabled s a => Lens' s a
L.disabled forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState a
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
value

{-|
Sets the given style options, overwriting the previous style, for each of the
states of a theme.

Useful for customizing a base theme.

@
theme = darkTheme
  & setThemeStyle L.dropdownStyle [padding 6, paddingV 6]
@

Note: In most cases 'mergeThemeStyle' is a better choice, since it keeps all the
other style settings intact.
-}
setThemeStyle
  :: Lens' ThemeState StyleState
  -> [StyleState]
  -> Theme
  -> Theme
setThemeStyle :: Lens' ThemeState StyleState -> [StyleState] -> Theme -> Theme
setThemeStyle Lens' ThemeState StyleState
field [StyleState]
styles Theme
theme = Theme
newTheme where
  !value :: StyleState
value = forall a. Monoid a => [a] -> a
mconcat [StyleState]
styles
  newTheme :: Theme
newTheme = Theme
theme
    forall a b. a -> (a -> b) -> b
& forall s a. HasBasic s a => Lens' s a
L.basic forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasHover s a => Lens' s a
L.hover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasFocus s a => Lens' s a
L.focus forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasFocusHover s a => Lens' s a
L.focusHover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasActive s a => Lens' s a
L.active forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasDisabled s a => Lens' s a
L.disabled forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall s t a b. ASetter s t a b -> b -> s -> t
.~ StyleState
value

{-|
Merges the given style options with the existing style, for each of the states
of a theme.

Useful for customizing a base theme.

@
theme = darkTheme
  & mergeThemeStyle L.dropdownStyle [padding 6, paddingV 6]
@
-}
mergeThemeStyle
  :: Lens' ThemeState StyleState
  -> [StyleState]
  -> Theme
  -> Theme
mergeThemeStyle :: Lens' ThemeState StyleState -> [StyleState] -> Theme -> Theme
mergeThemeStyle Lens' ThemeState StyleState
field [StyleState]
styles Theme
theme = Theme
newTheme where
  !value :: StyleState
value = forall a. Monoid a => [a] -> a
mconcat [StyleState]
styles
  newTheme :: Theme
newTheme = Theme
theme
    forall a b. a -> (a -> b) -> b
& forall s a. HasBasic s a => Lens' s a
L.basic forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall a s t. Semigroup a => ASetter s t a a -> a -> s -> t
<>~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasHover s a => Lens' s a
L.hover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall a s t. Semigroup a => ASetter s t a a -> a -> s -> t
<>~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasFocus s a => Lens' s a
L.focus forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall a s t. Semigroup a => ASetter s t a a -> a -> s -> t
<>~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasFocusHover s a => Lens' s a
L.focusHover forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall a s t. Semigroup a => ASetter s t a a -> a -> s -> t
<>~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasActive s a => Lens' s a
L.active forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall a s t. Semigroup a => ASetter s t a a -> a -> s -> t
<>~ StyleState
value
    forall a b. a -> (a -> b) -> b
& forall s a. HasDisabled s a => Lens' s a
L.disabled forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' ThemeState StyleState
field forall a s t. Semigroup a => ASetter s t a a -> a -> s -> t
<>~ StyleState
value