Skip to content

Commit

Permalink
Store WrappedConfig in LayoutState
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlogachev committed May 2, 2024
1 parent bd1bad7 commit d4c9311
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 72 deletions.
47 changes: 27 additions & 20 deletions src/GridLayout1.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module GridLayout1 exposing
( ScreenClass(..), LayoutState, LayoutConfig, GridMargin(..), WindowSize, windowSizeDecoder, init, update
( ScreenClass(..), LayoutState, LayoutConfig, WrappedConfig, GridMargin(..), WindowSize, windowSizeDecoder, init, update
, bodyAttributes, layoutOuterAttributes, layoutInnerAttributes
, gridRow, gridColumn, gridBox, widthOfGridSteps, heightOfGridSteps, scaleProportionallyToWidthOfGridSteps, widthOfGridStepsFloat, scaleProportionallyToWidthOfGridStepsFloat
)
Expand All @@ -9,7 +9,7 @@ module GridLayout1 exposing
# Shared
@docs ScreenClass, LayoutState, LayoutConfig, GridMargin, WindowSize, windowSizeDecoder, init, update
@docs ScreenClass, LayoutState, LayoutConfig, WrappedConfig, GridMargin, WindowSize, windowSizeDecoder, init, update
# Layout
Expand Down Expand Up @@ -69,7 +69,7 @@ type ScreenClass
type alias LayoutState =
{ window : WindowSize
, screenClass : ScreenClass
, config : LayoutConfig
, config : WrappedConfig
, grid :
{ contentWidth : Int
, columnCount : Int
Expand Down Expand Up @@ -125,7 +125,7 @@ init config window =
in
{ window = window
, screenClass = screenClass
, config = config
, config = WrappedConfig config
, grid =
case screenClass of
MobileScreen ->
Expand Down Expand Up @@ -164,7 +164,7 @@ init config window =
-}
update : LayoutState -> WindowSize -> LayoutState
update { config } window =
init config window
init (accessConfig config) window



Expand All @@ -175,7 +175,7 @@ update { config } window =
-}
bodyAttributes : LayoutState -> List (Attribute msg)
bodyAttributes layout =
[ width (fill |> minimum layout.config.mobileScreen.minGridWidth) ]
[ width (fill |> minimum (accessConfig layout.config).mobileScreen.minGridWidth) ]


{-| A helper to build the application `Layout`. See Readme for example usage.
Expand All @@ -194,7 +194,7 @@ layoutInnerAttributes layout =
maxWidth =
case layout.screenClass of
MobileScreen ->
layout.config.mobileScreen.maxGridWidth
(accessConfig layout.config).mobileScreen.maxGridWidth
|> Maybe.withDefault layout.window.width
in
[ width (fill |> maximum maxWidth)
Expand Down Expand Up @@ -297,26 +297,16 @@ Returns the width of specified number of grid steps (including gutters), in pixe
widthOfGridStepsFloat : LayoutState -> Int -> Float
widthOfGridStepsFloat layout numberOfSteps =
let
columnCount : Int
columnCount =
case layout.screenClass of
MobileScreen ->
layout.config.mobileScreen.columnCount

gutterCountBetween : Int
gutterCountBetween =
numberOfSteps - 1

gutterWidth : Int
gutterWidth =
layout.grid.gutter

stepWidth : Float
stepWidth =
(toFloat layout.grid.contentWidth - toFloat gutterWidth * (toFloat columnCount - 1))
/ toFloat columnCount
(toFloat layout.grid.contentWidth - toFloat layout.grid.gutter * (toFloat layout.grid.columnCount - 1))
/ toFloat layout.grid.columnCount
in
(stepWidth * toFloat numberOfSteps) + (toFloat gutterWidth * toFloat gutterCountBetween)
(stepWidth * toFloat numberOfSteps) + (toFloat layout.grid.gutter * toFloat gutterCountBetween)


{-| A to scale an element to specified width of steps, maintaining the original proportions (e.g. of an image which you want never to be cropped).
Expand Down Expand Up @@ -362,3 +352,20 @@ scaleProportionallyToWidthOfGridStepsFloat layout { originalWidth, originalHeigh
{ width = widthFloat
, height = widthFloat * (toFloat originalHeight / toFloat originalWidth)
}



-- IMPLEMENTATION DETAILS


{-| A config is not meant to be accessed from the client code, so it's wrapped in this type to prevent direct access.
-}
type WrappedConfig
= WrappedConfig LayoutConfig


{-| A helper to access the wrapped config.
-}
accessConfig : WrappedConfig -> LayoutConfig
accessConfig (WrappedConfig config) =
config
52 changes: 28 additions & 24 deletions src/GridLayout2.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module GridLayout2 exposing
( ScreenClass(..), LayoutState, LayoutConfig, GridMargin(..), WindowSize, windowSizeDecoder, init, update
( ScreenClass(..), LayoutState, LayoutConfig, WrappedConfig, GridMargin(..), WindowSize, windowSizeDecoder, init, update
, bodyAttributes, layoutOuterAttributes, layoutInnerAttributes
, gridRow, gridColumn, gridBox, widthOfGridSteps, heightOfGridSteps, scaleProportionallyToWidthOfGridSteps, widthOfGridStepsFloat, scaleProportionallyToWidthOfGridStepsFloat
)
Expand All @@ -9,7 +9,7 @@ module GridLayout2 exposing
# Shared
@docs ScreenClass, LayoutState, LayoutConfig, GridMargin, WindowSize, windowSizeDecoder, init, update
@docs ScreenClass, LayoutState, LayoutConfig, WrappedConfig, GridMargin, WindowSize, windowSizeDecoder, init, update
# Layout
Expand Down Expand Up @@ -70,7 +70,7 @@ type ScreenClass
type alias LayoutState =
{ window : WindowSize
, screenClass : ScreenClass
, config : LayoutConfig
, config : WrappedConfig
, grid :
{ contentWidth : Int
, columnCount : Int
Expand Down Expand Up @@ -143,7 +143,7 @@ init config window =
in
{ window = window
, screenClass = screenClass
, config = config
, config = WrappedConfig config
, grid =
case screenClass of
MobileScreen ->
Expand Down Expand Up @@ -212,7 +212,7 @@ init config window =
-}
update : LayoutState -> WindowSize -> LayoutState
update { config } window =
init config window
init (accessConfig config) window



Expand All @@ -223,7 +223,7 @@ update { config } window =
-}
bodyAttributes : LayoutState -> List (Attribute msg)
bodyAttributes layout =
[ width (fill |> minimum layout.config.mobileScreen.minGridWidth) ]
[ width (fill |> minimum (accessConfig layout.config).mobileScreen.minGridWidth) ]


{-| A helper to build the application `Layout`. See Readme for example usage.
Expand All @@ -242,11 +242,11 @@ layoutInnerAttributes layout =
maxWidth =
case layout.screenClass of
MobileScreen ->
layout.config.mobileScreen.maxGridWidth
(accessConfig layout.config).mobileScreen.maxGridWidth
|> Maybe.withDefault layout.window.width

DesktopScreen ->
layout.config.desktopScreen.maxGridWidth
(accessConfig layout.config).desktopScreen.maxGridWidth
|> Maybe.withDefault layout.window.width
in
[ width (fill |> maximum maxWidth)
Expand Down Expand Up @@ -349,29 +349,16 @@ Returns the width of specified number of grid steps (including gutters), in pixe
widthOfGridStepsFloat : LayoutState -> Int -> Float
widthOfGridStepsFloat layout numberOfSteps =
let
columnCount : Int
columnCount =
case layout.screenClass of
MobileScreen ->
layout.config.mobileScreen.columnCount

DesktopScreen ->
layout.config.desktopScreen.columnCount

gutterCountBetween : Int
gutterCountBetween =
numberOfSteps - 1

gutterWidth : Int
gutterWidth =
layout.grid.gutter

stepWidth : Float
stepWidth =
(toFloat layout.grid.contentWidth - toFloat gutterWidth * (toFloat columnCount - 1))
/ toFloat columnCount
(toFloat layout.grid.contentWidth - toFloat layout.grid.gutter * (toFloat layout.grid.columnCount - 1))
/ toFloat layout.grid.columnCount
in
(stepWidth * toFloat numberOfSteps) + (toFloat gutterWidth * toFloat gutterCountBetween)
(stepWidth * toFloat numberOfSteps) + (toFloat layout.grid.gutter * toFloat gutterCountBetween)


{-| A to scale an element to specified width of steps, maintaining the original proportions (e.g. of an image which you want never to be cropped).
Expand Down Expand Up @@ -417,3 +404,20 @@ scaleProportionallyToWidthOfGridStepsFloat layout { originalWidth, originalHeigh
{ width = widthFloat
, height = widthFloat * (toFloat originalHeight / toFloat originalWidth)
}



-- IMPLEMENTATION DETAILS


{-| A config is not meant to be accessed from the client code, so it's wrapped in this type to prevent direct access.
-}
type WrappedConfig
= WrappedConfig LayoutConfig


{-| A helper to access the wrapped config.
-}
accessConfig : WrappedConfig -> LayoutConfig
accessConfig (WrappedConfig config) =
config
57 changes: 29 additions & 28 deletions src/GridLayout3.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module GridLayout3 exposing
( ScreenClass(..), LayoutState, LayoutConfig, GridMargin(..), WindowSize, windowSizeDecoder, init, update
( ScreenClass(..), LayoutState, LayoutConfig, WrappedConfig, GridMargin(..), WindowSize, windowSizeDecoder, init, update
, bodyAttributes, layoutOuterAttributes, layoutInnerAttributes
, gridRow, gridColumn, gridBox, widthOfGridSteps, heightOfGridSteps, scaleProportionallyToWidthOfGridSteps, widthOfGridStepsFloat, scaleProportionallyToWidthOfGridStepsFloat
)
Expand All @@ -9,7 +9,7 @@ module GridLayout3 exposing
# Shared
@docs ScreenClass, LayoutState, LayoutConfig, GridMargin, WindowSize, windowSizeDecoder, init, update
@docs ScreenClass, LayoutState, LayoutConfig, WrappedConfig, GridMargin, WindowSize, windowSizeDecoder, init, update
# Layout
Expand Down Expand Up @@ -71,7 +71,7 @@ type ScreenClass
type alias LayoutState =
{ window : WindowSize
, screenClass : ScreenClass
, config : LayoutConfig
, config : WrappedConfig
, grid :
{ contentWidth : Int
, columnCount : Int
Expand Down Expand Up @@ -154,7 +154,7 @@ init config window =
in
{ window = window
, screenClass = screenClass
, config = config
, config = WrappedConfig config
, grid =
case screenClass of
MobileScreen ->
Expand Down Expand Up @@ -253,7 +253,7 @@ init config window =
-}
update : LayoutState -> WindowSize -> LayoutState
update { config } window =
init config window
init (accessConfig config) window



Expand All @@ -264,7 +264,7 @@ update { config } window =
-}
bodyAttributes : LayoutState -> List (Attribute msg)
bodyAttributes layout =
[ width (fill |> minimum layout.config.mobileScreen.minGridWidth) ]
[ width (fill |> minimum (accessConfig layout.config).mobileScreen.minGridWidth) ]


{-| A helper to build the application `Layout`. See Readme for example usage.
Expand All @@ -283,15 +283,15 @@ layoutInnerAttributes layout =
maxWidth =
case layout.screenClass of
MobileScreen ->
layout.config.mobileScreen.maxGridWidth
(accessConfig layout.config).mobileScreen.maxGridWidth
|> Maybe.withDefault layout.window.width

TabletScreen ->
layout.config.tabletScreen.maxGridWidth
(accessConfig layout.config).tabletScreen.maxGridWidth
|> Maybe.withDefault layout.window.width

DesktopScreen ->
layout.config.desktopScreen.maxGridWidth
(accessConfig layout.config).desktopScreen.maxGridWidth
|> Maybe.withDefault layout.window.width
in
[ width (fill |> maximum maxWidth)
Expand Down Expand Up @@ -394,32 +394,16 @@ Returns the width of specified number of grid steps (including gutters), in pixe
widthOfGridStepsFloat : LayoutState -> Int -> Float
widthOfGridStepsFloat layout numberOfSteps =
let
columnCount : Int
columnCount =
case layout.screenClass of
MobileScreen ->
layout.config.mobileScreen.columnCount

TabletScreen ->
layout.config.tabletScreen.columnCount

DesktopScreen ->
layout.config.desktopScreen.columnCount

gutterCountBetween : Int
gutterCountBetween =
numberOfSteps - 1

gutterWidth : Int
gutterWidth =
layout.grid.gutter

stepWidth : Float
stepWidth =
(toFloat layout.grid.contentWidth - toFloat gutterWidth * (toFloat columnCount - 1))
/ toFloat columnCount
(toFloat layout.grid.contentWidth - toFloat layout.grid.gutter * (toFloat layout.grid.columnCount - 1))
/ toFloat layout.grid.columnCount
in
(stepWidth * toFloat numberOfSteps) + (toFloat gutterWidth * toFloat gutterCountBetween)
(stepWidth * toFloat numberOfSteps) + (toFloat layout.grid.gutter * toFloat gutterCountBetween)


{-| A to scale an element to specified width of steps, maintaining the original proportions (e.g. of an image which you want never to be cropped).
Expand Down Expand Up @@ -465,3 +449,20 @@ scaleProportionallyToWidthOfGridStepsFloat layout { originalWidth, originalHeigh
{ width = widthFloat
, height = widthFloat * (toFloat originalHeight / toFloat originalWidth)
}



-- IMPLEMENTATION DETAILS


{-| A config is not meant to be accessed from the client code, so it's wrapped in this type to prevent direct access.
-}
type WrappedConfig
= WrappedConfig LayoutConfig


{-| A helper to access the wrapped config.
-}
accessConfig : WrappedConfig -> LayoutConfig
accessConfig (WrappedConfig config) =
config

0 comments on commit d4c9311

Please sign in to comment.