Skip to content

Allow label to be scaled #42

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

Merged
merged 4 commits into from
Dec 14, 2023
Merged

Conversation

lcmcninch
Copy link

Problem

When creating a Button, there isn't a way to set the scale for the label font, so the label for bigger buttons can be tiny. See #41.

Changes

  • __init__ for Button and ButtonBase now take an optional label_scale parameter, which ultimately defaults to 1
  • In the setter for label, when the Label is instantiated, it is passed the label scale. Then, to ensure the label is centered, the bounding box is modified by the scale factor as necessary.

Testing

I don't have a ton of experience with this library or its dependencies, so I admittedly didn't do much testing. My application uses some buttons with the font scaled to 2 and 4 and everything seems to work okay. I'm open to doing more testing if there are suggestions.

Notes

  • I'm not sure if it's a bug or intentional that the Label's bounding box isn't scaled, and I considered submitting a pull request in that repository to add a scaled_bounding_box property, but I figured the way the dependencies are for these libraries, it would be best for this change to be compatible with the current and previous Adafruit_CircuitPython_Display_Text versions.
  • As I'm writing this realizing that it is possible to create a small button then scale the whole button up and the text scales with it. That might be an okay work-around but I'm not sure it's intuitive so I still think this change is worthwhile.

Copy link
Author

@lcmcninch lcmcninch left a comment

Choose a reason for hiding this comment

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

I made a few inline code comments

self._label = Label(self._label_font, text=newtext, scale=self._label_scale)
dims = list(self._label.bounding_box)
dims[2] *= self._label.scale
dims[3] *= self._label.scale
Copy link
Author

Choose a reason for hiding this comment

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

I'm not in love with having to do this with the bounding box dimensions. As I mentioned in the description, I'm not sure if it was intentional or not that the bounding box for Label isn't scaled, but since that's a separate repository, I thought I'd just work with what we have. I'm open to suggestions here.

Choose a reason for hiding this comment

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

I do think the approach you've gone for here is fine. It may be worth considering changing in Label in the long term, but there could be reasons in there why it's like this, I don't know for certain.

@@ -72,6 +73,7 @@ def __init__(
self._label_color = label_color
self._label_font = label_font
self._selected_label = _check_color(selected_label)
self._label_scale = label_scale or 1
Copy link
Author

Choose a reason for hiding this comment

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

This is passed in to Label which wouldn't like a None value for scale, so this defaults the value to 1. I prefer for optional parameters to default to None, that way callers can pass in a None and get default behavior. But if it's desirable to set the default on the parameter to 1 as Label does it, I can change it.

@lcmcninch lcmcninch marked this pull request as ready for review December 3, 2023 20:06
Copy link

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

@lcmcninch Thanks for adding this functionality!

I think it's best if we can keep the different buttons having as similar of arguments as possible. Can you add the new label_scale argument over here inside of SpriteButton as well: https://github.com/adafruit/Adafruit_CircuitPython_Display_Button/blob/main/adafruit_button/sprite_button.py#L64 that way if folks swap from one type of button to the other this part of the functionality can remain consistent.

@lcmcninch
Copy link
Author

lcmcninch commented Dec 12, 2023

Can you add the new label_scale argument over here inside of SpriteButton as well:

Absolutely, that's a good call, I'll make the change.

@lcmcninch
Copy link
Author

lcmcninch commented Dec 12, 2023

@FoamyGuy I added the label_scaled argument to SpriteButton and also fixed a bug I caught in testing it that I'd missed when testing the original changes. Should be ready for another review.

@lcmcninch lcmcninch requested a review from FoamyGuy December 12, 2023 02:10
Copy link

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks @lcmcninch!

I tested this successfully on a PyPortal 9.0.0alpha.2 with the simpletest and spritebutton simpletest slightly modified to use the new parameter.

Really nice functionality to have especially with lots of larger resolution displays gaining support.

self._label = Label(self._label_font, text=newtext, scale=self._label_scale)
dims = list(self._label.bounding_box)
dims[2] *= self._label.scale
dims[3] *= self._label.scale

Choose a reason for hiding this comment

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

I do think the approach you've gone for here is fine. It may be worth considering changing in Label in the long term, but there could be reasons in there why it's like this, I don't know for certain.

@FoamyGuy FoamyGuy merged commit 3572dba into adafruit:main Dec 14, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Dec 14, 2023
@lcmcninch lcmcninch mentioned this pull request Dec 15, 2023
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.

2 participants