on develop help sign in
layout: fix bug in distribution of available space when it cannot be evenly distributed
commitd91e22d81663cde43c0cc2e37909b2aa8ed446e9 authorAli Abrar <aliabrar@gmail.com> dateWed 24 Mar 2021, 13:14 (6 years ago) parent5e82cc1
2 files changed+13−1
ChangeLog.md+1 src/Reflex/Vty/Widget/Layout.hs+12−1
ChangeLog.md+1
@@ -54,6 +54,7 @@
5454* _Breaking Change_: `ImageWriter` now has a method `mapImages` for transforming the images emitted by a child widget
5555* Add `hoistRunLayout` to apply a transformation to the context of a `Layout` action and run that action
5656* Add an `MFunctor` instance for `VtyWidget`
57+* Bug: fix distribution of available space when it cannot be evenly distributed. Previously, all leftover space would be allocated to the first stretchable widget.
5758
5859## 0.1.4.1
5960* Migrate to new dependent-sum / dependent-map (after the "some" package split)
src/Reflex/Vty/Widget/Layout.hs+12−1
@@ -319,16 +319,27 @@ solve o0 r0 (LayoutForest cs) =
319319 -> [(a, Constraint)]
320320 -> [(a, Int)]
321321 computeSizes available constraints =
322+ -- The minimum amount of space we need. Calculated by adding up all of
323+ -- the fixed size items and all the minimum sizes of stretchable items
322324 let minTotal = sum $ ffor constraints $ \case
323325 (_, Constraint_Fixed n) -> n
324326 (_, Constraint_Min n) -> n
327+ -- The leftover space is the area we can allow stretchable items to
328+ -- expand into
325329 leftover = max 0 (available - minTotal)
330+ -- The number of stretchable items that will try to share some of the
331+ -- leftover space
326332 numStretch = length $ filter (isMin . snd) constraints
333+ -- Space to allocate to the stretchable items (this is the same for all
334+ -- items and there may still be additional leftover space that will have
335+ -- to be unevenly distributed)
327336 szStretch = floor $ leftover % max numStretch 1
337+ -- Remainder of available space after even distribution. This extra space
338+ -- will be distributed to as many stretchable widgets as possible.
328339 adjustment = max 0 $ available - minTotal - szStretch * numStretch
329340 in snd $ mapAccumL (\adj (a, c) -> case c of
330341 Constraint_Fixed n -> (adj, (a, n))
331− Constraint_Min n -> (0, (a, n + szStretch + adj))) adjustment constraints
342+ Constraint_Min n -> (max 0 (adj-1), (a, n + szStretch + signum adj))) adjustment constraints
332343 isMin (Constraint_Min _) = True
333344 isMin _ = False
334345