shoes4 icon indicating copy to clipboard operation
shoes4 copied to clipboard

Buttons can't be centered

Open charlesetc opened this issue 10 years ago • 3 comments

Hello! I was working some buttons and noticed that this: https://gist.github.com/Charlesetc/9c0b29e30ab1564133a4 Looks like this: screen shot 2014-08-20 at 8 04 32 am

It seems like buttons aren't supposed to be centerable, according to the Shoes 3 documentation: screen shot 2014-08-20 at 8 07 10 am

Is this an oversight? I think centering buttons would be very helpful.

charlesetc avatar Aug 20 '14 06:08 charlesetc

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 :)

PragTob avatar Aug 20 '14 19:08 PragTob

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

tremendo avatar Sep 03 '14 07:09 tremendo

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 !

PragTob avatar Sep 03 '14 10:09 PragTob