4

With 'set text' in Threepenny-gui you can, given an element and an string, set the text of that element:

Prelude Graphics.UI.Threepenny.Core> :t set
set :: ReadWriteAttr x i o -> i -> UI x -> UI x
Prelude Graphics.UI.Threepenny.Core> :t set text
set text :: String -> UI Element -> UI Element

The existence of a get functions makes me think it should be possible to get the text given an element.

Prelude Graphics.UI.Threepenny.Core> :t get
get :: ReadWriteAttr x i o -> x -> UI o
Prelude Graphics.UI.Threepenny.Core> :t text
text :: WriteAttr Element String
Prelude Graphics.UI.Threepenny.Core> :t get text
get text :: Element -> UI ()

However, the return type of 'get text' is not what I expect. How do I recover the original String from an element?

Maarten Faddegon
  • 775
  • 5
  • 11
  • 1
    that's a bit unidiomatic (I have to search a bit - right now I don't know if there is even something to do this in there) - normaly you define your inputs as behaviours and work with them (you can find an easy example here: https://github.com/HeinrichApfelmus/threepenny-gui/blob/master/samples/CurrencyConverter.hs) – Random Dev Aug 19 '14 at 11:23
  • based on the hackage-docs there is right now no way to get it out-of-the-box. as you saw yourself `text` is a `WriteAttr` and those *return* unit per definition (https://hackage.haskell.org/package/threepenny-gui-0.4.2.0/docs/Graphics-UI-Threepenny-Core.html#t:WriteAttr) - maybe you can try and create your own reader-attribut using [mkReaderAttr](https://hackage.haskell.org/package/threepenny-gui-0.4.2.0/docs/Graphics-UI-Threepenny-Core.html#v:mkReadAttr) - see: https://hackage.haskell.org/package/threepenny-gui-0.4.2.0/docs/src/Graphics-UI-Threepenny-Core.html#text – Random Dev Aug 19 '14 at 11:26

1 Answers1

1

If I remember correctly, the problem is that the text value of an element includes all its subelements as HTML code. I recommend to use value instead, which works for input fields.

The ReadAttr and WriteAttr are implemented in terms of ReadWriteAttr to avoid overloading. As the types indicate, get doesn't do anything useful for WriteAttr and set doesn't do anything useful for ReadAttr, though.

Heinrich Apfelmus
  • 11,034
  • 1
  • 39
  • 67