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 ImageUtilities methods to help migrating away from "new ImageIcon" (SVG icon related) #8114

Merged
merged 2 commits into from
Jan 24, 2025

Conversation

eirikbakke
Copy link
Contributor

@eirikbakke eirikbakke commented Jan 5, 2025

Background: To ensure that SVG icons are loaded and drawn at full resolution, direct use of ImageIcon constructors should be avoided in the NetBeans codebase. The ImageIcon instances returned from methods in ImageUtilities, by contrast, are instances of a special subclass of ImageIcon that support vector graphics painting.

During work to remove uses of "new ImageIcon" constructors in the codebase (including #8109), I see that a few new utility methods would be useful in ImageUtilities. This PR proposes adding the following methods to ImageUtilities:

  • loadIcon(String,boolean) works like loadIconImage(String,boolean) but returns only a plain Icon (not IconImage). This would help discourage use of IconImage in the future.
  • loadIcon(String) is equivalent to loadIcon(String,false)
  • toImageIcon(Icon) helps cases where an existing API requires an ImageIcon to be returned, but where we only have an Icon.
  • loadImage(URL) helps migrate away from uses of Toolkit.getDefaultToolkit().createImage(), which has similar problems as "new ImageIcon".

This PR also contains, in a separate commit, migration away from the Toolkit.getDefaultToolkit().createImage() method, using the new loadImage(URL) method. This makes, for instance, the "lightbulb" icons in the editor gutter show up properly with their new SVG icons:

image

@eirikbakke eirikbakke added API Change [ci] enable extra API related tests Platform [ci] enable platform tests (platform/*) UI User Interface labels Jan 5, 2025
@eirikbakke
Copy link
Contributor Author

eirikbakke commented Jan 6, 2025

There is one more method I could add which might be useful, which is a mergeIcons(Icon,Icon,int,int) which works like mergeImages(Image,Image,int,int) but takes and returns Icon instances instead of Image. It could simplify some of the cases in #8109.

@eirikbakke eirikbakke force-pushed the pr-moreimageutils branch 3 times, most recently from 4ffa602 to 04ca39c Compare January 7, 2025 14:00
@eirikbakke
Copy link
Contributor Author

eirikbakke commented Jan 7, 2025

I pushed a revision to the two commits in this PR. Changes since your previous review:

  • Change the new loadImage(URL) to loadImage(URI). Update call sites accordingly in the second commit.
  • Clean up the Javadoc in ImageUtilities to avoid repeating the explanation of resource path loading semantics (substitution of dark mode suffixes and SVG images) for every similar method. Some other Javadoc edits for grammar, consistency etc.
  • Add the ImageUtilities.mergeIcons(Icon,Icon,int,int) method (like mergeImages(Image,Image,int,int) but takes and returns Icon instances instead of Image) as well, for future use.

@eirikbakke
Copy link
Contributor Author

(Latest push was just a rebase on master.)

Copy link
Member

@mbien mbien 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. Please check the comment I added.

@mbien mbien added this to the NB25 milestone Jan 15, 2025
@mbien
Copy link
Member

mbien commented Jan 23, 2025

don't forget to squash, looks like this could be 1-2 commits.

…ding methods used throughout the codebase easier. (Additive API change.)

Edit Javadoc to avoid repeating semantics that are common to many methods. Some other Javadoc cleanup.
…mageUtilities.loadImage(URI), to make SVG icons work in these cases.
@eirikbakke
Copy link
Contributor Author

Squashed so that two commits remain: The API change in isolation, and the call sites that make use of it.

@eirikbakke eirikbakke merged commit 3fa8212 into apache:master Jan 24, 2025
32 checks passed
eirikbakke added a commit to eirikbakke/incubator-netbeans that referenced this pull request Jan 25, 2025
This is a follow-up on apache#8114 and apache#8109 . To render at full HiDPI resolution, Icon/Image instances must be created via the methods in ImageUtilities rather than, in particular, the constructors of ImageIcon.

This PR, combined with the previously mentioned PRs, handles most of the remaining cases.

Specifically:
* Search for 'new ImageIcon(' and rewrite each case to use ImageUtilities to load icons instead.
* Search for 'instanceof ImageIcon' and generalize to 'instanceof Icon' when appropriate.
* Search for 'getLookAndFeel*getDisabledIcon' and switch to ImageUtilities.createDisabledIcon.
eirikbakke added a commit to eirikbakke/incubator-netbeans that referenced this pull request Jan 25, 2025
This is a follow-up on apache#8114 and apache#8109 . To render at full HiDPI resolution, Icon/Image instances must be created via the methods in ImageUtilities rather than, in particular, the constructors of ImageIcon.

This PR, combined with the previously mentioned PRs, handles most of the remaining cases.

Specifically:
* Search for 'new ImageIcon(' and rewrite each case to use ImageUtilities to load icons instead.
* Search for 'instanceof ImageIcon' and generalize to 'instanceof Icon' when appropriate.
* Search for 'getLookAndFeel*getDisabledIcon' and switch to ImageUtilities.createDisabledIcon.
@mbien
Copy link
Member

mbien commented Jan 25, 2025

i give it a 75% chance that someone will complain that the light bulbs are too large - but I think i like them after running RC1 for ~2 days :)

@eirikbakke
Copy link
Contributor Author

@mbien Yeah the old ones were scaled down a bit relative to the lightbulb-only icon. I might adjust it the next time I do a round of editing on these.

image

@mbien
Copy link
Member

mbien commented Jan 25, 2025

its fine - they are now using LEDs and were no longer available in the same size

eirikbakke added a commit to eirikbakke/incubator-netbeans that referenced this pull request Feb 24, 2025
This is a follow-up on apache#8114 and apache#8109 . To render at full HiDPI resolution, Icon/Image instances must be created via the methods in ImageUtilities rather than, in particular, the constructors of ImageIcon.

This PR, combined with the previously mentioned PRs, handles most of the remaining cases.

Specifically:
* Search for 'new ImageIcon(' and rewrite each case to use ImageUtilities to load icons instead.
* Search for 'instanceof ImageIcon' and generalize to 'instanceof Icon' when appropriate.
* Search for 'getLookAndFeel*getDisabledIcon' and switch to ImageUtilities.createDisabledIcon.
eirikbakke added a commit that referenced this pull request Feb 24, 2025
This is a follow-up on #8114 and #8109 . To render at full HiDPI resolution, Icon/Image instances must be created via the methods in ImageUtilities rather than, in particular, the constructors of ImageIcon.

This PR, combined with the previously mentioned PRs, handles most of the remaining cases.

Specifically:
* Search for 'new ImageIcon(' and rewrite each case to use ImageUtilities to load icons instead.
* Search for 'instanceof ImageIcon' and generalize to 'instanceof Icon' when appropriate.
* Search for 'getLookAndFeel*getDisabledIcon' and switch to ImageUtilities.createDisabledIcon.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Change [ci] enable extra API related tests Code cleanup Platform [ci] enable platform tests (platform/*) UI User Interface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The icon for running a test method is not displayed correctly in the dark editor theme
3 participants