-
-
Notifications
You must be signed in to change notification settings - Fork 685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve alignment options #1194
Comments
Closed on the basis that the question has been answered. [Edit by @mhsmith: I think there used to be another comment above with an attempted answer, but it must have been deleted.] |
Does this method o centering a button still work. I have tried to center a button using the tip above but it defaults to left.:
|
Simplified and self-contained version of the code above: import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
class HelloWorld(toga.App):
def startup(self):
main_box = toga.Box(style=Pack(direction=COLUMN))
name_label = toga.Label('Your name: ')
self.name_input = toga.TextInput(style=Pack(flex=1))
name_box = toga.Box(style=Pack(direction=ROW))
name_box.add(name_label)
name_box.add(self.name_input)
button = toga.Button('Say Hello!', style=Pack(font_size=15))
btn_box = toga.Box(style=Pack(direction=ROW))
btn_box.add(button)
main_box.add(name_box)
main_box.add(btn_box)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
def main():
return HelloWorld() With the current development version of Toga, it looks like this on Windows: If I change the button to be a child of the top-level COLUMN box instead: button = toga.Button('Say Hello!', style=Pack(font_size=15))
main_box.add(name_box)
main_box.add(button) Then it expands to fill the available width: The only way I found to center it without filling the width is to use a ROW box with some dummy views on each side:
I'm not sure if this is a limitation of the layout algorithm itself or a bug in its implementation on Windows. @freakboy3742: any thoughts? |
@mhsmith The behavior you're seeing is consistent with macOS, so it's not Windows-specific. I believe this is behaving as designed/documented; the key to that interpretation is the second paragraph of point 3 from the Pack specification. Parent elements have a final width set to 0, even if they have available width. That said:
|
Probably a new feature, you can just use |
I'm going to reopen this issue and merge from #1544 so the discussion is all in one place. |
Originally posted by @mhsmith in #1544 The Pack layout algorithm already supports using Russell suggested that a more general approach to this area would be to formally define the mapping between the Pack layout properties and the equivalent CSS. If that was done, there would no longer be any need for a separate Pack algorithm specification. It would also act as a step towards our long-term goal of supporting CSS directly. |
Originally posted by @freakboy3742 in #1544 (comment) To be clear - while we might be able to repurpose Convergence on a subset of CSS is definitely the preferred solution, though; the current formal specification of the Pack algorithm would be much better as a specification of the subset of CSS flags we honour, and the conditions under which we honour them. The Pack specification should be mostly this at present (it should mostly map to CSS3 flexbox, with the removal of any line splitting rules) - but there are clearly areas that we are missing, because horizontal centering can be handled by flexbox. |
Originally posted by @shape-warrior-t in #1544 (comment) My understanding is that currently, the final width of a row box with children is always the sum of its children's full widths, and same thing with the column boxes in the other dimension. In other words, right now, every row box is always left, center, and right aligned already, and same with the column boxes and vertical alignment. So in order to implement this, we'll probably have to change how we determine the final width of a parent element. A change that may or may not be backwards-compatible, depending on how we go about it. And if we change how the final width of a parent element is determined, we might also want to do the same thing with the final height. Right now, as far as I can tell, the final height of a row box is always the maximum of its children's full heights, and same with column boxes and width. I don't think the behaviour described in #1194 (comment) is actually a bug under current specifications -- if you have a row box with one child, then the final height of the row box will always be the same as the child's full height, so the row box will always already be top, center, and bottom aligned. (I just noticed -- the documented Pack algorithm doesn't actually say what the final height of a parent row box / final width of a parent column box is! So... maybe it actually is a bug...?) My brain currently needs a break from thinking, so I'm not going to propose anything right now. Maybe later. Just wanted to bring this issue up, because I think it's probably important. |
Originally posted by @shape-warrior-t in #1544 (comment) OK, one potential idea: CSS uses |
Originally posted by @freakboy3742 in #1544 (comment) As I said in my previous comment, my preferred approach here is not to introduce new properties, or custom interpretations - but to converge on a true "minimal CSS" interpretation - that is, a set of rules that are 100% consistent with true CSS, or have a 1-1 mapping with true CSS. Essentially, I would like to be able to answer any "is this a bug in Pack?" questions with "what does a HTML render of the same code do?". |
Having spent some time with Quality Time (tm) with Pack this week, I can add 2 things to this discussion:
On that basis, I think the issue isn't actually with Pack here. The box being allocated is the right size; the issue is what Button does within that box. We don't currently have a way to stylistically tell Button that it doesn't have a flexible intrinsic width. I'm not sure there's a good analog for this in CSS; this is almost a "lower level than CSS" problem. The same core issue exists with Label. However, in that context, a label that is the width of the screen is mostly rendered as the background color, rather than "button body"; text-alignment gets you the rest of the way, so the problem isn't as visually apparent. Four possible fixes I can see:
I could almost be convinced that (2) is the right answer. The current behaviour is likely a consequence of the "desirable" behaviour in Toga Tutorial 0 and 1, rather than a decision that is rooted in adherence to platform UX style guides. However - changing this would obviously be a huge backwards incompatibility. It would also mean there isn't a way to define an "as wide as the window" button; which some users may want, despite UX guidelines suggesting buttons shouldn't be used like that. |
Based on @mhsmith 's box hack, I have extended it to implement all the values of Works great but more |
Yes, I think increasing the number of supported CSS attributes is the way to go here, and #1778 has already made a start at defining Pack in terms of CSS. And based on that definition, the current behavior is almost correct:
Where I think we diverge from CSS is the way we implement |
If you take the example where There will be analogous situations with other widgets (e.g., ActivitySpinner) that have a base fixed size, and don't have any meaningful "stretch" behavior. The handling of
I think the discrepancy here has more to do with the An idle thought - what we need here is a Hypothesis test harness that can run comparisons of randomly generated HTML markups with a specific CSS mapping scheme and compare Pack's layouts with native CSS rendering... |
Do you mean this is what the CSS mapping would do, or what Pack currently does? I tried both of the following structures with Pack on Cocoa, and neither of them stretched the button vertically:
One possible fix for the CSS mapping would be that if |
I've accidentally discovered another way of centering a button: def startup(self):
main_box = toga.Box(style=Pack(direction=COLUMN, alignment="center"))
name_box = toga.Box(style=Pack(direction=ROW))
button = toga.Button('Say Hello!', style=Pack(font_size=15))
btn_box = toga.Box(style=Pack(direction=ROW))
btn_box.add(button)
main_box.add(name_box)
main_box.add(btn_box)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show() But if I comment out I guess this is a variation of what was mentioned in #1778: |
I was referring to what a CSS rendering generates for a div in an equivalent layout, which is what the reference rendering would become if we adopted this. The current Pack implementation sets a fixed intrinsic height on the Button, which then constraints the height of the box that contains it.
I think I agree in spirit, but not quite in implementation. In terms of Pack, |
Care to share? |
I don't find anyway to make a button widget in the center of a window/mainwindow
The text was updated successfully, but these errors were encountered: