Skip to content

Commit

Permalink
Fixed zooming.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Apr 9, 2024
1 parent 2c7e2be commit 4f2902d
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions pdfviewfx/src/main/java/com/dlsc/pdfviewfx/skins/PDFViewSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,29 @@ public PDFViewSkin(PDFView view) {

HBox searchNavigator = createSearchNavigator();

MainAreaScrollPane mainArea = new MainAreaScrollPane();
VBox.setVgrow(mainArea, Priority.ALWAYS);
MainAreaScrollPane mainAreaScrollPane = new MainAreaScrollPane();
VBox.setVgrow(mainAreaScrollPane, Priority.ALWAYS);

VBox rightSide = new VBox(searchNavigator, mainArea);
rightSide.getStyleClass().add("main-area");
rightSide.setFillWidth(true);
VBox mainArea = new VBox(searchNavigator, mainAreaScrollPane);
mainArea.getStyleClass().add("main-area");
mainArea.setFillWidth(true);

StackPane leftSide = new StackPane(thumbnailListView, searchResultListView);
leftSide.getStyleClass().add("tray");
leftSide.visibleProperty().bind(view.showThumbnailsProperty());
leftSide.managedProperty().bind(view.showThumbnailsProperty());

BorderPane borderPane = new BorderPane();
borderPane.getStyleClass().add("border-pane");
borderPane.setTop(toolBar);
borderPane.setLeft(leftSide);
borderPane.setCenter(rightSide);
borderPane.setCenter(mainArea);
borderPane.setFocusTraversable(false);

getChildren().add(borderPane);

view.documentProperty().addListener(it -> {
mainArea.setImage(null);
mainAreaScrollPane.setImage(null);
imageCache.clear();
view.setPage(-1);
view.setPage(0);
Expand Down Expand Up @@ -438,6 +439,7 @@ private HBox createSearchNavigator() {
BooleanBinding searchResultsAvailable = Bindings.isNotEmpty(view.getSearchResults());

HBox buttonBox = new HBox(previousResultButton, nextResultButton);
buttonBox.getStyleClass().add("button-box");

HBox searchBox = new HBox(searchLabel, buttonBox, doneButton);
searchBox.getStyleClass().add("search-navigator");
Expand Down Expand Up @@ -712,7 +714,15 @@ private void ensureVisible(Node node) {
group = new Group(wrapper);
pane.getChildren().addAll(group);

viewportBoundsProperty().addListener((observable, oldBounds, newBounds) -> {
/*
* THIS HAS TO BE AN INVALIDATION LISTENER AND NOT A CHANGE LISTENER, OTHERWISE
* ZOOMING WILL NOT WORK.
*
* I HAVE NO IDEA WHY ...
*/
viewportBoundsProperty().addListener(it -> { // do not change to change listener
final Bounds bounds = getViewportBounds();

pane.setPrefWidth(Region.USE_COMPUTED_SIZE);
pane.setMinWidth(Region.USE_COMPUTED_SIZE);

Expand All @@ -721,18 +731,18 @@ private void ensureVisible(Node node) {

if (isPortrait()) {

double prefWidth = newBounds.getWidth() * pdfView.getZoomFactor() - 5;
final double prefWidth = bounds.getWidth() * pdfView.getZoomFactor() - 5;
pane.setPrefWidth(prefWidth);
pane.setMinWidth(prefWidth);

if (pdfView.isShowAll()) {
pane.setPrefHeight(newBounds.getHeight() - 5);
pane.setPrefHeight(bounds.getHeight() - 5);
} else {
Image image = getImage();
if (image != null) {
double scale = newBounds.getWidth() / image.getWidth();
double scale = bounds.getWidth() / image.getWidth();
double scaledImageHeight = image.getHeight() * scale;
double prefHeight = scaledImageHeight * pdfView.getZoomFactor();
final double prefHeight = scaledImageHeight * pdfView.getZoomFactor();
pane.setPrefHeight(prefHeight);
pane.setMinHeight(prefHeight);
}
Expand All @@ -744,18 +754,18 @@ private void ensureVisible(Node node) {
* Image has been rotated.
*/

double prefHeight = newBounds.getHeight() * pdfView.getZoomFactor() - 5;
final double prefHeight = bounds.getHeight() * pdfView.getZoomFactor() - 5;
pane.setPrefHeight(prefHeight);
pane.setMinHeight(prefHeight);

if (pdfView.isShowAll()) {
pane.setPrefWidth(newBounds.getWidth() - 5);
pane.setPrefWidth(bounds.getWidth() - 5);
} else {
Image image = getImage();
if (image != null) {
double scale = newBounds.getHeight() / image.getWidth();
double scale = bounds.getHeight() / image.getWidth();
double scaledImageHeight = image.getHeight() * scale;
double prefWidth = scaledImageHeight * pdfView.getZoomFactor();
final double prefWidth = scaledImageHeight * pdfView.getZoomFactor();
pane.setPrefWidth(prefWidth);
pane.setMinWidth(prefWidth);
}
Expand Down

0 comments on commit 4f2902d

Please sign in to comment.