Reply To: MojoX GUI questions

About Monkey 2 Forums Monkey 2 Programming Help MojoX GUI questions Reply To: MojoX GUI questions


Mark Sibly


Here’s my little hack for initial auto-centering. Just replace RightDock.OnRender wth this:

OK, it’s still not all that pretty, and also relies on a ‘done’ style flag. This is alas not easy/possible to do in the ctor because the gui has not been laid out yet – layout is done just before render so while ctor is executing, nothing has been measured/positioned yet.

I chose this design to minimize layout overhead and to simplify things. Another approach is to, as nerobot says, make views update layout in response to resize events, but in my experience this can lead to considerable layout overhead, and even layout ‘lockups’,  esp. in the case of complex hierarchies as one resize can trigger lots of layout code. I’m not adverse to adding a SizeChanged event, but I am against the idea of it being used to trigger layout.

Mojox uses a pretty simple layout system (largely pinched form Android). Layout is a 2 pass process and is always performed just before rendering (could be optimized/improved here). In the first pass, views are measured ‘bottom up’ using OnMeasure. This initializes their MeasuredSize property. Since measuring is bottom up, parent views can therefore depend on children having already been measured when measuring themselves. In the second pass, views are positioned/laid out ‘top down’. This way, everything is measured *exactly* once, and positioned *exactly* once. Also, properties like ‘Rect’ and ‘Frame’ etc are updated once and always in sync with each other and their true ‘render position’.

There are of course some limitations and issues with the mojox approach. Layout of things like wordwrapped text or html views is tricky, because view height is dependant on view width, and there are some hacks in there for dealing with that . And having layout occur ‘at some point in the future’ can sometimes be inconvenient, but in my experience the benefits are worth it. YMMV.