Skip to content

Commit

Permalink
Implement keep aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Feb 7, 2024
1 parent 9c364a6 commit f9414e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,12 @@ public void onDeCompressed(Capture capture, int cacheHits, double compressionRat
}
if (fitToScreenActivated.get()) {
if (frame.getCanvas() == null) {
frame.computeScaleFactors(prevWidth, prevHeight);
frame.computeScaleFactors(prevWidth, prevHeight, false);
}
// 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));
}
frame.onCaptureUpdated(scaleImage(image.getKey(), frame.getCanvas().width, frame.getCanvas().height));
} else {
frame.onCaptureUpdated(image.getKey());
}
Expand Down Expand Up @@ -910,7 +913,7 @@ public void onClipboardSent() {
*/
@Override
public void onResizeScreen(int width, int height) {
frame.computeScaleFactors(width, height);
frame.computeScaleFactors(width, height, false);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mpo.dayon.common.gui.common.ImageNames;
import mpo.dayon.common.gui.statusbar.StatusBar;
import mpo.dayon.common.gui.toolbar.ToolBar;
import mpo.dayon.common.log.Log;
import mpo.dayon.common.monitoring.counter.Counter;

import javax.swing.*;
Expand Down Expand Up @@ -84,11 +85,11 @@ Dimension getCanvas() {
return canvas;
}

public double getxFactor() {
double getxFactor() {
return xFactor;
}

public double getyFactor() {
double getyFactor() {
return yFactor;
}

Expand Down Expand Up @@ -346,12 +347,23 @@ void onIOError(IOException error) {
JOptionPane.showMessageDialog(this, errorMessage, translate("comm.error"), JOptionPane.ERROR_MESSAGE);
}

void computeScaleFactors(int sourceWidth, int sourceHeight) {
void computeScaleFactors(int sourceWidth, int sourceHeight, boolean keepAspectRatio) {
canvas = assistantPanelWrapper.getSize();
canvas.setSize(canvas.getWidth() - assistantPanelWrapper.getVerticalScrollBar().getWidth() + OFFSET,
canvas.getHeight() - assistantPanelWrapper.getHorizontalScrollBar().getHeight() + OFFSET);
xFactor = canvas.getWidth() / sourceWidth;
yFactor = canvas.getHeight() / sourceHeight;
if (keepAspectRatio) {
Log.debug("%s", () -> format("Resize W:H %s:%s x:y %s:%s", this.getWidth(), this.getHeight(), xFactor, yFactor));
if (xFactor > yFactor) {
yFactor = xFactor;
this.setSize(this.getWidth(), (int) (sourceHeight * xFactor));
} else {
xFactor = yFactor;
this.setSize((int) (sourceWidth * yFactor), this.getHeight());
}
Log.debug("%s", () -> format("Resized W:H %s:%s x:y %s:%s", this.getWidth(), this.getHeight(), xFactor, yFactor));
}
}

void resetFactors() {
Expand Down

0 comments on commit f9414e2

Please sign in to comment.