-
-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "horizontal" and "vertical" aliases for align_items and justify_c…
…ontent (#3113) * Add "horizontal" and "vertical" aliases for align_items and justify_content * Work around Travertino _ALL_PROPERTIES version differences * Improve wording Co-authored-by: Russell Keith-Magee <[email protected]> --------- Co-authored-by: Russell Keith-Magee <[email protected]>
- Loading branch information
1 parent
b1926ee
commit 1bc7002
Showing
8 changed files
with
269 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The ``align_items`` and ``justify_content`` properties now have the aliases ``horizontal_align_items``, ``vertical_align_items``, ``horizontal_align_content`` and ``vertical_align_content`` that explicitly describe layout behavior in the named direction. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from toga.style.pack import Pack | ||
|
||
|
||
def with_init(**kwargs): | ||
return Pack(**kwargs) | ||
|
||
|
||
def with_update(**kwargs): | ||
style = Pack() | ||
style.update(**kwargs) | ||
return style | ||
|
||
|
||
def with_setattr(**kwargs): | ||
style = Pack() | ||
for name, value in kwargs.items(): | ||
setattr(style, name, value) | ||
return style | ||
|
||
|
||
def with_setitem(**kwargs): | ||
style = Pack() | ||
for name, value in kwargs.items(): | ||
style[name] = value | ||
return style | ||
|
||
|
||
def with_setitem_hyphen(**kwargs): | ||
style = Pack() | ||
for name, value in kwargs.items(): | ||
style[name.replace("_", "-")] = value | ||
return style | ||
|
||
|
||
def getitem(obj, name): | ||
return obj[name] | ||
|
||
|
||
def getitem_hyphen(obj, name): | ||
return obj[name.replace("_", "-")] | ||
|
||
|
||
def delitem(obj, name): | ||
del obj[name] | ||
|
||
|
||
def delitem_hyphen(obj, name): | ||
del obj[name.replace("_", "-")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import pytest | ||
from pytest import raises | ||
|
||
from toga.style.pack import CENTER, COLUMN, END, ROW | ||
|
||
from . import ( | ||
delitem, | ||
delitem_hyphen, | ||
getitem, | ||
getitem_hyphen, | ||
with_init, | ||
with_setattr, | ||
with_setitem, | ||
with_setitem_hyphen, | ||
with_update, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"css_name, row_alias, column_alias, default", | ||
[ | ||
( | ||
"align_items", | ||
"vertical_align_items", | ||
"horizontal_align_items", | ||
None, | ||
), | ||
( | ||
"justify_content", | ||
"horizontal_align_content", | ||
"vertical_align_content", | ||
"start", | ||
), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"style_with", | ||
(with_init, with_update, with_setattr, with_setitem, with_setitem_hyphen), | ||
) | ||
@pytest.mark.parametrize("get_fn", (getattr, getitem, getitem_hyphen)) | ||
@pytest.mark.parametrize("del_fn", (delattr, delitem, delitem_hyphen)) | ||
def test_align(css_name, row_alias, column_alias, default, style_with, get_fn, del_fn): | ||
"""The `vertical_align` and `horizontal_align` aliases work correctly.""" | ||
# Row alias | ||
style = style_with(**{row_alias: CENTER}) | ||
assert get_fn(style, css_name) == CENTER | ||
|
||
del_fn(style, row_alias) | ||
assert get_fn(style, css_name) == default | ||
|
||
style = style_with(**{css_name: CENTER}) | ||
assert get_fn(style, row_alias) == CENTER | ||
|
||
del_fn(style, css_name) | ||
assert get_fn(style, row_alias) == default | ||
|
||
# Column alias | ||
style = style_with(**{"direction": COLUMN, column_alias: CENTER}) | ||
assert get_fn(style, css_name) == CENTER | ||
|
||
del_fn(style, column_alias) | ||
assert get_fn(style, css_name) == default | ||
|
||
style = style_with(**{"direction": COLUMN, css_name: CENTER}) | ||
assert get_fn(style, column_alias) == CENTER | ||
|
||
del_fn(style, css_name) | ||
assert get_fn(style, column_alias) == default | ||
|
||
# Column alias is not accepted in a row, and vice versa. | ||
def assert_invalid_alias(alias, direction): | ||
style = style_with(direction=direction) | ||
raises_kwargs = dict( | ||
expected_exception=AttributeError, | ||
match=f"'{alias}' is not supported on a {direction}", | ||
) | ||
|
||
with raises(**raises_kwargs): | ||
get_fn(style, alias) | ||
with raises(**raises_kwargs): | ||
setattr(style, alias, END) | ||
with raises(**raises_kwargs): | ||
del_fn(style, alias) | ||
with raises(**raises_kwargs): | ||
style.update(**{"direction": direction, alias: END}) | ||
with raises(**raises_kwargs): | ||
style.update(**{alias: END, "direction": direction}) | ||
|
||
assert_invalid_alias(column_alias, ROW) | ||
assert_invalid_alias(row_alias, COLUMN) | ||
|
||
# Consistent values of direction and alias can be updated together, regardless of | ||
# argument order. | ||
style = style_with(direction=COLUMN) | ||
style.update(**{"direction": ROW, row_alias: CENTER}) | ||
assert get_fn(style, row_alias) == CENTER | ||
assert get_fn(style, css_name) == CENTER | ||
style.update(**{column_alias: END, "direction": COLUMN}) | ||
assert get_fn(style, column_alias) == END | ||
assert get_fn(style, css_name) == END | ||
|
||
style = style_with(direction=ROW) | ||
style.update(**{"direction": COLUMN, column_alias: CENTER}) | ||
assert get_fn(style, column_alias) == CENTER | ||
assert get_fn(style, css_name) == CENTER | ||
style.update(**{row_alias: END, "direction": ROW}) | ||
assert get_fn(style, row_alias) == END | ||
assert get_fn(style, css_name) == END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.