```package todolist.views import javafx.animation...
# tornadofx
j
Copy code
package todolist.views

import javafx.animation.Interpolator
import javafx.geometry.Pos
import javafx.scene.Node
import javafx.scene.control.Button
import javafx.scene.control.TextField
import javafx.scene.layout.VBox
import todolist.stylessheets.Styles
import tornadofx.*

class TodoList : View() {
    override val root = vbox()

    companion object {
        var taskInput: TextField by singleAssign()
        var addTaskButton: Button by singleAssign()
        var tasksList: VBox by singleAssign()
    }

    init {
        with(root) {
            title = "To-do List App"
            vbox {
                vbox {
                    addClass(Styles.header)
                    text("To-do List Manager") {
                        alignment = Pos.CENTER
                        addClass(Styles.h1)
                        vboxConstraints {
                            marginBottom = 20.0
                        }
                    }
                    taskInput = textfield()
                    taskInput.addClass(Styles.taskInput)
                    taskInput.apply {
                        tooltip("Type the task you're yet to do")
                    }
                    addTaskButton = button("Add Task") {
                        alignment = Pos.CENTER
                        addClass(Styles.taskButton)
                        vboxConstraints {
                            marginTop = 22.0
                        }
                        action {
                            addTask(tasksList, taskInput.text)
                        }
                        shortcut("Enter")
                    }
                }
                scrollpane {
                    tasksList = vbox {
                        vboxConstraints {
                            marginRight = 20.0
                            marginLeft = 20.0
                            marginTop = 20.0
                            marginBottom = 20.0
                        }
                    }
                    style {
                        backgroundInsets += box(0.px)
                    }
                }
            }
            prefHeight = 600.0
        }
    }

    fun addTask (parent: VBox, text: String) {
        if (validate()) {
            parent.add(vbox {
                addClass(Styles.tasks)
                text(text)
                vboxConstraints {
                    marginTop = 10.0
                    marginBottom = 10.0
                    marginLeft = 20.0
                }
                setOnMouseClicked {
                    removeTask(this)
                }
                tooltip("Click this task to flag as done and remove")
            })
            taskInput.text = ""
        } else {
            return
        }
    }

    fun removeTask (child: Node) {
        timeline {
            keyframe(0.3.seconds) {
                keyvalue(child.translateXProperty(), 130, interpolator = Interpolator.EASE_BOTH)
                keyvalue(child.opacityProperty(), 0, interpolator = Interpolator.EASE_BOTH)
                setOnFinished {
                    child.removeFromParent()
                }
            }
        }
    }

    fun validate (): Boolean {
        var bol = true
        if (taskInput.text == "")
            bol = false
        return bol
    }
}