scoop:scoop.window:0.1.0
src/dialogs/creation.scoop
Selected production source · src/dialogs/creation.scoop
1
package scoop.window3
import scoop.platform.fs.Path4
import scoop.platform.system.OsString6
public fun ActiveApplication.openFile(7
target: DialogTarget,8
options: OpenFileOptions = OpenFileOptions()9
): FileDialogSession<Path> =10
FileDialogSession(11
owner.createFileDialog(12
target,13
0,14
options.title,15
options.initialDirectory,16
None,17
options.types18
)19
) { paths -> paths[0L] }21
public fun ActiveApplication.openFiles(22
target: DialogTarget,23
options: OpenFileOptions = OpenFileOptions()24
): FileDialogSession<Array<Path>> =25
FileDialogSession(26
owner.createFileDialog(27
target,28
1,29
options.title,30
options.initialDirectory,31
None,32
options.types33
)34
) { paths -> paths }36
public fun ActiveApplication.saveFile(37
target: DialogTarget,38
options: SaveFileOptions = SaveFileOptions()39
): FileDialogSession<Path> =40
FileDialogSession(41
owner.createFileDialog(42
target,43
2,44
options.title,45
options.initialDirectory,46
options.suggestedName,47
options.types48
)49
) { paths -> paths[0L] }51
public fun ActiveApplication.selectDirectory(52
target: DialogTarget,53
options: DirectoryDialogOptions = DirectoryDialogOptions()54
): FileDialogSession<Path> =55
FileDialogSession(56
owner.createFileDialog(57
target,58
3,59
options.title,60
options.initialDirectory,61
None,62
FileTypes.Any63
)64
) { paths -> paths[0L] }66
private fun ApplicationOwner.createFileDialog(67
target: DialogTarget,68
kind: Int,69
title: String?,70
directory: Path?,71
name: OsString?,72
types: FileTypes73
): FileDialogOwner {74
requireRunning("file dialog")75
val parent =76
when (target) {77
case Owner(val value) -> {78
if (!(value.owner.application === this))79
throw IllegalArgumentException(80
Some("dialog owner belongs to another application")81
)82
Some(value.owner)83
}84
case Application -> None85
}86
when (parent) {87
case Some(val value) -> {88
requireDialogOwnerAvailable(value)89
for (session in modals)90
if (session.window.owner.isOwnedBy(value))91
throw WindowException("dialog owner is busy", WindowErrorKind.ResourceBusy)92
value.requireInteractive()93
val snapshot = value.snapshot()94
if (snapshot.visible == 0 || snapshot.minimized != 0)95
throw WindowException("dialog owner visibility", WindowErrorKind.InvalidState)96
}97
case None -> if (fileDialogs.size != 0L)98
throw WindowException("application file dialog is busy", WindowErrorKind.ResourceBusy)99
}100
when (title) {101
case Some(val value) -> for (character in value)102
requireWindow(character.code != 0, "dialog title contains NUL")103
case None -> {}104
}105
when (name) {106
case Some(val value) -> requireWindow(107
Path.fromOsString(value).fileName == Some(value),108
"suggested name must be a nonempty file name"109
)110
case None -> {}111
}112
val extensions =113
when (types) {114
case Any -> Array<String>(0L) { _ -> "" }115
case Extensions(val values) -> {116
requireWindow(values.size > 0L, "file extensions cannot be empty")117
for (value in values) {118
requireWindow(119
value.charLength > 0L && value.getCharAt(0L) != '.',120
"invalid file extension"121
)122
for (character in value)123
requireWindow(124
character.code !=125
0 &&126
character !=127
'/' &&128
character !=129
'\\' &&130
character !=131
'*' &&132
character !=133
'?' &&134
character !=135
';' &&136
character !=137
'[' &&138
character !=139
']',140
"invalid file extension"141
)142
}143
Array<String>(values.size) { index -> values[index] }144
}145
}146
if (nextFileDialog == Long.MAX_VALUE)147
throw WindowException("file dialog identity", WindowErrorKind.NativeFailure)148
@Unsafe149
{150
val native =151
created(152
nativeFileCreate(this.native!!, parent?.pointer ?: None, kind),153
"prepare file dialog"154
)155
try {156
when (title) {157
case Some(val value) -> setFileText(native, 0, value)158
case None -> {}159
}160
when (directory) {161
case Some(val value) -> setFilePathOption(native, 1, value.toOsString())162
case None -> {}163
}164
when (name) {165
case Some(val value) -> setFilePathOption(native, 2, value)166
case None -> {}167
}168
for (extension in extensions)169
setFileText(native, 3, extension)170
} catch (error: Throwable) {171
nativeFileRelease(native)172
throw error173
}174
val session =175
FileDialogOwner(this, FileDialogSessionId(nextFileDialog), parent, kind, native)176
nextFileDialog += 1L177
when (parent) {178
case Some(val value) -> value.leases += 1L179
case None -> {}180
}181
fileDialogs.add(session)182
updateModalInput()183
deferNativeOperation { session.start() }184
return session185
}186
}188
private fun setFileText(native: Ptr<Unit>, field: Int, value: String) {189
@Unsafe190
{191
value.toByteArray().withDataPointer {192
data,193
count -> @Unsafe194
{195
checkNativeStatus(196
nativeFileOption(native, field, data, count),197
"file dialog option"198
)199
}200
}201
}202
}204
private fun setFilePathOption(native: Ptr<Unit>, field: Int, value: OsString) {205
@Unsafe206
{207
val status =208
withDialogString(value) {209
data,210
count -> @Unsafe211
{212
return@withDialogString nativeFileOption(native, field, data, count)213
}214
}215
checkNativeStatus(status, "file dialog path option")216
}217
}