Ticket #24677: more-tightly-scoped.diff
| File more-tightly-scoped.diff, 3.9 KB (added by , 6 days ago) |
|---|
-
src/org/openstreetmap/josm/gui/tagging/presets/TaggingPreset.java
21 21 import java.util.Objects; 22 22 import java.util.Set; 23 23 import java.util.concurrent.CompletableFuture; 24 import java.util.concurrent.Executor; 24 25 import java.util.function.Predicate; 25 26 import java.util.stream.Collectors; 26 27 … … 277 278 File arch = TaggingPresetReader.getZipIcons(); 278 279 final Collection<String> s = TaggingPresets.ICON_SOURCES.get(); 279 280 this.iconFuture = new CompletableFuture<>(); 280 new ImageProvider(iconName)281 final ImageProvider provider = new ImageProvider(iconName) 281 282 .setDirs(s) 282 283 .setId("presets") 283 284 .setArchive(arch) 284 .setOptional(true) 285 .getResourceAsync(result -> { 285 .setOptional(true); 286 final Executor fetcher = ImageProvider.getImageFetchExecutor(); 287 // Explicitly dispatch to the image fetch executor so that even local JAR-bundled icons 288 // are loaded asynchronously, keeping expensive SVG pre-rendering off the startup thread. 289 CompletableFuture.supplyAsync(provider::getResource, fetcher) 290 .thenAcceptAsync(result -> { 286 291 if (result != null) { 287 292 // Pre-render off EDT to avoid flooding the event queue with expensive SVG rendering 288 293 ImageIcon small = result.getImageIcon(ImageProvider.ImageSizes.SMALLICON.getImageDimension()); … … 303 308 Logging.warn(toString() + ": " + PRESET_ICON_ERROR_MSG_PREFIX + iconName); 304 309 iconFuture.complete(null); 305 310 } 306 } );311 }, fetcher); 307 312 } 308 313 309 314 /** -
src/org/openstreetmap/josm/tools/ImageProvider.java
44 44 import java.util.Objects; 45 45 import java.util.concurrent.CompletableFuture; 46 46 import java.util.concurrent.ConcurrentHashMap; 47 import java.util.concurrent.Executor; 47 48 import java.util.concurrent.ExecutorService; 48 49 import java.util.concurrent.Executors; 49 50 import java.util.function.Consumer; … … 717 718 /** 718 719 * Load the image in a background thread. 719 720 * <p> 720 * This method returns immediately and runs the image request asynchronously. 721 * This method returns immediately and runs the image request asynchronously for remote resources. 722 * For local resources, the request is executed synchronously in the current thread. 721 723 * @param action the action that will deal with the image 722 724 * 723 725 * @return the future of the requested image … … 724 726 * @since 13252 725 727 */ 726 728 public CompletableFuture<Void> getResourceAsync(Consumer<? super ImageResource> action) { 727 return CompletableFuture.supplyAsync(this::getResource, IMAGE_FETCHER).thenAcceptAsync(action, IMAGE_FETCHER); 729 return isRemote() 730 ? CompletableFuture.supplyAsync(this::getResource, IMAGE_FETCHER).thenAcceptAsync(action, IMAGE_FETCHER) 731 : CompletableFuture.completedFuture(getResource()).thenAccept(action); 728 732 } 729 733 730 734 /** 735 * Returns the executor used for background image fetching. 736 * Callers that need to force asynchronous loading even for local resources 737 * (e.g. to off-load expensive SVG pre-rendering) may use this executor directly. 738 * @return the image fetch executor 739 */ 740 public static Executor getImageFetchExecutor() { 741 return IMAGE_FETCHER; 742 } 743 744 /** 731 745 * Load an image with a given file name. 732 746 * 733 747 * @param subdir subdirectory the image lies in
