Scoop API

scoop:scoop.window:0.1.0

src/input/clipboard.scoop

Selected production source · src/input/clipboard.scoop

1package scoop.window
3import scoop.text.lowercaseAscii
4import scoop.text.startsWith
5import scoop.text.contains
6import scoop.collections.HashSet
8/** One Unicode representation of a clipboard item, identified by a text MIME type. */
9public struct ClipboardText(val mediaType: String, val text: String)
11/** System clipboard text with shared ownership and standard media type mapping. */
12public 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 -> None
20 }
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 }
51/** Obtains a clipboard facade that remains valid while this application runs. */
52public val ActiveApplication.clipboard: Clipboard get() {
53 val value = Clipboard(owner)
54 value.checkActive("get clipboard")
55 return value
58/** Uses the application clipboard without extending the lifetime of this window. */
59public val Window.clipboard: Clipboard get() {
60 val value = Clipboard(owner.application)
61 value.checkActive("get clipboard")
62 return value
65private 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 = 0L
70 for (character in value) {
71 if (index >= 5L) {
72 val code = character.code
73 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 += 1L
80 }
81 return value
84internal fun clipboardText(text: String, writing: Boolean, normalizeLines: Boolean): String {
85 var hasCarriageReturn = false
86 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 = true
94 }
95 if (!normalizeLines || !hasCarriageReturn)
96 return text
97 val result = StringBuilder(text.byteLength)
98 var carriage = false
99 for (character in text) {
100 if (character.code == 13)
101 result.append("\n")
102 else
103 if (character.code != 10 || !carriage)
104 result.append(character.toString())
105 carriage = character.code == 13
106 }
107 return result.toString()