scoop:scoop.window:0.1.0
src/input/clipboard.scoop
Selected production source · src/input/clipboard.scoop
1
package scoop.window3
import scoop.text.lowercaseAscii4
import scoop.text.startsWith5
import scoop.text.contains6
import scoop.collections.HashSet8
/** One Unicode representation of a clipboard item, identified by a text MIME type. */9
public struct ClipboardText(val mediaType: String, val text: String)11
/** System clipboard text with shared ownership and standard media type mapping. */12
public class Clipboard internal constructor(private val owner: ApplicationOwner) {13
/** Reads a text representation; absence and an existing empty string are distinct. */14
public fun readText(mediaType: String = "text/plain"): String? {15
checkActive("read clipboard")16
val type = clipboardMediaType(mediaType)17
return when (platformReadClipboard(type)) {18
case Some(val text) -> Some(clipboardText(text, false, type == "text/plain"))19
case None -> None20
}21
}23
public fun writeText(text: String) {24
writeTexts([ClipboardText("text/plain", text)])25
}27
/** Publishes all representations of one item after validating the complete input. */28
public fun writeTexts(representations: List<ClipboardText>) {29
checkActive("write clipboard")30
if (representations.size == 0L)31
throw IllegalArgumentException(Some("clipboard representations must not be empty"))32
val values = ArrayList<ClipboardText>()33
val types = HashSet<String>()34
for (representation in representations) {35
val type = clipboardMediaType(representation.mediaType)36
if (!types.add(type))37
throw IllegalArgumentException(Some("duplicate clipboard text media type"))38
values.add(39
ClipboardText(type, clipboardText(representation.text, true, type == "text/plain"))40
)41
}42
platformWriteClipboard(owner.native!!, values)43
}44
internal fun checkActive(operation: String) {45
owner.checkThread()46
if (owner.native == None || owner.exiting)47
throw WindowException(operation, WindowErrorKind.InvalidState)48
}49
}51
/** Obtains a clipboard facade that remains valid while this application runs. */52
public val ActiveApplication.clipboard: Clipboard get() {53
val value = Clipboard(owner)54
value.checkActive("get clipboard")55
return value56
}58
/** Uses the application clipboard without extending the lifetime of this window. */59
public val Window.clipboard: Clipboard get() {60
val value = Clipboard(owner.application)61
value.checkActive("get clipboard")62
return value63
}65
private fun clipboardMediaType(mediaType: String): String {66
val value = mediaType.lowercaseAscii()67
if (!value.startsWith("text/") || value.byteLength <= 5L)68
throw IllegalArgumentException(Some("clipboard text requires a concrete text MIME type"))69
var index = 0L70
for (character in value) {71
if (index >= 5L) {72
val code = character.code73
val letterOrDigit = (code >= 97 && code <= 122) || (code >= 48 && code <= 57)74
if (!letterOrDigit && (index == 5L || !"!#$&^_.+-".contains(character.toString())))75
throw IllegalArgumentException(76
Some("clipboard text MIME type cannot contain parameters or wildcards")77
)78
}79
index += 1L80
}81
return value82
}84
internal fun clipboardText(text: String, writing: Boolean, normalizeLines: Boolean): String {85
var hasCarriageReturn = false86
for (character in text) {87
if (character.code == 0) {88
if (writing)89
throw IllegalArgumentException(Some("clipboard text contains NUL"))90
throw WindowException("read clipboard", WindowErrorKind.Unsupported)91
}92
if (character.code == 13)93
hasCarriageReturn = true94
}95
if (!normalizeLines || !hasCarriageReturn)96
return text97
val result = StringBuilder(text.byteLength)98
var carriage = false99
for (character in text) {100
if (character.code == 13)101
result.append("\n")102
else103
if (character.code != 10 || !carriage)104
result.append(character.toString())105
carriage = character.code == 13106
}107
return result.toString()108
}