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

Buttons with no content don't lay out correctly #1513

Closed
jbphet opened this issue Jan 3, 2023 · 7 comments
Closed

Buttons with no content don't lay out correctly #1513

jbphet opened this issue Jan 3, 2023 · 7 comments
Assignees

Comments

@jbphet
Copy link
Contributor

jbphet commented Jan 3, 2023

Saw this in the sun demo just now while testing changes for phetsims/sun#292.

image

@jbphet jbphet self-assigned this Jan 3, 2023
@jbphet
Copy link
Contributor Author

jbphet commented Jan 3, 2023

I got some help from @marlitas on this, and we discovered that the preferred height and width are zero for these buttons. Here's a screen shot:

image

We also found that adding any content to the buttons makes them lay out correctly, though they seem to size themselves a little oddly. Here is a screenshot of a case where I added the option content: new Text( 'X' ) to each of the momentary buttons in demoMomentaryButtons.ts:

image

This led @marlitas and I to conclude that this is a general problem with WidthSizable, specifically its handling of the case where a button has no content. With this in mind, I'm going to move this issue to the scenery repo, change the name, and assign it to @marlitas and @jonathanolson.

@jbphet jbphet changed the title Some layouts in the button demo are broken Buttons with no content don't lay out correctly Jan 3, 2023
@jbphet jbphet transferred this issue from phetsims/sun Jan 3, 2023
@jbphet jbphet assigned marlitas and jonathanolson and unassigned jbphet Jan 3, 2023
@marlitas
Copy link
Contributor

marlitas commented Nov 9, 2023

I ran into a buttonConstraint layout issue in Number Line Integers as well. I don't think they're exactly related, but seem to be in the same area. In this case ButtonNodeConstraint is not setting the correct minimumHeight for a RoundButton if the radius option is not passed in by the client.

The below patch fixes the bug I encountered, but also feels like it might be potentially mis-using minUnstrokedHeight and minUnstrokedWidth.

Would like to discuss with @jonathanolson

Subject: [PATCH] Center text dynamically, see: https://github.com/phetsims/number-line-integers/issues/108
---
Index: js/buttons/RoundButton.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/js/buttons/RoundButton.ts b/js/buttons/RoundButton.ts
--- a/js/buttons/RoundButton.ts	(revision 26851caba268b49f4abbd8a50e4ce4372cc625b6)
+++ b/js/buttons/RoundButton.ts	(date 1699573275871)
@@ -78,14 +78,6 @@
       assert && assert( typeof options.radius === 'number', `invalid radius: ${options.radius}` );
     }
 
-    if ( options.radius ) {
-      assert && assert( options.xMargin < options.radius, 'xMargin cannot be larger than radius' );
-      assert && assert( options.yMargin < options.radius, 'yMargin cannot be larger than radius' );
-
-      options.minUnstrokedWidth = options.radius * 2;
-      options.minUnstrokedHeight = options.radius * 2;
-    }
-
     // If no options were explicitly passed in for the button appearance strategy, pass through the general appearance
     // options.
     if ( !options.buttonAppearanceStrategyOptions ) {
@@ -99,6 +91,18 @@
     const buttonRadius = options.radius ||
                          Math.max( options.content!.width + options.xMargin * 2, options.content!.height + options.yMargin * 2 ) / 2;
 
+    if ( options.radius ) {
+      assert && assert( options.xMargin < options.radius, 'xMargin cannot be larger than radius' );
+      assert && assert( options.yMargin < options.radius, 'yMargin cannot be larger than radius' );
+
+      options.minUnstrokedWidth = options.radius * 2;
+      options.minUnstrokedHeight = options.radius * 2;
+    }
+    else {
+      options.minUnstrokedWidth = buttonRadius * 2;
+      options.minUnstrokedHeight = buttonRadius * 2;
+    }
+
     if ( options.content && options.radius ) {
       const previousContent = options.content;
       const minScale = Math.min(

jonathanolson added a commit to phetsims/sun that referenced this issue Nov 10, 2023
@jonathanolson
Copy link
Contributor

Tracking progress in button-layout branch

jonathanolson added a commit to phetsims/solar-system-common that referenced this issue Jun 21, 2024
jonathanolson added a commit to phetsims/models-of-the-hydrogen-atom that referenced this issue Jun 21, 2024
jonathanolson added a commit to phetsims/states-of-matter that referenced this issue Jun 21, 2024
jonathanolson added a commit to phetsims/greenhouse-effect that referenced this issue Jun 21, 2024
jonathanolson added a commit to phetsims/models-of-the-hydrogen-atom that referenced this issue Jun 21, 2024
jonathanolson added a commit to phetsims/build-a-nucleus that referenced this issue Jun 21, 2024
@jonathanolson
Copy link
Contributor

@jbphet I believe this is resolved. Do you mind checking briefly?

@jbphet
Copy link
Contributor Author

jbphet commented Jun 26, 2024

Sun demo (where the problem was initially noticed) is lookin' good:

image

Also, the values shown by the Scenery helper seem more reasonable:

image

I also checked the case where text content is added to the buttons as a sort of a regression test, and it looked fine.

@jonathanolson - Hopefully this is what you meant by "checking briefly". Let me know if you need any additional review. I haven't review the code, just the behavior.

@jbphet jbphet assigned jonathanolson and unassigned jbphet Jun 26, 2024
@jonathanolson
Copy link
Contributor

Yup, thanks! Closing.

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

No branches or pull requests

3 participants