All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.nacular.doodle.theme.native.NativeFileSelectorBehavior.kt Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
package io.nacular.doodle.theme.native

import io.nacular.doodle.controls.files.FileSelector
import io.nacular.doodle.controls.files.FileSelectorBehavior
import io.nacular.doodle.core.Behavior
import io.nacular.doodle.drawing.Canvas
import io.nacular.doodle.drawing.impl.NativeFileSelectorFactory
import io.nacular.doodle.geometry.Size
import io.nacular.doodle.system.Cursor

internal class NativeFileSelectorStylerImpl(private val nativeFileSelectorFactory: NativeFileSelectorFactory):
    NativeFileSelectorStyler {
    override fun invoke(fileSelector: FileSelector, behavior: Behavior): Behavior = NativeFileSelectorBehaviorWrapper(
        nativeFileSelectorFactory,
        fileSelector,
        behavior
    )
}

private class NativeFileSelectorBehaviorWrapper(
    nativeFileSelectorFactory: NativeFileSelectorFactory,
    fileSelector             : FileSelector,
    private val delegate   : Behavior): FileSelectorBehavior, Behavior by delegate {

    private val nativePeer by lazy {
        nativeFileSelectorFactory(
            fileSelector,
            customRenderer = { selector, canvas -> delegate.render(selector, canvas) },
            delegate.clipCanvasToBounds(fileSelector)
        ) {
            fileSelector.files = it
        }
    }

    override fun render(view: FileSelector, canvas: Canvas) {
        nativePeer.render(canvas)
    }
}

internal class NativeFileSelectorBehavior(nativeFileSelectorFactory: NativeFileSelectorFactory, fileSelector: FileSelector): FileSelectorBehavior {
    private val nativePeer by lazy {
        nativeFileSelectorFactory(fileSelector) {
            fileSelector.files = it
        }
    }

    private var oldCursor   : Cursor? = null
    private var oldIdealSize: Size? = null

    override fun render(view: FileSelector, canvas: Canvas) {
        nativePeer.render(canvas)
    }

    override fun mirrorWhenRightToLeft(view: FileSelector) = false

    override fun install(view: FileSelector) {
        super.install(view)

        view.apply {
            cursor    = Cursor.Default
            idealSize = nativePeer.idealSize

            rerender()
        }
    }

    override fun uninstall(view: FileSelector) {
        super.uninstall(view)

        nativePeer.discard()

        view.apply {
            cursor    = oldCursor
            idealSize = oldIdealSize
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy