scoop:scoop.window:0.1.0
src/input/primary_selection.scoop
Selected production source · src/input/primary_selection.scoop
1
package scoop.window3
/** A platform PRIMARY selection, separate from the explicit copy/paste clipboard. */4
public class PrimarySelection internal constructor(private val owner: ApplicationOwner) {5
/** False when the backend only supplies a process-local placeholder. */6
public val isAvailable: Boolean get() {7
checkActive("query primary selection")8
return platformPrimaryFlags(owner.native!!).and(1) != 09
}10
/** Resolves the native middle-button preference, or the platform's enabled default. */11
public val defaultPasteEnabled: Boolean get() {12
checkActive("query primary paste preference")13
return platformPrimaryFlags(owner.native!!).and(2) != 014
}15
public fun readText(): String? {16
checkActive("read primary selection")17
return when (platformReadPrimary(owner.native!!)) {18
case Some(val text) -> Some(clipboardText(text, false, true))19
case None -> None20
}21
}22
/** Claims one owned plain-text value; unavailable platforms return None. */23
public fun claimText(text: String): PrimarySelectionClaim? {24
checkActive("claim primary selection")25
val value = clipboardText(text, true, true)26
val generation = platformClaimPrimary(owner.native!!, value)27
return if (generation == 0L)28
None29
else30
Some(PrimarySelectionClaim(owner, generation))31
}32
internal fun checkActive(operation: String) {33
owner.checkThread()34
if (owner.native == None || owner.exiting)35
throw WindowException(operation, WindowErrorKind.InvalidState)36
}37
}39
/** Closing a claim never clears the contents of a later or external owner. */40
public class PrimarySelectionClaim internal constructor(41
private val owner: ApplicationOwner,42
private val generation: Long43
): Closeable {44
private var closed = false45
public val isCurrent: Boolean get() {46
owner.checkThread()47
if (closed || owner.exiting)48
return false49
return when (owner.native) {50
case Some(val native) -> platformPrimaryCurrent(native, generation)51
case None -> false52
}53
}54
public override fun close() {55
owner.checkThread()56
if (closed)57
return58
closed = true59
when (owner.native) {60
case Some(val native) -> platformReleasePrimary(native, generation)61
case None -> {}62
}63
}64
}66
public val ActiveApplication.primarySelection: PrimarySelection get() {67
val value = PrimarySelection(owner)68
value.checkActive("get primary selection")69
return value70
}72
public val Window.primarySelection: PrimarySelection get() {73
val value = PrimarySelection(owner.application)74
value.checkActive("get primary selection")75
return value76
}