scoop:scoop.ui.component:0.1.0
src/carousel/handle.scoop
Selected production source · src/carousel/handle.scoop
1
package scoop.ui.component3
import scoop.platform.concurrent.currentThreadId4
import scoop.ui.*6
public class CarouselHandle public constructor() {7
private val thread = currentThreadId()8
private var target: CarouselControl? = None9
private val value = state(CarouselSnapshot())10
private fun checkThread() {11
if (currentThreadId() != thread)12
throw IllegalStateException(Some("carousel handle used from another thread"))13
}14
public val isAttached: Boolean get() {15
checkThread();16
return target?.active ?: false17
}18
public val snapshot: CarouselSnapshot get() {19
checkThread();20
return value.value21
}22
public val position: Double get() {23
checkThread();24
return target?.position ?: 0.025
}26
public val snapPositions: Array<Double> get() {27
checkThread();28
return target?.snaps ?: []29
}30
public fun scrollTo(index: Long, animated: Boolean = true): Boolean {31
checkThread();32
return target?.select(index, animated) ?: false33
}34
public fun previous(animated: Boolean = true): Boolean {35
checkThread();36
return target?.step(false, animated) ?: false37
}38
public fun next(animated: Boolean = true): Boolean {39
checkThread();40
return target?.step(true, animated) ?: false41
}42
internal fun wheel(amount: Double): Double {43
checkThread()44
return when (target) {45
case Some(val control) -> carouselWheel(control, amount)46
case None -> 0.047
}48
}49
internal fun finishWheel() {50
checkThread()51
when (target) {52
case Some(val control) -> carouselFinishWheel(control)53
case None -> {}54
}55
}56
internal fun bind(control: CarouselControl) {57
checkThread()58
when (target) {59
case Some(val old) -> requireComponent(60
old === control || !old.active,61
"carousel handle already has a target"62
)63
case None -> {}64
}65
target = Some(control);66
publish(CarouselSnapshot())67
}68
internal fun unbind(control: CarouselControl) {69
checkThread()70
when (target) {71
case Some(val old) -> if (old === control) {72
target = None;73
publish(CarouselSnapshot())74
}75
case None -> {}76
}77
}78
internal fun publish(snapshot: CarouselSnapshot) {79
if (value.value != snapshot)80
value.value = snapshot81
}82
}