Skip to content

Commit

Permalink
Add function to hide previous JFXDialog before showing a new one, u…
Browse files Browse the repository at this point in the history
…se `WeakReference` and `WeakHashMap` to store singletons
  • Loading branch information
hanggrian committed Sep 1, 2024
1 parent 6fba43d commit 6e5ec9f
Show file tree
Hide file tree
Showing 28 changed files with 123 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlin.contracts.contract
* @param configuration custom dialog action.
* @return selected item.
*/
public inline fun <T> selector(
public inline fun <T> choiceDialog(
items: Collection<T>? = null,
prefill: T? = null,
configuration: ChoiceDialog<T>.() -> Unit,
Expand All @@ -35,7 +35,7 @@ public inline fun <T> selector(
* @param configuration custom dialog action.
* @return selected item.
*/
public inline fun <T> selector(
public inline fun <T> choiceDialog(
vararg items: T,
prefill: T? = null,
configuration: ChoiceDialog<T>.() -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.contracts.contract
* @param configuration custom dialog action.
* @return input text.
*/
public inline fun inputDialog(
public inline fun textInputDialog(
prefill: String = "",
configuration: TextInputDialog.() -> Unit,
): Optional<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class ToggleGroupsTest {
class ToggleGroupTest {
private lateinit var button: RadioButton
private lateinit var group: ToggleGroup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class AlertsTest : DialogShowingTest() {
class AlertTest : DialogShowingTest() {
@Test
fun alert() {
interact {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class SelectorsTest : DialogShowingTest() {
class ChoiceDialogTest : DialogShowingTest() {
@Test
fun selector() {
fun choiceDialog() {
interact {
assertEquals(
"Jump off bridge",
selector<String>(
choiceDialog<String>(
listOf("Jump off bridge", "Live a happy life"),
"Live a happy life",
) {
Expand All @@ -30,7 +30,7 @@ class SelectorsTest : DialogShowingTest() {
)
assertEquals(
"Jump off bridge",
selector(
choiceDialog(
"Jump off bridge",
"Live a happy life",
prefill = "Live a happy life",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class InputDialogsTest : DialogShowingTest() {
class TextInputDialogTest : DialogShowingTest() {
@Test
fun inputDialog() {
fun textInputDialog() {
interact {
assertEquals(
"Awesome input",
inputDialog("Awful input") {
textInputDialog("Awful input") {
headerTitle = "Input dialog"
graphicIcon = sampleGraphic

Expand Down
49 changes: 24 additions & 25 deletions ktfx-layouts/src/main/kotlin/javafx/scene/layout/KtfxAnchorPane.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,45 +53,45 @@ public open class KtfxAnchorPane :

/** Set children anchor on each side in this layout. */
public fun <T : Node> T.anchor(
horizontal: Int = Int.MAX_VALUE,
vertical: Int = Int.MAX_VALUE,
horizontal: Int = Int.MIN_VALUE,
vertical: Int = Int.MIN_VALUE,
): T = anchor(vertical, horizontal, vertical, horizontal)

/** Set children anchor on each side in this layout. */
public fun <T : Node> T.anchor(
top: Int = Int.MAX_VALUE,
right: Int = Int.MAX_VALUE,
bottom: Int = Int.MAX_VALUE,
left: Int = Int.MAX_VALUE,
top: Int = Int.MIN_VALUE,
right: Int = Int.MIN_VALUE,
bottom: Int = Int.MIN_VALUE,
left: Int = Int.MIN_VALUE,
): T =
anchor(
if (top != Int.MAX_VALUE) top.toDouble() else Double.NaN,
if (right != Int.MAX_VALUE) right.toDouble() else Double.NaN,
if (bottom != Int.MAX_VALUE) bottom.toDouble() else Double.NaN,
if (left != Int.MAX_VALUE) left.toDouble() else Double.NaN,
if (top != Int.MIN_VALUE) top.toDouble() else Double.NaN,
if (right != Int.MIN_VALUE) right.toDouble() else Double.NaN,
if (bottom != Int.MIN_VALUE) bottom.toDouble() else Double.NaN,
if (left != Int.MIN_VALUE) left.toDouble() else Double.NaN,
)

/** Set children anchor on all side in this layout. */
public fun <T : Node> T.anchor(all: Long): T = anchor(all, all, all, all)

/** Set children anchor on each side in this layout. */
public fun <T : Node> T.anchor(
horizontal: Long = Long.MAX_VALUE,
vertical: Long = Long.MAX_VALUE,
horizontal: Long = Long.MIN_VALUE,
vertical: Long = Long.MIN_VALUE,
): T = anchor(vertical, horizontal, vertical, horizontal)

/** Set children anchor on each side in this layout. */
public fun <T : Node> T.anchor(
top: Long = Long.MAX_VALUE,
right: Long = Long.MAX_VALUE,
bottom: Long = Long.MAX_VALUE,
left: Long = Long.MAX_VALUE,
top: Long = Long.MIN_VALUE,
right: Long = Long.MIN_VALUE,
bottom: Long = Long.MIN_VALUE,
left: Long = Long.MIN_VALUE,
): T =
anchor(
if (top != Long.MAX_VALUE) top.toDouble() else Double.NaN,
if (right != Long.MAX_VALUE) right.toDouble() else Double.NaN,
if (bottom != Long.MAX_VALUE) bottom.toDouble() else Double.NaN,
if (left != Long.MAX_VALUE) left.toDouble() else Double.NaN,
if (top != Long.MIN_VALUE) top.toDouble() else Double.NaN,
if (right != Long.MIN_VALUE) right.toDouble() else Double.NaN,
if (bottom != Long.MIN_VALUE) bottom.toDouble() else Double.NaN,
if (left != Long.MIN_VALUE) left.toDouble() else Double.NaN,
)

/** Set children anchor on all side in this layout. */
Expand All @@ -104,17 +104,16 @@ public open class KtfxAnchorPane :
): T = anchor(vertical, horizontal, vertical, horizontal)

/** Set children anchor on each side in this layout. */
@Suppress("ConvertNaNEquality")
public fun <T : Node> T.anchor(
top: Double = Double.NaN,
right: Double = Double.NaN,
bottom: Double = Double.NaN,
left: Double = Double.NaN,
): T {
if (topAnchor != Double.NaN) topAnchor = top
if (rightAnchor != Double.NaN) rightAnchor = right
if (bottomAnchor != Double.NaN) bottomAnchor = bottom
if (leftAnchor != Double.NaN) leftAnchor = left
if (!top.isNaN()) topAnchor = top
if (!right.isNaN()) rightAnchor = right
if (!bottom.isNaN()) bottomAnchor = bottom
if (!left.isNaN()) leftAnchor = left
return this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
@file:JvmName("ControlsfxPlatformKt")
@file:Suppress("NOTHING_TO_INLINE")

package ktfx.controlsfx

import org.controlsfx.tools.Platform

/** Returns true if this is running on Windows. */
public inline fun isPlatformWindows(): Boolean = Platform.getCurrent() == Platform.WINDOWS
public fun isPlatformWindows(): Boolean = Platform.getCurrent() == Platform.WINDOWS

/** Returns true if this is running on OSX. */
public inline fun isPlatformOsx(): Boolean = Platform.getCurrent() == Platform.OSX
public fun isPlatformOsx(): Boolean = Platform.getCurrent() == Platform.OSX

/** Returns true if this is running on Unix. */
public inline fun isPlatformUnix(): Boolean = Platform.getCurrent() == Platform.UNIX
public fun isPlatformUnix(): Boolean = Platform.getCurrent() == Platform.UNIX
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import javafx.scene.control.Control
import org.controlsfx.validation.Severity
import org.controlsfx.validation.ValidationSupport
import org.controlsfx.validation.Validator
import java.lang.ref.WeakReference

private lateinit var support: ValidationSupport
private val singletonSupport: ValidationSupport
get() {
if (!::support.isInitialized) {
support = ValidationSupport()
}
return support
}
private lateinit var validationSupportRef: WeakReference<ValidationSupport>
private val validationSupport: ValidationSupport
get() =
validationSupportRef.get()
?: ValidationSupport().also { validationSupportRef = WeakReference(it) }

/** Set control's required flag. */
public inline var Control.isValidationRequired: Boolean
Expand All @@ -24,7 +22,7 @@ public fun <T> Control.registerEmptyValidator(
message: String,
severity: Severity = Severity.ERROR,
required: Boolean = true,
support: ValidationSupport = singletonSupport,
support: ValidationSupport = validationSupport,
): Boolean =
support.registerValidator(
this,
Expand All @@ -38,7 +36,7 @@ public fun <T> Control.registerEqualsValidator(
collection: Collection<T>,
severity: Severity = Severity.ERROR,
required: Boolean = true,
support: ValidationSupport = singletonSupport,
support: ValidationSupport = validationSupport,
): Boolean =
support.registerValidator(
this,
Expand All @@ -51,7 +49,7 @@ public fun <T> Control.registerPredicateValidator(
message: String,
severity: Severity = Severity.ERROR,
required: Boolean = true,
support: ValidationSupport = singletonSupport,
support: ValidationSupport = validationSupport,
predicate: (T) -> Boolean,
): Boolean =
support.registerValidator(
Expand All @@ -66,7 +64,7 @@ public fun Control.registerRegexValidator(
regex: String,
severity: Severity = Severity.ERROR,
required: Boolean = true,
support: ValidationSupport = singletonSupport,
support: ValidationSupport = validationSupport,
): Boolean =
support.registerValidator(
this,
Expand All @@ -80,7 +78,7 @@ public fun Control.registerRegexValidator(
regex: Regex,
severity: Severity = Severity.ERROR,
required: Boolean = true,
support: ValidationSupport = singletonSupport,
support: ValidationSupport = validationSupport,
): Boolean =
support.registerValidator(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class CommandLinksDialogsTest : DialogShowingTest() {
class CommandLinksDialogTest : DialogShowingTest() {
@Test
fun commandLinksDialogFromArray() {
interact {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class ExceptionDialogsTest : DialogShowingTest() {
class ExceptionDialogTest : DialogShowingTest() {
@Test
fun exceptionDialog() {
interact {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class FontSelectorsTest : DialogShowingTest() {
class FontSelectorTest : DialogShowingTest() {
private val arial18 = Font.font("Arial", 18.0)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class LoginDialogsTest : DialogShowingTest() {
class LoginDialogTest : DialogShowingTest() {
private val userInfo = "Hello" to "World"

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

@Ignore
class ProgressDialogsTest : DialogShowingTest() {
class ProgressDialogTest : DialogShowingTest() {
private lateinit var helloWorldService: Service<String>

override fun start(stage: Stage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@file:Suppress("NOTHING_TO_INLINE")
@file:OptIn(ExperimentalContracts::class)
@file:Suppress("NOTHING_TO_INLINE")

package ktfx.jfoenix.controls

Expand All @@ -17,7 +17,13 @@ import kotlin.contracts.contract
public inline fun JFXSnackbar.show(content: Node, duration: Duration): Unit =
enqueue(JFXSnackbar.SnackbarEvent(content, duration))

/** Show this snackbar with default layout. */
/**
* Show this snackbar with default layout.
* May throw `NullPointerException` due to JFXSnackbar bug.
*
* @throws NullPointerException
* @see [GitHub issue](https://github.com/sshahine/JFoenix/issues/1101)
*/
public fun JFXSnackbar.show(
message: String,
duration: Duration,
Expand All @@ -26,8 +32,8 @@ public fun JFXSnackbar.show(
): Unit =
show(
JFXSnackbarLayout(message, actionText) {
close()
action?.invoke(it)
close()
},
duration,
)
Expand All @@ -36,16 +42,22 @@ public fun JFXSnackbar.show(
public inline fun JFXSnackbar.showIndefinite(content: Node): Unit =
enqueue(JFXSnackbar.SnackbarEvent(content, Duration.INDEFINITE))

/** Show this snackbar indefinitely with default layout. */
/**
* Show this snackbar indefinitely with default layout.
* May throw `NullPointerException` due to JFXSnackbar bug.
*
* @throws NullPointerException
* @see [GitHub issue](https://github.com/sshahine/JFoenix/issues/1101)
*/
public fun JFXSnackbar.showIndefinite(
message: String,
actionText: String? = null,
action: ((ActionEvent) -> Unit)? = null,
): Unit =
showIndefinite(
JFXSnackbarLayout(message, actionText) {
close()
action?.invoke(it)
close()
},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ktfx.jfoenix.dialogs

import com.jfoenix.controls.JFXDialog
import javafx.scene.layout.StackPane
import java.util.WeakHashMap

private val shownDialogs = WeakHashMap<StackPane, JFXDialog>()

/**
* Track a previously shown dialog within a container to close it before showing a new dialog.
*
* @param container parent layout, or the dialog's default container.
*/
public fun JFXDialog.showSingle(container: StackPane = dialogContainer) {
shownDialogs[container]
?.takeIf { it in container.children }
?.close()
shownDialogs[container] = apply { show() }
}
Loading

0 comments on commit 6e5ec9f

Please sign in to comment.