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.
ViewGenerator
- a plain Kotlin object that contains instructions for rendering a view.ViewFactory
- An interface for creating views for a particular platform. The views returned could be of any type, so don't expect to interact with them after creation. You will probably extend this interface for your particular application to add new view types as necessary. Concrete implementations of the base one already exist for each platform.Theme
- A general color theme to use in the app. Frequently passed in to concrete implementations of ViewFactory
to give it a color scheme.ObservableProperty
- a property you can observeMutableObservableProperty
- a property you can observe and change(Mutable)ObservableList
- a list you can observe, frequently used to keep track of navigation stacksrepositories {
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}"
}
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()
)
}
}
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))
}
}
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>())
)
}
}
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)
}