Scoop API

scoop:scoop.ui:0.1.0

src/application/scope.scoop

Selected production source · src/application/scope.scoop

1package scoop.ui
3import scoop.window.*
5public class ApplicationScope internal constructor(internal val root: UiApplicationRoot) {
6 private var active = true
7 internal val resources = ScopeResources()
8 internal val events =
9 ScopedNativeEvents(root.host, resources, { checkMounted() }, { action -> dispatch(action) })
11 public val application: ActiveApplication get() {
12 checkMounted();
13 return root.application
14 }
15 public val uiHost: UiHost get() {
16 checkMounted();
17 return root.host
18 }
19 public val isMounted: Boolean get() {
20 root.host.checkThread();
21 return active && root.isOpen
22 }
23 internal fun checkMounted() {
24 if (!isMounted)
25 throw IllegalStateException(Some("application scope is no longer mounted"))
26 }
27 public fun <R: Closeable> resource(create: () -> R): R {
28 checkMounted()
29 val value = create()
30 resources.cleanup.add { value.close() }
31 return value
32 }
33 public fun onDispose(action: () -> Unit) {
34 checkMounted()
35 resources.cleanup.add(action)
36 }
37 public fun dispatch(action: () -> Unit): Boolean {
38 if (!isMounted)
39 return false
40 return root.dispatch {
41 if (isMounted)
42 action()
43 } &&
44 isMounted
45 }
46 public fun observeWindow(window: Window, action: (WindowEvent) -> Unit): Closeable =
47 events.window(window, action)
48 internal fun deactivate() {
49 active = false
50 }
51 internal fun retireManaged(): Throwable? = resources.retireManaged()
52 internal fun release(): Throwable? = resources.release()