Scoop API

scoop:scoop.window:0.1.0

src/dialogs/lifecycle.scoop

Selected production source · src/dialogs/lifecycle.scoop

1package scoop.window
3import scoop.platform.fs.Path
5internal fun ApplicationOwner.requireDialogOwnerAvailable(window: WindowOwner) {
6 for (session in fileDialogs)
7 if (session.blocks(window))
8 throw WindowException("dialog owner is busy", WindowErrorKind.ResourceBusy)
9}
11internal fun ApplicationOwner.cancelOwnedFileDialogs(
12 window: WindowOwner,
13 reason: FileDialogCancelReason
14) {
15 for (session in fileDialogs)
16 if (session.owns(window))
17 session.cancel(reason)
20internal fun ApplicationOwner.syncFileDialogVisibility() {
21 for (session in fileDialogs) {
22 val window = session.window ?: continue
23 if (window.phase != WindowState.Open && window.phase != WindowState.PendingConfigure)
24 continue
25 val snapshot = window.snapshot()
26 if (
27 !window.requestedVisible ||
28 window.visibilitySuppressed ||
29 snapshot.visible ==
30 0 ||
31 snapshot.minimized !=
32 0
33 )
34 session.cancel(FileDialogCancelReason.OwnerHidden)
35 }
38internal fun ApplicationOwner.drainFileDialogs() {
39 var index = fileDialogs.size
40 while (index > 0L) {
41 index -= 1L
42 val session = fileDialogs[index]
43 if (session.commandPending)
44 continue
45 @Unsafe
46 {
47 val native = nativeFileResult(session.pointer!!)
48 if (native.outcome == 0)
49 continue
50 val result =
51 try {
52 readFileDialogResult(session, native)
53 } catch (error: WindowException) {
54 FileDialogResult.Failed<Array<Path>>(error)
55 } catch (error: Throwable) {
56 recordFailure(error)
57 beginExit()
58 FileDialogResult.Failed<Array<Path>>(
59 WindowException("decode file dialog result", WindowErrorKind.NativeFailure)
60 )
61 }
62 nativeFileRelease(session.pointer!!)
63 session.pointer = None
64 session.completion = Some(result)
65 session.phase = FileDialogState.Completed
66 fileDialogs.removeAt(index)
67 when (session.window) {
68 case Some(val window) -> window.leases -= 1L
69 case None -> {}
70 }
71 }
72 updateModalInput()
73 if (!exiting) {
74 when (session.window) {
75 case Some(val window) -> if (
76 window.phase ==
77 WindowState.Open &&
78 !window.inputBlocked &&
79 !window.visibilitySuppressed &&
80 (!window.hasModal || window.modalSession!!.phase == ModalState.Showing)
81 ) {
82 val snapshot = window.snapshot()
83 if (snapshot.visible != 0 && snapshot.minimized == 0)
84 Window(window).requestFocus()
85 }
86 case None -> {}
87 }
88 deliver(ApplicationEvent.FileDialogCompleted(session.id))
89 }
90 }
93@Unsafe
94private fun readFileDialogResult(
95 session: FileDialogOwner,
96 native: NativeFileResult
97): FileDialogResult<Array<Path>> {
98 when (session.cancellation) {
99 case Some(val reason) -> return FileDialogResult.Cancelled<Array<Path>>(reason)
100 case None -> {}
101 }
102 if (native.outcome == 2)
103 return FileDialogResult.Cancelled<Array<Path>>(FileDialogCancelReason.UserCancelled)
104 if (native.outcome == 3) {
105 val domain =
106 when (native.domain) {
107 case Some(val value) -> Some(String.fromUtf8(value, native.domainLength))
108 case None -> None
109 }
110 val kind =
111 if (domain != None)
112 WindowErrorKind.NativeFailure
113 else
114 when (native.error) {
115 -1L -> WindowErrorKind.Unsupported
116 -2L -> WindowErrorKind.Unavailable
117 -3L -> WindowErrorKind.Disconnected
118 -4L -> WindowErrorKind.InvalidState
119 -5L -> WindowErrorKind.ResourceBusy
120 else -> WindowErrorKind.NativeFailure
121 }
122 return FileDialogResult.Failed<Array<Path>>(
123 WindowException(
124 "file dialog",
125 kind,
126 if (kind == WindowErrorKind.NativeFailure)
127 Some(native.error)
128 else
129 None,
130 domain
131 )
132 )
133 }
134 if (native.outcome != 1 || native.count < 1L || (session.kind != 1 && native.count != 1L))
135 return FileDialogResult.Failed<Array<Path>>(
136 WindowException("file dialog result", WindowErrorKind.NativeFailure)
137 )
138 return FileDialogResult.Accepted<Array<Path>>(
139 Array<Path>(native.count) {
140 index -> @Unsafe
141 {
142 val path = nativeFilePath(session.pointer!!, index)
143 if (path.data == None || path.length <= 0L)
144 throw WindowException("native file path", WindowErrorKind.NativeFailure)
145 return@Array Path.fromOsString(copyDialogString(path))
146 }
147 }
148 )