scoop:scoop.window:0.1.0
src/dialogs/lifecycle.scoop
Selected production source · src/dialogs/lifecycle.scoop
1
package scoop.window3
import scoop.platform.fs.Path5
internal 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
}11
internal fun ApplicationOwner.cancelOwnedFileDialogs(12
window: WindowOwner,13
reason: FileDialogCancelReason14
) {15
for (session in fileDialogs)16
if (session.owns(window))17
session.cancel(reason)18
}20
internal fun ApplicationOwner.syncFileDialogVisibility() {21
for (session in fileDialogs) {22
val window = session.window ?: continue23
if (window.phase != WindowState.Open && window.phase != WindowState.PendingConfigure)24
continue25
val snapshot = window.snapshot()26
if (27
!window.requestedVisible ||28
window.visibilitySuppressed ||29
snapshot.visible ==30
0 ||31
snapshot.minimized !=32
033
)34
session.cancel(FileDialogCancelReason.OwnerHidden)35
}36
}38
internal fun ApplicationOwner.drainFileDialogs() {39
var index = fileDialogs.size40
while (index > 0L) {41
index -= 1L42
val session = fileDialogs[index]43
if (session.commandPending)44
continue45
@Unsafe46
{47
val native = nativeFileResult(session.pointer!!)48
if (native.outcome == 0)49
continue50
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 = None64
session.completion = Some(result)65
session.phase = FileDialogState.Completed66
fileDialogs.removeAt(index)67
when (session.window) {68
case Some(val window) -> window.leases -= 1L69
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
}91
}93
@Unsafe94
private fun readFileDialogResult(95
session: FileDialogOwner,96
native: NativeFileResult97
): 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 -> None109
}110
val kind =111
if (domain != None)112
WindowErrorKind.NativeFailure113
else114
when (native.error) {115
-1L -> WindowErrorKind.Unsupported116
-2L -> WindowErrorKind.Unavailable117
-3L -> WindowErrorKind.Disconnected118
-4L -> WindowErrorKind.InvalidState119
-5L -> WindowErrorKind.ResourceBusy120
else -> WindowErrorKind.NativeFailure121
}122
return FileDialogResult.Failed<Array<Path>>(123
WindowException(124
"file dialog",125
kind,126
if (kind == WindowErrorKind.NativeFailure)127
Some(native.error)128
else129
None,130
domain131
)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 -> @Unsafe141
{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
)149
}