Skip to content

Commit

Permalink
Prevent unnecessary resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed May 22, 2024
1 parent 46b6209 commit 588b34b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,6 @@ public void onDeCompressed(Capture capture, int cacheHits, double compressionRat
synchronized (prevBufferLOCK) {
image = capture.createBufferedImage(prevBuffer, prevWidth, prevHeight);
prevBuffer = image.getValue();
// set to capture.getWidth()/getHeight() to visualize changed tiles only
prevWidth = image.getKey().getWidth();
prevHeight = image.getKey().getHeight();
}
Expand All @@ -728,8 +727,9 @@ public void onDeCompressed(Capture capture, int cacheHits, double compressionRat
frame.computeScaleFactors(prevWidth, prevHeight, frame.getKeepAspectRatioActivated());
}
// required as the canvas might have been reset if keepAspectRatio caused a resizing of the window
if (frame.getCanvas() != null) {
frame.onCaptureUpdated(scaleImage(image.getKey(), frame.getCanvas().width, frame.getCanvas().height));
final Dimension canvasDimension = frame.getCanvas();
if (canvasDimension != null) {
frame.onCaptureUpdated(scaleImage(image.getKey(), canvasDimension.width, canvasDimension.height));
}
} else {
frame.onCaptureUpdated(image.getKey());
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static java.awt.event.KeyEvent.*;
import static java.lang.Math.abs;
import static java.lang.String.format;
import static mpo.dayon.common.babylon.Babylon.translate;
import static mpo.dayon.common.gui.common.ImageUtilities.getOrCreateIcon;
Expand Down Expand Up @@ -203,9 +204,15 @@ public void keyReleased(KeyEvent ev) {

private void addResizeListener() {
addComponentListener(new ComponentAdapter() {
private Timer resizeTimer;
@Override
public void componentResized(ComponentEvent ev) {
resetCanvas();
if (resizeTimer != null) {
resizeTimer.stop();
}
resizeTimer = new Timer(500, e -> resetCanvas());
resizeTimer.setRepeats(false);
resizeTimer.start();
}
});
}
Expand Down Expand Up @@ -526,7 +533,7 @@ void computeScaleFactors(int sourceWidth, int sourceHeight, boolean keepAspectRa
canvas.setSize(canvas.getWidth() - OFFSET, canvas.getHeight() - OFFSET);
xFactor = canvas.getWidth() / sourceWidth;
yFactor = canvas.getHeight() / sourceHeight;
if (keepAspectRatio && !isImmutableWindowsSize.get()) {
if (keepAspectRatio && !isImmutableWindowsSize.get() && abs(xFactor - yFactor) > 0.01) {
resizeWindow(sourceWidth, sourceHeight);
}
}
Expand Down

0 comments on commit 588b34b

Please sign in to comment.