Skip to content

Commit

Permalink
[prmr#508] Added notification spawning and rearranging methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthusWQZ committed Oct 16, 2023
1 parent b2dd94d commit e57b602
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/org/jetuml/gui/NotificationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
public final class NotificationHandler
{

private static final int NOTIFICATION_STACK_SPACING = 8;
private static final int NOTIFICATION_STACK_X_MARGIN = 18;
private static final int NOTIFICATION_STACK_Y_MARGIN = 18;

/**
* A callback for Notification objects called when they reached the end of their lifetime.
* It removes them from the handler notification list.
Expand All @@ -22,6 +26,7 @@ private CleanUpCallback()

/**
* Removes the specified notification object from the handler notification list.
*
* @param pNotification The notification object that calls this function
*/
public void execute(Notification pNotification)
Expand Down Expand Up @@ -50,11 +55,55 @@ public static NotificationHandler instance()

/**
* Sets the parent stage of all the notification stages.
*
* @param pStage The target parent stage of the notification objects
*/
public void setMainStage(Stage pStage)
{
this.aMainStage = pStage;
}

/**
* Rearranges the notifications so that they are stacked properly and do not overlap.
*/
public void updatePosition()
{

double yBuf = this.aMainStage.getY() + this.aMainStage.getHeight() - NOTIFICATION_STACK_Y_MARGIN;
double x = this.aMainStage.getX() + NOTIFICATION_STACK_X_MARGIN;

for (int i = aNotificationList.size() - 1; i >= 0; i--)
{
Notification notification = aNotificationList.get(i);
notification.setX(x);
notification.setY(yBuf);
yBuf = yBuf - notification.getHeight() - NOTIFICATION_STACK_SPACING;
}
}

/**
* Spawns a new toast notification.
*
* @param pText The text to show on the toast
*/
public void spawn(String pText)
{

if (this.aMainStage == null)
{
return;
}

double x = this.aMainStage.getX() + NOTIFICATION_STACK_X_MARGIN;
double y = this.aMainStage.getY() + this.aMainStage.getHeight() - NOTIFICATION_STACK_Y_MARGIN;

ToastNotification toast = new ToastNotification(pText, this.aMainStage);

aNotificationList.add(toast);

toast.show(x, y, new CleanUpCallback());
this.updatePosition();

}

}

0 comments on commit e57b602

Please sign in to comment.