Shoes: Element.width return 0

2019-08-15 08:19发布

问题:

I don't understand why the width function is implemented on all elements if it returns 0 for non-zero width elements. The following returns 0 for me.

Shoes.app do
  p = para "My width is: "
  para p.width
end

Why is that? (app.width does not return 0)

回答1:

The problem is that the size of the para object is determined dynamically when it is drawn. At the time you create the second para, nothing has actually been laid out yet, so a width hasn't been set. You can see that accessing the width after drawing works as expected:

Shoes.app do
  p = para "My width is: "
  @para = para p.width
  button 'Get Width' do
    @para.text = p.width
  end
end

The way to get around this is to use the start method, which is called when the containing slot is drawn for the first time:

Shoes.app do
  p = para "My width is: "
  width = para p.width
  start do
    width.text = p.width
  end
end


标签: ruby shoes