Skip to content

Instantly share code, notes, and snippets.

@NikkyAI
Last active July 19, 2018 05:35
Show Gist options
  • Select an option

  • Save NikkyAI/aad9fa2d4fb47f7e2b0e1c5459591d5c to your computer and use it in GitHub Desktop.

Select an option

Save NikkyAI/aad9fa2d4fb47f7e2b0e1c5459591d5c to your computer and use it in GitHub Desktop.
package com.threelite.scraper.gui.view
import com.threelite.scraper.gui.controller.TabController
import javafx.collections.ObservableList
import javafx.scene.control.Tab
import kfoenix.jfxtabpane
import tornadofx.*
class DriverTabPane : View() {
private val controller: TabController by inject()
override val root = jfxtabpane {
// tabClosingPolicy = TabPane.TabClosingPolicy.SELECTED_TAB //TODO: do cleanup on close of tab
controller.selectionModel = selectionModel
bindTabs(tabs, controller.list) {
Tab(it).apply {
//TODO: add real UI
vbox {
label(it)
}
}
}
}
private inline fun <reified T> bindTabs(into: ObservableList<Tab>, list: ObservableList<T>, crossinline conversion: (T) -> Tab) {
list.forEach { t ->
val existing = into.find { it.properties["tracker"] == t }
if (existing == null)
into.add(
conversion(t).apply {
tag = t
properties["tracker"] = t
}
)
}
list.onChange { c ->
while (c.next()) {
if (c.wasAdded()) {
c.addedSubList.forEach { t ->
val existing = into.find { it.properties["tracker"] == t }
if (existing == null)
into.add(
conversion(t).apply {
tag = t
properties["tracker"] = t
}
)
}
}
if (c.wasRemoved()) {
c.removed.forEach { t ->
into.removeIf { it.properties["tracker"] == t }
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment