scoop:scoop.ui:0.1.0
platform/desktop/scoped_events.scoop
Selected production source · platform/desktop/scoped_events.scoop
1
package scoop.ui3
import scoop.window.*5
private fun <E> ScopedNativeEvents.completed(value: E, action: (E) -> Unit): Closeable {6
val registration =7
own(8
UiEventRegistration<E>(9
{ host.checkThread() },10
{ event -> dispatch { action(event) } },11
true12
)13
)14
registration.deliver(value)15
return registration16
}17
internal fun ScopedNativeEvents.menu(menu: Menu, action: (MenuActivation) -> Unit): Closeable {18
checkMounted()19
return own(20
host.nativeEvents.menus.subscribe(menu.id, false) {21
value -> dispatch { action(value) }22
}23
)24
}25
internal fun ScopedNativeEvents.popup(session: PopupSession, action: (PopupResult) -> Unit): Closeable {26
checkMounted()27
when (session.result) {28
case Some(val value) -> return completed(value, action)29
case None -> {}30
}31
return own(32
host.nativeEvents.popups.subscribe(session.id, true) {33
value -> dispatch { action(value) }34
}35
)36
}37
internal fun ScopedNativeEvents.modal(session: ModalSession, action: (ModalResult) -> Unit): Closeable {38
checkMounted()39
when (session.result) {40
case Some(val value) -> return completed(value, action)41
case None -> {}42
}43
return own(44
host.nativeEvents.modals.subscribe(session.id, true) {45
value -> dispatch { action(value) }46
}47
)48
}49
internal fun <T> ScopedNativeEvents.file(session: FileDialogSession<T>, action: (FileDialogResult<T>) -> Unit): Closeable {50
checkMounted()51
when (session.result) {52
case Some(val value) -> return completed(value, action)53
case None -> {}54
}55
return own(56
host.nativeEvents.files.subscribe(session.id, true) {57
_ -> dispatch { action(session.result!!) }58
}59
)60
}61
internal fun ScopedNativeEvents.frame(window: Window, action: (WindowFrameMetrics?) -> Unit): Closeable {62
checkMounted()63
val registration =64
own(65
host.frameEvents.subscribe(window.id, false) {66
_ -> dispatch { action(window.frameMetrics) }67
}68
)69
registration.deliver(Unit)70
return registration71
}