lightningkite / koolui

Kotlin cross-platform UI
48 stars 3 forks source link

KoolUI

By Lightning Kite

Maven: Download

Abstracts view creation for multiplatform projects, allowing you to write a short description of your UI and have it be implemented on all platforms according to how the given platform renders it.

Video by Imgur

Design Philosophy

Concepts

To Do / Help Requests

Gradle Inclusion

repositories {
    maven { url 'https://dl.bintray.com/lightningkite/com.lightningkite.krosslin' }
    ...
}
...
dependencies {
    ...
    //Depending on the version you need
    api "com.lightningkite:koolui-metadata:${kooluiVersion}"
    api "com.lightningkite:koolui-jvmvirtual:${kooluiVersion}"
    api "com.lightningkite:koolui-javafx:${kooluiVersion}"
    api "com.lightningkite:koolui-android:${kooluiVersion}"
    api "com.lightningkite:koolui-js:${kooluiVersion}"
}

Suggested Platform Setup

Multiplatform

Define how your views work on this platform:

//settings.kt

interface MyViewFactory<VIEW> : ViewFactory<VIEW> {}
typealias MyViewGenerator<VIEW> = ViewGenerator<MyViewFactory<VIEW>, VIEW>

For your root view, create a ViewGenerator like this:

class MainVG<VIEW>() : ViewGenerator<ViewFactory<VIEW>, VIEW> {
    override val title: String = "KotlinX UI Test"

    //This will be our navigation stack.
    val stack = WrapperObservableList<ViewGenerator<ViewFactory<VIEW>, VIEW>>()

    init {
        //On start, add a different view generator to the stack
        stack.push(SelectorVG(stack))
    }

    //The view for this ViewGenerator is created here:
    override fun generate(dependency: ViewFactory<VIEW>): VIEW = with(dependency) {
        window(
                dependency = dependency,
                stack = stack,
                tabs = listOf()
        )
    }
}

Android Setup

Use this code for your main activity:

class MainActivity : AccessibleActivity() {

    companion object {
        val main = MainVG<View>()
    }

    class Factory(
            activity: AccessibleActivity
    ) : MyViewFactory<View>, ViewFactory<View> by AndroidMaterialViewFactory(activity, Theme.dark()) {}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        ApplicationAccess.init(this, R.drawable.ic_notifications)

        setContentView(Factory(this).contentRoot(main))
    }
}

JS Setup

Add this to your main file:

class Factory() : MyViewFactory<HTMLElement>, ViewFactory<HTMLElement> by HtmlViewFactory(Theme.dark()) {}

fun main(args: Array<String>) {
    window.onload = {
        document.body!!.appendChild(
                Factory().contentRoot(MainVG<HTMLElement>())
        )
    }
}

JavaFX Setup

Use this for your application:

class Main : Application() {

    companion object {
        val mainVg = MainVG<Node>()
    }

    class Factory() : MyViewFactory<Node>, ViewFactory<Node> by MaterialJavaFxViewFactory(Theme.dark(), scale = 1.0) {}

    override fun start(primaryStage: Stage) {
        ApplicationAccess.init(Main::class.java.classLoader, primaryStage)
        primaryStage.scene = Scene(Factory().contentRoot(mainVg) as Parent)
        primaryStage.show()
    }
}

fun main(vararg args: String) {
    Application.launch(Main::class.java)
}