diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt index bd2fb1e2..4bc18c82 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt @@ -23,6 +23,8 @@ class MainView : View("BASE Game Editor") { private val projectStructureView = find() private val codeStructure = find() + private val openTabs = mutableMapOf() + init { projectContext.projectProperty.addListener { _, _, project -> val projectName = project?.let { " :: ${it.name} (${it.sourceDirectory.absolutePath})" } ?: "" @@ -34,37 +36,22 @@ class MainView : View("BASE Game Editor") { top = mainMenuView.root center = tabpane { - tabs.bind(mainController.openItems) { scope, item -> - when (item) { - is GameMap -> Tab().apply { - val vm = GameMapVM(item) - setInScope(vm, scope) - projectContext.project?.maps?.first { it.uid == item.uid } - ?.let { textProperty().bindBidirectional(it.nameProperty) } - content = find(scope).root - graphic = FontIcon("fa-map") - setOnClosed { mainController.openItems.remove(scope) } - } - - is Code -> Tab().apply { - val vm = CodeVM(item) - setInScope(vm, scope) - content = find(scope).root - textProperty().bindBidirectional(item.fileProperty.select { it.name.toProperty() }) - graphic = FontIcon("fa-code") - setOnClosed { mainController.openItems.remove(scope) } - } - - else -> throw IllegalStateException("Unsupported tab item") - } - } // FIXME - // For some reason cleaning mainController.openMaps just takes off the tabs content keeping open themselves. - // The workaround is to detect if map is cleaned and the clean the tabs by hand. + // For some reason the plain object binding does not work between tabs and mainController.openItems. + // Because of that, the cache openTabs map has been created and the binding is done by following listener: mainController.openItems.addListener(MapChangeListener { - if (it.map.isEmpty()) { - tabs.clear() + when { + it.wasAdded() -> createTab(it.key, it.valueAdded).let { tab -> + openTabs[it.key] = tab + tabs += tab + } + + it.wasRemoved() -> { + val tab = openTabs[it.key] + tabs -= tab + openTabs.remove(it.key) + } } }) } @@ -79,4 +66,27 @@ class MainView : View("BASE Game Editor") { } } } + + private fun createTab(scope: Scope, item: Any) = when (item) { + is GameMap -> Tab().apply { + val vm = GameMapVM(item) + setInScope(vm, scope) + projectContext.project?.maps?.first { it.uid == item.uid } + ?.let { textProperty().bindBidirectional(it.nameProperty) } + content = find(scope).root + graphic = FontIcon("fa-map") + setOnClosed { mainController.openItems.remove(scope) } + } + + is Code -> Tab().apply { + val vm = CodeVM(item) + setInScope(vm, scope) + content = find(scope).root + textProperty().bindBidirectional(item.fileProperty.select { it.name.toProperty() }) + graphic = FontIcon("fa-code") + setOnClosed { mainController.openItems.remove(scope) } + } + + else -> throw IllegalStateException("Unsupported tab item") + } } \ No newline at end of file