diff --git a/src/main/kotlin/insulator/App.kt b/src/main/kotlin/insulator/App.kt index d0f0f5a4..6aa67ae6 100644 --- a/src/main/kotlin/insulator/App.kt +++ b/src/main/kotlin/insulator/App.kt @@ -14,9 +14,6 @@ import insulator.views.style.ScrollBarStyle import insulator.views.style.ScrollPaneStyle import insulator.views.style.TableViewStyle import insulator.views.style.TextStyle -import javafx.scene.Scene -import javafx.stage.Stage -import javafx.stage.Window import tornadofx.* // ktlint-disable no-wildcard-imports class Insulator : App( @@ -33,20 +30,7 @@ class Insulator : App( ScrollPaneStyle::class, MainViewStyle::class, ScrollBarStyle::class -) { - - override fun createPrimaryScene(view: UIComponent): Scene { - FX.primaryStage.setOnCloseRequest { stop() } - return super.createPrimaryScene(view) - } - - override fun stop() { - Window.getWindows().map { it as? Stage } - .filter { it != FX.primaryStage } - .forEach { it?.close() } - super.stop() - } -} +) fun main(args: Array) { FX.dicontainer = DIContainer() diff --git a/src/main/kotlin/insulator/viewmodel/main/topic/ListTopicViewModel.kt b/src/main/kotlin/insulator/viewmodel/main/topic/ListTopicViewModel.kt index e16d5ad6..437fbf6a 100644 --- a/src/main/kotlin/insulator/viewmodel/main/topic/ListTopicViewModel.kt +++ b/src/main/kotlin/insulator/viewmodel/main/topic/ListTopicViewModel.kt @@ -7,7 +7,6 @@ import insulator.lib.helpers.map import insulator.lib.kafka.AdminApi import insulator.viewmodel.common.InsulatorViewModel import insulator.views.common.StringScope -import insulator.views.common.customOpenWindow import insulator.views.main.topic.CreateTopicView import insulator.views.main.topic.TopicView import javafx.beans.property.SimpleStringProperty @@ -55,10 +54,8 @@ class ListTopicViewModel : InsulatorViewModel() { .let { topicView -> setMainContent(selectedTopicName, topicView) } } - fun createNewTopic() = - with(StringScope("CreateNewTopic").withComponent(CreateTopicViewModel())) { - find(this).also { - it.whenUndockedOnce { refresh(); this.close() } - }.customOpenWindow(StageStyle.UTILITY, Modality.WINDOW_MODAL) - } + fun createNewTopic() = StringScope("CreateNewTopic") + .withComponent(CreateTopicViewModel()) + .let { scope -> find(scope).also { it.whenUndockedOnce { refresh(); scope.close() } } } + .openWindow(StageStyle.UTILITY, Modality.WINDOW_MODAL) } diff --git a/src/main/kotlin/insulator/viewmodel/main/topic/TopicViewModel.kt b/src/main/kotlin/insulator/viewmodel/main/topic/TopicViewModel.kt index 45f52662..343da4a5 100644 --- a/src/main/kotlin/insulator/viewmodel/main/topic/TopicViewModel.kt +++ b/src/main/kotlin/insulator/viewmodel/main/topic/TopicViewModel.kt @@ -10,7 +10,6 @@ import insulator.lib.kafka.Consumer import insulator.lib.kafka.DeserializationFormat import insulator.viewmodel.common.InsulatorViewModel import insulator.views.common.StringScope -import insulator.views.common.customOpenWindow import insulator.views.main.topic.ProducerView import javafx.beans.binding.Bindings import javafx.beans.property.SimpleBooleanProperty @@ -23,6 +22,8 @@ import javafx.beans.value.ObservableValue import javafx.collections.FXCollections import javafx.collections.ObservableList import javafx.scene.input.Clipboard +import javafx.stage.Modality +import javafx.stage.StageStyle import tornadofx.* // ktlint-disable no-wildcard-imports import java.util.LinkedList @@ -122,10 +123,10 @@ class TopicViewModel(val topicName: String) : InsulatorViewModel() { } } - fun showProduceView() = - with(StringScope(topicName).withComponent(ProducerViewModel(topicName))) { - find(this).customOpenWindow(owner = null) - } + fun showProduceView() = StringScope("producer-$topicName") + .withComponent(ProducerViewModel(topicName)) + .let { find(it) } + .openWindow(modality = Modality.WINDOW_MODAL, stageStyle = StageStyle.UTILITY) fun configureFilteredRecords(comparator: ObservableValue>) { filteredRecords.set( diff --git a/src/main/kotlin/insulator/views/common/Assets.kt b/src/main/kotlin/insulator/views/common/Assets.kt deleted file mode 100644 index cc2061a6..00000000 --- a/src/main/kotlin/insulator/views/common/Assets.kt +++ /dev/null @@ -1 +0,0 @@ -package insulator.views.common diff --git a/src/main/kotlin/insulator/views/common/CustomOpenModal.kt b/src/main/kotlin/insulator/views/common/CustomOpenModal.kt deleted file mode 100644 index 7e88f2dd..00000000 --- a/src/main/kotlin/insulator/views/common/CustomOpenModal.kt +++ /dev/null @@ -1,77 +0,0 @@ -package insulator.views.common - -import javafx.beans.property.SimpleObjectProperty -import javafx.scene.Parent -import javafx.stage.Modality -import javafx.stage.Stage -import javafx.stage.StageStyle -import javafx.stage.Window -import tornadofx.* // ktlint-disable no-wildcard-imports -import kotlin.reflect.full.functions -import kotlin.reflect.full.memberProperties - -// PATCH: https://github.com/edvin/tornadofx/issues/928 -@JvmOverloads -fun UIComponent.customOpenWindow( - stageStyle: StageStyle = StageStyle.DECORATED, - modality: Modality = Modality.NONE, - owner: Window? = currentWindow, - resizable: Boolean? = null -): Stage? { - if (modalStage != null) { - with(modalStage!!) { - isIconified = false - show() - toFront() - } - return modalStage - } - modalStage = Stage(stageStyle) - // modalStage needs to be set before this code to make close() work in blocking mode - with(modalStage!!) { - aboutToBeShown = true - if (resizable != null) isResizable = resizable - titleProperty().bind(titleProperty) - initModality(modality) - if (owner != null) initOwner(owner) - - if (customGetRootWrapper().scene != null) { - scene = customGetRootWrapper().scene - this.properties["tornadofx.scene"] = customGetRootWrapper().scene - } else { - javafx.scene.Scene(customGetRootWrapper()).apply { - FX.applyStylesheetsTo(this) - scene = this - this.properties["tornadofx.scene"] = this - } - } - - val primaryStage = FX.getPrimaryStage(scope) - if (primaryStage != null) icons += primaryStage.icons - hookGlobalShortcuts() - onBeforeShow() - setOnShown { - callInternalMethod("callOnDock") - if (FX.reloadStylesheetsOnFocus || FX.reloadViewsOnFocus) { - callInternalMethod("configureReloading") - } - aboutToBeShown = false - } - setOnHidden { - modalStage = null - callInternalMethod("callOnUndock") - } - show() - } - return modalStage -} - -private fun UIComponent.callInternalMethod(method: String) { - UIComponent::class.functions.find { it.name == method }?.call(this) -} - -@Suppress("UNCHECKED_CAST") -private fun UIComponent.customGetRootWrapper(): Parent { - val wrapperProp = UIComponent::class.memberProperties.find { it.name == "wrapperProperty" }?.get(this) as SimpleObjectProperty - return wrapperProp.value ?: this.root -} diff --git a/src/main/kotlin/insulator/views/configurations/ListClusterView.kt b/src/main/kotlin/insulator/views/configurations/ListClusterView.kt index 2dac1cd2..b7a7745c 100644 --- a/src/main/kotlin/insulator/views/configurations/ListClusterView.kt +++ b/src/main/kotlin/insulator/views/configurations/ListClusterView.kt @@ -4,7 +4,6 @@ import insulator.lib.configuration.model.Cluster import insulator.lib.update.VersionChecker import insulator.viewmodel.configurations.ListClusterViewModel import insulator.views.common.InsulatorView -import insulator.views.common.customOpenWindow import insulator.views.component.action import insulator.views.component.h1 import insulator.views.component.h2 @@ -55,7 +54,7 @@ class ListClusterView : InsulatorView("Insulator", ListClu if (wasVersionChecked.compareAndSet(false, true)) VersionChecker().getCurrentVersion().map { if (it.latestRelease != null) - UpdateInfoView(it.latestRelease).customOpenWindow(modality = Modality.WINDOW_MODAL) + UpdateInfoView(it.latestRelease).openWindow(modality = Modality.WINDOW_MODAL) } } diff --git a/src/test/kotlin/insulator/InsulatorTest.kt b/src/test/kotlin/insulator/InsulatorTest.kt deleted file mode 100644 index 008e7e3b..00000000 --- a/src/test/kotlin/insulator/InsulatorTest.kt +++ /dev/null @@ -1,62 +0,0 @@ -package insulator - -import helper.cleanupFXFramework -import helper.configureFXFramework -import insulator.di.DIContainer -import insulator.lib.helpers.runOnFXThread -import io.kotest.core.spec.style.FunSpec -import io.kotest.matchers.shouldNotBe -import io.mockk.clearAllMocks -import io.mockk.every -import io.mockk.mockk -import io.mockk.mockkStatic -import io.mockk.verify -import javafx.collections.FXCollections -import javafx.stage.Stage -import javafx.stage.Window -import tornadofx.* // ktlint-disable no-wildcard-imports - -class InsulatorTest : FunSpec({ - test("Set the primary stage onClose handler") { - // arrange - val sut = Insulator() - - runOnFXThread { - // act - sut.start(FX.primaryStage) - // assert - FX.primaryStage.onCloseRequestProperty().value shouldNotBe null - // cleanup - sut.stop() - } - } - - test("On stop all windows are closed") { - // arrange - val sut = Insulator() - val mockWindows = (1..10).map { mockk(relaxed = true) } - runOnFXThread { - sut.start(FX.primaryStage) - mockkStatic(Window::class) - every { Window.getWindows() } returns FXCollections.observableArrayList(mockWindows) - - // act - sut.stop() - - // assert - verify(exactly = 1) { Window.getWindows() } - mockWindows.forEach { - verify(exactly = 1) { it.close() } - } - clearAllMocks() - } - } - - beforeTest { - FX.dicontainer = DIContainer() - configureFXFramework() - } - afterTest { - cleanupFXFramework() - } -})