Add Monomer.Helper for not exported util functions

This commit is contained in:
Francisco Vallarino 2021-06-02 17:50:15 -03:00
parent e1bc25e318
commit 1075c578da
20 changed files with 57 additions and 52 deletions

View File

@ -34,6 +34,7 @@ import Monomer.Core.BasicTypes
import Monomer.Core.StyleTypes
import Monomer.Core.StyleUtil
import Monomer.Core.Util
import Monomer.Helper
import qualified Monomer.Core.Lens as L

View File

@ -31,8 +31,7 @@ module Monomer.Core.StyleUtil (
addOuterSize,
addOuterBounds,
removeOuterSize,
removeOuterBounds,
setStyleValue
removeOuterBounds
) where
import Control.Lens ((&), (^.), (^?), (.~), (?~), _Just, non)
@ -258,13 +257,3 @@ borderWidths (Just border) = (bl, br, bt, bb) where
justDef :: (Default a) => Maybe a -> a
justDef val = fromMaybe def val
setStyleValue style fieldL op value = newStyle where
val = _styleActive style
newStyle = style
& op (L.basic . non def . fieldL) value
& op (L.hover . non def . fieldL) value
& op (L.focus . non def . fieldL) value
& op (L.focusHover . non def . fieldL) value
& op (L.active . non def . fieldL) value
& op (L.disabled . non def . fieldL) value

View File

