On Sep 27, 10:29 am, Joel Webber <j...@google.com> wrote:
> getOffsetWidth/Height() always read the current state of the widget's
> backing element, which won't be updated until the layout panels are done
> laying themselves out. All the layout panels have the potential to be
> animated, and defer actually laying things out until the end of the current
> event (in this case, the current "event" is likely the module startup). This
> is pretty important for optimization, because layout can be expensive, and
> you don't want to do any layout work until the app's done fiddling with its
> structure. That's why you're seeing the "correct" value in a DeferredCommand
> -- it's guaranteed to run after the current event.
>
> If you really need to force layout earlier, you can call
> LayoutPanel.forceLayout(). But be careful with this, because you can end up
> forcing more layouts than you mean to (in the example you show, it would be
> fine, but if you were to take a similar structure and nest it within another
> that *also* called forceLayout(), you would end up doing extra work).
>
> Hope that helps,
> joel.

Yes Joel, that does help.  You're definitely right in that we need to
be careful about executing relayouts in the GWT panels or by the
browser.  The reason the DeferredCommand is not optimal is because it
allows the browser to display the UI before all of it has been
properly sized (the bits that need to call getOffset*) which results
in the browser "jumping".  The user sees un-initialized panels briefly
and then it redraws correctly which can be unpleasant.  I think we
want to avoid that.

I just tried a quick experiment to try your suggestion but the next
question is on which component to call forceLayout.  In my simple
example, just calling RootLayoutPanel.get().forceLayout() inside the
onLoad method works but if that FlowPanel is buried somewhere deep in
the app, what then?  Do I rely on the very top component
(onModuleLoad) to call forceLayout/onResize.  Does my onLoad call
forceLayout on the entire RootLayoutPanel regardless of how deep it is
or just its parent?  What should happen if the FlowPanel in question
is lazily created and added to the DOM later (in which case it's
onLoad method is the one place that knows for sure it's been
attached).

Unless there is some discussion I have missed, I think that perhaps
this is worth starting a "Best Practices" thread.  I have some ideas
on how to use onLoad and onResize and avoid some of the performance
issues and now forceLayout needs to be included in the mix but I won't
pretend to have the right or best answer.

-- 
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to