-
-
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
Updated Pack property names to match CSS #3033
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a whole lot of changes, but it's mostly a mechanical search and replace.
A couple of minor typos in docs flagged inline.
The only issue of substance is the set of allowed values for align-items
. If we're going to the effort to ensure that we're using a "true" CSS subset, it seems like we should also be correcting the inconsistency in the allowed values. This would have the added benefit of removing the ambiguity around the interpretation with direction
.
core/src/toga/style/pack.py
Outdated
@@ -56,12 +60,12 @@ | |||
DISPLAY_CHOICES = Choices(PACK, NONE) | |||
VISIBILITY_CHOICES = Choices(VISIBLE, HIDDEN) | |||
DIRECTION_CHOICES = Choices(ROW, COLUMN) | |||
ALIGNMENT_CHOICES = Choices(LEFT, RIGHT, TOP, BOTTOM, CENTER) | |||
ALIGN_ITEMS_CHOICES = Choices(LEFT, RIGHT, TOP, BOTTOM, CENTER) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth noting that these aren't valid values for CSS3-flexbox align_items
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we implement this by simply making left
and top
aliases for start
, and right
and bottom
aliases for end
? This would be a change in the current documented behavior:
If a value is provided, but the value isn’t honored, the alignment reverts to the default for the direction.
But any code that's setting invalid alignment options is broken anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an edge case if the direction is RTL as well (left
and start
aren't analogs if direction=RTL
) - but I think I could live with both of those inconsistencies as long as using the old names raises an appropriate warning on first use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't had time to get to it yet, but for maximum compatibility, what I envisioned was only storing whichever version was set last — alignment
with directions, or align_items
with start
/center
/end
. Then if the same one is requested, return that, but if the other is requested, compute it based on the set value and the current row/column direction and RTL/LTR direction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's possible to make align_items
the single source of truth, while keeping full compatibility with the current alignment
behavior:
-
In the
alignment
setter, check the currentdirection
andtext_direction
, and transformtop
,bottom
,left
orright
tostart
orend
according to the current behavior. In particular, this means that settingalignment="bottom"
whendirection="column"
will result inalign_items="start"
, which is "the default for the direction". -
In the
alignment
getter, do the reverse transformation, which is simpler.
As discussed in #1194, there are some bugs in the layout algorithm. For example, the actual default alignment is sometimes start
and sometimes what CSS calls stretch
, and center
doesn't always work. I'm working on fixing this in another branch which I'll stack on top of this one, so please don't make any structural changes to the layout algorithm beyond renaming the attributes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
alignment
setter, check the currentdirection
andtext_direction
I guess this also means that when setting multiple properties at once, either via the constructor or via update
, we must make sure to set direction
and text_direction
before alignment
. Code that sets the properties individually may experience a change in behavior, but I think that should be rare enough that we can accept it, especially since we're planning a version number bump to 0.5.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But what if existing code sets alignment
and then changes direction
or text_direction
? This is why I think it's safer to just preserve the existing behavior for the old name. And if align_items
is requested while alignment
is currently an invalid value for the current direction
/text_direction
, then align_items
can read as not having been set.
As discussed in #1194, there are some bugs in the layout algorithm. For example, the actual default alignment is sometimes
start
and sometimes what CSS callsstretch
, andcenter
doesn't always work. I'm working on fixing this in another branch which I'll stack on top of this one, so please don't make any structural changes to the layout algorithm beyond renaming the attributes.
Thanks for the heads up! I was looking at potentially merging the nearly-identical row and column layout methods, either here or in a second PR, but will avoid mucking about with any of that for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for maximum compatibility, what I envisioned was only storing whichever version was set last. [...] Then if the same one is requested, return that, but if the other is requested, compute it
It would be more complex that way, but I guess it's the only way to achieve 100% backward compatibility. Either approach is OK with me.
If we're going to all this trouble, we might as well match the CSS name of |
The Pack property is already named That said, I do realize that's an inconsistency, and I'm not against switching it. |
I agree the grammar is questionable, but I think that's outweighed by the advantage of using the CSS name everywhere. It would be one less thing we need to remember, and it might be useful for some form of automation in the future. |
docs/reference/style/pack.rst
Outdated
The amount of space to allocate between the edge of the box, and the edge of the content | ||
in the box, in :ref:`CSS pixels <css-units>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current wording describes interior padding, which we have never supported, regardless of what the property was called.
The amount of space to allocate between the edge of the box, and the edge of the content | |
in the box, in :ref:`CSS pixels <css-units>`. | |
The amount of space to allocate outside the edge of the widget, in :ref:`CSS pixels | |
<css-units>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only thing I amended here is keeping "box" instead of "widget", to be consistent with how the page discusses geometry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align_items
now coexists with alignment
. It's a pretty convoluted backwards-compatibility section, but it does seem to work as expected.
I've also made the requested tweaks to the docs, rewritten the part about align_items
, and applied a consistent text_align
name everywhere.
@@ -867,33 +996,19 @@ def __css__(self) -> str: | |||
if self.height != NONE: | |||
css.append(f"height: {self.height}px;") | |||
|
|||
# alignment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This CSS had its main and cross axes reversed, but it wasn't being caught because the tests were also flipped. Should be a moot point now.
@@ -22,7 +22,7 @@ def toga_color(color_int): | |||
) | |||
|
|||
|
|||
def toga_alignment(gravity, justification_mode=None): | |||
def to_toga_text_align(gravity, justification_mode=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While changing all instances of text_alignment
to text_align
, I've also added to_
to the beginning of converters like this, to help avoid the ambiguity of "align" being a verb.
@mhsmith pointed out in #3011 that the
padding
(et al) andalignment
Pack properties would be better namedmargin
andalign_items
, to match their corresponding CSS properties. I've renamed them throughout the codebase, and set up an aliasing system for backwards compatibility (with tests). I've also taken the liberty of clarifying the name of text alignment whenever I encountered it in my searching-and-replacing.PR Checklist: