commonMain.io.nacular.doodle.theme.basic.spinner.BasicMutableSpinnerBehavior.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of themes Show documentation
Show all versions of themes Show documentation
A pure Kotlin, UI framework for the Web and Desktop
The newest version!
package io.nacular.doodle.theme.basic.spinner
import io.nacular.doodle.controls.EditOperation
import io.nacular.doodle.controls.spinner.MutableSpinButton
import io.nacular.doodle.controls.spinner.MutableSpinButtonBehavior
import io.nacular.doodle.controls.spinner.MutableSpinButtonModel
import io.nacular.doodle.controls.spinner.SpinButton
import io.nacular.doodle.core.View
import io.nacular.doodle.drawing.Canvas
import io.nacular.doodle.drawing.Color
import io.nacular.doodle.drawing.TextMetrics
import io.nacular.doodle.event.PointerEvent
import io.nacular.doodle.event.PointerListener
import io.nacular.doodle.focus.FocusManager
import io.nacular.doodle.geometry.Point
import io.nacular.doodle.theme.basic.ColorMapper
import io.nacular.doodle.theme.basic.GenericTextEditOperation
import io.nacular.doodle.utils.Encoder
@Deprecated("Use BasicMutableSpinButtonBehavior", replaceWith = ReplaceWith("BasicMutableSpinButtonBehavior"))
public typealias BasicMutableSpinnerBehavior = BasicMutableSpinButtonBehavior
@Deprecated("Use SpinButtonTextEditOperation", replaceWith = ReplaceWith("SpinButtonTextEditOperation"))
public typealias SpinnerTextEditOperation = SpinButtonTextEditOperation
public class BasicMutableSpinButtonBehavior>(
textMetrics : TextMetrics,
backgroundColor : Color,
darkBackgroundColor: Color,
foregroundColor : Color,
cornerRadius : Double,
buttonWidth : Double,
focusManager : FocusManager? = null,
incrementLabel : String? = null,
decrementLabel : String? = null,
): MutableSpinButtonBehavior(), PointerListener {
private val delegate = BasicSpinButtonBehavior(
textMetrics,
backgroundColor,
darkBackgroundColor,
foregroundColor,
cornerRadius,
buttonWidth,
focusManager,
incrementLabel,
decrementLabel,
).apply {
centerChanged += { spinner, old, new ->
(spinner as? MutableSpinButton)?.let {
spinner.cancelEditing()
setupEditingTriggers(old, new)
}
}
}
public var hoverColorMapper : ColorMapper get() = delegate.hoverColorMapper; set(new) { delegate.hoverColorMapper = new }
public var disabledColorMapper: ColorMapper get() = delegate.disabledColorMapper; set(new) { delegate.disabledColorMapper = new }
override fun contains (view: SpinButton, point: Point): Boolean = delegate.contains(view, point)
override fun mirrorWhenRightToLeft(view: SpinButton ): Boolean = delegate.mirrorWhenRightToLeft(view)
override fun clipCanvasToBounds (view: SpinButton ): Boolean = delegate.clipCanvasToBounds(view)
override fun install (view: SpinButton ) { delegate.install(view) }
override fun uninstall(view: SpinButton): Unit = delegate.uninstall(view)
override fun render(view: SpinButton, canvas: Canvas) { delegate.render(view, canvas) }
override fun editingStarted(spinner: MutableSpinButton, value: T): EditOperation? {
val center = delegate.visualizedValue(spinner)
return spinner.editor?.edit(spinner, value, center!!)?.also { operation ->
operation()?.let { newCenter -> delegate.updateCenter(spinner, oldCenter = center, newCenter) }
}
}
override fun editingEnded(spinner: MutableSpinButton) {
delegate.updateCenter(spinner)
}
override fun changed(spinner: SpinButton): Unit = delegate.changed(spinner)
override fun pressed(event: PointerEvent) {
event.source.let { spinner ->
if (event.clickCount >= 2 && spinner is MutableSpinButton<*,*>) {
spinner.startEditing()
event.consume()
}
}
}
private fun setupEditingTriggers(old: View?, new: View) {
old?.pointerFilter?.minusAssign(this)
new.pointerFilter += this
}
}
public open class SpinButtonTextEditOperation(
focusManager: FocusManager?,
mapper : Encoder,
private val spinner : MutableSpinButton,
value : T,
current : View
): GenericTextEditOperation>(focusManager, mapper, spinner, value, current) {
private val changed = { _: SpinButton ->
spinner.cancelEditing()
}
init {
spinner.changed += changed
}
override fun cancel() {
spinner.changed -= changed
}
}