[release/3.6] 修复无法加载 WebP 图标的问题#4436
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes WebP image loading issues in HMCL by refactoring the remote image loading system to support WebP format properly, addressing the problem described in issue #4171.
- Adds WebP image loading support with scaling capabilities
- Refactors remote image loading to use asynchronous tasks instead of direct Image constructor
- Updates UI components to use property binding for remote images
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| SwingFXUtils.java | Adds new method for scaling BufferedImage to WritableImage with aspect ratio preservation |
| FXUtils.java | Major refactor of remote image loading system with WebP support and async task-based approach |
| DownloadPage.java | Updates image loading calls to use new property binding method |
| DownloadListPage.java | Updates image loading calls to use new property binding method |
| HTMLRenderer.java | Refactors image loading to use synchronous task execution with improved error handling |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if (requestedWidth <= 0. || requestedHeight <= 0.) { | ||
| return toFXImage(bimg, null); | ||
| } | ||
|
|
||
| int width = (int) requestedWidth; | ||
| int height = (int) requestedHeight; | ||
|
|
There was a problem hiding this comment.
Use integer comparison instead of floating-point comparison for dimensions. Since width and height are cast to int later, compare against 0 directly: if (requestedWidth <= 0 || requestedHeight <= 0)
| if (requestedWidth <= 0. || requestedHeight <= 0.) { | |
| return toFXImage(bimg, null); | |
| } | |
| int width = (int) requestedWidth; | |
| int height = (int) requestedHeight; | |
| int width = (int) requestedWidth; | |
| int height = (int) requestedHeight; | |
| if (width <= 0 || height <= 0) { | |
| return toFXImage(bimg, null); | |
| } |
| @@ -851,15 +907,37 @@ public static Image newBuiltinImage(String url, double requestedWidth, double re | |||
| } | |||
| } | |||
|
|
|||
There was a problem hiding this comment.
This public method lacks JavaDoc documentation. It should include documentation explaining the parameters, return value, and behavior, especially since it's part of a utility class API.
| /** | |
| * Downloads an image from a remote URL asynchronously, caches it locally, and returns a {@link Task} that loads the image | |
| * with the specified parameters. If the image has already been downloaded and cached, it will be loaded from the cache. | |
| * If the cached file is unavailable or unreadable, it will be re-downloaded. | |
| * | |
| * @param url the URL of the remote image to download. | |
| * @param requestedWidth the bounding box width for the loaded image. | |
| * @param requestedHeight the bounding box height for the loaded image. | |
| * @param preserveRatio whether to preserve the aspect ratio of the original image when scaling. | |
| * @param smooth whether to use a better quality filtering algorithm or a faster one when scaling the image. | |
| * @return a {@link Task} that, when executed, provides the loaded {@link Image}. | |
| */ |
| Image image = FXUtils.getRemoteImageTask(src, width, height, true, true) | ||
| .run(); | ||
| if (image == null) | ||
| throw new AssertionError("Image loading task returned null"); |
There was a problem hiding this comment.
Calling .run() on a Task executes it synchronously on the current thread, which can block the UI thread if this is called from JavaFX Application Thread. Consider using async execution or documenting that this method should not be called from the UI thread.
#4171