Scoop API

scoop:scoop.ui.component:0.1.0

platform/desktop/native/controllers/dialog.scoop

Selected production source · platform/desktop/native/controllers/dialog.scoop

1package scoop.ui.component
3import scoop.graphics.text.FontCollection
4import scoop.ui.*
5import scoop.window.*
6import scoop.window.ModalOptions as WindowModalOptions
7import scoop.window.copy as copyWindowAttributes
9public enum NativeDialogResult<T> {
10 Accepted(T), Cancelled(ModalCancelReason), Failed(WindowException)
13/** Refers to one presentation; retained replies never target a later dialog. */
14public class NativeDialogResponse<T> internal constructor(
15 private val presentation: NativeDialogPresentation<T>
16) {
17 public val window: Window get() = presentation.owned.window
18 public fun accept(value: T): Boolean = presentation.accept(value)
19 public fun cancel() {
20 presentation.cancel()
21 }
24public class NativeDialogController<T> internal constructor(
25 private val scope: NativeControllerScope,
26 private val target: DialogTarget
27): Closeable {
28 private var current: NativeDialogPresentation<T>? = None
29 private val pending = state(false)
30 private var closed = false
31 public val isPending: Boolean get() = pending.value
32 public val window: Window? get() {
33 pending.value;
34 return current?.owned?.window
35 }
37 /** Native creation or presentation failures return Failed without retiring the caller. */
38 public fun <C: Component> open(
39 create: (NativeDialogResponse<T>, ComponentScope) -> C,
40 onResult: (NativeDialogResult<T>) -> Unit,
41 attributes: WindowAttributes = windowAttributes(),
42 options: WindowModalOptions? = None,
43 fonts: FontCollection? = None
44 ): Boolean =
45 scope.request {
46 if (closed || pending.value)
47 return@request false
48 val relation =
49 when (target) {
50 case Owner(val window) -> WindowRelation.Owned(window)
51 case Application -> WindowRelation.Independent
52 }
53 val modal =
54 options ?:
55 WindowModalOptions(
56 scope =
57 when (target) {
58 case Owner(_) -> ModalScope.Owner;
59 case Application -> ModalScope.Application
60 }
61 )
62 val window =
63 try {
64 scope.application.createWindow(
65 attributes.copyWindowAttributes {
66 this.relation = relation
67 this.visible = false
68 this.requestActivation = false
69 }
70 )
71 } catch (error: WindowException) {
72 onResult(NativeDialogResult.Failed<T>(error))
73 return@request true
74 }
75 val owned = NativeOwnedWindow(window)
76 val presentation =
77 NativeDialogPresentation<T>(scope, owned) {
78 result ->
79 current = None
80 pending.value = false
81 if (!closed)
82 onResult(result)
83 }
84 current = Some(presentation)
85 pending.value = true
86 var nativeFailure: WindowException? = None
87 try {
88 val response = NativeDialogResponse(presentation)
89 owned.events =
90 Some(
91 scope.observeWindow(window) {
92 event -> when (event) {
93 case CloseRequested -> presentation.cancel();
94 else -> {}
95 }
96 }
97 )
98 owned.ui = scope.host.attach(window, { mount -> create(response, mount) }, fonts)
99 when (owned.ui) {
100 case None -> {
101 current = None
102 pending.value = false
103 presentation.close()
104 return@request false
105 }
106 case Some(_) -> {}
107 }
108 if (closed || !scope.mounted) {
109 presentation.close()
110 return@request false
111 }
112 val session =
113 try {
114 Some(window.showModal(modal))
115 } catch (error: WindowException) {
116 nativeFailure = Some(error)
117 None
118 }
119 when (session) {
120 case Some(val active) -> presentation.start(active)
121 case None -> Unit
122 }
123 } catch (error: Throwable) {
124 current = None
125 pending.value = false
126 throwNative(cleanupNative(Some(error)) { presentation.close() })
127 return@request false
128 }
129 when (nativeFailure) {
130 case Some(val error) -> {
131 current = None
132 pending.value = false
133 try {
134 presentation.close()
135 } catch (cleanup: Throwable) {
136 throw ResourceUseException(error, cleanup)
137 }
138 if (!closed)
139 onResult(NativeDialogResult.Failed<T>(error))
140 }
141 case None -> Unit
142 }
143 true
144 }
145 public fun cancel() {
146 if (!closed)
147 current?.cancel()
148 }
149 public override fun close() {
150 scope.mounted
151 if (closed)
152 return
153 closed = true
154 val presentation = current
155 current = None
156 pending.value = false
157 presentation?.close()
158 }
161public fun <T> ComponentScope.nativeDialogController(target: DialogTarget): NativeDialogController<
162 T
163> = resource { NativeDialogController<T>(NativeControllerScope.Window(this), target) }
165public fun <T> ApplicationScope.nativeDialogController(
166 target: DialogTarget
167): NativeDialogController<T> =
168 resource { NativeDialogController<T>(NativeControllerScope.Application(this), target) }