Scoop API

scoop:scoop.ui:0.1.0

platform/desktop/scoped_events.scoop

Selected production source · platform/desktop/scoped_events.scoop

1package scoop.ui
3import scoop.window.*
5private 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 true
12 )
13 )
14 registration.deliver(value)
15 return registration
17internal 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 )
25internal 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 )
37internal 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 )
49internal 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 )
61internal 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 registration