Calculation of widget size during onModuleLoad() => move to DeferredCommand?

19 views
Skip to first unread message

Chris Lercher

unread,
Jun 6, 2010, 3:58:39 PM6/6/10
to Google Web Toolkit
Hi,

I want to perform a size calculation on a composite generated by
UiBinder, using UIObject.getOffsetWidth(). The composite's ui.xml
(simplified) looks like this:

<ui:style>
.test {
width: 80px;
height: 50px;
}
</ui:style>
<g:HTMLPanel styleName="{style.test}">
</g:HTMLPanel>

When I execute getOffsetWidth() during onModuleLoad(), the result is
987 (the same as the body's width in my case). That's quite
understandable: I assume, the CSS from the ui.xml isn't applied yet.

So I put getOffsetWidth() in a DeferredCommand, and now it returns 80,
which is correct.

But I'm a bit worried, that this may actually be a race condition: Can
I be sure, that the CSS will be fully loaded, before the
DeferredCommand gets its turn? Or is there a better place to put that
code?

Thanks
Chris

Chris Lercher

unread,
Jun 7, 2010, 12:58:54 PM6/7/10
to Google Web Toolkit
BTW, here's the uibinder composite "MyComposite", and the code that
uses it:


public class MyComposite extends Composite {

private static MyCompositeUiBinder uiBinder =
GWT.create(MyCompositeUiBinder.class);

interface MyCompositeUiBinder extends UiBinder<Widget, MyComposite> {
}

public MyComposite() {
initWidget(uiBinder.createAndBindUi(this));
}
}


public class MyEntryPoint implements EntryPoint {

public void onModuleLoad() {
final MyComposite myComposite = new MyComposite();
rootPanel.add(myComposite);

rootPanel.add(new Label("" + myComposite.getOffsetWidth()));

DeferredCommand.addCommand(new Command() {

@Override
public void execute() {
rootPanel.add(new Label("" + myComposite.getOffsetWidth()));
}
});
}
}

The first label shows 987 (or whatever the width of <body> is), the
second label shows 80.

kozura

unread,
Jun 7, 2010, 2:27:06 PM6/7/10
to Google Web Toolkit
Yes, this is fine and there is no race condition; I do this all the
time. The deferred command is guaranteed to run after all other
processing of the current event stack is completed, which includes all
rendering that determines final sizing.

Chris Lercher

unread,
Jun 7, 2010, 3:19:41 PM6/7/10
to Google Web Toolkit
Thanks!

Stefan Bachert

unread,
Jun 8, 2010, 12:55:08 PM6/8/10
to Google Web Toolkit
Hi Chris,

I dont think, that the css is not applied yet, rather the whole
uibinder isnt applied, yet.
However, you did not show us the full code.

I usally put elements in a certain environment to measure its real
size (inclunding border, padding, margin).
I always get immediate results.

Stefan Bachert
http://gwtworld.de

Chris Lercher

unread,
Jun 8, 2010, 1:39:52 PM6/8/10
to Google Web Toolkit
Hi Stefan,

not sure, if it's only the CSS, or the entire UiBinder - but I had
assumed, that it's only the CSS, because the div has the same width as
the body (which makes sense, if the CSS selector of the div doesn't
find its CSS class).

Oh, the code I posted is all there is for this little test project!
(The only thing that was lost somehow on copy&paste is the source code
line "RootPanel rootPanel = RootPanel.get();") The HTML file is just
the basic template from StockWatcher, with the unnecessary example
content removed from the body tag.

> I usally put elements in a certain environment to measure its real
> size (inclunding border, padding, margin).

Sounds interesting - how do I get that environment?

Cheers
Chris

Chris Lercher

unread,
Jun 8, 2010, 1:50:42 PM6/8/10
to Google Web Toolkit
[I see, that you replied to the first message in this thread - most of
the code is in the second message.]

The full thread is here:
http://groups.google.com/group/google-web-toolkit/browse_thread/thread/1c632fbbb37f6f66/54b2d8927811fe14
Reply all
Reply to author
Forward
0 new messages