Use SizeReq factor in stack

This commit is contained in:
Francisco Vallarino 2020-10-04 13:15:35 -03:00
parent cafe26e648
commit 091b234336
5 changed files with 45 additions and 28 deletions

View File

@ -81,9 +81,7 @@ main = do
winSize <- getDrawableSize window
let model = def {
_textField1 = "This is a test! Or not? Well, we'll see"
}
let model = def
let devicePixelRate = _sW winSize / fromIntegral screenWidth
let appWidget = createApp model (Just InitApp) handleAppEvent buildUI
let monomerContext = initMonomerContext () winSize useHiDPI devicePixelRate
@ -150,9 +148,10 @@ buildUI model = trace "Creating UI" widgetTree where
label_ "This is a really long label used to check what I did works fine" [textEllipsis],
label "Short label"
],
hgrid [
image "assets/images/pecans.jpg",
image_ "https://picsum.photos/600/400" [fitHeight, transparency 1]
hstack [
image_ "assets/images/pecans.jpg" [fitFill],
spacer,
image_ "https://picsum.photos/600/400" [fitHeight]
],
hstack [
label "Test"

View File

@ -50,8 +50,8 @@ instance Monoid ThemeState where
-- | Basic styling attributes
data SizeReq
= FlexSize Coord Factor
| FixedSize Coord
= FixedSize Coord
| FlexSize Coord Factor
-- | MinSize Coord
-- | MaxSize Coord
-- | RangeSize Coord Coord

View File

@ -4,6 +4,8 @@ module Monomer.Widget.Internal (
handleStyleChange,
isStrictReq,
getReqCoord,
getReqFactor,
getReqFactored,
modifyReqCoord
) where
@ -68,12 +70,19 @@ isStrictReq FlexSize{} = False
isStrictReq _ = True
getReqCoord :: SizeReq -> Coord
getReqCoord (FlexSize c _) = c
getReqCoord (FixedSize c) = c
getReqCoord (FlexSize c _) = c
--getReqCoord (MinSize c) = c
--getReqCoord (MaxSize c) = c
--getReqCoord (RangeSize c1 _) = c1
getReqFactor :: SizeReq -> Factor
getReqFactor (FixedSize _) = 1
getReqFactor (FlexSize _ f) = f
getReqFactored :: SizeReq -> Coord
getReqFactored req = getReqFactor req * getReqCoord req
modifyReqCoord :: SizeReq -> (Coord -> Coord) -> SizeReq
modifyReqCoord (FlexSize c factor) f = FlexSize (f c) factor
modifyReqCoord (FixedSize c) f = FixedSize (f c)

View File

@ -108,7 +108,8 @@ makeCheckbox widgetData config = widget where
reqs = setValueReq ++ _ckcOnChangeReq config
clickReqs = setFocusReq : reqs
getSizeReq wenv inst = (FixedSize checkboxWidth, FixedSize checkboxWidth)
getSizeReq wenv inst =
(FixedSize checkboxWidth, FixedSize checkboxWidth)
render renderer wenv inst = do
renderCheckbox renderer config rarea fgColor

View File

@ -52,17 +52,18 @@ makeStack isHorizontal = widget where
resize wenv viewport renderArea children widgetInst = resized where
Rect l t w h = renderArea
vchildren = Seq.filter _wiVisible children
mainSize = if isHorizontal then w else h
mainStart = if isHorizontal then l else t
vchildren = Seq.filter _wiVisible children
sChildren = Seq.filter (isStrictReq . mainReqSelector) vchildren
fChildren = Seq.filter (not . isStrictReq . mainReqSelector) vchildren
fExists = not $ null fChildren
sSize = sizeSelector $ calcSize sChildren
fSize = sizeSelector $ calcSize fChildren
fSizeFactor = sizeSelector $ calcSizeFactor fChildren
rSize = max 0 (mainSize - sSize)
fExtra
| fExists && fSize > 0 = (rSize - fSize) / fSize
| fExists && fSize > 0 = (rSize - fSize) / fSizeFactor
| otherwise = 0
assignedArea = Seq.zip newViewports newViewports
(newViewports, _) = foldl' foldHelper (Seq.empty, mainStart) children
@ -77,7 +78,8 @@ makeStack isHorizontal = widget where
emptyRect = Rect l t 0 0
calcMainSize = case mainReqSelector child of
FixedSize sz -> sz
FlexSize sz factor -> (1 + fExtra) * sz -- factor still not accounted for
-- factor still not accounted for
FlexSize sz factor -> (1 + fExtra * factor) * sz -- (1 + fExtra) * sz
calcSndSize total = case sndReqSelector child of
FixedSize sz -> sz
_ -> total
@ -88,26 +90,32 @@ makeStack isHorizontal = widget where
| isHorizontal = hRect
| otherwise = vRect
calcSize vchildren = Size width height where
(maxWidth, sumWidth, maxHeight, sumHeight) = calcDimensions vchildren
width
| isHorizontal = sumWidth
| otherwise = maxWidth
height
| isHorizontal = maxHeight
| otherwise = sumHeight
calcSize vchildren = calcSize_ vchildren False
calcSizeFactor vchildren = calcSize_ vchildren True
calcDimensions vchildren = (maxWidth, sumWidth, maxHeight, sumHeight) where
calcSize_ vchildren useFactor = Size width height where
(maxW, sumW, maxH, sumH) = calcDimensions vchildren useFactor
width
| isHorizontal = sumW
| otherwise = maxW
height
| isHorizontal = maxH
| otherwise = sumH
calcDimensions vchildren useFactor = (maxW, sumW, maxH, sumH) where
getReqSize
| useFactor = getReqFactored
| otherwise = getReqCoord
vreqsW = _wiSizeReqW <$> vchildren
vreqsH = _wiSizeReqH <$> vchildren
sumWidth = (sum . fmap getReqCoord) vreqsW
sumHeight = (sum . fmap getReqCoord) vreqsH
maxWidth
sumW = (sum . fmap getReqSize) vreqsW
sumH = (sum . fmap getReqSize) vreqsH
maxW
| Seq.null vchildren = 0
| otherwise = (maximum . fmap getReqCoord) vreqsW
maxHeight
| otherwise = (maximum . fmap getReqSize) vreqsW
maxH
| Seq.null vchildren = 0
| otherwise = (maximum . fmap getReqCoord) vreqsH
| otherwise = (maximum . fmap getReqSize) vreqsH
mainReqSelector
| isHorizontal = _wiSizeReqW