scoop:scoop.ui.component:0.1.0
platform/desktop/native/controllers/scope.scoop
Selected production source · platform/desktop/native/controllers/scope.scoop
1
package scoop.ui.component3
import scoop.ui.*4
import scoop.window.*6
/** The two existing boundaries retain their concrete scope for typed session observation. */7
internal enum NativeControllerScope {8
Window(ComponentScope), Application(ApplicationScope)9
}11
internal val NativeControllerScope.application: ActiveApplication get() =12
when (this) {13
case Window(val scope) -> scope.application14
case Application(val scope) -> scope.application15
}17
internal val NativeControllerScope.host: UiHost get() =18
when (this) {19
case Window(val scope) -> scope.uiHost20
case Application(val scope) -> scope.uiHost21
}23
internal val NativeControllerScope.mounted: Boolean get() =24
when (this) {25
case Window(val scope) -> scope.isMounted26
case Application(val scope) -> scope.isMounted27
}29
internal fun NativeControllerScope.dispatch(action: () -> Unit): Boolean =30
when (this) {31
case Window(val scope) -> scope.dispatch(action)32
case Application(val scope) -> scope.dispatch(action)33
}35
internal fun NativeControllerScope.observeWindow(36
window: Window,37
action: (WindowEvent) -> Unit38
): Closeable =39
when (this) {40
case Window(val scope) -> scope.observeWindow(window, action)41
case Application(val scope) -> scope.observeWindow(window, action)42
}44
internal fun NativeControllerScope.observeModal(45
session: ModalSession,46
action: (ModalResult) -> Unit47
): Closeable =48
when (this) {49
case Window(val scope) -> scope.observeModal(session, action)50
case Application(val scope) -> scope.observeModal(session, action)51
}53
internal fun <T> NativeControllerScope.observeFile(54
session: FileDialogSession<T>,55
action: (FileDialogResult<T>) -> Unit56
): Closeable =57
when (this) {58
case Window(val scope) -> scope.observeFileDialog(session, action)59
case Application(val scope) -> scope.observeFileDialog(session, action)60
}62
private class NativeRequestResult {63
var accepted = false64
}66
internal fun NativeControllerScope.request(action: () -> Boolean): Boolean {67
if (!mounted)68
return false69
val result = NativeRequestResult()70
return dispatch { result.accepted = action() } && result.accepted71
}73
internal fun cleanupNative(previous: Throwable?, action: () -> Unit): Throwable? =74
try {75
action();76
previous77
}78
catch (error: Throwable) {79
when (previous) {80
case Some(val first) -> Some(ResourceUseException(first, error));81
case None -> Some(error)82
}83
}85
internal fun throwNative(error: Throwable?) {86
when (error) {87
case Some(val value) -> throw value;88
case None -> {}89
}90
}