diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/view/Popup.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/view/Popup.java index 3f74d0d..8d28e55 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/view/Popup.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/view/Popup.java @@ -15,6 +15,8 @@ import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.Priority; +import javafx.scene.layout.Region; +import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; @@ -69,7 +71,28 @@ protected Popup(int width, int height, int x, int y, App type) { scrollPane.getStyleClass().add("popup-scroll-pane"); scrollPane.setFitToWidth(true); scrollPane.setFitToHeight(false); - scrollPane.setPrefSize(width, height); + scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); + scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); + content.heightProperty().addListener((obs, ov, nv) -> + scrollPane.setVbarPolicy( + nv.doubleValue() > scrollPane.getHeight() + ? ScrollPane.ScrollBarPolicy.AS_NEEDED + : ScrollPane.ScrollBarPolicy.NEVER + ) + ); + scrollPane.heightProperty().addListener((obs, ov, nv) -> + scrollPane.setVbarPolicy( + content.getHeight() > nv.doubleValue() + ? ScrollPane.ScrollBarPolicy.AS_NEEDED + : ScrollPane.ScrollBarPolicy.NEVER + ) + ); + if (width > 0) { + scrollPane.setPrefWidth(width); + } + if (height > 0) { + scrollPane.setPrefHeight(height); + } closeButton = buildCloseButton(); header = buildHeader(closeButton, type.getDisplayName()); @@ -83,7 +106,6 @@ protected Popup(int width, int height, int x, int y, App type) { Rectangle borderOverlay = buildBorderOverlay(); wrapper = new Pane(root, borderOverlay); - // Bind wrapper size to root's size so centering logic works wrapper.prefWidthProperty().bind(root.widthProperty()); wrapper.prefHeightProperty().bind(root.heightProperty()); borderOverlay.widthProperty().bind(root.widthProperty()); @@ -96,7 +118,6 @@ protected Popup(int width, int height, int x, int y, App type) { wrapper.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> wrapper.toFront()); loadStylesheets(); - } private Rectangle buildClip(BorderPane target) { @@ -127,7 +148,6 @@ private void loadStylesheets() { sheets.add("/css/popup.css"); sheets.add("/css/components.css"); - // Map App to CSS String appCss = switch (type) { case BANK -> "/css/bank.css"; case MAIL -> "/css/mail.css"; @@ -142,7 +162,6 @@ private void loadStylesheets() { sheets.add(appCss); } - // Some apps need receipt.css if (type == App.STOCK || type == App.WEEKENDREPORT) { sheets.add("/css/receipt.css"); } @@ -169,11 +188,12 @@ private HBox buildHeader(Button closeButton, String title) { Label titleLabel = new Label(title); titleLabel.getStyleClass().add("popup-title"); - HBox titleContainer = new HBox(titleLabel); - titleContainer.setAlignment(Pos.CENTER); + StackPane titleContainer = new StackPane(titleLabel); + StackPane.setAlignment(titleLabel, Pos.CENTER); + titleContainer.setMinHeight(Region.USE_PREF_SIZE); HBox.setHgrow(titleContainer, Priority.ALWAYS); - HBox hbox = new HBox(10, closeButton, titleContainer); + HBox hbox = new HBox(closeButton, titleContainer); hbox.setAlignment(Pos.CENTER_LEFT); hbox.setPadding(new Insets(5, 10, 5, 10)); hbox.getStyleClass().add("popup-header"); @@ -192,11 +212,10 @@ private String resource(final String path) { public void centerInParent() { Runnable bind = () -> { if (wrapper.getParent() instanceof Pane pane) { - // Use root.widthProperty/heightProperty for centering wrapper.layoutXProperty().bind( - pane.widthProperty().subtract(root.widthProperty()).divide(2)); + pane.widthProperty().subtract(wrapper.prefWidthProperty()).divide(2)); wrapper.layoutYProperty().bind( - pane.heightProperty().subtract(root.heightProperty()).divide(2)); + pane.heightProperty().subtract(wrapper.prefHeightProperty()).divide(2)); } }; bind.run(); @@ -238,5 +257,4 @@ public HBox getHeader() { public Button getCloseButton() { return closeButton; } - } \ No newline at end of file