formselementdimensionselminspection

Elm - Form inspection


The below function includes the use of collage, only to convert the Shape/Form into an Element.

makeSlider : Element -> Form -> Element
makeSlider sliderShape sliderBarForm =
  let                                                                                                 
    sliderBar =
      --convert sliderBarForm from type Form to Element
      collage 1000 1000 [(move (0,0) sliderBarForm)]
  in 
    layers [sliderShape, sliderBar]

Is there a way to extract a Form's dimensions, like Graphics.Element.sizeOf, so that the makeSlider function does not need to know the explicit dimensions of sliderBarForm?

Or does a toElement function exist so that

collage 1000 1000 [(move (0,0) sliderBarForm)]

can be replaced with

toElement (move (0,0) sliderBarForm)?


Solution

  • I don't think you can easily get the dimensions of a form. I guess that's because a form can be so many different kinds of things (shapes, paths, etc). When I need to know the dimensions later, I keep track of them when I create the form.

    type alias MyForm =
      {   form : Form
        , width : Int
        , height : Int
      }
    
    makeRectForm: Float -> Float -> MyForm
    makeRectForm  w h = {form: (filled grey (rect w h)), width: (round w), height: (round h)}
    

    Then in your case you can use a MyForm with the collage.

    makeSlider : Element -> MyForm -> Element
    makeSlider sliderShape sliderBarMyForm =
      let                                                                                                 
        sliderBar =
          --convert sliderBarForm from type Form to Element
          collage sliderBarMyForm.width sliderBarMyForm.height [(move (0,0) sliderBarMyForm)]
      in 
        layers [sliderShape, sliderBar]