Skip to content
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

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

aerickson
Copy link

@aerickson aerickson commented Nov 15, 2024

Makes debugging Toga's layout system easier by visually indicating container boundaries. See #2856.

features:

  • randomized background color (using colors optimized for color-blindness) for boxes
  • enabled via TOGA_DEBUG_LAYOUT env var

usage:

TOGA_DEBUG_LAYOUT=1 briefcase dev

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

@aerickson
Copy link
Author

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:
Copy link
Contributor

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.
Copy link
Member

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!":

Suggested change
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.

Comment on lines +60 to +67
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
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
]

Copy link
Contributor

@HalfWhitt HalfWhitt Nov 15, 2024

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"
Copy link
Member

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()
Copy link
Member

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():
Copy link
Member

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)
Copy link
Member

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.

Copy link
Member

@freakboy3742 freakboy3742 left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants