packages feed

geomancy-layout-0.2.1 / test / Menu / Probe.hs

module Menu.Probe  ( invariants  ) whereimport Geomancy.Layout.Box (Box)import Geomancy.Layout.Box qualified as Boximport Geomancy.Layout.View (View(..))import Geomancy.Vec2 (Vec2, withVec2)import Menu.Kitimport Numeric (showFFloat)data Edge = L | T | R | BedgeOf :: Edge -> Box -> FloatedgeOf e box =  Box.withTRBL box \t r b l ->    case e of      L -> l      T -> t      R -> r      B -> bleaves :: View Box Widget -> [(Widget, Box)]leaves = \case  View{ann, stuff} -> [(stuff, ann)]  node -> concatMap leaves (subviews node)subviews :: View ann stuff -> [View ann stuff]subviews = \case  View{} -> []  Spacer{} -> []  HStack{children} -> children  VStack{children} -> children  ZStack{children} -> children  Grid{rows} -> concat rows  Overlay{primary, secondary} -> [primary, secondary]  Background{primary, secondary} -> [secondary, primary]  Frame{inner} -> [inner]  FlexibleFrame{inner} -> [inner]  Padding{inner} -> [inner]  Offset{inner} -> [inner]  Span{inner} -> [inner]  AspectRatio{inner} -> [inner]num :: Float -> Stringnum x  | isNaN x = "NaN"  | isInfinite x = if x > 0 then "inf" else "-inf"  | x == fromIntegral (round x :: Int) = show (round x :: Int)  | otherwise = showFFloat (Just 1) x ""invariants :: Vec2 -> View Box Widget -> [String]invariants screen tree =  broken tree <> offscreen <> overlaps tree  where    broken node =      [ "bad size in " <> kind node <> " " <> describe node.ann      | Box.withTRBL node.ann \t r b l -> any isNaN [t, r, b, l] || r - l < -0.01 || b - t < -0.01      ]        <> concatMap broken (subviews node)    offscreen =      withVec2 screen \sw sh ->        [ "off screen: " <> w.name <> " " <> describe box        | (w, box) <- leaves tree        , Box.withTRBL box \t r b l -> l < -0.5 || t < -0.5 || r > sw + 0.5 || b > sh + 0.5        ]    overlaps node =      case node of        HStack{children} -> pairs R L children <> concatMap overlaps children        VStack{children} -> pairs B T children <> concatMap overlaps children        _ -> concatMap overlaps (subviews node)    pairs trailing leading children =      [ "overlap in stack: " <> firstName a <> " / " <> firstName b <> " by " <> num (edgeOf trailing a.ann - edgeOf leading b.ann)      | (a, b) <- zip children (drop 1 children)      , edgeOf leading b.ann < edgeOf trailing a.ann - 0.5      ]    firstName node =      case leaves node of        (w, _) : _ -> w.name        [] -> kind nodedescribe :: Box -> Stringdescribe box =  Box.withTRBL box \t r b l ->    "x " <> num l <> ".." <> num r <> " y " <> num t <> ".." <> num bkind :: View ann stuff -> Stringkind = \case  View{} -> "View"  Spacer{} -> "Spacer"  HStack{} -> "HStack"  VStack{} -> "VStack"  ZStack{} -> "ZStack"  Grid{} -> "Grid"  Overlay{} -> "Overlay"  Background{} -> "Background"  Frame{} -> "Frame"  FlexibleFrame{} -> "FlexibleFrame"  AspectRatio{} -> "AspectRatio"  Padding{} -> "Padding"  Offset{} -> "Offset"  Span{} -> "Span"