-
-
Notifications
You must be signed in to change notification settings - Fork 674
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
add a layout debug mode that sets background colors #2953
base: main
Are you sure you want to change the base?
Conversation
when TOGA_DEBUG_LAYOUT env var is 1, box background will be alternated
Throwing this up for initial feedback, will complete docs and tests once things are looking good. |
@@ -33,6 +54,20 @@ def __init__( | |||
:param style: A style object. If no style is provided, a default style | |||
will be applied to the widget. | |||
""" | |||
# if the object has _USE_DEBUG_BACKGROUND=True and layout debug mode is on, change bg color | |||
if hasattr(self, "_USE_DEBUG_BACKGROUND") and self._USE_DEBUG_BACKGROUND: |
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.
Hope I'm not butting in here, but a handy thing you may not be aware of: getattr()
can take an optional third parameter as a default to supply if the attribute is missing. Dictionaries have a roughly analogous get()
method, which returns None
or a user-supplied default if the key is missing.
So lines 58 and 59 could do the same thing and be written as:
if getattr(self, "_USE_DEBUG_BACKGROUND", False):
if environ.get("TOGA_DEBUG_LAYOUT") == "1":
@@ -0,0 +1 @@ | |||
Added a layout debugging mode that sets containers to random background colors. |
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 change note is the draft of the release note for the feature, rather than being a commit summary; so - it should be worded in terms of "exiting new feature you can now use!!1!!BBQ!":
Added a layout debugging mode that sets containers to random background colors. | |
Toga now has a layout debugging mode. If you set ``TOGA_DEBUG_LAYOUT=1`` in your app's runtime environment, widgets will be rendered with different background colors, making it easier to identify how space is being allocated by Toga's layout algorithm. |
if Widget._debug_color_index == len(debug_background_palette) - 1: | ||
Widget._debug_color_index = 0 | ||
else: | ||
Widget._debug_color_index += 1 | ||
style = style if style else Pack() | ||
style.background_color = debug_background_palette[ | ||
Widget._debug_color_index | ||
] |
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.
if Widget._debug_color_index == len(debug_background_palette) - 1: | |
Widget._debug_color_index = 0 | |
else: | |
Widget._debug_color_index += 1 | |
style = style if style else Pack() | |
style.background_color = debug_background_palette[ | |
Widget._debug_color_index | |
] | |
Widget._debug_color_index += 1 | |
style = style if style else Pack() | |
style.background_color = debug_background_palette[ | |
Widget._debug_color_index % len(debug_background_palette) | |
] |
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 don't think it would make a difference here, since one blank Pack should be the same as any other, but checking style is not None
would be more consistent with other things that will interact with future versions of Travertino...
Edit: to clarify, as of beeware/travertino#143, style objects with no properties set evaluate as falsey.
def test_button_debug_background(): | ||
"""A Button in layout debug mode has a default background.""" | ||
# Enable layout debug mode | ||
environ["TOGA_DEBUG_LAYOUT"] = "1" |
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 modifies the global environment, so it will leak into external tests (which means this test can impact on other tests). For something like this, it's a good idea to use monkeypatch.setenv
- that will automatically reset the environment variable when the test finishes.
|
||
# need enough for coverage of palette array index rollover | ||
box = toga.Box() | ||
toga.Box() |
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 i in range(0, 10):
...
|
||
|
||
# test that a non-container-like widget in layout debug mode has a default background | ||
def test_button_debug_background(): |
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.
Good idea testing a widget other than Box; - but also worth adding at test for (a) a container widget, and (b) a widget where the debug background won't be applied.
|
||
# assert that the bg is not default/white | ||
assert hasattr(box.style, "background_color") | ||
assert not box.style.background_color == rgb(0, 0, 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.
"Not white" can cover a multitude of sins :-)
A better test would be to evaluate the specific colors for all the widgets created in a specific order. The order of allocation should be predictable, once you've found the color of the first box.
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 know this is still marked as a draft, but I couldn't help myself providing some initial feedback. tl;dr - this is all looking very promising; with a couple of fairly minor tweaks, it should be pretty close to merge-ready.
Makes debugging Toga's layout system easier by visually indicating container boundaries. See #2856.
features:
usage:
PR Checklist: