Scoop API

scoop:scoop.ui.component:0.1.0

platform/desktop/native/controllers/scope.scoop

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

1package scoop.ui.component
3import scoop.ui.*
4import scoop.window.*
6/** The two existing boundaries retain their concrete scope for typed session observation. */
7internal enum NativeControllerScope {
8 Window(ComponentScope), Application(ApplicationScope)
9}
11internal val NativeControllerScope.application: ActiveApplication get() =
12 when (this) {
13 case Window(val scope) -> scope.application
14 case Application(val scope) -> scope.application
15 }
17internal val NativeControllerScope.host: UiHost get() =
18 when (this) {
19 case Window(val scope) -> scope.uiHost
20 case Application(val scope) -> scope.uiHost
21 }
23internal val NativeControllerScope.mounted: Boolean get() =
24 when (this) {
25 case Window(val scope) -> scope.isMounted
26 case Application(val scope) -> scope.isMounted
27 }
29internal 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 }
35internal fun NativeControllerScope.observeWindow(
36 window: Window,
37 action: (WindowEvent) -> Unit
38): Closeable =
39 when (this) {
40 case Window(val scope) -> scope.observeWindow(window, action)
41 case Application(val scope) -> scope.observeWindow(window, action)
42 }
44internal fun NativeControllerScope.observeModal(
45 session: ModalSession,
46 action: (ModalResult) -> Unit
47): Closeable =
48 when (this) {
49 case Window(val scope) -> scope.observeModal(session, action)
50 case Application(val scope) -> scope.observeModal(session, action)
51 }
53internal fun <T> NativeControllerScope.observeFile(
54 session: FileDialogSession<T>,
55 action: (FileDialogResult<T>) -> Unit
56): Closeable =
57 when (this) {
58 case Window(val scope) -> scope.observeFileDialog(session, action)
59 case Application(val scope) -> scope.observeFileDialog(session, action)
60 }
62private class NativeRequestResult {
63 var accepted = false
66internal fun NativeControllerScope.request(action: () -> Boolean): Boolean {
67 if (!mounted)
68 return false
69 val result = NativeRequestResult()
70 return dispatch { result.accepted = action() } && result.accepted
73internal fun cleanupNative(previous: Throwable?, action: () -> Unit): Throwable? =
74 try {
75 action();
76 previous
77 }
78 catch (error: Throwable) {
79 when (previous) {
80 case Some(val first) -> Some(ResourceUseException(first, error));
81 case None -> Some(error)
82 }
83 }
85internal fun throwNative(error: Throwable?) {
86 when (error) {
87 case Some(val value) -> throw value;
88 case None -> {}
89 }