[Editor] Improve MainView's TabPane binding to the mainController.openItems

This commit is contained in:
2021-02-24 13:05:23 +01:00
parent c5bfaec39e
commit af9cb39c95

View File

@@ -23,6 +23,8 @@ class MainView : View("BASE Game Editor") {
private val projectStructureView = find<ProjectStructureView>() private val projectStructureView = find<ProjectStructureView>()
private val codeStructure = find<CodeStructureView>() private val codeStructure = find<CodeStructureView>()
private val openTabs = mutableMapOf<Scope, Tab>()
init { init {
projectContext.projectProperty.addListener { _, _, project -> projectContext.projectProperty.addListener { _, _, project ->
val projectName = project?.let { " :: ${it.name} (${it.sourceDirectory.absolutePath})" } ?: "" val projectName = project?.let { " :: ${it.name} (${it.sourceDirectory.absolutePath})" } ?: ""
@@ -34,37 +36,22 @@ class MainView : View("BASE Game Editor") {
top = mainMenuView.root top = mainMenuView.root
center = tabpane { 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<MapFragment>(scope).root
graphic = FontIcon("fa-map")
setOnClosed { mainController.openItems.remove(scope) }
}
is Code -> Tab().apply {
val vm = CodeVM(item)
setInScope(vm, scope)
content = find<CodeEditorFragment>(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 // FIXME
// For some reason cleaning mainController.openMaps just takes off the tabs content keeping open themselves. // For some reason the plain object binding does not work between tabs and mainController.openItems.
// The workaround is to detect if map is cleaned and the clean the tabs by hand. // Because of that, the cache openTabs map has been created and the binding is done by following listener:
mainController.openItems.addListener(MapChangeListener { mainController.openItems.addListener(MapChangeListener {
if (it.map.isEmpty()) { when {
tabs.clear() 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<MapFragment>(scope).root
graphic = FontIcon("fa-map")
setOnClosed { mainController.openItems.remove(scope) }
}
is Code -> Tab().apply {
val vm = CodeVM(item)
setInScope(vm, scope)
content = find<CodeEditorFragment>(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")
}
} }