shoes4
shoes4 copied to clipboard
Buttons can't be centered
Hello!
I was working some buttons and noticed that this:
https://gist.github.com/Charlesetc/9c0b29e30ab1564133a4
Looks like this:
It seems like buttons aren't supposed to be centerable, according to the Shoes 3 documentation:
Is this an oversight? I think centering buttons would be very helpful.
Thank you for bringing this to our attention!
It seems shoes3 only supports centering for all the text styles. You are right however, that a more general centering mechanism might be nice. I put it on milestone 4.1.0 for future features but like this one very much myself so it might get in before that :)
Implementation wise/idea/hint for contributors:
This could be implemented as a general style for the Dimension
class. If available it could calculate an appropriate left value based on the own width and the parent width. Shouldn't be difficult :)
I was looking into this trying to see how to implement within Dimension
as suggested by @PragTob
From what I can see, the only way for the Dimension
class to know the width is for it to be explicitly passed as part of the style hash. That is, to "manually" set it, which doesn't happen in the gist by @Charlesetc
Button
itself can't know its own width until after @gui = Shoes.configuration.backend_for(self, @parent.gui)
and this can't/doesn't happen until after the call to Dimensions
. Otherwise the alignment of the button can be done within class Button
with something like the following:
@alignment = styles.fetch(:align, 'left')
case @alignment
when 'center'
@dimensions.left = (@parent.width - self.width) / 2
when 'right'
@dimensions.left = @parent.width - self.width
else
# default is 'left'
end
Surely I'm missing some obvious way to accomplish this within Dimension
, my excuse is that I'm new ;b
You are not missing something :-)
What you are writing is right, the width is set later by the GUI backend. E.g. in this method. To set the size we already need dimensions, so it's kind of a hen and egg problem. So yes Dimension is initialized first, and afterwards the GUI backend sets the appropriate size, if no size was send. Then it will probably need a guard to only do alignment calculations if width is set.
But it still happens. So this code could be included in the code that determines the left value... which would mean start
in the Dimension class. It needs to be calculated dynamically either way as the size of the parent container might change, or at least I think so :-)
Maybe there is another way to hook Dimensions and size setting from the GUI together but I don't see it at the moment :-(
Thanks for the work @tremendo !