scoop:scoop.ui.component:0.1.0
platform/desktop/native/controllers/dialog.scoop
Selected production source · platform/desktop/native/controllers/dialog.scoop
1
package scoop.ui.component3
import scoop.graphics.text.FontCollection4
import scoop.ui.*5
import scoop.window.*6
import scoop.window.ModalOptions as WindowModalOptions7
import scoop.window.copy as copyWindowAttributes9
public enum NativeDialogResult<T> {10
Accepted(T), Cancelled(ModalCancelReason), Failed(WindowException)11
}13
/** Refers to one presentation; retained replies never target a later dialog. */14
public class NativeDialogResponse<T> internal constructor(15
private val presentation: NativeDialogPresentation<T>16
) {17
public val window: Window get() = presentation.owned.window18
public fun accept(value: T): Boolean = presentation.accept(value)19
public fun cancel() {20
presentation.cancel()21
}22
}24
public class NativeDialogController<T> internal constructor(25
private val scope: NativeControllerScope,26
private val target: DialogTarget27
): Closeable {28
private var current: NativeDialogPresentation<T>? = None29
private val pending = state(false)30
private var closed = false31
public val isPending: Boolean get() = pending.value32
public val window: Window? get() {33
pending.value;34
return current?.owned?.window35
}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? = None44
): Boolean =45
scope.request {46
if (closed || pending.value)47
return@request false48
val relation =49
when (target) {50
case Owner(val window) -> WindowRelation.Owned(window)51
case Application -> WindowRelation.Independent52
}53
val modal =54
options ?:55
WindowModalOptions(56
scope =57
when (target) {58
case Owner(_) -> ModalScope.Owner;59
case Application -> ModalScope.Application60
}61
)62
val window =63
try {64
scope.application.createWindow(65
attributes.copyWindowAttributes {66
this.relation = relation67
this.visible = false68
this.requestActivation = false69
}70
)71
} catch (error: WindowException) {72
onResult(NativeDialogResult.Failed<T>(error))73
return@request true74
}75
val owned = NativeOwnedWindow(window)76
val presentation =77
NativeDialogPresentation<T>(scope, owned) {78
result ->79
current = None80
pending.value = false81
if (!closed)82
onResult(result)83
}84
current = Some(presentation)85
pending.value = true86
var nativeFailure: WindowException? = None87
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 = None102
pending.value = false103
presentation.close()104
return@request false105
}106
case Some(_) -> {}107
}108
if (closed || !scope.mounted) {109
presentation.close()110
return@request false111
}112
val session =113
try {114
Some(window.showModal(modal))115
} catch (error: WindowException) {116
nativeFailure = Some(error)117
None118
}119
when (session) {120
case Some(val active) -> presentation.start(active)121
case None -> Unit122
}123
} catch (error: Throwable) {124
current = None125
pending.value = false126
throwNative(cleanupNative(Some(error)) { presentation.close() })127
return@request false128
}129
when (nativeFailure) {130
case Some(val error) -> {131
current = None132
pending.value = false133
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 -> Unit142
}143
true144
}145
public fun cancel() {146
if (!closed)147
current?.cancel()148
}149
public override fun close() {150
scope.mounted151
if (closed)152
return153
closed = true154
val presentation = current155
current = None156
pending.value = false157
presentation?.close()158
}159
}161
public fun <T> ComponentScope.nativeDialogController(target: DialogTarget): NativeDialogController<162
T163
> = resource { NativeDialogController<T>(NativeControllerScope.Window(this), target) }165
public fun <T> ApplicationScope.nativeDialogController(166
target: DialogTarget167
): NativeDialogController<T> =168
resource { NativeDialogController<T>(NativeControllerScope.Application(this), target) }