Skip to content

add popup functionality: keep in bounds & bring to front #70

Merged
merged 1 commit into from
Apr 14, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.View.DesktopView;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.Popup;
import javafx.scene.layout.Pane;

import java.util.ArrayList;
import java.util.List;

/**
* Controller for managing popups in a pane.
* Handles adding, removing, and keeping popups within bounds.
*/
public class PopupController {

private List<Popup> popups;
private final List<Popup> popups;

/**
* Constructs a new popup controller.
*/
public PopupController() {
popups = new ArrayList<>();
}

public List<Popup> getPopups() {
return popups;
}

/**
* Adds a popup to the controller and sets up its events.
*
* @param popup the popup to add
* @return true if added, false otherwise
*/
public boolean add(Popup popup) {
if (popup == null || popups.contains(popup)) {
return false;
}
popup.getCloseButton().setOnAction(e -> remove(popup));
popup.getRoot().setOnMousePressed(e -> bringToFront(popup));
popup.getContent().setOnMousePressed(e -> bringToFront(popup));
return popups.add(popup);
}

/**
* Removes a popup from its parent and the controller.
*
* @param popup the popup to remove
* @return true if removed, false otherwise
*/
public boolean remove(Popup popup) {
if (popup == null || !popups.contains(popup)) {
return false;
Expand All @@ -36,4 +51,39 @@ public boolean remove(Popup popup) {
}
return popups.remove(popup);
}

/**
* Brings the specified popup to the front of all others.
*
* @param popup the popup to bring to front
*/
public void bringToFront(Popup popup) {
if (popup.getRoot().getParent() instanceof Pane parent) {
popup.getRoot().toFront();
}
}

/**
* Checks if a popup is out of bounds and moves it back if necessary.
*
* @param popup the popup to check
*/
public void keepInBounds(Popup popup) {
if (popup.getRoot().getParent() instanceof Pane parent) {
double screenWidth = parent.getWidth();
double screenHeight = parent.getHeight();

if (popup.getX() < 0) {
popup.getRoot().setLayoutX(0);
} else if (popup.getX() + popup.getWidth() > screenWidth) {
popup.getRoot().setLayoutX(screenWidth - popup.getWidth());
}

if (popup.getY() < 0) {
popup.getRoot().setLayoutY(0);
} else if (popup.getY() + popup.getHeight() > screenHeight) {
popup.getRoot().setLayoutY(screenHeight - popup.getHeight());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ public BorderPane getRoot() {
int x = random.nextInt(1000);
int y = random.nextInt(700);
Popup popup = new SimplePopup(width, height, x, y);
popupController.add(popup);
center.getChildren().add(popup.getRoot());
popupController.add(popup);
popupController.keepInBounds(popup);

popup.getRoot().setOnMouseDragged(event -> {
popupController.keepInBounds(popup);
});
});

center.getChildren().add(button);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

/**
* An abstract class for all popups.
* Handles dragging, layout, and common components.
*/
public abstract class Popup {

protected int width;
protected int height;
protected int x;
protected int y;
protected double dx;
protected double dy;

Expand All @@ -22,11 +24,17 @@ public abstract class Popup {
protected VBox content;
protected Button closeButton;

public Popup(int width, int height, int x, int y) {
/**
* Constructs a new popup.
*
* @param width width of the popup
* @param height height of the popup
* @param x x-position of the popup
* @param y y-position of the popup
*/
protected Popup(int width, int height, int x, int y) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;

root = new BorderPane();
scrollPane = new ScrollPane();
Expand All @@ -53,30 +61,51 @@ public Popup(int width, int height, int x, int y) {
makeDraggable();
}

/**
* Returns the width of the popup.
*/
public int getWidth() {
return width;
}

/**
* Returns the height of the popup.
*/
public int getHeight() {
return height;
}

public int getX() {
return x;
/**
* Returns the x-position of the popup.
*/
public double getX() {
return root.getLayoutX();
}

public int getY() {
return y;
/**
* Returns the y-position of the popup.
*/
public double getY() {
return root.getLayoutY();
}

/**
* Returns the root node of the popup.
*/
public BorderPane getRoot() {
return root;
}

/**
* Returns the content node of the popup.
*/
public VBox getContent() {
return content;
}

/**
* Returns the close button of the popup.
*/
public Button getCloseButton() {
return closeButton;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

/**
* A simple implementation of a popup.
*/
public class SimplePopup extends Popup {

/**
* Constructs a new simple popup.
*
* @param width width of the popup
* @param height height of the popup
* @param x x-position of the popup
* @param y y-position of the popup
*/
public SimplePopup(int width, int height, int x, int y) {
super(width, height, x, y);
getContent().setStyle("-fx-background-color: blue;");
Expand Down