Initial window size and other initial signal values

66 views
Skip to first unread message

Bob Hutchison

unread,
Oct 24, 2015, 10:27:54 AM10/24/15
to elm-d...@googlegroups.com
Hi,

I’m new to Elm and am experimenting a bit, in particular with programmatic placement of HTML elements. To place something on the screen it helps to know what the possible positions are, and to know that I think I need the initial window size. I have a signal of window dimensions and that works fine for dimension changes but, as is well known on this list, there will be no signal change event that provides the initial size of the window.

I’ve googled around and there are a few possible solutions to this that I’ve come across or that suggest themselves, and I’m not particularly happy with any of them and am hoping for someone to suggest a better approach.

1) don’t worry, resize the window. This is what I’m doing now, and obviously, it’s not a solution.

2) create a port and pass the initial size of the window in through it, and never use the port again because the window dimension signal will reflect future changes. Seems like a lot of stuff/noise for something so simple.

3) On another thread, Janis Voigtländer suggested using foldp’ from http://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/3.2.1/Signal-Extra. It’s not clear to me what those actions to the init function would be, but I tried it anyway and of course got a NoOp action. It seems I don’t know enough Elm to make use of this suggestion.

Help?

Thanks,
Bob

Sean Clark Hess

unread,
Oct 24, 2015, 10:45:05 AM10/24/15
to elm-d...@googlegroups.com
Hi bob. I have an outstanding pull request that addresses this for start app. It is unclear if it will end up merged or as a fork, but it works. For now you can paste StartApp.elm into your project and run with it if you like. 

Also feel free to chime in on the pull request if you would find it helpful!
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bob Hutchison

unread,
Oct 25, 2015, 11:42:00 AM10/25/15
to elm-d...@googlegroups.com

> On Oct 24, 2015, at 10:45 AM, Sean Clark Hess <sean...@gmail.com> wrote:
>
> Hi bob. I have an outstanding pull request that addresses this for start app. It is unclear if it will end up merged or as a fork, but it works. For now you can paste StartApp.elm into your project and run with it if you like.
>
> Also feel free to chime in on the pull request if you would find it helpful!

Thanks for your response Sean. I had a look at your pull request and don’t have anything useful to say, other than the obvious: I’d be nice if it could be made to work.

However, I did manage to make things work for my specific case this morning.

First, I noticed that Signal.mergeMany drops all but the left-most simultaneous signal. So, after getting over my surprise, and a little ‘debugging’ with Debug.log (printf debugging will be with us forever :-) I re-arranged the signals in my mergeMany so the left-most was the window dimension signal. This almost worked, but update wasn’t called. Okay, so this is what Signal.Extra.foldp’ is for, and I called update with the initial window dimension signal. And it’s working well enough.

Turns out if you do it this way there’s (at least sometimes, certainly in Chrome with the console open) *two* window dimension signals at the start up, the second being slightly smaller and, just guessing here, looks to be accounting for window controls. This is a minor annoyance, because I get what amounts to a window resize just after initialising.

What I did here works but it’s hardly a general solution. If I had two or more things that had initial values I’d have to do something else. It seems to me I’d have to use one of the Signal.mapN functions to create a Signal of the combined values. Signal.Extra has some zip functions that combine signals into tuples (up to 4) and this is probably handy, but I think I’d prefer (but what do I know) a list of the actual simultaneous actions (all with the type Action (or whatever)). This way you’d have the changes rather than a snapshot. So you could write something like:

actions : Signal Action
actions =
Signal.combineMany
[
(Signal.map WindowSize Window.dimensions)
, (Signal.map KeyStroke Keyboard.presses)
, (Signal.map MouseAt Mouse.position)
]

and get a list of Action as a result. Yeah, I know, and then what… probably have to have a Action of Actions which is manageable.

Cheers,
Bob
Reply all
Reply to author
Forward
0 new messages