Scoop API

scoop:scoop.ui.component:0.1.0

src/drawer/handle.scoop

Selected production source · src/drawer/handle.scoop

1package scoop.ui.component
3import scoop.platform.concurrent.currentThreadId
4import scoop.ui.*
6public class DrawerHandle public constructor() {
7 private val thread = currentThreadId()
8 private var current: DrawerControl? = None
9 private val value = state(DrawerSnapshot())
10 private fun checkThread() {
11 if (currentThreadId() != thread)
12 throw IllegalStateException(Some("drawer handle used from another thread"))
13 }
14 internal val target: DrawerControl? get() {
15 checkThread();
16 return current
17 }
18 public val isAttached: Boolean get() {
19 checkThread();
20 return current?.active ?: false
21 }
22 public val snapshot: DrawerSnapshot get() {
23 checkThread();
24 return value.value
25 }
26 public fun snapTo(index: Long, animated: Boolean = true): Boolean {
27 checkThread();
28 return current?.select(index, animated) ?: false
29 }
30 internal fun bind(control: DrawerControl) {
31 checkThread()
32 when (current) {
33 case Some(val old) -> requireComponent(
34 old === control || !old.active,
35 "drawer handle already has a target"
36 )
37 case None -> {}
38 }
39 current = Some(control)
40 }
41 internal fun unbind(control: DrawerControl) {
42 checkThread()
43 when (current) {
44 case Some(val old) -> if (old === control) {
45 current = None;
46 publish(DrawerSnapshot())
47 }
48 case None -> {}
49 }
50 }
51 internal fun publish(snapshot: DrawerSnapshot) {
52 if (value.value != snapshot)
53 value.value = snapshot
54 }