From 5ac55596de7af50971848e24d996aa99a038cf8e Mon Sep 17 00:00:00 2001 From: Robinson Date: Fri, 24 Nov 2023 22:25:54 +0100 Subject: [PATCH] Added more notification positions --- src/dorkbox/notify/AppNotify.kt | 14 ++++++++---- src/dorkbox/notify/DesktopNotify.kt | 34 ++++++++++++++--------------- src/dorkbox/notify/LAFUtil.kt | 7 +++++- src/dorkbox/notify/Position.kt | 12 +++++++++- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/dorkbox/notify/AppNotify.kt b/src/dorkbox/notify/AppNotify.kt index 8fb6d9e..d0f1ae7 100755 --- a/src/dorkbox/notify/AppNotify.kt +++ b/src/dorkbox/notify/AppNotify.kt @@ -1,5 +1,5 @@ /* - * Copyright 2017 dorkbox, llc + * Copyright 2023 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,8 +47,11 @@ internal class AppNotify(override val notification: Notify): Canvas(), NotifyTyp val screenWidth = bounds.getWidth().toInt() return when (position) { + // LEFT ALIGN Position.TOP_LEFT, Position.BOTTOM_LEFT -> Notify.MARGIN + startX - Position.CENTER -> startX + screenWidth / 2 - Notify.WIDTH / 2 - Notify.MARGIN / 2 + // CENTER ALIGN + Position.TOP, Position.CENTER, Position.BOTTOM -> startX + screenWidth / 2 - Notify.WIDTH / 2 - Notify.MARGIN / 2 + // RIGHT ALIGN Position.TOP_RIGHT, Position.BOTTOM_RIGHT -> startX + screenWidth - Notify.WIDTH - Notify.MARGIN } } @@ -58,9 +61,12 @@ internal class AppNotify(override val notification: Notify): Canvas(), NotifyTyp val screenHeight = bounds.getHeight().toInt() return when (position) { - Position.TOP_LEFT, Position.TOP_RIGHT -> startY + Notify.MARGIN + // TOP ALIGN + Position.TOP_LEFT, Position.TOP, Position.TOP_RIGHT -> startY + Notify.MARGIN + // CENTER ALIGN Position.CENTER -> startY + screenHeight / 2 - Notify.HEIGHT / 2 - Notify.MARGIN / 2 - Notify.SPACER - Position.BOTTOM_LEFT, Position.BOTTOM_RIGHT -> screenHeight - Notify.HEIGHT - Notify.MARGIN - Notify.SPACER * 2 + // BOTTOM ALIGN + Position.BOTTOM_LEFT, Position.BOTTOM, Position.BOTTOM_RIGHT -> screenHeight - Notify.HEIGHT - Notify.MARGIN - Notify.SPACER * 2 } } } diff --git a/src/dorkbox/notify/DesktopNotify.kt b/src/dorkbox/notify/DesktopNotify.kt index 4383301..44f964e 100755 --- a/src/dorkbox/notify/DesktopNotify.kt +++ b/src/dorkbox/notify/DesktopNotify.kt @@ -21,12 +21,7 @@ import dorkbox.tweenEngine.Tween import dorkbox.tweenEngine.TweenEquations import dorkbox.tweenEngine.TweenEvents import dorkbox.util.ScreenUtil -import java.awt.Dimension -import java.awt.Graphics -import java.awt.MouseInfo -import java.awt.Point -import java.awt.Rectangle -import java.awt.Toolkit +import java.awt.* import java.awt.image.BufferedImage import javax.swing.JWindow @@ -47,8 +42,11 @@ internal class DesktopNotify(override val notification: Notify) : JWindow(), Not val screenWidth = bounds.getWidth().toInt() return when (position) { + // LEFT ALIGN Position.TOP_LEFT, Position.BOTTOM_LEFT -> Notify.MARGIN + startX - Position.CENTER -> startX + screenWidth / 2 - Notify.WIDTH / 2 - Notify.MARGIN / 2 + // CENTER ALIGN + Position.TOP, Position.CENTER, Position.BOTTOM -> startX + screenWidth / 2 - Notify.WIDTH / 2 - Notify.MARGIN / 2 + // RIGHT ALIGN Position.TOP_RIGHT, Position.BOTTOM_RIGHT -> startX + screenWidth - Notify.WIDTH - Notify.MARGIN } } @@ -58,9 +56,12 @@ internal class DesktopNotify(override val notification: Notify) : JWindow(), Not val screenHeight = bounds.getHeight().toInt() return when (position) { - Position.TOP_LEFT, Position.TOP_RIGHT -> startY + Notify.MARGIN + // TOP ALIGN + Position.TOP_LEFT, Position.TOP, Position.TOP_RIGHT -> startY + Notify.MARGIN + // CENTER ALIGN Position.CENTER -> startY + screenHeight / 2 - Notify.HEIGHT / 2 - Notify.MARGIN / 2 - Position.BOTTOM_LEFT, Position.BOTTOM_RIGHT -> startY + screenHeight - Notify.HEIGHT - Notify.MARGIN + // BOTTOM ALIGN + Position.BOTTOM_LEFT, Position.BOTTOM, Position.BOTTOM_RIGHT -> startY + screenHeight - Notify.HEIGHT - Notify.MARGIN } } } @@ -144,19 +145,16 @@ internal class DesktopNotify(override val notification: Notify) : JWindow(), Not val gc = ScreenUtil.getMonitorAtLocation(point)?.defaultConfiguration ?: return 0 val screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc) - if (showFromTop) { - if (screenInsets.top > 0) { - return screenInsets.top - Notify.MARGIN - } + // " - Notify.MARGIN" is required BECAUSE each entry is offset by the margin, and we want the notification to be touching the edge!! + return if (showFromTop) { + screenInsets.top - Notify.MARGIN } else { - if (screenInsets.bottom > 0) { - return screenInsets.bottom + Notify.MARGIN - } + -(screenInsets.bottom - Notify.MARGIN) } - - return 0 } + + override fun paint(g: Graphics) { // we cache the text + image (to an image), the two states of the close "button" and then always render the progressbar try { diff --git a/src/dorkbox/notify/LAFUtil.kt b/src/dorkbox/notify/LAFUtil.kt index f7a27d1..8cd962f 100755 --- a/src/dorkbox/notify/LAFUtil.kt +++ b/src/dorkbox/notify/LAFUtil.kt @@ -152,9 +152,14 @@ internal object LAFUtil { } } + /** + * Controls which positions will grow the notification stack downwards + * + * @return true to grow downwards + */ fun growDown(notify: NotifyType<*>): Boolean { return when (notify.notification.position) { - Position.TOP_LEFT, Position.TOP_RIGHT, Position.CENTER -> true + Position.TOP_LEFT, Position.TOP, Position.TOP_RIGHT, Position.CENTER -> true else -> false } } diff --git a/src/dorkbox/notify/Position.kt b/src/dorkbox/notify/Position.kt index d4e16a1..1db8e0e 100755 --- a/src/dorkbox/notify/Position.kt +++ b/src/dorkbox/notify/Position.kt @@ -1,5 +1,5 @@ /* - * Copyright 2015 dorkbox, llc + * Copyright 2023 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,11 +26,21 @@ enum class Position { */ TOP_RIGHT, + /** + * top vertically, center horizontally + */ + TOP, + /** * center both vertically and horizontally */ CENTER, + /** + * bottom vertically, center horizontally + */ + BOTTOM, + /** * bottom vertically, left horizontally */