Add image caching
This commit is contained in:
parent
08bd915f04
commit
3c7f2da752
@ -132,7 +132,6 @@ public final class ChatScene implements EventListener, Restorable {
|
|||||||
|
|
||||||
private Chat currentChat;
|
private Chat currentChat;
|
||||||
private FilteredList<Chat> chats;
|
private FilteredList<Chat> chats;
|
||||||
private boolean recording;
|
|
||||||
private Attachment pendingAttachment;
|
private Attachment pendingAttachment;
|
||||||
private boolean postingPermanentlyDisabled;
|
private boolean postingPermanentlyDisabled;
|
||||||
private boolean isCustomAttachmentImage;
|
private boolean isCustomAttachmentImage;
|
||||||
@ -354,7 +353,8 @@ public final class ChatScene implements EventListener, Restorable {
|
|||||||
// Discard the pending attachment
|
// Discard the pending attachment
|
||||||
if (recorder.isRecording()) {
|
if (recorder.isRecording()) {
|
||||||
recorder.cancel();
|
recorder.cancel();
|
||||||
recording = false;
|
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
|
||||||
|
voiceButton.setText(null);
|
||||||
}
|
}
|
||||||
pendingAttachment = null;
|
pendingAttachment = null;
|
||||||
updateAttachmentView(false);
|
updateAttachmentView(false);
|
||||||
@ -414,8 +414,7 @@ public final class ChatScene implements EventListener, Restorable {
|
|||||||
private void voiceButtonClicked() {
|
private void voiceButtonClicked() {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
if (!recording) {
|
if (!recorder.isRecording()) {
|
||||||
recording = true;
|
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
voiceButton.setText("Recording");
|
voiceButton.setText("Recording");
|
||||||
voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", DEFAULT_ICON_SIZE)));
|
voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", DEFAULT_ICON_SIZE)));
|
||||||
@ -425,7 +424,6 @@ public final class ChatScene implements EventListener, Restorable {
|
|||||||
pendingAttachment = new Attachment(recorder.finish(), "Voice_recording_"
|
pendingAttachment = new Attachment(recorder.finish(), "Voice_recording_"
|
||||||
+ DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss").format(LocalDateTime.now()) + "." + AudioRecorder.FILE_FORMAT,
|
+ DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss").format(LocalDateTime.now()) + "." + AudioRecorder.FILE_FORMAT,
|
||||||
AttachmentType.VOICE);
|
AttachmentType.VOICE);
|
||||||
recording = false;
|
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
|
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
|
||||||
voiceButton.setText(null);
|
voiceButton.setText(null);
|
||||||
|
@ -21,6 +21,9 @@ import envoy.util.EnvoyLog;
|
|||||||
*/
|
*/
|
||||||
public final class IconUtil {
|
public final class IconUtil {
|
||||||
|
|
||||||
|
private static final HashMap<String, Image> cache = new HashMap<>();
|
||||||
|
private static final HashMap<String, BufferedImage> awtCache = new HashMap<>();
|
||||||
|
|
||||||
private IconUtil() {}
|
private IconUtil() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,7 +33,14 @@ public final class IconUtil {
|
|||||||
* @return the loaded image
|
* @return the loaded image
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public static Image load(String path) { return new Image(IconUtil.class.getResource(path).toExternalForm()); }
|
public static Image load(String path) {
|
||||||
|
if (cache.containsKey(path)) return cache.get(path);
|
||||||
|
else {
|
||||||
|
final var image = new Image(IconUtil.class.getResource(path).toExternalForm());
|
||||||
|
cache.put(path, image);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads an image from the resource folder and scales it to the given size.
|
* Loads an image from the resource folder and scales it to the given size.
|
||||||
@ -39,9 +49,21 @@ public final class IconUtil {
|
|||||||
* @param size the size to scale the icon to
|
* @param size the size to scale the icon to
|
||||||
* @return the scaled image
|
* @return the scaled image
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
|
* @apiNote There is an extraordinarily low chance that there is a size and name
|
||||||
|
* conflict. To achieve that however, you basically have to name your
|
||||||
|
* image intentionally like that. For normal human readable names or
|
||||||
|
* even automatically generated ones it will never occur.
|
||||||
*/
|
*/
|
||||||
public static Image load(String path, int size) {
|
public static Image load(String path, int size) {
|
||||||
return new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true);
|
|
||||||
|
// Minimizing the risk of size and name conflict
|
||||||
|
final var sizeSpecificString = path + size + "+a* ";
|
||||||
|
if (cache.containsKey(sizeSpecificString)) return cache.get(sizeSpecificString);
|
||||||
|
else {
|
||||||
|
final var image = new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true);
|
||||||
|
cache.put(sizeSpecificString, image);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,14 +160,20 @@ public final class IconUtil {
|
|||||||
* @since Envoy Client v0.2-beta
|
* @since Envoy Client v0.2-beta
|
||||||
*/
|
*/
|
||||||
public static BufferedImage loadAWTCompatible(String path) {
|
public static BufferedImage loadAWTCompatible(String path) {
|
||||||
|
if (awtCache.containsKey(path)) return awtCache.get(path);
|
||||||
|
else {
|
||||||
BufferedImage image = null;
|
BufferedImage image = null;
|
||||||
try {
|
try {
|
||||||
image = ImageIO.read(IconUtil.class.getResource(path));
|
image = ImageIO.read(IconUtil.class.getResource(path));
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e);
|
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If an IOException occurs we don't assume it'll be fixed on the next call
|
||||||
|
awtCache.put(path, image);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method should be called if the display of an image depends upon the
|
* This method should be called if the display of an image depends upon the
|
||||||
|
Reference in New Issue
Block a user