Skip to content

Commit

Permalink
Merge pull request #380 from devoxx/feat-379
Browse files Browse the repository at this point in the history
Cache icons to improve performance
  • Loading branch information
stephanj authored Dec 12, 2024
2 parents 2fd33fb + 1d07030 commit 4737039
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/main/java/com/devoxx/genie/ui/util/FileTypeIconUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devoxx.genie.ui.util;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.concurrency.AppExecutorUtil;
Expand All @@ -17,22 +18,29 @@

public class FileTypeIconUtil {

private static final Logger LOG = Logger.getInstance(FileTypeIconUtil.class);

private FileTypeIconUtil() {
}

public static Icon getFileTypeIcon(VirtualFile virtualFile) {
Future<Icon> iconFuture = AppExecutorUtil.getAppExecutorService().submit(() ->
ApplicationManager.getApplication().runReadAction((Computable<Icon>) () -> {
Icon interfaceIcon = getIcon(virtualFile);
if (interfaceIcon != null) return interfaceIcon;
return virtualFile.getFileType().getName().equals("UNKNOWN") ? CodeSnippetIcon : ClassIcon;
}));
ApplicationManager.getApplication().runReadAction((Computable<Icon>) () -> {
Icon interfaceIcon = getIcon(virtualFile);
if (interfaceIcon != null) {
LOG.debug("Found icon for file: " + virtualFile.getPath());
return interfaceIcon;
}
String fileTypeName = virtualFile.getFileType().getName();
LOG.debug("Using default icon for file type: " + fileTypeName);
return fileTypeName.equals("UNKNOWN") ? CodeSnippetIcon : ClassIcon;
}));

try {
return iconFuture.get(100, TimeUnit.MILLISECONDS); // Adjust timeout as needed
return iconFuture.get(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
// Log the error if needed
return ClassIcon; // Return a default icon in case of any error
LOG.error("Error getting icon for file: " + virtualFile.getPath(), e);
return ClassIcon;
}
}

Expand All @@ -50,6 +58,7 @@ public static Icon getFileTypeIcon(VirtualFile virtualFile) {
}
}
} catch (IOException e) {
LOG.error("Error reading file content: " + virtualFile.getPath(), e);
throw new RuntimeException(e);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Thu Dec 12 15:44:26 CET 2024
version=0.4.3
#Thu Dec 12 16:17:56 CET 2024
version=0.4.4

0 comments on commit 4737039

Please sign in to comment.