@ -25,6 +25,7 @@ import qualified Data.Text as T
import Monomer.Core.BasicTypes
import Monomer.Core.Style
import Monomer.Core.WidgetTypes
import Monomer.Helper
import qualified Monomer.Lens as L
@ -53,6 +54,13 @@ getLayoutDirection :: Bool -> LayoutDirection
getLayoutDirection False = LayoutVertical
getLayoutDirection True = LayoutHorizontal
-- | Filters user events from a list of WidgetRequests.
eventsFromReqs :: Seq (WidgetRequest s e) -> Seq e
eventsFromReqs reqs = seqCatMaybes mevents where
mevents = flip fmap reqs $ \case
RaiseEvent ev -> cast ev
_ -> Nothing
{-|
Ignore events generated by the parent. Could be used to consume the tab key and
avoid having the focus move to the next widget.
@ -267,36 +275,6 @@ isResizeAnyResult res = isResizeResult res || isResizeImmediateResult res
isMacOS :: WidgetEnv s e -> Bool
isMacOS wenv = _weOs wenv == "Mac OS X"
-- | Checks if the first sequence is a prefix of the second.
seqStartsWith :: Eq a => Seq a -> Seq a -> Bool
seqStartsWith prefix seq = Seq.take (length prefix) seq == prefix
-- | Filters Nothing instances out of a Seq, and removes the Just wrapper.
seqCatMaybes :: Seq (Maybe a) -> Seq a
seqCatMaybes Empty = Empty
seqCatMaybes (x :<| xs) = case x of
Just val -> val :<| seqCatMaybes xs
_ -> seqCatMaybes xs
-- | Filters user events from a list of WidgetRequests.
eventsFromReqs :: Seq (WidgetRequest s e) -> Seq e
eventsFromReqs reqs = seqCatMaybes mevents where
mevents = flip fmap reqs $ \case
RaiseEvent ev -> cast ev
_ -> Nothing
-- Returns the maximum value of a given floating type.
maxNumericValue :: (RealFloat a) => a
maxNumericValue = x where
n = floatDigits x
b = floatRadix x
(_, u) = floatRange x
x = encodeFloat (b^n - 1) (u - n)
-- | Restricts a value to a given range.
clamp :: (Ord a) => a -> a -> a -> a
clamp mn mx = max mn . min mx
-- | Returns a string description of a node and its children.
widgetTreeDesc :: Int -> WidgetNode s e -> String
widgetTreeDesc level node = desc where

View File

@ -20,6 +20,7 @@ module Monomer.Graphics.Color (
import Data.Char (digitToInt)
import Monomer.Graphics.Types
import Monomer.Helper
-- | Restricts a color channel to its valid range.
clampChannel :: Int -> Int
@ -54,10 +55,6 @@ rgba r g b a = Color {
_colorA = clampAlpha a
}
-- Creates a non visible color.
-- | Creates a non visible color.
transparent :: Color
transparent = rgba 0 0 0 0
-- Helpers
clamp :: (Ord a) => a -> a -> a -> a
clamp mn mx = max mn . min mx

View File

@ -607,14 +607,11 @@ strokeBorder renderer from to (Just BorderSide{..}) = do
renderLineTo renderer to
stroke renderer
justDef :: (Default a) => Maybe a -> a
justDef val = fromMaybe def val
p2 :: Double -> Double -> Point
p2 x y = Point x y
radW :: Maybe RadiusCorner -> Double
radW r = _rcrWidth (justDef r)
radW r = _rcrWidth (fromMaybe def r)
fixRadius :: Rect -> Radius -> Radius
fixRadius (Rect _ _ w h) (Radius tl tr bl br) = newRadius where

View File

@ -39,6 +39,7 @@ import Monomer.Core.BasicTypes
import Monomer.Core.StyleTypes
import Monomer.Core.StyleUtil
import Monomer.Graphics.Types
import Monomer.Helper
import Monomer.Lens as L

28
src/Monomer/Helper.hs Normal file
View File

@ -0,0 +1,28 @@
module Monomer.Helper where
import Data.Sequence (Seq(..))
import qualified Data.Sequence as Seq
-- | Checks if the first sequence is a prefix of the second.
seqStartsWith :: Eq a => Seq a -> Seq a -> Bool
seqStartsWith prefix seq = Seq.take (length prefix) seq == prefix
-- | Filters Nothing instances out of a Seq, and removes the Just wrapper.
seqCatMaybes :: Seq (Maybe a) -> Seq a
seqCatMaybes Empty = Empty
seqCatMaybes (x :<| xs) = case x of
Just val -> val :<| seqCatMaybes xs
_ -> seqCatMaybes xs
-- Returns the maximum value of a given floating type.
maxNumericValue :: (RealFloat a) => a
maxNumericValue = x where
n = floatDigits x
b = floatRadix x
(_, u) = floatRange x
x = encodeFloat (b^n - 1) (u - n)
-- | Restricts a value to a given range.
clamp :: (Ord a) => a -> a -> a -> a
clamp mn mx = max mn . min mx

View File

@ -51,6 +51,7 @@ import qualified SDL.Raw.Types as SDLT
import Monomer.Core
import Monomer.Event
import Monomer.Helper
import Monomer.Main.Types
import Monomer.Main.Util

View File

@ -45,6 +45,7 @@ import GHC.Generics
import qualified Data.Sequence as Seq
import Monomer.Helper
import Monomer.Widgets.Container
import Monomer.Widgets.Animation.Types

View File

@ -87,6 +87,7 @@ import Monomer.Core
import Monomer.Core.Combinators
import Monomer.Event
import Monomer.Graphics
import Monomer.Helper
import Monomer.Widgets.Singles.Spacer
import Monomer.Widgets.Util

View File

@ -56,6 +56,7 @@ import GHC.Generics
import qualified Data.Sequence as Seq
import Monomer.Helper
import Monomer.Widgets.Container
import Monomer.Widgets.Containers.SelectList
import Monomer.Widgets.Singles.Label

View File

@ -48,6 +48,7 @@ import Monomer.Core
import Monomer.Core.Combinators
import Monomer.Event
import Monomer.Graphics
import Monomer.Helper
import Monomer.Widgets.Util
import qualified Monomer.Lens as L

View File

@ -37,6 +37,7 @@ import GHC.Generics
import qualified Data.Sequence as Seq
import qualified Data.Text as T
import Monomer.Helper
import Monomer.Widgets.Single
import qualified Monomer.Lens as L

View File

@ -48,6 +48,7 @@ import GHC.Generics
import qualified Data.Sequence as Seq
import Monomer.Helper
import Monomer.Widgets.Single
import qualified Monomer.Lens as L

View File

@ -54,6 +54,7 @@ import GHC.Generics
import qualified Data.Sequence as Seq
import Monomer.Helper
import Monomer.Widgets.Single
import qualified Monomer.Lens as L

View File

@ -49,6 +49,7 @@ import GHC.Generics
import qualified Data.Sequence as Seq
import qualified Data.Text as T
import Monomer.Helper
import Monomer.Widgets.Containers.Scroll
import Monomer.Widgets.Single

View File

@ -31,6 +31,7 @@ import qualified Data.Sequence as Seq
import Monomer.Core
import Monomer.Event.Types
import Monomer.Helper
import Monomer.Widgets.Util.Hover
import Monomer.Widgets.Util.Widget

View File

@ -31,6 +31,7 @@ import Monomer.Core
import Monomer.Event.Types
import Monomer.Event.Util
import Monomer.Graphics.Types
import Monomer.Helper
import qualified Monomer.Lens as L

View File

@ -37,6 +37,7 @@ import qualified Data.Sequence as Seq
import Monomer.Core
import Monomer.Event
import Monomer.Graphics
import Monomer.Helper
import Monomer.Widgets.Util.Focus
import Monomer.Widgets.Util.Hover
import Monomer.Widgets.Util.Types

View File

@ -644,10 +644,12 @@
- KeyReleased / BtnReleased is inconsistent. Use a single convention.
- restrictValue -> clamp.
- Split CmbImageFit
- Does adding all the isXXXXEvent back make sense? Added.
Next
- Review after documenting
- Does adding all the isXXXXEvent back make sense?
- CmbVisible and CmbVisible.
- Add Event Util isXXXX.
- Can validModel (inputField) handle WidgetData instead of only Lens?
- Check _wniOverlay. Can it be replaced with overlayPath check?
- Scroll related test case is also weird.